Skip to content

Commit

Permalink
feat: revision 0.0.12 (#6)
Browse files Browse the repository at this point in the history
* feat: optmize callbacks with lua ref
* feat: bump engine version 0.0.12
* feat: simplify cmakelists.txt
  • Loading branch information
RodrigoDornelles authored Nov 17, 2024
1 parent bf14e60 commit f262792
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 65 deletions.
74 changes: 26 additions & 48 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ gba_create_rom(${PROJECT_NAME}

# Zig
set(ZIG_VERSION "0.13.0")
set(ZIG_DIR "${CMAKE_SOURCE_DIR}/vendor/zig")
set(ZIG_DOWNLOAD "http://ziglang.org/download")
set(ZIG_PATH "${CMAKE_SOURCE_DIR}/vendor/zig/zig")
string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} CMAKE_HOST_SYSTEM_NAME_LOWER)
if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Darwin")
set(ZIG_DOWNLOAD "${ZIG_DOWNLOAD}/${ZIG_VERSION}/zig-macos-${CMAKE_HOST_SYSTEM_PROCESSOR}-${ZIG_VERSION}.tar.xz")
Expand All @@ -47,85 +47,63 @@ elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows")
set(ZIG_DOWNLOAD "${ZIG_DOWNLOAD}/${ZIG_VERSION}/zig-windows-${CMAKE_HOST_SYSTEM_PROCESSOR}-${ZIG_VERSION}.zip")
endif()
if(NOT EXISTS ${ZIG_PATH})
FetchContent_Populate(
compiler_zig
URL "${ZIG_DOWNLOAD}"
SOURCE_DIR "${CMAKE_SOURCE_DIR}/vendor/zig"
SUBBUILD_DIR "${CMAKE_SOURCE_DIR}/vendor/_deps/zig-subbuild"
BINARY_DIR "${CMAKE_SOURCE_DIR}/vendor/_deps/zig-build"
)
if(NOT EXISTS ${ZIG_DIR})
FetchContent_Populate(dep_zig URL ${ZIG_DOWNLOAD} SOURCE_DIR ${ZIG_DIR})
endif()
find_program(ZIGC zig PATHS "${CMAKE_SOURCE_DIR}/vendor/zig" REQUIRED NO_DEFAULT_PATH)
find_program(ZIGC zig PATHS ${ZIG_DIR} REQUIRED NO_DEFAULT_PATH)

# Font
set(FONT_DOWNLOAD "https://github.com/gamelly/gly-type/archive/refs/heads/main.zip")
if(NOT EXISTS "${LUA_PATH}/font")
FetchContent_Populate(
lib_font
URL "${FONT_DOWNLOAD}"
SOURCE_DIR "${CMAKE_SOURCE_DIR}/vendor/font"
SUBBUILD_DIR "${CMAKE_SOURCE_DIR}/vendor/_deps/font-subbuild"
BINARY_DIR "${CMAKE_SOURCE_DIR}/vendor/_deps/font-build"
)
set(FONT_VERSION "0.1")
set(FONT_DIR "${CMAKE_SOURCE_DIR}/vendor/font")
set(FONT_DOWNLOAD "https://github.com/gamelly/gly-type/archive/refs/tags/${FONT_VERSION}.tar.gz")
if(NOT EXISTS ${FONT_DIR})
FetchContent_Populate(dep_font URL ${FONT_DOWNLOAD} SOURCE_DIR ${FONT_DIR})
endif()

# Lua
set(LUA_VERSION "5.4.7")
set(LUA_DIR "${CMAKE_SOURCE_DIR}/vendor/lua")
set(LUA_DOWNLOAD "https://github.com/lua/lua/archive/refs/tags/v${LUA_VERSION}.tar.gz")
set(LUA_PATH "${CMAKE_SOURCE_DIR}/vendor/lua")
set(LUA_OPTMIZE FALSE)
if(NOT EXISTS "${LUA_PATH}/lua")
set(LUA_OPTMIZE TRUE)
FetchContent_Populate(
compiler_lua
URL "${LUA_DOWNLOAD}"
SOURCE_DIR "${CMAKE_SOURCE_DIR}/vendor/lua"
SUBBUILD_DIR "${CMAKE_SOURCE_DIR}/vendor/_deps/lua-subbuild"
BINARY_DIR "${CMAKE_SOURCE_DIR}/vendor/_deps/lua-build"
)
FetchContent_Populate(dep_lua URL "${LUA_DOWNLOAD}" SOURCE_DIR ${LUA_DIR})
execute_process(
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/vendor/lua"
WORKING_DIRECTORY ${LUA_DIR}
COMMAND ${ZIGC} cc -DMAKE_LUA onelua.c -o lua
-target ${CMAKE_HOST_SYSTEM_PROCESSOR}-${CMAKE_HOST_SYSTEM_NAME_LOWER}
)
endif()
file(GLOB lua_files "${CMAKE_SOURCE_DIR}/vendor/lua/*.c")
list(REMOVE_ITEM lua_files "${CMAKE_SOURCE_DIR}/vendor/lua/lua.c")
list(REMOVE_ITEM lua_files "${CMAKE_SOURCE_DIR}/vendor/lua/onelua.c")
add_library(lua-static STATIC "${lua_files}")
find_program(LUAC lua PATHS "${CMAKE_SOURCE_DIR}/vendor/lua" REQUIRED NO_DEFAULT_PATH)
file(GLOB lua_files "${LUA_DIR}/*.c")
list(REMOVE_ITEM lua_files "${LUA_DIR}/lua.c")
list(REMOVE_ITEM lua_files "${LUA_DIR}/onelua.c")
add_library(lua-static STATIC ${lua_files})
find_program(LUAC lua PATHS ${LUA_DIR} REQUIRED NO_DEFAULT_PATH)

