From 4737ff0f5804f3c32783441655470caf902b0538 Mon Sep 17 00:00:00 2001 From: Wouter Wijsman Date: Fri, 16 Aug 2024 15:28:44 +0200 Subject: [PATCH 1/3] Improve basic programs page with better code It did not make sense for me that the SDL2 and shape example looks slightly different. While changing this I also found some bugs in some of the examples and make the text look a little bit better. Hope this isn't too much for one PR. --- _includes/samples/sdl2/main.c | 7 ++++++- _includes/samples/shape/main.c | 30 +++++++++++++++++++++++++++--- basic_programs.md | 18 ++++++++++-------- images/sdl2.png | Bin 371 -> 0 bytes images/shape.png | Bin 338 -> 371 bytes 5 files changed, 43 insertions(+), 12 deletions(-) delete mode 100644 images/sdl2.png diff --git a/_includes/samples/sdl2/main.c b/_includes/samples/sdl2/main.c index e705689..a4886c6 100644 --- a/_includes/samples/sdl2/main.c +++ b/_includes/samples/sdl2/main.c @@ -20,17 +20,22 @@ int main(int argc, char *argv[]) int running = 1; SDL_Event event; while (running) { + // Process input if (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: + // If the close button is pressed on pc, close the program running = 0; break; case SDL_CONTROLLERDEVICEADDED: + // Connect a controller when it is connected SDL_GameControllerOpen(event.cdevice.which); break; case SDL_CONTROLLERBUTTONDOWN: - if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) + if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) { + // Close the program if start is pressed running = 0; + } break; } } diff --git a/_includes/samples/shape/main.c b/_includes/samples/shape/main.c index e5f3687..9660340 100644 --- a/_includes/samples/shape/main.c +++ b/_includes/samples/shape/main.c @@ -12,6 +12,25 @@ PSP_MAIN_THREAD_ATTR(THREAD_ATTR_VFPU | THREAD_ATTR_USER); char list[0x20000] __attribute__((aligned(64))); +int exit_callback(int arg1, int arg2, void *common) { + sceKernelExitGame(); + return 0; +} + +int callback_thread(SceSize args, void *argp) { + int cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL); + sceKernelRegisterExitCallback(cbid); + sceKernelSleepThreadCB(); + return 0; +} + +int setup_callbacks(void) { + int thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0); + if(thid >= 0) + sceKernelStartThread(thid, 0, 0); + return thid; +} + void initGu(){ sceGuInit(); @@ -67,8 +86,8 @@ void drawRect(float x, float y, float w, float h) { vertices[0].x = x; vertices[0].y = y; - vertices[1].x = y + w; - vertices[1].y = x + h; + vertices[1].x = x + w; + vertices[1].y = y + h; sceGuColor(0xFF0000FF); // Red, colors are ABGR sceGuDrawArray(GU_SPRITES, GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, 0, vertices); @@ -76,12 +95,17 @@ void drawRect(float x, float y, float w, float h) { int main() { + // Make exiting with the home button possible + setup_callbacks(); + + // Setup the library used for rendering initGu(); + int running = 1; while(running){ startFrame(); - drawRect(32, 32, 64, 64); + drawRect(216, 96, 34, 64); endFrame(); } diff --git a/basic_programs.md b/basic_programs.md index 88e6216..2dfdfca 100644 --- a/basic_programs.md +++ b/basic_programs.md @@ -12,7 +12,7 @@ nav_order: 3 ![](images/hello.png) -> This is a simple Hello World program for the PSP. +This is a simple Hello World program for the PSP. Click on view source below to see the code and how to build it. @@ -50,7 +50,7 @@ This will result in an EBOOT.PBP file in the build directory. Put it in a direct ![](images/shape.png) -> This is a simple square drawn on the PSP. It uses the native libgu library. +This is a simple square drawn on the PSP. It uses the native libgu library. Click on view source below to see the code and how to build it. @@ -89,7 +89,7 @@ More libgu examples can be found SDL2 is a library which handles system specific things like input, audio and window management for you. It can also be used to render shapes and images, just like the native libgu. This will be slower, but will result in code that can be run more easily on multiple platforms. +Despite this example adding an option to close by pressing the start button, the code is much shorter. It can even be build for Linux without any further modifications. Click on view source below for the to see the code and how to build it. @@ -212,7 +214,7 @@ More documentation on SDL can be found