move duktape code into a static library.

this means i can try out making a launcher that launches into other
javascript engines like quickjs.
This commit is contained in:
Fries 2024-06-25 14:08:04 -07:00
parent ad4dc2332b
commit 63bb2916ca
17 changed files with 108 additions and 66 deletions

View file

@ -1,18 +1,7 @@
add_custom_command( add_subdirectory(assets)
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/asset_generator.py ${CMAKE_CURRENT_SOURCE_DIR}/game.js ${CMAKE_CURRENT_SOURCE_DIR}/game.c add_subdirectory(duktape)
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/game.c
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/game.js ${CMAKE_CURRENT_SOURCE_DIR}/asset_generator.py
COMMENT "Creating game.c from game.js"
)
add_custom_command( add_executable(WiiDuktape main.c)
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/data_asset_generator.py ${CMAKE_SOURCE_DIR}/data ${CMAKE_CURRENT_SOURCE_DIR}/data_generated.c
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/data_generated.c
DEPENDS ${CMAKE_SOURCE_DIR}/data ${CMAKE_CURRENT_SOURCE_DIR}/data_asset_generator.py
COMMENT "Creating data_generated.c from the data directory"
)
add_executable(WiiDuktape main.c grrlib_duk.c wii_duk.c rgba.c game.c data_generated.c)
target_link_libraries(WiiDuktape duktape)
target_link_libraries(WiiDuktape GRRLIB) target_link_libraries(WiiDuktape GRRLIB)
target_link_libraries(WiiDuktape duktape_engine)
ogc_create_dol(WiiDuktape) ogc_create_dol(WiiDuktape)

View file

@ -0,0 +1,16 @@
add_custom_command(
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/asset_generator.py ${CMAKE_CURRENT_SOURCE_DIR}/game.js ${CMAKE_CURRENT_SOURCE_DIR}/game.c
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/game.c
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/game.js ${CMAKE_CURRENT_SOURCE_DIR}/asset_generator.py
COMMENT "Creating game.c from game.js"
)
add_custom_command(
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/data_asset_generator.py ${CMAKE_SOURCE_DIR}/data ${CMAKE_CURRENT_SOURCE_DIR}/data_generated.c
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/data_generated.c
DEPENDS ${CMAKE_SOURCE_DIR}/data ${CMAKE_CURRENT_SOURCE_DIR}/data_asset_generator.py
COMMENT "Creating data_generated.c from the data directory"
)
add_library(assets game.c data_generated.c)
target_include_directories(assets PUBLIC ./include)

View file

@ -12,7 +12,7 @@ args = parser.parse_args()
script = Path(args.input).read_text().replace("\n", "\\n").replace('"', '\\"') script = Path(args.input).read_text().replace("\n", "\\n").replace('"', '\\"')
code = f""" code = f"""#include <game.h>
const char * program = "{script}"; const char * program = "{script}";
""" """

View file

@ -0,0 +1,4 @@
#ifndef GAME_H
#define GAME_H
extern const char * program;
#endif

View file

@ -0,0 +1,13 @@
set(duktape_engine_sources
duktape_engine.c
grrlib_duk.c
rgba.c
wii_duk.c
)
add_library(duktape_engine ${duktape_engine_sources})
target_link_libraries(duktape_engine duktape)
target_link_libraries(duktape_engine GRRLIB)
target_link_libraries(duktape_engine assets)
target_include_directories(duktape_engine PUBLIC ./include)
target_include_directories(duktape_engine PRIVATE ./include_private)

View file