# Engine
set(ENGINE_VERSION "0.0.12")
set(ENGINE_DIR "${CMAKE_SOURCE_DIR}/vendor/engine")
set(ENGINE_VERSION "0.0.11")
set(ENGINE_CLI "${ENGINE_DIR}/src/cli/main.lua")
set(ENGINE_DOWNLOAD "https://github.com/gamelly/gly-engine/archive/refs/tags/${ENGINE_VERSION}.tar.gz")
set(ENGINE_HEADER "${ENGINE_DIR}/bytecode.h")
if(NOT EXISTS ${ENGINE_HEADER})
FetchContent_Declare(
lib_engine
URL "${ENGINE_DOWNLOAD}"
SOURCE_DIR ${ENGINE_DIR}
SUBBUILD_DIR "${CMAKE_SOURCE_DIR}/vendor/_deps/engine-subbuild"
BINARY_DIR "${CMAKE_SOURCE_DIR}/vendor/_deps/engine-build"
)
FetchContent_MakeAvailable(lib_engine)
FetchContent_Populate(dep_engine URL ${ENGINE_DOWNLOAD} SOURCE_DIR ${ENGINE_DIR})
file(MAKE_DIRECTORY "${ENGINE_DIR}/lite")
file(MAKE_DIRECTORY "${ENGINE_DIR}/native")
file(MAKE_DIRECTORY "${ENGINE_DIR}/examples/template")
execute_process(
WORKING_DIRECTORY ${ENGINE_DIR}
COMMAND ${LUAC} src/cli/main.lua build --core lite --bundler --dist ${ENGINE_DIR}/lite/
COMMAND ${LUAC} ${ENGINE_CLI} build --core lite --bundler --dist ${ENGINE_DIR}/lite/
COMMAND_ERROR_IS_FATAL ANY
)
execute_process(
WORKING_DIRECTORY ${ENGINE_DIR}
COMMAND ${LUAC} src/cli/main.lua build --core native --bundler --dist ${ENGINE_DIR}/native/
COMMAND ${LUAC} ${ENGINE_CLI} build --core native --bundler --dist ${ENGINE_DIR}/native/
COMMAND_ERROR_IS_FATAL ANY
)
execute_process(
WORKING_DIRECTORY ${ENGINE_DIR}
COMMAND ${LUAC} src/cli/main.lua tool-template-fill ${ENGINE_DIR}/examples/template/game.lua 204800
COMMAND ${LUAC} ${ENGINE_CLI} tool-template-fill ${ENGINE_DIR}/examples/template/game.lua 204800
)
set(ENGINE "@lite")
endif()
Expand All @@ -140,7 +118,7 @@ if(DEFINED ENGINE)
get_filename_component(ENGINE "${ENGINE}" ABSOLUTE)
execute_process(
WORKING_DIRECTORY ${ENGINE_DIR}
COMMAND ${LUAC} src/cli/main.lua fs-xxd-i ${ENGINE} ${ENGINE_HEADER} --const --name engine_bytecode_lua
COMMAND ${LUAC} ${ENGINE_CLI} fs-xxd-i ${ENGINE} ${ENGINE_HEADER} --const --name engine_bytecode_lua
COMMAND_ERROR_IS_FATAL ANY
)
endif()
Expand All @@ -164,7 +142,7 @@ if(DEFINED GAME AND GAME_LENGTH)
file(MAKE_DIRECTORY "${CMAKE_SOURCE_DIR}/vendor/game")
execute_process(
WORKING_DIRECTORY ${ENGINE_DIR}
COMMAND ${LUAC} src/cli/main.lua fs-xxd-i ${GAME} ${GAME_HEADER} --const --name game_bytecode_lua
COMMAND ${LUAC} ${ENGINE_CLI} fs-xxd-i ${GAME} ${GAME_HEADER} --const --name game_bytecode_lua
COMMAND_ERROR_IS_FATAL ANY
)
endif()
Expand All @@ -173,7 +151,7 @@ endif()
if(LUA_OPTMIZE)
execute_process(
WORKING_DIRECTORY ${ENGINE_DIR}
COMMAND ${LUAC} src/cli/main.lua fs-luaconf ${LUA_PATH}/luaconf.h --32bits
COMMAND ${LUAC} ${ENGINE_CLI} fs-luaconf ${LUA_DIR}/luaconf.h --32bits
)
endif()

