draw a square with color.

This commit is contained in:
Fries 2024-06-19 23:37:41 -07:00
parent e9349f026a
commit d1dca0720c
5 changed files with 78 additions and 14 deletions

View file

@ -5,7 +5,7 @@ add_custom_command(
COMMENT "Creating game.c from game.js" 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 duktape)
target_link_libraries(WiiDuktape GRRLIB) target_link_libraries(WiiDuktape GRRLIB)
ogc_create_dol(WiiDuktape) ogc_create_dol(WiiDuktape)

View file

@ -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_START = 0x1000;
const PAD_BUTTON_A = 0x0100; 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() { function start() {
} }
@ -14,7 +42,9 @@ function update() {
if (pressed & PAD_BUTTON_A) { if (pressed & PAD_BUTTON_A) {
wii.print("a pressed"); 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);
} }

40
source/grrlib_duk.c Normal file
View file

@ -0,0 +1,40 @@
#include "grrlib_duk.h"
#include <grrlib.h>
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");
}

5
source/grrlib_duk.h Normal file
View file

@ -0,0 +1,5 @@
#ifndef GRRLIB_DUK_H
#define GRRLIB_DUK_H
#include <duktape.h>
void define_grrlib_object(duk_context *ctx);
#endif

View file

@ -1,5 +1,6 @@
#include <grrlib.h> #include <grrlib.h>
#include <duktape.h> #include <duktape.h>
#include "grrlib_duk.h"
extern const char* program; extern const char* program;
static bool running = true; 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); 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() { static duk_ret_t pad_scan_pads() {
PAD_ScanPads(); PAD_ScanPads();
return 0; return 0;
@ -44,13 +40,6 @@ static void define_pad_object(duk_context *ctx) {
duk_put_prop_string(ctx, pad_obj, "buttons_down"); 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) { static void define_wii_object(duk_context *ctx) {
duk_idx_t wii_obj = duk_push_object(ctx); duk_idx_t wii_obj = duk_push_object(ctx);