WiiDuktape/source/wii_duk.c
Fries 3362687710 get the bird to draw.
the bird now draws! i also got a font loaded too.
2024-06-21 00:45:28 -07:00

76 lines
1.8 KiB
C

#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");
}