Expand Down
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Custom Configs
DOXYFILE_ENCODING = UTF-8
PROJECT_NAME = "Core Native GBA"
PROJECT_NUMBER = 0.0.11
PROJECT_NUMBER = 0.0.12
PROJECT_BRIEF = "create your own game-engine with just lua for nitendo game boy advance console."
PROJECT_LOGO =
OUTPUT_DIRECTORY = .
Expand Down
4 changes: 2 additions & 2 deletions src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ static int native_draw_image(lua_State *L) {
return 0;
}

void draw_callback_update(lua_State* L)
void draw_callback_update(lua_State* L, int callback)
{
lua_getglobal(L, "native_callback_draw");
lua_rawgeti(L, LUA_REGISTRYINDEX, callback);
lua_pcall(L, 0, 0, 0);
}

Expand Down
2 changes: 1 addition & 1 deletion src/draw_logo.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void draw_logo()
gly_type_render(95, 67, 5, "native", draw_queue_clojure(51));
#endif
gly_type_render(95, 74, 5, "engine", draw_queue_clojure(51));
gly_type_render(97, 80, 4, "0.0.11", draw_queue_clojure(51));
gly_type_render(97, 80, 4, "0.0.12", draw_queue_clojure(51));
draw_queue_burn(1);
#endif
}
10 changes: 5 additions & 5 deletions src/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ static const char *const key_bindings[] = {
"d"
};

static void key_update(lua_State* L, const char *const key, uint8_t value)
static void key_update(lua_State* L, int callback, const char *const key, uint8_t value)
{
lua_getglobal(L, "native_callback_keyboard");
lua_rawgeti(L, LUA_REGISTRYINDEX, callback);
lua_pushstring(L, key);
lua_pushinteger(L, value);
lua_pcall(L, 2, 0, 0);
Expand All @@ -28,18 +28,18 @@ static void key_update(lua_State* L, const char *const key, uint8_t value)
* @li 1 released
* @li 0 pressing
*/
void keys_callback_update(lua_State* L)
void keys_callback_update(lua_State* L, int callback)
{
uint16_t mask = 1;
uint8_t i = 0;
keys_current = *(uint16_t*)(0x4000130);

while (i < sizeof(key_bindings)) {
if ((keys_current &~ keys_old) & mask) {
key_update(L, key_bindings[i], 0);
key_update(L, callback, key_bindings[i], 0);
}
else if ((~keys_current & keys_old) & mask) {
key_update(L, key_bindings[i], 1);
key_update(L, callback, key_bindings[i], 1);
}
mask = mask << 1;
i = i + 1;
Expand Down
4 changes: 2 additions & 2 deletions src/loop.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "zeebo.h"

void loop_callback_update(lua_State* L, uint8_t dt)
void loop_callback_update(lua_State* L, int callback, uint8_t dt)
{
lua_getglobal(L, "native_callback_loop");
lua_rawgeti(L, LUA_REGISTRYINDEX, callback);
lua_pushnumber(L, dt);
lua_pcall(L, 1, 0, 0);
}
16 changes: 13 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,27 @@ int main()
lua_pcall(L, 0, 1, 0);
lua_pcall(L, 3, 0, 0);


lua_getglobal(L, "native_callback_loop");
int native_callback_loop = luaL_ref(L, LUA_REGISTRYINDEX);

lua_getglobal(L, "native_callback_draw");
int native_callback_draw = luaL_ref(L, LUA_REGISTRYINDEX);

lua_getglobal(L, "native_callback_keyboard");
int native_callback_keyboard = luaL_ref(L, LUA_REGISTRYINDEX);

static uint8_t page = 0;
static uint8_t frameskip = 0;

while (1) {
keys_callback_update(L);
loop_callback_update(L, 16);
keys_callback_update(L, native_callback_keyboard);
loop_callback_update(L, native_callback_loop, 16);
while(*(volatile uint16_t*) 0x04000006 >= 160);
while(*(volatile uint16_t*) 0x04000006 < 160);
if (++frameskip & 1) {
draw_queue_page(page++);
draw_callback_update(L);
draw_callback_update(L, native_callback_draw);
} else {
draw_cmd_mode(0, 1, 0, 1);
draw_queue_burn(page++);
Expand Down
6 changes: 3 additions & 3 deletions src/zeebo.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ void draw_queue_push(uint8_t cmd, uint8_t a, uint8_t b, uint8_t c, uint8_t d);
cmd_t draw_queue_clojure(uint8_t cmd);

void draw_library_install(lua_State* L);
void draw_callback_update(lua_State* L);
void keys_callback_update(lua_State *L);
void loop_callback_update(lua_State* L, uint8_t dt);
void draw_callback_update(lua_State* L, int);
void keys_callback_update(lua_State *L, int);
void loop_callback_update(lua_State* L, int, uint8_t dt);

0 comments on commit f262792

Please sign in to comment.