WiiDuktape/source/wii_duk.c

87 lines
2.1 KiB
C
Raw Normal View History

#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 float * deltatime;
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) {
unsigned int 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 duk_ret_t get_deltatime(duk_context * ctx) {
duk_push_number(ctx, *deltatime);
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_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, float * global_deltatime) {
font = grrlib_font;
running = global_running;
deltatime = global_deltatime;
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");
duk_push_c_function(ctx, get_deltatime, 0);
duk_put_prop_string(ctx, wii_obj, "get_deltatime");
}