@ -0,0 +1,60 @@
#include <grrlib.h>
#include <duktape.h>
#include <stdio.h>
#include <stdlib.h>
#include <ogc/lwp_watchdog.h>
#include <data.h>
#include <wii_duk.h>
#include <game.h>
const float MICROSECOND = 0.000001f;
static float deltatime = 1.0f/60;
static unsigned int green = RGBA(0, 255, 0, 255);
static GRRLIB_ttfFont * font;
static void duk_fatal_error(void *udata, const char *msg) {
const char * err_message_format = "an error has occured: %s";
size_t message_size = strlen(err_message_format) + strlen(msg) * sizeof(char *);
char * message = malloc(message_size);
snprintf(message, message_size, err_message_format, msg);
while (1) {
GRRLIB_PrintfTTF(0, 0, font, message, 12, green);
GRRLIB_Render();
}
}
void duktape(GRRLIB_ttfFont * global_font, bool * running) {
font = global_font;
duk_context * ctx = duk_create_heap(NULL, NULL, NULL, NULL, duk_fatal_error);
// create a wii object on the global object (globalThis)
define_wii_object(ctx, font, running, &deltatime);
duk_put_global_string(ctx, "wii");
// evaluate functions from game.js
duk_eval_string_noresult(ctx, program);
// call start function
duk_get_global_string(ctx, "start");
duk_call(ctx, 0);
duk_pop(ctx);
while (*running) {
const unsigned int beginningOfFrame = ticks_to_microsecs(gettime());
PAD_ScanPads();
// call update function
duk_get_global_string(ctx, "update");
duk_call(ctx, 0);
duk_pop(ctx);
GRRLIB_Render();
const unsigned int endOfFrame = ticks_to_microsecs(gettime());
deltatime = (endOfFrame - beginningOfFrame) * MICROSECOND;
}
}

View file

@ -0,0 +1,5 @@
#ifndef DUKTAPE_ENGINE_H
#define DUKTAPE_ENGINE_H
#include <grrlib.h>
void duktape(GRRLIB_ttfFont * global_font, bool * running);
#endif

View file

@ -4,27 +4,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <ogc/lwp_watchdog.h> #include <ogc/lwp_watchdog.h>
#include "data.h" #include <data.h>
#include "wii_duk.h" #include <duktape_engine.h>
const float MICROSECOND = 0.000001f;
extern const char * program;
static bool running = true; static bool running = true;
static GRRLIB_ttfFont * font; static GRRLIB_ttfFont * font;
static unsigned int green = RGBA(0, 255, 0, 255);
static float deltatime = 1.0f/60;
static void duk_fatal_error(void *udata, const char *msg) {
const char * err_message_format = "an error has occured: %s";
size_t message_size = strlen(err_message_format) + strlen(msg) * sizeof(char *);
char * message = malloc(message_size);
snprintf(message, message_size, err_message_format, msg);
while (1) {
GRRLIB_PrintfTTF(0, 0, font, message, 12, green);
GRRLIB_Render();
}
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
GRRLIB_Init(); GRRLIB_Init();
@ -37,37 +21,8 @@ int main(int argc, char **argv) {
font = GRRLIB_LoadTTF(pixel_operator_font, pixel_operator_font_size); font = GRRLIB_LoadTTF(pixel_operator_font, pixel_operator_font_size);
duk_context * ctx = duk_create_heap(NULL, NULL, NULL, NULL, duk_fatal_error); duktape(font, &running);
// create a wii object on the global object (globalThis)
define_wii_object(ctx, font, &running, &deltatime);
duk_put_global_string(ctx, "wii");
// evaluate functions from game.js
duk_eval_string_noresult(ctx, program);
// call start function
duk_get_global_string(ctx, "start");
duk_call(ctx, 0);
duk_pop(ctx);
while (running) {
const unsigned int beginningOfFrame = ticks_to_microsecs(gettime());
PAD_ScanPads();
// call update function
duk_get_global_string(ctx, "update");
duk_call(ctx, 0);
duk_pop(ctx);
GRRLIB_Render();
const unsigned int endOfFrame = ticks_to_microsecs(gettime());
deltatime = (endOfFrame - beginningOfFrame) * MICROSECOND;
}
duk_destroy_heap(ctx);
GRRLIB_Exit(); GRRLIB_Exit();
return 0; return 0;