WiiDuktape/source/main.c

133 lines
3.3 KiB
C
Raw Normal View History

2024-06-19 04:51:50 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <ogcsys.h>
2024-06-19 04:51:50 +00:00
#include <duktape.h>
static void *xfb = NULL;
static GXRModeObj *rmode = NULL;
extern const char* program;
2024-06-19 04:51:50 +00:00
static duk_ret_t native_print(duk_context *ctx) {
printf("%s\n", duk_to_string(ctx, 0));
return 0;
}
static duk_ret_t native_exit(duk_context *ctx) {
duk_push_global_object(ctx);
duk_push_string(ctx, "wii");
duk_get_prop(ctx, -2);
duk_push_boolean(ctx, false);
duk_put_prop_string(ctx, -2, "running");
}
static void duk_fatal_error(void *udata, const char *msg) {
printf("an error has occured: %s\n", msg);
}
static duk_ret_t video_wait_vsync(duk_context *ctx) {
VIDEO_WaitVSync();
return 0;
}
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_video_object(duk_context *ctx) {
duk_idx_t video_obj = duk_push_object(ctx);
duk_push_c_function(ctx, video_wait_vsync, 0);
duk_put_prop_string(ctx, video_obj, "wait_vsync");
}
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_video_object(ctx);
duk_put_prop_string(ctx, wii_obj, "video");
duk_push_boolean(ctx, true);
duk_put_prop_string(ctx, wii_obj, "running");
}
2024-06-19 04:51:50 +00:00
int main(int argc, char **argv) {
// Initialise the video system
VIDEO_Init();
// This function initialises the attached controllers
PAD_Init();
2024-06-19 04:51:50 +00:00
// Obtain the preferred video mode from the system
// This will correspond to the settings in the Wii menu
rmode = VIDEO_GetPreferredMode(NULL);
// Allocate memory for the display in the uncached region
xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
// Initialise the console, required for printf
console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
// Set up the video registers with the chosen mode
VIDEO_Configure(rmode);
// Tell the video hardware where our display memory is
VIDEO_SetNextFramebuffer(xfb);
// Make the display visible
VIDEO_SetBlack(false);
// Flush the video register changes to the hardware
VIDEO_Flush();
// Wait for Video setup to complete
VIDEO_WaitVSync();
if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
// The console understands VT terminal escape codes
// This positions the cursor on row 2, column 0
// we can use variables for this with format codes too
// e.g. printf ("\x1b[%d;%dH", row, column );
printf("\x1b[2;0H");
duk_context *ctx = duk_create_heap(NULL, NULL, NULL, NULL, duk_fatal_error);
2024-06-19 04:51:50 +00:00
define_wii_object(ctx);
duk_put_global_string(ctx, "wii");
2024-06-19 04:51:50 +00:00
duk_eval_string_noresult(ctx, program);
2024-06-19 04:51:50 +00:00
printf("destroying heap\n");
duk_destroy_heap(ctx);
2024-06-19 04:51:50 +00:00
printf("exit time\n");
2024-06-19 04:51:50 +00:00
return 0;
}