WiiDuktape/source/duktape/wii_duk.c

92 lines
2.3 KiB
C
Raw Permalink Normal View History

#include "wii_duk.h"
#include "data.h"
#include "grrlib_duk.h"
#include "rgba.h"
#include <wii_obj.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) {
shared_native_print(duk_to_string(ctx, 0), font);
return 0;
}
static duk_ret_t native_exit(duk_context * ctx) {
shared_native_exit(running);
return 0;
}
static duk_ret_t pad_buttons_down(duk_context * ctx) {
duk_push_number(ctx, shared_pad_buttons_down());
return 1;
}
static duk_ret_t get_file(duk_context * ctx) {
const char* filename = duk_to_string(ctx, 0);
shared_file_obj file = shared_get_file(filename);
if (shared_is_valid_file(file)) {
duk_idx_t file_obj = duk_push_object(ctx);
duk_push_number(ctx, file.size);
duk_put_prop_string(ctx, file_obj, "size");
duk_push_pointer(ctx, file.file_ptr);
duk_put_prop_string(ctx, file_obj, "file_ptr");
duk_push_string(ctx, file.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 duk_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");
duk_push_c_function(ctx, rgba, 4);
duk_put_prop_string(ctx, wii_obj, "rgba");
duk_push_c_function(ctx, compare_rgba, 2);
duk_put_prop_string(ctx, wii_obj, "compare_rgba");
}