From d1dca0720c2b904849369b3c141cf9918e529a2b Mon Sep 17 00:00:00 2001 From: Fries Date: Wed, 19 Jun 2024 23:37:41 -0700 Subject: [PATCH] draw a square with color. --- source/CMakeLists.txt | 2 +- source/game.js | 32 +++++++++++++++++++++++++++++++- source/grrlib_duk.c | 40 ++++++++++++++++++++++++++++++++++++++++ source/grrlib_duk.h | 5 +++++ source/main.c | 13 +------------ 5 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 source/grrlib_duk.c create mode 100644 source/grrlib_duk.h diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index dddf9f8..5a9ef26 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -5,7 +5,7 @@ add_custom_command( COMMENT "Creating game.c from game.js" ) -add_executable(WiiDuktape main.c game.c) +add_executable(WiiDuktape main.c grrlib_duk.c game.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 bf128e2..c68d0a1 100644 --- a/source/game.js +++ b/source/game.js @@ -1,5 +1,33 @@ +function rgba(r, g, b, a) { + const buffer = Uint8Array.allocPlain(4); + buffer[0] = r; + buffer[1] = g; + buffer[2] = b; + buffer[3] = a; + return buffer; +} + const PAD_BUTTON_START = 0x1000; const PAD_BUTTON_A = 0x0100; +const RED_COLOR = rgba(255, 0, 0, 255); +const GREEN_COLOR = rgba(0, 255, 0, 255); + +function rgba_compare(rgba1, rgba2) { + return (rgba1[0] == rgba2[0]) && + (rgba1[1] == rgba2[1]) && + (rgba1[2] == rgba2[2]) && + (rgba1[3] == rgba2[3]); +} + +globalThis.rectangle_color = RED_COLOR; + +function swap_colors() { + if (rgba_compare(rectangle_color, RED_COLOR)) { + rectangle_color = GREEN_COLOR; + } else if (rgba_compare(rectangle_color, GREEN_COLOR)) { + rectangle_color = RED_COLOR; + } +} function start() { } @@ -14,7 +42,9 @@ function update() { if (pressed & PAD_BUTTON_A) { wii.print("a pressed"); + swap_colors(); } - wii.grrlib.fill_screen(); + wii.grrlib.fill_screen(rgba(255, 255, 255, 255)); + wii.grrlib.rectangle(0, 0, 320, 240, rectangle_color, true); } diff --git a/source/grrlib_duk.c b/source/grrlib_duk.c new file mode 100644 index 0000000..ea2f197 --- /dev/null +++ b/source/grrlib_duk.c @@ -0,0 +1,40 @@ +#include "grrlib_duk.h" +#include + +static unsigned int get_color(duk_context *ctx, duk_idx_t index) { + size_t rgba_size; + unsigned char * rgba = duk_get_buffer(ctx, index, &rgba_size); + return rgba[0] << 24 | rgba[1] << 16 | rgba[2] << 8 | rgba[3]; +} + +static duk_ret_t grrlib_fill_screen(duk_context *ctx) { + unsigned int color = get_color(ctx, 0); + + GRRLIB_FillScreen(color); + return 0; +} + +static duk_ret_t grrlib_rectangle(duk_context *ctx) { + float x = duk_to_number(ctx, 0); + + float y = duk_to_number(ctx, 1); + float width = duk_to_number(ctx, 2); + float height = duk_to_number(ctx, 3); + + unsigned int color = get_color(ctx, 4); + + bool filled = duk_to_boolean(ctx, 5); + + GRRLIB_Rectangle(x, y, width, height, color, filled); + return 0; +} + +void define_grrlib_object(duk_context *ctx) { + duk_idx_t grrlib_obj = duk_push_object(ctx); + + duk_push_c_function(ctx, grrlib_fill_screen, 1); + duk_put_prop_string(ctx, grrlib_obj, "fill_screen"); + + duk_push_c_function(ctx, grrlib_rectangle, 6); + duk_put_prop_string(ctx, grrlib_obj, "rectangle"); +} diff --git a/source/grrlib_duk.h b/source/grrlib_duk.h new file mode 100644 index 0000000..7c6b87a --- /dev/null +++ b/source/grrlib_duk.h @@ -0,0 +1,5 @@ +#ifndef GRRLIB_DUK_H +#define GRRLIB_DUK_H +#include +void define_grrlib_object(duk_context *ctx); +#endif diff --git a/source/main.c b/source/main.c index 554dca3..5c71b3a 100644 --- a/source/main.c +++ b/source/main.c @@ -1,5 +1,6 @@ #include #include +#include "grrlib_duk.h" extern const char* program; static bool running = true; @@ -18,11 +19,6 @@ static void duk_fatal_error(void *udata, const char *msg) { GRRLIB_GeckoPrintf("an error has occured: %s\n", msg); } -static duk_ret_t grrlib_fill_screen(duk_context *ctx) { - GRRLIB_FillScreen(0xFFFFFFFF); - return 0; -} - static duk_ret_t pad_scan_pads() { PAD_ScanPads(); return 0; @@ -44,13 +40,6 @@ static void define_pad_object(duk_context *ctx) { duk_put_prop_string(ctx, pad_obj, "buttons_down"); } -static void define_grrlib_object(duk_context *ctx) { - duk_idx_t grrlib_obj = duk_push_object(ctx); - - duk_push_c_function(ctx, grrlib_fill_screen, 0); - duk_put_prop_string(ctx, grrlib_obj, "fill_screen"); -} - static void define_wii_object(duk_context *ctx) { duk_idx_t wii_obj = duk_push_object(ctx);