Gly Game Engine 0.0.11
Game engine in lua
Loading...
Searching...
No Matches
Manual

port from scratch

support the engine on some custom platform building from scratch

Warning
This is an advanced API!
only for advanced programmers, You might be lost if you are a beginner.

You can easily port the Gly Engine to any platform by following a few straight forward steps. The main requirement is to create interfaces that connect with the native graphics libraries and hardware specific to your platform. Here’s how to get started:

By following these steps, you can effectively port the Gly Engine to any platform of your choice.

Note
the functions are represented in C, but just so you understand the typing, in fact you have to manipulate the Lua VM stack in most cases.

Global lua functions

Using the Lua C API, you must expose these functions to your Lua VM, so that the engine can communicate with the low-level resources of the system.

API Level 0

Even if you have nothing to do in draw or loop of a game, these functions will be called to the microengine to know the start and end of the intention to paint each frame.

void native_draw_start()
void native_draw_flush()

API Level 1

void native_draw_color(int color)
void native_draw_clear(int color)
void native_draw_rect(int mode, double x, double y, double width, double heigth)

API Level 2

void native_draw_line(double x1, double y1, double x1, double y1)
void native_draw_text(double x, double y, char* text)
void native_draw_font(int size)
void native_draw_font(char* name, int size)

Global lua callbacks

API Level 1

void native_callback_loop(double dt)
void native_callback_draw()
void native_callback_keyboard(char* key, bool value)
void native_callback_init(width, height, game_lua)

API Level 2

void native_callback_resize(double width, double height)

Examples

native_draw_line in SDL2

/// @short @c std.draw.line
/// @param[in] x1 @c double
/// @param[in] y1 @c double
/// @param[in] x2 @c double
/// @param[in] y2 @c double
static int native_draw_line(lua_State *L) {
assert(lua_gettop(L) == 4);
float x1 = luaL_checknumber(L, 1);
float y1 = luaL_checknumber(L, 2);
float x2 = luaL_checknumber(L, 3);
float y2 = luaL_checknumber(L, 4);
SDL_RenderDrawLineF(renderer, x1, y1, x2, y2);
lua_pop(L, 4);
return 0;
}

micro-engine start

void gly_engine_start()
{
luaL_loadbuffer(L, engine_bytecode_lua, engine_bytecode_lua_len, "");
lua_pcall(L, 0, 0, 0);
lua_getglobal(L, "native_callback_init");
lua_pushnumber(L, 1280);
lua_pushnumber(L, 720);
luaL_loadbuffer(L, game_bytecode_lua, game_bytecode_lua_len, "");
lua_pcall(L, 0, 1, 0);
lua_pcall(L, 3, 0, 0);
}

micro-engine loop

void gly_engine_loop()
{
lua_getglobal(L, "native_callback_loop");
lua_pushnumber(L, 16);
lua_pcall(L, 1, 0, 0);
lua_getglobal(L, "native_callback_draw");
lua_pushnumber(L, 16);
lua_pcall(L, 1, 0, 0);
usleep(16000);
}