diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2c63c08 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} diff --git a/data/PixelOperator.ttf b/data/PixelOperator.ttf new file mode 100644 index 0000000..34fe947 Binary files /dev/null and b/data/PixelOperator.ttf differ diff --git a/data/yellowbird-midflap.png b/data/yellowbird-midflap.png index 2ca3c2d..f9f3da6 100644 Binary files a/data/yellowbird-midflap.png and b/data/yellowbird-midflap.png differ diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..c24ac80 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "target": "ESNext" + } +} diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 7aefc3f..5e5c203 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -12,7 +12,7 @@ add_custom_command( COMMENT "Creating data_generated.c from the data directory" ) -add_executable(WiiDuktape main.c grrlib_duk.c game.c data_generated.c) +add_executable(WiiDuktape main.c grrlib_duk.c wii_duk.c game.c data_generated.c) target_link_libraries(WiiDuktape duktape) target_link_libraries(WiiDuktape GRRLIB) ogc_create_dol(WiiDuktape) diff --git a/source/game.js b/source/game.js index d95b03e..ed5d9e6 100644 --- a/source/game.js +++ b/source/game.js @@ -1,3 +1,11 @@ +/** + * returns an RGBA buffer + * @param {number} r red + * @param {number} g green + * @param {number} b blue + * @param {number} a alpha + * @returns {RGBA} + */ function rgba(r, g, b, a) { const buffer = Uint8Array.allocPlain(4); buffer[0] = r; @@ -21,7 +29,11 @@ function rgba_compare(rgba1, rgba2) { } globalThis.rectangle_color = RED_COLOR; + +/** @type {GRRLibTexture} */ globalThis.background = null; +/** @type {GRRLibTexture} */ +globalThis.bird = null; function swap_colors() { if (rgba_compare(rectangle_color, RED_COLOR)) { @@ -53,7 +65,6 @@ function update() { } wii.grrlib.fill_screen(WHITE_COLOR); - // wii.grrlib.rectangle(0, 0, 320, 240, rectangle_color, true); - wii.grrlib.draw_img(320, 0, background, 0, 1, 1, WHITE_COLOR); + wii.grrlib.draw_img(0, 0, background, 0, 1, 1, WHITE_COLOR); wii.grrlib.draw_img(0, 0, bird, 0, 1, 1, WHITE_COLOR); } diff --git a/source/grrlib_duk.c b/source/grrlib_duk.c index 82c7f0b..e972d3c 100644 --- a/source/grrlib_duk.c +++ b/source/grrlib_duk.c @@ -45,6 +45,15 @@ static duk_ret_t grrlib_load_texture(duk_context *ctx) { GRRLIB_texImg* texture = GRRLIB_LoadTexture(file_ptr); + if (texture->data == NULL) { + duk_push_string(ctx, "filename"); + duk_get_prop(ctx, 0); + const char * filename = duk_to_string(ctx, -1); + duk_pop(ctx); + + return duk_error(ctx, DUK_ERR_ERROR, "An error has occured while trying load the texture for the file %s.", filename); + } + duk_push_pointer(ctx, texture); return 1; diff --git a/source/main.c b/source/main.c index cf2266d..8435ebc 100644 --- a/source/main.c +++ b/source/main.c @@ -1,94 +1,42 @@ #include #include -#include -#include "grrlib_duk.h" +#include +#include + #include "data.h" +#include "wii_duk.h" -extern const char* program; +extern const char * program; static bool running = true; - -static duk_ret_t native_print(duk_context *ctx) { - GRRLIB_GeckoPrintf("%s\n", duk_to_string(ctx, 0)); - return 0; -} - -static duk_ret_t native_exit(duk_context *ctx) { - running = false; - return 0; -} +static GRRLIB_ttfFont * font; +static unsigned int green = RGBA(0, 255, 0, 255); static void duk_fatal_error(void *udata, const char *msg) { - GRRLIB_GeckoPrintf("an error has occured: %s\n", msg); -} - -static duk_ret_t pad_scan_pads() { - PAD_ScanPads(); - return 0; -} - -static duk_ret_t pad_buttons_down(duk_context *ctx) { - u32 pressed = PAD_ButtonsDown(0); - duk_push_number(ctx, pressed); - return 1; -} - -static duk_ret_t get_file(duk_context *ctx) { - const char* filename = duk_to_string(ctx, 0); - - void* ptr; - size_t size; - - if (get_file_pointer(filename, &ptr, &size)) { - duk_idx_t file_obj = duk_push_object(ctx); - - duk_push_number(ctx, size); - duk_put_prop_string(ctx, file_obj, "size"); - - duk_push_pointer(ctx, ptr); - duk_put_prop_string(ctx, file_obj, "file_ptr"); - - return 1; + 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(); } - - return duk_error(ctx, DUK_ERR_ERROR, "Error trying to get the file %s.", filename); -} - -static void define_pad_object(duk_context *ctx) { - duk_idx_t pad_obj = duk_push_object(ctx); - - duk_push_c_function(ctx, pad_scan_pads, 0); - duk_put_prop_string(ctx, pad_obj, "scan_pads"); - - duk_push_c_function(ctx, pad_buttons_down, 0); - duk_put_prop_string(ctx, pad_obj, "buttons_down"); -} - -static void define_wii_object(duk_context *ctx) { - duk_idx_t wii_obj = duk_push_object(ctx); - - duk_push_c_function(ctx, native_print, 1); - duk_put_prop_string(ctx, wii_obj, "print"); - - duk_push_c_function(ctx, native_exit, 0); - duk_put_prop_string(ctx, wii_obj, "exit"); - - define_pad_object(ctx); - duk_put_prop_string(ctx, wii_obj, "pad"); - - define_grrlib_object(ctx); - duk_put_prop_string(ctx, wii_obj, "grrlib"); - - duk_push_c_function(ctx, get_file, 1); - duk_put_prop_string(ctx, wii_obj, "get_file"); } int main(int argc, char **argv) { GRRLIB_Init(); PAD_Init(); - duk_context *ctx = duk_create_heap(NULL, NULL, NULL, NULL, duk_fatal_error); + // get the pixel operator font's data pointer and size + void * pixel_operator_font; + size_t pixel_operator_font_size; + get_file_pointer("PixelOperator.ttf", &pixel_operator_font, &pixel_operator_font_size); - define_wii_object(ctx); + font = GRRLIB_LoadTTF(pixel_operator_font, pixel_operator_font_size); + + 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); duk_put_global_string(ctx, "wii"); // evaluate functions from game.js @@ -102,6 +50,7 @@ int main(int argc, char **argv) { while (running) { PAD_ScanPads(); + // call update function duk_get_global_string(ctx, "update"); duk_call(ctx, 0); duk_pop(ctx); @@ -109,10 +58,7 @@ int main(int argc, char **argv) { GRRLIB_Render(); } - // printf("destroying heap\n"); duk_destroy_heap(ctx); - - // printf("exit time\n"); GRRLIB_Exit(); return 0; diff --git a/source/wii_duk.c b/source/wii_duk.c new file mode 100644 index 0000000..e0d9c5a --- /dev/null +++ b/source/wii_duk.c @@ -0,0 +1,76 @@ +#include "wii_duk.h" +#include "data.h" +#include "grrlib_duk.h" + +static GRRLIB_ttfFont * font; +static bool * running; +static unsigned int green = RGBA(0, 255, 0, 255); + +static duk_ret_t native_print(duk_context *ctx) { + GRRLIB_PrintfTTF(0, 0, font, duk_to_string(ctx, 0), 32, green); + return 0; +} + +static duk_ret_t native_exit(duk_context *ctx) { + *running = false; + return 0; +} + +static duk_ret_t pad_buttons_down(duk_context *ctx) { + u32 pressed = PAD_ButtonsDown(0); + duk_push_number(ctx, pressed); + return 1; +} + +static duk_ret_t get_file(duk_context *ctx) { + const char* filename = duk_to_string(ctx, 0); + + void* ptr; + size_t size; + + if (get_file_pointer(filename, &ptr, &size)) { + duk_idx_t file_obj = duk_push_object(ctx); + + duk_push_number(ctx, size); + duk_put_prop_string(ctx, file_obj, "size"); + + duk_push_pointer(ctx, ptr); + duk_put_prop_string(ctx, file_obj, "file_ptr"); + + duk_push_string(ctx, filename); + duk_put_prop_string(ctx, file_obj, "filename"); + + return 1; + } + + return duk_error(ctx, DUK_ERR_ERROR, "Error trying to get the file %s.", filename); +} + +static void define_pad_object(duk_context *ctx) { + duk_idx_t pad_obj = duk_push_object(ctx); + + duk_push_c_function(ctx, pad_buttons_down, 0); + duk_put_prop_string(ctx, pad_obj, "buttons_down"); +} + +void define_wii_object(duk_context *ctx, struct GRRLIB_Font * grrlib_font, bool * global_running) { + font = grrlib_font; + running = global_running; + + duk_idx_t wii_obj = duk_push_object(ctx); + + duk_push_c_function(ctx, native_print, 1); + duk_put_prop_string(ctx, wii_obj, "print"); + + duk_push_c_function(ctx, native_exit, 0); + duk_put_prop_string(ctx, wii_obj, "exit"); + + define_pad_object(ctx); + duk_put_prop_string(ctx, wii_obj, "pad"); + + define_grrlib_object(ctx); + duk_put_prop_string(ctx, wii_obj, "grrlib"); + + duk_push_c_function(ctx, get_file, 1); + duk_put_prop_string(ctx, wii_obj, "get_file"); +} diff --git a/source/wii_duk.h b/source/wii_duk.h new file mode 100644 index 0000000..071f088 --- /dev/null +++ b/source/wii_duk.h @@ -0,0 +1,6 @@ +#ifndef WII_DUK_H +#define WII_DUK_H +#include +#include +void define_wii_object(duk_context * ctx, struct GRRLIB_Font * grrlib_font, bool * global_running); +#endif diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..11049c6 --- /dev/null +++ b/types.d.ts @@ -0,0 +1,15 @@ +declare interface RGBA {} +declare interface FilePtr {} +declare interface GRRLibTexture {} + +declare namespace wii { + function exit(): void + function get_file(filename: string) : FilePtr + namespace grrlib { + function load_texture(file: FilePtr) : GRRLibTexture + function fill_screen(color: RGBA): void + function draw_img(xPos: number, yPos: number, texture: GRRLibTexture, degrees: number, scaleX: number, scaleY: number, color: RGBA) : void + function rectangle(x: number, y: number, width: number, height: number, color: RGBA, filled: bool) : void + } +} + diff --git a/yellowbird-midflap.png b/yellowbird-midflap.png new file mode 100644 index 0000000..2ca3c2d Binary files /dev/null and b/yellowbird-midflap.png differ