WiiDuktape/source/main.c
2024-06-19 23:37:41 -07:00

96 lines
2 KiB
C

#include <grrlib.h>
#include <duktape.h>
#include "grrlib_duk.h"
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 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 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_boolean(ctx, true);
duk_put_prop_string(ctx, wii_obj, "running");
}
int main(int argc, char **argv) {
GRRLIB_Init();
PAD_Init();
duk_context *ctx = duk_create_heap(NULL, NULL, NULL, NULL, duk_fatal_error);
define_wii_object(ctx);
duk_put_global_string(ctx, "wii");
// evaluate functions from game.js
duk_eval_string_noresult(ctx, program);
// call start function
duk_get_global_string(ctx, "start");
duk_call(ctx, 0);
duk_pop(ctx);
while (running) {
PAD_ScanPads();
duk_get_global_string(ctx, "update");
duk_call(ctx, 0);
duk_pop(ctx);
GRRLIB_Render();
}
// printf("destroying heap\n");
duk_destroy_heap(ctx);
// printf("exit time\n");
GRRLIB_Exit();
return 0;
}