From e9349f026a5d80ef5c2dd50508643a9dd6e0e5c6 Mon Sep 17 00:00:00 2001 From: Fries Date: Wed, 19 Jun 2024 21:08:16 -0700 Subject: [PATCH] add grrlib. also restructure the code so the js file has an update and start function that gets called. --- .gitmodules | 3 + .vscode/launch.json | 34 +++++++++++ CMakeLists.txt | 2 +- dependencies/CMakeLists.txt | 2 + dependencies/GRRLIB/CMakeLists.txt | 42 ++++++++++++++ dependencies/GRRLIB/source | 1 + dolphin.sh | 2 + source/CMakeLists.txt | 1 + source/game.js | 7 ++- source/main.c | 93 +++++++++++------------------- 10 files changed, 124 insertions(+), 63 deletions(-) create mode 100644 .gitmodules create mode 100644 .vscode/launch.json create mode 100644 dependencies/CMakeLists.txt create mode 100644 dependencies/GRRLIB/CMakeLists.txt create mode 160000 dependencies/GRRLIB/source create mode 100755 dolphin.sh diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..05140dc --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "dependencies/GRRLIB/source"] + path = dependencies/GRRLIB/source + url = https://git.fries.gay/fries/GRRLIB diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b17c896 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,34 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/source/WiiDuktape.elf", + "args": [], + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "/opt/devkitpro/devkitPPC/bin/powerpc-eabi-gdb", + "miDebuggerServerAddress": "127.0.0.1:42069", + "debugServerPath": "${workspaceFolder}/dolphin.sh", + "debugServerArgs": "-e ${workspaceFolder}/build/source/WiiDuktape.elf --batch" + }, + { + "name": "(gdb) Attach", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/source/WiiDuktape.elf", + "cwd": "${fileDirname}", + "MIMode": "gdb", + "miDebuggerPath": "/opt/devkitpro/devkitPPC/bin/powerpc-eabi-gdb", + "miDebuggerServerAddress": "127.0.0.1:42069", + }, + ] +} diff --git a/CMakeLists.txt b/CMakeLists.txt index 61f1759..3e2cbcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,5 +11,5 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) project(WiiDuktape) -add_subdirectory(dependencies/duktape) +add_subdirectory(dependencies) add_subdirectory(source) diff --git a/dependencies/CMakeLists.txt b/dependencies/CMakeLists.txt new file mode 100644 index 0000000..9a12469 --- /dev/null +++ b/dependencies/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(duktape) +add_subdirectory(GRRLIB) diff --git a/dependencies/GRRLIB/CMakeLists.txt b/dependencies/GRRLIB/CMakeLists.txt new file mode 100644 index 0000000..4ec456f --- /dev/null +++ b/dependencies/GRRLIB/CMakeLists.txt @@ -0,0 +1,42 @@ +cmake_minimum_required(VERSION 3.13) + +project(GRRLIB) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(PNG REQUIRED libpng) +pkg_check_modules(FREETYPE REQUIRED freetype2) + + +set(GRRLIB_DIR ./source/GRRLIB/GRRLIB) +set(PNGU_DIR ./source/GRRLIB/lib/pngu) + +set (pngu_sources + ${PNGU_DIR}/pngu.c +) +add_library(pngu ${pngu_sources}) +target_link_libraries(pngu PRIVATE ${PNG_LIBRARIES}) +target_include_directories(pngu PRIVATE ${PNG_INCLUDE_DIRS}) +target_include_directories(pngu PUBLIC ${PNGU_DIR}) + +set(grrlib_sources + ${GRRLIB_DIR}/GRRLIB_3D.c + ${GRRLIB_DIR}/GRRLIB_bmf.c + ${GRRLIB_DIR}/GRRLIB_bmfx.c + ${GRRLIB_DIR}/GRRLIB_core.c + ${GRRLIB_DIR}/GRRLIB_fbAdvanced.c + ${GRRLIB_DIR}/GRRLIB_fileIO.c + ${GRRLIB_DIR}/GRRLIB_gecko.c + ${GRRLIB_DIR}/GRRLIB_print.c + ${GRRLIB_DIR}/GRRLIB_render.c + ${GRRLIB_DIR}/GRRLIB_snapshot.c + ${GRRLIB_DIR}/GRRLIB_ttf.c +) + +add_library(GRRLIB ${grrlib_sources}) +target_link_libraries(GRRLIB PRIVATE pngu) +target_link_libraries(GRRLIB PRIVATE ${FREETYPE_LIBRARIES}) +target_link_libraries(GRRLIB PRIVATE fat) +target_include_directories(GRRLIB PRIVATE ${FREETYPE_INCLUDE_DIRS}) + +target_include_directories(GRRLIB PUBLIC ${GRRLIB_DIR}) + diff --git a/dependencies/GRRLIB/source b/dependencies/GRRLIB/source new file mode 160000 index 0000000..d93847e --- /dev/null +++ b/dependencies/GRRLIB/source @@ -0,0 +1 @@ +Subproject commit d93847e6a3e350bd1157d61cc1315d8bbff76968 diff --git a/dolphin.sh b/dolphin.sh new file mode 100755 index 0000000..c2329c1 --- /dev/null +++ b/dolphin.sh @@ -0,0 +1,2 @@ +#!/bin/sh +QT_QPA_PLATFORM=xcb /usr/bin/dolphin-emu $@ diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 0fc4071..dddf9f8 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -7,4 +7,5 @@ add_custom_command( add_executable(WiiDuktape main.c game.c) target_link_libraries(WiiDuktape duktape) +target_link_libraries(WiiDuktape GRRLIB) ogc_create_dol(WiiDuktape) diff --git a/source/game.js b/source/game.js index 6da2202..bf128e2 100644 --- a/source/game.js +++ b/source/game.js @@ -1,9 +1,10 @@ const PAD_BUTTON_START = 0x1000; const PAD_BUTTON_A = 0x0100; -while (wii.running) { - wii.pad.scan_pads(); +function start() { +} +function update() { const pressed = wii.pad.buttons_down(); if (pressed & PAD_BUTTON_START) { @@ -15,5 +16,5 @@ while (wii.running) { wii.print("a pressed"); } - wii.video.wait_vsync(); + wii.grrlib.fill_screen(); } diff --git a/source/main.c b/source/main.c index 3c04970..554dca3 100644 --- a/source/main.c +++ b/source/main.c @@ -1,32 +1,25 @@ -#include -#include -#include -#include +#include #include -static void *xfb = NULL; -static GXRModeObj *rmode = NULL; extern const char* program; +static bool running = true; static duk_ret_t native_print(duk_context *ctx) { - printf("%s\n", duk_to_string(ctx, 0)); + GRRLIB_GeckoPrintf("%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"); + running = false; + return 0; } static void duk_fatal_error(void *udata, const char *msg) { - printf("an error has occured: %s\n", msg); + GRRLIB_GeckoPrintf("an error has occured: %s\n", msg); } -static duk_ret_t video_wait_vsync(duk_context *ctx) { - VIDEO_WaitVSync(); +static duk_ret_t grrlib_fill_screen(duk_context *ctx) { + GRRLIB_FillScreen(0xFFFFFFFF); return 0; } @@ -51,11 +44,11 @@ static void define_pad_object(duk_context *ctx) { 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); +static void define_grrlib_object(duk_context *ctx) { + duk_idx_t grrlib_obj = duk_push_object(ctx); - duk_push_c_function(ctx, video_wait_vsync, 0); - duk_put_prop_string(ctx, video_obj, "wait_vsync"); + duk_push_c_function(ctx, grrlib_fill_screen, 0); + duk_put_prop_string(ctx, grrlib_obj, "fill_screen"); } static void define_wii_object(duk_context *ctx) { @@ -70,63 +63,45 @@ static void define_wii_object(duk_context *ctx) { define_pad_object(ctx); duk_put_prop_string(ctx, wii_obj, "pad"); - define_video_object(ctx); - duk_put_prop_string(ctx, wii_obj, "video"); + define_grrlib_object(ctx); + duk_put_prop_string(ctx, wii_obj, "grrlib"); duk_push_boolean(ctx, true); duk_put_prop_string(ctx, wii_obj, "running"); } int main(int argc, char **argv) { - // Initialise the video system - VIDEO_Init(); - - // This function initialises the attached controllers + GRRLIB_Init(); PAD_Init(); - // 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); define_wii_object(ctx); duk_put_global_string(ctx, "wii"); + // evaluate functions from game.js duk_eval_string_noresult(ctx, program); - printf("destroying heap\n"); + // 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"); + // printf("exit time\n"); + GRRLIB_Exit(); return 0; }