2024-06-21 07:45:28 +00:00
|
|
|
#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);
|
2024-06-22 05:57:08 +00:00
|
|
|
static float * deltatime;
|
2024-06-21 07:45:28 +00:00
|
|
|
|
2024-06-22 05:57:08 +00:00
|
|
|
static duk_ret_t native_print(duk_context * ctx) {
|
2024-06-21 07:45:28 +00:00
|
|
|
GRRLIB_PrintfTTF(0, 0, font, duk_to_string(ctx, 0), 32, green);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-22 05:57:08 +00:00
|
|
|
static duk_ret_t native_exit(duk_context * ctx) {
|
2024-06-21 07:45:28 +00:00
|
|
|
*running = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-22 05:57:08 +00:00
|
|
|
static duk_ret_t pad_buttons_down(duk_context * ctx) {
|
|
|
|
unsigned int pressed = PAD_ButtonsDown(0);
|
2024-06-21 07:45:28 +00:00
|
|
|
duk_push_number(ctx, pressed);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-22 05:57:08 +00:00
|
|
|
static duk_ret_t get_file(duk_context * ctx) {
|
2024-06-21 07:45:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-06-22 05:57:08 +00:00
|
|
|
static duk_ret_t get_deltatime(duk_context * ctx) {
|
|
|
|
duk_push_number(ctx, *deltatime);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-21 07:45:28 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2024-06-22 05:57:08 +00:00
|
|
|
void define_wii_object(duk_context * ctx, struct GRRLIB_Font * grrlib_font, bool * global_running, float * global_deltatime) {
|
2024-06-21 07:45:28 +00:00
|
|
|
font = grrlib_font;
|
2024-06-22 05:57:08 +00:00
|
|
|
running = global_running;
|
|
|
|
deltatime = global_deltatime;
|
2024-06-21 07:45:28 +00:00
|
|
|
|
|
|
|
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");
|
2024-06-22 05:57:08 +00:00
|
|
|
|
|
|
|
duk_push_c_function(ctx, get_deltatime, 0);
|
|
|
|
duk_put_prop_string(ctx, wii_obj, "get_deltatime");
|
2024-06-21 07:45:28 +00:00
|
|
|
}
|