diff --git a/.gitmodules b/.gitmodules index 05140dc..75f8863 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "dependencies/GRRLIB/source"] path = dependencies/GRRLIB/source url = https://git.fries.gay/fries/GRRLIB +[submodule "dependencies/quickjs"] + path = dependencies/quickjs + url = https://git.fries.gay/fries/quickjs diff --git a/dependencies/CMakeLists.txt b/dependencies/CMakeLists.txt index 9a12469..e9d13d2 100644 --- a/dependencies/CMakeLists.txt +++ b/dependencies/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(duktape) add_subdirectory(GRRLIB) +add_subdirectory(quickjs) diff --git a/dependencies/quickjs b/dependencies/quickjs new file mode 160000 index 0000000..7b20dcd --- /dev/null +++ b/dependencies/quickjs @@ -0,0 +1 @@ +Subproject commit 7b20dcd58fea53cd203df27e2da74bbd2fd013db diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index b035e76..1252b70 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,8 +1,11 @@ add_subdirectory(assets) +add_subdirectory(shared) add_subdirectory(duktape) +add_subdirectory(quickjs) # add_subdirectory(flacplayer) add_executable(WiiDuktape main.c) target_link_libraries(WiiDuktape GRRLIB) target_link_libraries(WiiDuktape duktape_engine) +target_link_libraries(WiiDuktape quickjs_engine) ogc_create_dol(WiiDuktape) diff --git a/source/duktape/CMakeLists.txt b/source/duktape/CMakeLists.txt index 390a4f8..b957a29 100644 --- a/source/duktape/CMakeLists.txt +++ b/source/duktape/CMakeLists.txt @@ -9,5 +9,6 @@ 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_link_libraries(duktape_engine shared) target_include_directories(duktape_engine PUBLIC ./include) target_include_directories(duktape_engine PRIVATE ./include_private) diff --git a/source/duktape/duktape_engine.c b/source/duktape/duktape_engine.c index 19de8e3..56e9580 100644 --- a/source/duktape/duktape_engine.c +++ b/source/duktape/duktape_engine.c @@ -7,8 +7,7 @@ #include #include #include - -const float MICROSECOND = 0.000001f; +#include static float deltatime = 1.0f/60; static unsigned int green = RGBA(0, 255, 0, 255); @@ -53,7 +52,7 @@ void duktape(GRRLIB_ttfFont * global_font, bool * running) { 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_define_wii_object(ctx, font, running, &deltatime); duk_put_global_string(ctx, "wii"); // evaluate functions from game.js diff --git a/source/duktape/include_private/wii_duk.h b/source/duktape/include_private/wii_duk.h index 6c6fb39..69f0ee0 100644 --- a/source/duktape/include_private/wii_duk.h +++ b/source/duktape/include_private/wii_duk.h @@ -2,5 +2,5 @@ #define WII_DUK_H #include #include -void define_wii_object(duk_context * ctx, struct GRRLIB_Font * grrlib_font, bool * global_running, float * global_deltatime); +void duk_define_wii_object(duk_context * ctx, struct GRRLIB_Font * grrlib_font, bool * global_running, float * global_deltatime); #endif diff --git a/source/duktape/wii_duk.c b/source/duktape/wii_duk.c index fa6ea19..2088f0e 100644 --- a/source/duktape/wii_duk.c +++ b/source/duktape/wii_duk.c @@ -2,6 +2,7 @@ #include "data.h" #include "grrlib_duk.h" #include "rgba.h" +#include static GRRLIB_ttfFont * font; static bool * running; @@ -9,37 +10,34 @@ static unsigned int green = RGBA(0, 255, 0, 255); static float * deltatime; static duk_ret_t native_print(duk_context * ctx) { - GRRLIB_PrintfTTF(0, 0, font, duk_to_string(ctx, 0), 32, green); + shared_native_print(duk_to_string(ctx, 0), font); return 0; } static duk_ret_t native_exit(duk_context * ctx) { - *running = false; + shared_native_exit(running); return 0; } static duk_ret_t pad_buttons_down(duk_context * ctx) { - unsigned int pressed = PAD_ButtonsDown(0); - duk_push_number(ctx, pressed); + duk_push_number(ctx, shared_pad_buttons_down()); return 1; } static duk_ret_t get_file(duk_context * ctx) { const char* filename = duk_to_string(ctx, 0); + shared_file_obj file = shared_get_file(filename); - void* ptr; - size_t size; - - if (get_file_pointer(filename, &ptr, &size)) { + if (shared_is_valid_file(file)) { duk_idx_t file_obj = duk_push_object(ctx); - duk_push_number(ctx, size); + duk_push_number(ctx, file.size); duk_put_prop_string(ctx, file_obj, "size"); - duk_push_pointer(ctx, ptr); + duk_push_pointer(ctx, file.file_ptr); duk_put_prop_string(ctx, file_obj, "file_ptr"); - duk_push_string(ctx, filename); + duk_push_string(ctx, file.filename); duk_put_prop_string(ctx, file_obj, "filename"); return 1; @@ -60,7 +58,7 @@ static void define_pad_object(duk_context *ctx) { duk_put_prop_string(ctx, pad_obj, "buttons_down"); } -void define_wii_object(duk_context * ctx, struct GRRLIB_Font * grrlib_font, bool * global_running, float * global_deltatime) { +void duk_define_wii_object(duk_context * ctx, struct GRRLIB_Font * grrlib_font, bool * global_running, float * global_deltatime) { font = grrlib_font; running = global_running; deltatime = global_deltatime; diff --git a/source/main.c b/source/main.c index 6420cc2..4af2580 100644 --- a/source/main.c +++ b/source/main.c @@ -7,6 +7,7 @@ #include #include +#include #define LENGTH(arr) (sizeof(arr) / sizeof(arr[0])) @@ -45,15 +46,16 @@ static void start_duktape() { static void start_quickjs() { running = true; - while (running) { - PAD_ScanPads(); - unsigned int pressed = PAD_ButtonsDown(0); - GRRLIB_PrintfTTF(0, 0, font, "QuickJS is not implemented, yet..", 48, RGBA(255, 255, 255, 255)); - GRRLIB_Render(); - if (pressed & PAD_BUTTON_START) { - running = false; - } - } + quickjs(font, &running); + // while (running) { + // PAD_ScanPads(); + // unsigned int pressed = PAD_ButtonsDown(0); + // GRRLIB_PrintfTTF(0, 0, font, "QuickJS is not implemented, yet..", 48, RGBA(255, 255, 255, 255)); + // GRRLIB_Render(); + // if (pressed & PAD_BUTTON_START) { + // running = false; + // } + // } } static void update_menu(unsigned int * pressed) { diff --git a/source/quickjs/CMakeLists.txt b/source/quickjs/CMakeLists.txt new file mode 100644 index 0000000..f861f7f --- /dev/null +++ b/source/quickjs/CMakeLists.txt @@ -0,0 +1,12 @@ +set(quickjs_engine_sources + quickjs_engine.c + wii_qjs.c +) + +add_library(quickjs_engine ${quickjs_engine_sources}) +target_link_libraries(quickjs_engine PRIVATE qjs) +target_link_libraries(quickjs_engine PRIVATE GRRLIB) +target_link_libraries(quickjs_engine PRIVATE assets) +target_link_libraries(quickjs_engine PRIVATE shared) +target_include_directories(quickjs_engine PUBLIC ./include) +target_include_directories(quickjs_engine PRIVATE ./include_private) diff --git a/source/quickjs/include/quickjs_engine.h b/source/quickjs/include/quickjs_engine.h new file mode 100644 index 0000000..f3293a0 --- /dev/null +++ b/source/quickjs/include/quickjs_engine.h @@ -0,0 +1,5 @@ +#ifndef QUICKJS_ENGINE_H +#define QUICKJS_ENGINE_H +#include +void quickjs(GRRLIB_ttfFont * global_font, bool * running); +#endif diff --git a/source/quickjs/include_private/wii_qjs.h b/source/quickjs/include_private/wii_qjs.h new file mode 100644 index 0000000..d2c4db5 --- /dev/null +++ b/source/quickjs/include_private/wii_qjs.h @@ -0,0 +1,6 @@ +#ifndef WII_QJS_H +#define WII_QJS_H +#include +#include +JSValue qjs_define_wii_object(JSContext * ctx, GRRLIB_ttfFont * global_font, bool * global_running, float * global_deltatime); +#endif diff --git a/source/quickjs/quickjs_engine.c b/source/quickjs/quickjs_engine.c new file mode 100644 index 0000000..d2a188d --- /dev/null +++ b/source/quickjs/quickjs_engine.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include + +static float deltatime = 1.0f/60; +static unsigned int green = RGBA(0, 255, 0, 255); +static GRRLIB_ttfFont * font; + +void quickjs(GRRLIB_ttfFont * global_font, bool * running) { + font = global_font; + + JSRuntime * rt = JS_NewRuntime(); + JSContext * ctx = JS_NewContext(rt); + + JSValue wii_obj = qjs_define_wii_object(ctx, font, running, &deltatime); + JSValue global_obj = JS_GetGlobalObject(ctx); + JS_SetPropertyStr(ctx, global_obj, "wii", wii_obj); + + while (*running) { + const unsigned int beginningOfFrame = ticks_to_microsecs(gettime()); + + PAD_ScanPads(); + + // fun + JS_Eval(ctx, "wii.print('hi')", strlen("wii.print('hi')"), "file", 0); + + GRRLIB_Render(); + + const unsigned int endOfFrame = ticks_to_microsecs(gettime()); + deltatime = (endOfFrame - beginningOfFrame) * MICROSECOND; + } +} diff --git a/source/quickjs/wii_qjs.c b/source/quickjs/wii_qjs.c new file mode 100644 index 0000000..5bc96e7 --- /dev/null +++ b/source/quickjs/wii_qjs.c @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +#define LENGTH(arr) (sizeof(arr) / sizeof(arr[0])) + +static GRRLIB_ttfFont * font; +static bool * running; +static unsigned int green = RGBA(0, 255, 0, 255); +static float * deltatime; + +static JSClassID file_class_id; + +static void file_class_finalizer(JSRuntime * rt, JSValue val) { + +} + +static JSClassDef file_class = { + "FILE", + .finalizer = file_class_finalizer +}; + +static JSValue native_print(JSContext * ctx, JSValue this_val, int argc, JSValue * argv) { + shared_native_print(JS_ToCString(ctx, argv[0]), font); + return 0; +} + +static JSValue native_exit(JSContext * ctx, JSValue this_val, int argc, JSValue * argv) { + shared_native_exit(running); + return 0; +} + +static JSValue pad_buttons_down(JSContext * ctx, JSValue this_val, int argc, JSValue * argv) { + return JS_NewUint32(ctx, shared_pad_buttons_down()); +} + +static JSValue get_file(JSContext * ctx, JSValue this_val, int argc, JSValue * argv) { + const char * filename = JS_ToCString(ctx, argv[0]); + shared_file_obj file = shared_get_file(filename); + + if (shared_is_valid_file(file)) { + JSValue wii_obj = JS_NewObject(ctx); + + JSValue file_ptr = JS_NewObjectClass(ctx, file_class_id); + JS_SetOpaque(file_ptr, file.file_ptr); + + JS_SetPropertyStr(ctx, wii_obj, "size", JS_NewNumber(ctx, file.size)); + JS_SetPropertyStr(ctx, wii_obj, "file_ptr", file_ptr); + JS_SetPropertyStr(ctx, wii_obj, "filename", JS_NewString(ctx, file.filename)); + + return wii_obj; + } + + return JS_EXCEPTION; +} + +static JSValue get_deltatime(JSContext * ctx, JSValue this_val, int argc, JSValue * argv) { + return JS_NewNumber(ctx, *deltatime); +} + +static JSValue define_pad_object(JSContext * ctx) { + JSValue pad_obj = JS_NewObject(ctx); + const JSCFunctionListEntry pad_obj_funcs[] = { + JS_CFUNC_DEF("buttons_down", 0, pad_buttons_down) + }; + JS_SetPropertyFunctionList(ctx, pad_obj, pad_obj_funcs, LENGTH(pad_obj_funcs)); + return pad_obj; +} + +static const JSCFunctionListEntry wii_obj_funcs[] = { + JS_CFUNC_DEF("print", 1, native_print), + JS_CFUNC_DEF("exit", 0, native_exit), + JS_CFUNC_DEF("get_file", 1, get_file), + JS_CFUNC_DEF("get_deltatime", 0, get_deltatime) +}; + +JSValue qjs_define_wii_object(JSContext * ctx, GRRLIB_ttfFont * global_font, bool * global_running, float * global_deltatime) { + font = global_font; + running = global_running; + deltatime = global_deltatime; + + JSRuntime * rt = JS_GetRuntime(ctx); + + JS_NewClassID(rt, &file_class_id); + JS_NewClass(rt, file_class_id, &file_class); + + JSValue wii_obj = JS_NewObject(ctx); + JS_SetPropertyFunctionList(ctx, wii_obj, wii_obj_funcs, LENGTH(wii_obj_funcs)); + return wii_obj; +} + diff --git a/source/shared/CMakeLists.txt b/source/shared/CMakeLists.txt new file mode 100644 index 0000000..67bc2cd --- /dev/null +++ b/source/shared/CMakeLists.txt @@ -0,0 +1,8 @@ +set(shared_sources + wii_obj.c +) + +add_library(shared ${shared_sources}) +target_link_libraries(shared PRIVATE GRRLIB) +target_link_libraries(shared PRIVATE assets) +target_include_directories(shared PUBLIC ./include) diff --git a/source/shared/include/microsecond.h b/source/shared/include/microsecond.h new file mode 100644 index 0000000..a07ac79 --- /dev/null +++ b/source/shared/include/microsecond.h @@ -0,0 +1,4 @@ +#ifndef MICROSECOND_H +#define MICROSECOND_H +#define MICROSECOND 0.000001f +#endif diff --git a/source/shared/include/wii_obj.h b/source/shared/include/wii_obj.h new file mode 100644 index 0000000..e9449a5 --- /dev/null +++ b/source/shared/include/wii_obj.h @@ -0,0 +1,17 @@ +#ifndef WII_OBJ_H +#define WII_OBJ_H +#include + +typedef struct { + size_t size; + void * file_ptr; + const char * filename; +} shared_file_obj; + +void shared_native_print(const char * message, GRRLIB_ttfFont * font); +void shared_native_exit(bool * running); +unsigned int shared_pad_buttons_down(); +shared_file_obj shared_get_file(const char * filename); +bool shared_is_valid_file(shared_file_obj file); + +#endif diff --git a/source/shared/wii_obj.c b/source/shared/wii_obj.c new file mode 100644 index 0000000..572462f --- /dev/null +++ b/source/shared/wii_obj.c @@ -0,0 +1,43 @@ +#include +#include + +static unsigned int green = RGBA(0, 255, 0, 255); + +void shared_native_print(const char * message, GRRLIB_ttfFont * font) { + GRRLIB_PrintfTTF(0, 0, font, message, 32, green); +} + +void shared_native_exit(bool * running) { + *running = false; +} + +unsigned int shared_pad_buttons_down() { + return PAD_ButtonsDown(0); +} + +shared_file_obj shared_get_file(const char * filename) { + void* ptr; + size_t size; + + if (get_file_pointer(filename, &ptr, &size)) { + + shared_file_obj file = { + .size = size, + .file_ptr = ptr, + .filename = filename + }; + return file; + } + + shared_file_obj file = { + .size = 0, + .file_ptr = NULL, + .filename = NULL + }; + + return file; +} + +bool shared_is_valid_file(shared_file_obj file) { + return file.size > 0 && file.file_ptr != NULL && file.filename != NULL; +}