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:
- Implement a Micro Engine: Develop a small engine that serves as a wrapper around the graphics libraries available on your target platform. This micro engine will handle all low-level graphics operations.
- Ensure Lua Support: Embed some Lua distribution in your software, even if you intend to use other languages such as JavaScript (like QuickJS), all the main behavior of the engine is written in Lua, if you are using a version of Lua that is not compatible, you can open an issue that we can work on in support.
- Initialize the Engine: After you’ve set up your micro engine and ensured Lua is ready, you can load the Gly Engine.You need to call a specific function to initialize the engine within your application. Use the function
native_callback_init
to properly set everything up.
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.
- C API implementation
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 FUNCTION LEVEL 1
estimated implementation time: 6 hours 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)
void native_draw_line(double x1, double y1, double x1, double y1)
- API FUNCTION LEVEL 2
estimated implementation time: 2~4 days void native_text_print(double x, double y, char* text)
void native_text_font_size(int size)
void native_text_font_name(char* name)
void native_text_font_default(int font_id)
void native_text_font_previous()
void native_draw_image(double x, double y, char* src)
- API FUNCTION LEVEL 3
estimated implementation time: 2~4 weaks void native_media_load(int channel, char* url)
void native_media_play(int channel)
void native_media_pause(int channel)
void native_media_resize(int channel, double width, double height)
void native_media_position(int channel, double x, double y)
- API CALLBACK LEVEL 1
estimated implementation time: 4 hours 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 CALLBACK LEVEL 2
estimated implementation time: 2 hours void native_callback_resize(double width, double height)
- API CALLBACK LEVEL 3
implementation time depends function API difficulty. void native_callback_media(int channel, char* event)
- Examples
native_draw_line
(with SDL2)
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;
}
native_callback_init
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);
}
native_callback_loop
and native_callback_draw
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_pcall(L, 0, 0, 0);
usleep(16000);
}