WiiDuktape/source/main.c

131 lines
3 KiB
C
Raw Normal View History

#include <grrlib.h>
2024-06-19 04:51:50 +00:00
#include <duktape.h>
#include <ogc/pad.h>
#include <stdio.h>
#include <stdlib.h>
#include <ogc/lwp_watchdog.h>
#include <data.h>
#include <duktape_engine.h>
2024-06-19 04:51:50 +00:00
#define LENGTH(arr) (sizeof(arr) / sizeof(arr[0]))
static bool launcher_running = true;
static bool running = false;
static GRRLIB_ttfFont * font;
static unsigned int green = RGBA(0, 255, 0, 255);
static unsigned int white = RGBA(255, 255, 255, 255);
struct menu_item {
const char * message;
void (*entrypoint)();
};
struct menu_items {
size_t selected_index;
struct menu_item * items[2];
};
static struct menu_items items = {
.selected_index = 0,
.items = {}
};
static struct menu_item * create_menu_item(const char * message, void (*entrypoint)()) {
struct menu_item * item = malloc(sizeof(struct menu_item));
item->message = message;
item->entrypoint = entrypoint;
return item;
}
static void start_duktape() {
running = true;
duktape(font, &running);
}
static void start_quickjs() {
running = true;
while (running) {
PAD_ScanPads();
unsigned int pressed = PAD_ButtonsDown(0);
GRRLIB_PrintfTTF(0, 0, font, "QuickJS is not implemented, yet..", 48, RGBA(255, 255, 255, 255));
GRRLIB_Render();
if (pressed & PAD_BUTTON_START) {
running = false;
}
}
}
static void update_menu(unsigned int * pressed) {
if (*pressed & PAD_BUTTON_DOWN) {
int selected_index = items.selected_index;
selected_index += 1;
if (selected_index >= LENGTH(items.items)) {
selected_index = 0;
}
items.selected_index = selected_index;
} else if (*pressed & PAD_BUTTON_UP) {
int selected_index = items.selected_index;
selected_index -= 1;
if (selected_index < 0) {
selected_index = LENGTH(items.items) - 1;
}
items.selected_index = selected_index;
}
if (*pressed & PAD_BUTTON_A) {
items.items[items.selected_index]->entrypoint();
}
for (size_t i = 0; i < LENGTH(items.items); i++) {
unsigned int color;
if (items.selected_index == i) {
color = green;
} else {
color = white;
}
GRRLIB_PrintfTTF(0, 80 + (50 * i), font, items.items[i]->message, 64, color);
}
}
static void launcher_loop() {
while (launcher_running) {
PAD_ScanPads();
unsigned int pressed = PAD_ButtonsDown(0);
if (pressed & PAD_BUTTON_START) {
launcher_running = false;
return;
}
GRRLIB_PrintfTTF(0, 0, font, "WiiDuktape Launcher", 64, RGBA(255, 255, 255, 255));
update_menu(&pressed);
GRRLIB_Render();
}
}
2024-06-19 04:51:50 +00:00
int main(int argc, char **argv) {
GRRLIB_SetAntiAliasing(false);
GRRLIB_Init();
PAD_Init();
2024-06-19 04:51:50 +00:00
// get the pixel operator font's data pointer and size
void * pixel_operator_font;
size_t pixel_operator_font_size;
get_file_pointer("PixelOperator.ttf", &pixel_operator_font, &pixel_operator_font_size);
font = GRRLIB_LoadTTF(pixel_operator_font, pixel_operator_font_size);
2024-06-19 04:51:50 +00:00
items.items[0] = create_menu_item("Duktape", start_duktape);
items.items[1] = create_menu_item("QuickJS", start_quickjs);
launcher_loop();
for (size_t i = 0; i < LENGTH(items.items); i++) {
// clear out heap allocated memory.
free(items.items[i]);
}
GRRLIB_Exit();
2024-06-19 04:51:50 +00:00
return 0;
}