WiiDuktape/source/main.c
Fries 9f1b00d39d add file support
file loading is implemented with an asset generator script that reads
all the files in the data folder and converts them into c arrays and
puts them inside a c source file.
2024-06-20 18:41:49 -07:00

119 lines
2.5 KiB
C

#include <grrlib.h>
#include <duktape.h>
#include <stdint.h>
#include "grrlib_duk.h"
#include "data.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 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");
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_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_c_function(ctx, get_file, 1);
duk_put_prop_string(ctx, wii_obj, "get_file");
}
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;
}