From 3b672e7aff32d93d5b8b8dea9ab5ede7fc276bdc Mon Sep 17 00:00:00 2001 From: Wouter Wijsman Date: Sun, 5 Jul 2026 20:29:07 +0200 Subject: [PATCH 1/5] Initial work porting examples to SDL3 --- .../samples/{sdl2 => sdl}/CMakeLists.txt | 9 +- _includes/samples/{sdl2 => sdl}/main.c | 21 ++-- _includes/samples/sdl2_ttf/main.c | 104 ------------------ .../{sdl2_image => sdl_image}/CMakeLists.txt | 16 +-- .../samples/{sdl2_image => sdl_image}/main.c | 36 +++--- .../{sdl2_mixer => sdl_mixer}/CMakeLists.txt | 0 .../samples/{sdl2_mixer => sdl_mixer}/main.c | 20 ++-- .../{sdl2_ttf => sdl_ttf}/CMakeLists.txt | 16 +-- _includes/samples/sdl_ttf/main.c | 102 +++++++++++++++++ basic_programs.md | 48 ++++---- images/{sdl2_mixer.png => sdl_mixer.png} | Bin images/{sdl2_ttf.jpg => sdl_ttf.jpg} | Bin 12 files changed, 175 insertions(+), 197 deletions(-) rename _includes/samples/{sdl2 => sdl}/CMakeLists.txt (66%) rename _includes/samples/{sdl2 => sdl}/main.c (68%) delete mode 100644 _includes/samples/sdl2_ttf/main.c rename _includes/samples/{sdl2_image => sdl_image}/CMakeLists.txt (55%) rename _includes/samples/{sdl2_image => sdl_image}/main.c (60%) rename _includes/samples/{sdl2_mixer => sdl_mixer}/CMakeLists.txt (100%) rename _includes/samples/{sdl2_mixer => sdl_mixer}/main.c (86%) rename _includes/samples/{sdl2_ttf => sdl_ttf}/CMakeLists.txt (54%) create mode 100644 _includes/samples/sdl_ttf/main.c rename images/{sdl2_mixer.png => sdl_mixer.png} (100%) rename images/{sdl2_ttf.jpg => sdl_ttf.jpg} (100%) diff --git a/_includes/samples/sdl2/CMakeLists.txt b/_includes/samples/sdl/CMakeLists.txt similarity index 66% rename from _includes/samples/sdl2/CMakeLists.txt rename to _includes/samples/sdl/CMakeLists.txt index 3383735..520f844 100644 --- a/_includes/samples/sdl2/CMakeLists.txt +++ b/_includes/samples/sdl/CMakeLists.txt @@ -1,16 +1,13 @@ cmake_minimum_required(VERSION 3.11) -project(sdl2) +project(sdl) add_executable(${PROJECT_NAME} main.c) -include(FindPkgConfig) -pkg_search_module(SDL2 REQUIRED sdl2) - -target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_INCLUDE_DIRS}) +find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3) target_link_libraries(${PROJECT_NAME} PRIVATE - ${SDL2_LIBRARIES} + SDL3::SDL3 ) if(PSP) diff --git a/_includes/samples/sdl2/main.c b/_includes/samples/sdl/main.c similarity index 68% rename from _includes/samples/sdl2/main.c rename to _includes/samples/sdl/main.c index fc7b6f4..bca1dbe 100644 --- a/_includes/samples/sdl2/main.c +++ b/_includes/samples/sdl/main.c @@ -1,21 +1,20 @@ -#include +#include +#include int main(int argc, char *argv[]) { - SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER); + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD); SDL_Window * window = SDL_CreateWindow( "window", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, 480, 272, 0 ); - SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + SDL_Renderer * renderer = SDL_CreateRenderer(window, NULL); - SDL_Rect square = {216, 96, 34, 64}; + SDL_FRect square = {216, 96, 34, 64}; int running = 1; SDL_Event event; @@ -23,16 +22,16 @@ int main(int argc, char *argv[]) // Process input if (SDL_PollEvent(&event)) { switch (event.type) { - case SDL_QUIT: + case SDL_EVENT_QUIT: // End the loop if the programs is being closed running = 0; break; - case SDL_CONTROLLERDEVICEADDED: + case SDL_EVENT_GAMEPAD_ADDED: // Connect a controller when it is connected - SDL_GameControllerOpen(event.cdevice.which); + SDL_OpenGamepad(event.cdevice.which); break; - case SDL_CONTROLLERBUTTONDOWN: - if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) { + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: + if(event.gbutton.button == SDL_GAMEPAD_BUTTON_START) { // Close the program if start is pressed running = 0; } diff --git a/_includes/samples/sdl2_ttf/main.c b/_includes/samples/sdl2_ttf/main.c deleted file mode 100644 index 949c577..0000000 --- a/_includes/samples/sdl2_ttf/main.c +++ /dev/null @@ -1,104 +0,0 @@ -#include - -#include -#include - -// Define screen dimensions -#define SCREEN_WIDTH 480 -#define SCREEN_HEIGHT 272 - -int main(int argc, char **argv) -{ - (void)argc; - (void)argv; - - // Initialize SDL2 - if (SDL_Init(SDL_INIT_VIDEO) < 0) - { - printf("SDL2 could not be initialized!\n" - "SDL2 Error: %s\n", - SDL_GetError()); - return 0; - } - - // Initialize SDL2_ttf - if (TTF_Init() < 0) - { - printf("SDL2_ttf could not be initialized!\n" - "SDL2_ttf Error: %s\n", - SDL_GetError()); - return 0; - } - - SDL_Window *win = SDL_CreateWindow( - "window", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - SCREEN_WIDTH, - SCREEN_HEIGHT, - 0); - - if (!win) - { - printf("Window could not be created!\n" - "SDL_Error: %s\n", - SDL_GetError()); - } - - SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, 0); - TTF_Font *font = TTF_OpenFont("Pacifico.ttf", 40); - - // Set the text and background color - SDL_Color text_color = {0xff, 0xff, 0xff, 0xff}; - SDL_Color bg_color = {0x00, 0x00, 0x00, 0xff}; - - SDL_Rect text_rect; - SDL_Surface *surface = TTF_RenderText(font, "Hello World!", text_color, bg_color); - SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); - - // Get text dimensions - text_rect.w = surface->w; - text_rect.h = surface->h; - - SDL_FreeSurface(surface); - - text_rect.x = (SCREEN_WIDTH - text_rect.w) / 2; - text_rect.y = text_rect.h + 30; - - int running = 1; - SDL_Event e; - while (running) - { - if (SDL_PollEvent(&e)) - { - switch (e.type) - { - case SDL_QUIT: - running = 0; - break; - case SDL_CONTROLLERDEVICEADDED: - SDL_GameControllerOpen(e.cdevice.which); - break; - case SDL_CONTROLLERBUTTONDOWN: - if (e.cbutton.button == SDL_CONTROLLER_BUTTON_START) - { - running = 0; - } - break; - } - } - - SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff); - SDL_RenderClear(renderer); - SDL_SetRenderDrawColor(renderer, 0xff, 0x00, 0x00, 0xff); - SDL_RenderCopy(renderer, texture, NULL, &text_rect); - SDL_RenderPresent(renderer); - } - - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(win); - TTF_Quit(); - SDL_Quit(); - - return 0; -} \ No newline at end of file diff --git a/_includes/samples/sdl2_image/CMakeLists.txt b/_includes/samples/sdl_image/CMakeLists.txt similarity index 55% rename from _includes/samples/sdl2_image/CMakeLists.txt rename to _includes/samples/sdl_image/CMakeLists.txt index a2c75b5..ed67726 100644 --- a/_includes/samples/sdl2_image/CMakeLists.txt +++ b/_includes/samples/sdl_image/CMakeLists.txt @@ -1,21 +1,15 @@ cmake_minimum_required(VERSION 3.11) -project(sdl2-image) +project(sdl-image) add_executable(${PROJECT_NAME} main.c) -include(FindPkgConfig) -pkg_search_module(SDL2 REQUIRED sdl2) -pkg_search_module(SDL2_IMAGE REQUIRED SDL2_image) - -target_include_directories(${PROJECT_NAME} PRIVATE - ${SDL2_INCLUDE_DIRS} - ${SDL2_IMAGE_INCLUDE_DIRS} -) +find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3) +find_package(SDL3_image REQUIRED) target_link_libraries(${PROJECT_NAME} PRIVATE - ${SDL2_LIBRARIES} - ${SDL2_IMAGE_LIBRARIES} + SDL3::SDL3 + SDL3_image::SDL3_image ) if(PSP) diff --git a/_includes/samples/sdl2_image/main.c b/_includes/samples/sdl_image/main.c similarity index 60% rename from _includes/samples/sdl2_image/main.c rename to _includes/samples/sdl_image/main.c index eafecc9..bdbc5aa 100644 --- a/_includes/samples/sdl2_image/main.c +++ b/_includes/samples/sdl_image/main.c @@ -1,36 +1,32 @@ -#include -#include +#include +#include +#include int main(int argc, char *argv[]) { - SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER); - - // Enable png support for SDL2_image - IMG_Init(IMG_INIT_PNG); + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD); SDL_Window * window = SDL_CreateWindow( "window", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, 480, 272, 0 ); - SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + SDL_Renderer * renderer = SDL_CreateRenderer(window, NULL); // Load the texture SDL_Surface * pixels = IMG_Load("grass.png"); SDL_Texture * sprite = SDL_CreateTextureFromSurface(renderer, pixels); - SDL_FreeSurface(pixels); + SDL_DestroySurface(pixels); // Store the dimensions of the texture - SDL_Rect sprite_rect; - SDL_QueryTexture(sprite, NULL, NULL, &sprite_rect.w, &sprite_rect.h); + SDL_FRect sprite_rect; + SDL_GetTextureSize(sprite, &sprite_rect.w, &sprite_rect.h); // Set the position to draw to in the middle of the screen - sprite_rect.x = 480/2 - sprite_rect.w/2; - sprite_rect.y = 272/2 - sprite_rect.h/2; + sprite_rect.x = 480.0f / 2.0f - sprite_rect.w / 2.0f; + sprite_rect.y = 272.0f / 2.0f - sprite_rect.h / 2.0f; int running = 1; SDL_Event event; @@ -38,16 +34,16 @@ int main(int argc, char *argv[]) // Process input if (SDL_PollEvent(&event)) { switch (event.type) { - case SDL_QUIT: + case SDL_EVENT_QUIT: // End the loop if the programs is being closed running = 0; break; - case SDL_CONTROLLERDEVICEADDED: + case SDL_EVENT_GAMEPAD_ADDED: // Connect a controller when it is connected - SDL_GameControllerOpen(event.cdevice.which); + SDL_OpenGamepad(event.cdevice.which); break; - case SDL_CONTROLLERBUTTONDOWN: - if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) { + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: + if(event.gbutton.button == SDL_GAMEPAD_BUTTON_START) { // Close the program if start is pressed running = 0; } @@ -59,7 +55,7 @@ int main(int argc, char *argv[]) SDL_RenderClear(renderer); // Draw the 'grass' sprite - SDL_RenderCopy(renderer, sprite, NULL, &sprite_rect); + SDL_RenderTexture(renderer, sprite, NULL, &sprite_rect); // Draw everything on a white background SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); diff --git a/_includes/samples/sdl2_mixer/CMakeLists.txt b/_includes/samples/sdl_mixer/CMakeLists.txt similarity index 100% rename from _includes/samples/sdl2_mixer/CMakeLists.txt rename to _includes/samples/sdl_mixer/CMakeLists.txt diff --git a/_includes/samples/sdl2_mixer/main.c b/_includes/samples/sdl_mixer/main.c similarity index 86% rename from _includes/samples/sdl2_mixer/main.c rename to _includes/samples/sdl_mixer/main.c index 95ef8a0..49bcdb1 100644 --- a/_includes/samples/sdl2_mixer/main.c +++ b/_includes/samples/sdl_mixer/main.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include // Define MIN macro #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) @@ -18,7 +18,7 @@ int main(int argc, char **argv) { // Initialize sdl SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | - SDL_INIT_GAMECONTROLLER + SDL_INIT_GAMEPAD ); // Initialize sdl2_mixer @@ -81,22 +81,22 @@ int main(int argc, char **argv) { while (running) { if(SDL_PollEvent(&e)) { switch(e.type) { - case SDL_QUIT: + case SDL_EVENT_QUIT: running = 0; break; - case SDL_CONTROLLERDEVICEADDED: - SDL_GameControllerOpen(e.cdevice.which); + case SDL_EVENT_GAMEPAD_ADDED: + SDL_OpenGamepad(e.cdevice.which); break; - case SDL_CONTROLLERBUTTONDOWN: + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: // pause using cross button - if (e.cbutton.button == SDL_CONTROLLER_BUTTON_A) { + if (e.cbutton.button == SDL_GAMEPAD_BUTTON_SOUTH) { Mix_PauseMusic(); // resume using circle button - } else if (e.cbutton.button == SDL_CONTROLLER_BUTTON_B) { + } else if (e.cbutton.button == SDL_GAMEPAD_BUTTON_EAST) { Mix_ResumeMusic(); } // press start button to exit - if (e.cbutton.button == SDL_CONTROLLER_BUTTON_START) { + if (e.cbutton.button == SDL_GAMEPAD_BUTTON_START) { running = 0; } break; diff --git a/_includes/samples/sdl2_ttf/CMakeLists.txt b/_includes/samples/sdl_ttf/CMakeLists.txt similarity index 54% rename from _includes/samples/sdl2_ttf/CMakeLists.txt rename to _includes/samples/sdl_ttf/CMakeLists.txt index 4dc0986..fa43626 100644 --- a/_includes/samples/sdl2_ttf/CMakeLists.txt +++ b/_includes/samples/sdl_ttf/CMakeLists.txt @@ -1,21 +1,15 @@ cmake_minimum_required(VERSION 3.11) -project(sdl2_ttf) +project(sdl-ttf) add_executable(${PROJECT_NAME} main.c) -include(FindPkgConfig) -pkg_search_module(SDL2 REQUIRED sdl2) -pkg_search_module(SDL2_TTF REQUIRED SDL2_ttf) - -target_include_directories(${PROJECT_NAME} PRIVATE - ${SDL2_INCLUDE_DIRS} - ${SDL2_TTF_INCLUDE_DIRS} -) +find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3) +find_package(SDL3_ttf REQUIRED) target_link_libraries(${PROJECT_NAME} PRIVATE - ${SDL2_LIBRARIES} - ${SDL2_TTF_LIBRARIES} + SDL3::SDL3 + SDL3_ttf::SDL3_ttf ) if(PSP) diff --git a/_includes/samples/sdl_ttf/main.c b/_includes/samples/sdl_ttf/main.c new file mode 100644 index 0000000..4b964fb --- /dev/null +++ b/_includes/samples/sdl_ttf/main.c @@ -0,0 +1,102 @@ +#include +#include +#include + +// Define screen dimensions +#define SCREEN_WIDTH 480 +#define SCREEN_HEIGHT 272 + +int main(int argc, char **argv) +{ + (void)argc; + (void)argv; + + // Initialize SDL + if (!SDL_Init(SDL_INIT_VIDEO)) + { + SDL_Log("SDL2 could not be initialized!\n" + "SDL2 Error: %s", + SDL_GetError()); + return 1; + } + + // Initialize SDL_ttf + if (!TTF_Init()) + { + SDL_Log("SDL2_ttf could not be initialized!\n" + "SDL2_ttf Error: %s", + SDL_GetError()); + return 2; + } + + SDL_Window *win = SDL_CreateWindow( + "window", + SCREEN_WIDTH, + SCREEN_HEIGHT, + 0); + if (!win) + { + SDL_Log("Window could not be created!" + "SDL_Error: %s\n", + SDL_GetError()); + } + + SDL_Renderer *renderer = SDL_CreateRenderer(win, NULL); + TTF_Font *font = TTF_OpenFont("Pacifico.ttf", 40); + + // Set the text and background color + SDL_Color text_color = {0xff, 0xff, 0xff, 0xff}; + SDL_Color bg_color = {0x00, 0x00, 0x00, 0xff}; + + const char * text = "Hello World!"; + SDL_FRect text_rect; + SDL_Surface *surface = TTF_RenderText_Blended(font, text, SDL_strlen(text), text_color); + text_rect.w = (float) surface->w; + text_rect.h = (float) surface->h; + SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); + + // Get text dimensions + text_rect.w = (float) surface->w; + text_rect.h = (float) surface->h; + + SDL_DestroySurface(surface); + + text_rect.x = (SCREEN_WIDTH - text_rect.w) / 2.0f; + text_rect.y = text_rect.h + 30.0f; + + int running = 1; + SDL_Event e; + while (running) + { + if (SDL_PollEvent(&e)) + { + switch (e.type) + { + case SDL_EVENT_QUIT: + running = 0; + break; + case SDL_EVENT_GAMEPAD_ADDED: + SDL_OpenGamepad(e.cdevice.which); + break; + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: + if (e.gbutton.button == SDL_GAMEPAD_BUTTON_START) + { + running = 0; + } + break; + } + } + + SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a); + SDL_RenderClear(renderer); + SDL_RenderTexture(renderer, texture, NULL, &text_rect); + SDL_RenderPresent(renderer); + } + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(win); + TTF_Quit(); + SDL_Quit(); + + return 0; +} \ No newline at end of file diff --git a/basic_programs.md b/basic_programs.md index fa2b39c..3091a1a 100644 --- a/basic_programs.md +++ b/basic_programs.md @@ -89,7 +89,7 @@ More libgu examples can be found -## Using SDL2 +## Using SDL {: .fs-6 .fw-700 } ![](images/shape.png) -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 can be slower, but will result in code that is easier to read and can run on multiple platforms. +SDL 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 can be slower, but will result in code that is easier to read and can run 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. @@ -217,13 +217,13 @@ Click on view source below for the to see the code and how to build it. **main.c** ```c -{% include samples/sdl2/main.c %} +{% include samples/sdl/main.c %} ``` **CMakeLists.txt** ```cmake -{% include samples/sdl2/CMakeLists.txt %} +{% include samples/sdl/CMakeLists.txt %} `````` Building can be done with: @@ -236,7 +236,7 @@ make

This will result in an EBOOT.PBP` file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and the PSP can run it.

-If you have sdl2 dev package and a compiler installed this code will also build on Linux for Linux by running: +If you have sdl dev package and a compiler installed this code will also build on Linux for Linux by running: ```shell mkdir build && cd build @@ -248,12 +248,12 @@ More documentation on SDL can be found
here, to be able to run it on the PSP. -If you have sdl2 sdl2-image dev packages and a compiler installed this code will also build on Linux for Linux by running: +If you have sdl sdl-image dev packages and a compiler installed this code will also build on Linux for Linux by running: ```shell mkdir build && cd build @@ -293,17 +293,17 @@ cmake .. make ``` -Documentation for SDL2_image can be found here. +Documentation for SDL_image can be found here. -## Using SDL2 mixer +## Using SDL mixer {: .fs-6 .fw-700 } -![](images/sdl2_mixer.png) +![](images/sdl_mixer.png) -This is a simple program to use the SDL2_mixer library. It handle audio playback in multimedia applications and games. It supports various audio formats(MP3/OGG). +This is a simple program to use the SDL_mixer library. It handle audio playback in multimedia applications and games. It supports various audio formats(MP3/OGG). Click on view source below to see the code and how to build it. @@ -314,13 +314,13 @@ Click on view source below to see the code and how to build it. **main.c** ```c -{% include samples/sdl2_mixer/main.c %} +{% include samples/sdl_mixer/main.c %} ``` **CMakeLists.txt** ```cmake -{% include samples/sdl2_mixer/CMakeLists.txt %} +{% include samples/sdl_mixer/CMakeLists.txt %} `````` Building can be done with: @@ -333,16 +333,16 @@ make This will result in an EBOOT.PBP file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and you need an audio file to test the program, download it from here. Put it in a directory in ms0:/MUSIC/ and then rename the audio file same as name on your *MUSIC_PATH* macro in your C code and the PSP can run it. -Documentation for SDL2_mixer can be found here. +Documentation for SDL_mixer can be found here. -## Using SDL2 ttf +## Using SDL ttf {: .fs-6 .fw-700 } -![](images/sdl2_ttf.jpg) +![](images/sdl_ttf.jpg) -This is a simple program to use the SDL2_ttf library. It provides functionality for rendering TrueType fonts for your PSP. +This is a simple program to use the SDL_ttf library. It provides functionality for rendering TrueType fonts for your PSP. Click on view source below to see the code and how to build it. @@ -353,13 +353,13 @@ Click on view source below to see the code and how to build it. **main.c** ```c -{% include samples/sdl2_ttf/main.c %} +{% include samples/sdl_ttf/main.c %} ``` **CMakeLists.txt** ```cmake -{% include samples/sdl2_ttf/CMakeLists.txt %} +{% include samples/sdl_ttf/CMakeLists.txt %} `````` Building can be done with: @@ -372,7 +372,7 @@ make This will result in an EBOOT.PBP file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and you need a font file to test the program, download it from here. Put it in a directory same as EBOOT.PBP and the PSP can run it. -Documentation for SDL2_ttf can be found here. +Documentation for SDL_ttf can be found here. diff --git a/images/sdl2_mixer.png b/images/sdl_mixer.png similarity index 100% rename from images/sdl2_mixer.png rename to images/sdl_mixer.png diff --git a/images/sdl2_ttf.jpg b/images/sdl_ttf.jpg similarity index 100% rename from images/sdl2_ttf.jpg rename to images/sdl_ttf.jpg From 92528db2e3d8c1426b6a8efc45ba0057eaa183d0 Mon Sep 17 00:00:00 2001 From: Wouter Wijsman Date: Thu, 9 Jul 2026 20:04:09 +0200 Subject: [PATCH 2/5] Improve error handling and code formatting --- _includes/samples/sdl/main.c | 20 +++-- _includes/samples/sdl_image/main.c | 32 ++++--- _includes/samples/sdl_mixer/CMakeLists.txt | 16 ++-- _includes/samples/sdl_mixer/main.c | 8 +- _includes/samples/sdl_ttf/main.c | 100 +++++++++++---------- 5 files changed, 94 insertions(+), 82 deletions(-) diff --git a/_includes/samples/sdl/main.c b/_includes/samples/sdl/main.c index bca1dbe..9be2285 100644 --- a/_includes/samples/sdl/main.c +++ b/_includes/samples/sdl/main.c @@ -3,16 +3,18 @@ int main(int argc, char *argv[]) { - SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD); - - SDL_Window * window = SDL_CreateWindow( - "window", - 480, - 272, - 0 - ); + if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) { + SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); + return 1; + } - SDL_Renderer * renderer = SDL_CreateRenderer(window, NULL); + SDL_Window * window = NULL; + SDL_Renderer * renderer = NULL; + if (!SDL_CreateWindowAndRenderer("window", 480, 272, 0, &window, &renderer)) { + SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); + SDL_Quit(); + return 2; + } SDL_FRect square = {216, 96, 34, 64}; diff --git a/_includes/samples/sdl_image/main.c b/_includes/samples/sdl_image/main.c index bdbc5aa..1d6a65e 100644 --- a/_includes/samples/sdl_image/main.c +++ b/_includes/samples/sdl_image/main.c @@ -4,21 +4,33 @@ int main(int argc, char *argv[]) { - SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD); - - SDL_Window * window = SDL_CreateWindow( - "window", - 480, - 272, - 0 - ); + if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) { + SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); + return 1; + } - SDL_Renderer * renderer = SDL_CreateRenderer(window, NULL); + SDL_Window * window = NULL; + SDL_Renderer * renderer = NULL; + if (!SDL_CreateWindowAndRenderer("window", 480, 272, 0, &window, &renderer)) { + SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); + SDL_Quit(); + return 2; + } // Load the texture - SDL_Surface * pixels = IMG_Load("grass.png"); + SDL_Surface * pixels = IMG_Load("grass.png"); // For png you can also use SDL_LoadPNG instead, which does not require SDL image + if (!pixels) { + SDL_Log("Couldn't load grass.png: %s", SDL_GetError()); + SDL_Quit(); + return 3; + } SDL_Texture * sprite = SDL_CreateTextureFromSurface(renderer, pixels); SDL_DestroySurface(pixels); + if (!sprite) { + SDL_Log("Couldn't create texture: %s", SDL_GetError()); + SDL_Quit(); + return 4; + } // Store the dimensions of the texture SDL_FRect sprite_rect; diff --git a/_includes/samples/sdl_mixer/CMakeLists.txt b/_includes/samples/sdl_mixer/CMakeLists.txt index 6b4651c..1149244 100644 --- a/_includes/samples/sdl_mixer/CMakeLists.txt +++ b/_includes/samples/sdl_mixer/CMakeLists.txt @@ -1,21 +1,15 @@ cmake_minimum_required(VERSION 3.11) -project(sdl2_mixer) +project(sdl-mixer) add_executable(${PROJECT_NAME} main.c) -include(FindPkgConfig) -pkg_search_module(SDL2 REQUIRED sdl2) -pkg_search_module(SDL2_MIXER REQUIRED SDL2_mixer) - -target_include_directories(${PROJECT_NAME} PRIVATE - ${SDL2_INCLUDE_DIRS} - ${SDL2_MIXER_INCLUDE_DIRS} -) +find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3) +find_package(SDL3_mixer REQUIRED) target_link_libraries(${PROJECT_NAME} PRIVATE - ${SDL2_LIBRARIES} - ${SDL2_MIXER_LIBRARIES} + SDL3::SDL3 + SDL3_mixer::SDL3_mixer ) if(PSP) diff --git a/_includes/samples/sdl_mixer/main.c b/_includes/samples/sdl_mixer/main.c index 49bcdb1..dcfdcb5 100644 --- a/_includes/samples/sdl_mixer/main.c +++ b/_includes/samples/sdl_mixer/main.c @@ -1,9 +1,7 @@ #include +#include #include -// Define MIN macro -#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) - // Define screen dimensions #define SCREEN_WIDTH 480 #define SCREEN_HEIGHT 272 @@ -53,8 +51,8 @@ int main(int argc, char **argv) { SDL_Rect rect; // Square dimensions: Half of the min(SCREEN_WIDTH, SCREEN_HEIGHT) - rect.w = MIN(SCREEN_WIDTH, SCREEN_HEIGHT) / 2; - rect.h = MIN(SCREEN_WIDTH, SCREEN_HEIGHT) / 2; + rect.w = SDL_min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2; + rect.h = SDL_min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2; // Square position: In the middle of the screen rect.x = SCREEN_WIDTH / 2 - rect.w / 2; diff --git a/_includes/samples/sdl_ttf/main.c b/_includes/samples/sdl_ttf/main.c index 4b964fb..2fbc653 100644 --- a/_includes/samples/sdl_ttf/main.c +++ b/_includes/samples/sdl_ttf/main.c @@ -8,51 +8,58 @@ int main(int argc, char **argv) { + // This prevents compiler warnings + // We don't actually need these variables, but they do need to be there so SDL_main works (void)argc; (void)argv; - // Initialize SDL - if (!SDL_Init(SDL_INIT_VIDEO)) - { - SDL_Log("SDL2 could not be initialized!\n" - "SDL2 Error: %s", - SDL_GetError()); + if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) { + SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); return 1; } - // Initialize SDL_ttf - if (!TTF_Init()) - { - SDL_Log("SDL2_ttf could not be initialized!\n" - "SDL2_ttf Error: %s", - SDL_GetError()); + // Initialize SDL ttf + if (!TTF_Init()) { + SDL_Log("Couldn't initialize SDL ttf: %s", SDL_GetError()); + SDL_Quit(); return 2; } - SDL_Window *win = SDL_CreateWindow( - "window", - SCREEN_WIDTH, - SCREEN_HEIGHT, - 0); - if (!win) - { - SDL_Log("Window could not be created!" - "SDL_Error: %s\n", - SDL_GetError()); + SDL_Window * window = NULL; + SDL_Renderer * renderer = NULL; + if (!SDL_CreateWindowAndRenderer("window", 480, 272, 0, &window, &renderer)) { + SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); + SDL_Quit(); + return 3; } - SDL_Renderer *renderer = SDL_CreateRenderer(win, NULL); + // Load the font file, make sure it is in the directory the eboot file is in TTF_Font *font = TTF_OpenFont("Pacifico.ttf", 40); + if (!font) { + SDL_Log("Couldn't load font: %s", SDL_GetError()); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + TTF_Quit(); + SDL_Quit(); + return 4; + } // Set the text and background color - SDL_Color text_color = {0xff, 0xff, 0xff, 0xff}; - SDL_Color bg_color = {0x00, 0x00, 0x00, 0xff}; + SDL_Color text_color = {0x00, 0x00, 0x00, 0xff}; + SDL_Color bg_color = {0xff, 0xff, 0xff, 0xff}; + // Create a texture using a string and SDL ttf const char * text = "Hello World!"; - SDL_FRect text_rect; - SDL_Surface *surface = TTF_RenderText_Blended(font, text, SDL_strlen(text), text_color); - text_rect.w = (float) surface->w; - text_rect.h = (float) surface->h; + SDL_FRect text_rect = {0, 0, 0, 0}; + SDL_Surface *surface = TTF_RenderText_Solid(font, text, SDL_strlen(text), text_color); + if (!surface) { + SDL_Log("Couldn't render text: %s", SDL_GetError()); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + TTF_Quit(); + SDL_Quit(); + return 5; + } SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); // Get text dimensions @@ -66,35 +73,34 @@ int main(int argc, char **argv) int running = 1; SDL_Event e; - while (running) - { - if (SDL_PollEvent(&e)) - { - switch (e.type) - { - case SDL_EVENT_QUIT: - running = 0; - break; - case SDL_EVENT_GAMEPAD_ADDED: - SDL_OpenGamepad(e.cdevice.which); - break; - case SDL_EVENT_GAMEPAD_BUTTON_DOWN: - if (e.gbutton.button == SDL_GAMEPAD_BUTTON_START) - { + while (running) { + if (SDL_PollEvent(&e)) { + switch (e.type) { + case SDL_EVENT_QUIT: // Event that is passed when the user closes the program running = 0; - } - break; + break; + case SDL_EVENT_GAMEPAD_ADDED: // Activate any gamepad that is connected + SDL_OpenGamepad(e.cdevice.which); + break; + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: + if (e.gbutton.button == SDL_GAMEPAD_BUTTON_START) { + running = 0; + } + break; } } + // Refresh the screen and redraw the text every frame SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a); SDL_RenderClear(renderer); SDL_RenderTexture(renderer, texture, NULL, &text_rect); SDL_RenderPresent(renderer); } + // Clean up + SDL_DestroyTexture(texture); SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(win); + SDL_DestroyWindow(window); TTF_Quit(); SDL_Quit(); From c7a9d3be05d73c58606646b90bdc9953aff37b63 Mon Sep 17 00:00:00 2001 From: Wouter Wijsman Date: Fri, 10 Jul 2026 21:11:19 +0200 Subject: [PATCH 3/5] Update SDL examples --- .../{sdl_image => sdl-sprite}/CMakeLists.txt | 4 +-- .../samples/{sdl_image => sdl-sprite}/main.c | 12 ++++--- _includes/samples/sdl/main.c | 9 +++-- _includes/samples/sdl_mixer/main.c | 34 +++++++----------- _includes/samples/sdl_ttf/main.c | 4 +-- basic_programs.md | 16 ++++----- images/sdl_ttf.jpg | Bin 9253 -> 0 bytes images/sdl_ttf.png | Bin 0 -> 2622 bytes 8 files changed, 39 insertions(+), 40 deletions(-) rename _includes/samples/{sdl_image => sdl-sprite}/CMakeLists.txt (84%) rename _includes/samples/{sdl_image => sdl-sprite}/main.c (89%) delete mode 100644 images/sdl_ttf.jpg create mode 100644 images/sdl_ttf.png diff --git a/_includes/samples/sdl_image/CMakeLists.txt b/_includes/samples/sdl-sprite/CMakeLists.txt similarity index 84% rename from _includes/samples/sdl_image/CMakeLists.txt rename to _includes/samples/sdl-sprite/CMakeLists.txt index ed67726..f561983 100644 --- a/_includes/samples/sdl_image/CMakeLists.txt +++ b/_includes/samples/sdl-sprite/CMakeLists.txt @@ -1,15 +1,13 @@ cmake_minimum_required(VERSION 3.11) -project(sdl-image) +project(sdl-sprite) add_executable(${PROJECT_NAME} main.c) find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3) -find_package(SDL3_image REQUIRED) target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3 - SDL3_image::SDL3_image ) if(PSP) diff --git a/_includes/samples/sdl_image/main.c b/_includes/samples/sdl-sprite/main.c similarity index 89% rename from _includes/samples/sdl_image/main.c rename to _includes/samples/sdl-sprite/main.c index 1d6a65e..c5f36d4 100644 --- a/_includes/samples/sdl_image/main.c +++ b/_includes/samples/sdl-sprite/main.c @@ -1,9 +1,13 @@ #include #include -#include -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { + // This prevents compiler warnings + // We don't actually need these variables, but they do need to be there so SDL_main works + (void)argc; + (void)argv; + + // Initialize sdl if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) { SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); return 1; @@ -18,7 +22,7 @@ int main(int argc, char *argv[]) } // Load the texture - SDL_Surface * pixels = IMG_Load("grass.png"); // For png you can also use SDL_LoadPNG instead, which does not require SDL image + SDL_Surface * pixels = SDL_LoadPNG("grass.png"); if (!pixels) { SDL_Log("Couldn't load grass.png: %s", SDL_GetError()); SDL_Quit(); diff --git a/_includes/samples/sdl/main.c b/_includes/samples/sdl/main.c index 9be2285..5289d93 100644 --- a/_includes/samples/sdl/main.c +++ b/_includes/samples/sdl/main.c @@ -1,8 +1,13 @@ #include #include -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { + // This prevents compiler warnings + // We don't actually need these variables, but they do need to be there so SDL_main works + (void)argc; + (void)argv; + + // Initialize sdl if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) { SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); return 1; diff --git a/_includes/samples/sdl_mixer/main.c b/_includes/samples/sdl_mixer/main.c index dcfdcb5..1df1f47 100644 --- a/_includes/samples/sdl_mixer/main.c +++ b/_includes/samples/sdl_mixer/main.c @@ -6,18 +6,17 @@ #define SCREEN_WIDTH 480 #define SCREEN_HEIGHT 272 -// audio file path -#define MUSIC_PATH "ms0:/MUSIC/test.ogg" // ogg/mp3 file format - int main(int argc, char **argv) { + // This prevents compiler warnings + // We don't actually need these variables, but they do need to be there so SDL_main works (void)argc; (void)argv; // Initialize sdl - SDL_Init(SDL_INIT_VIDEO | - SDL_INIT_AUDIO | - SDL_INIT_GAMEPAD - ); + if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD)) { + SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); + return 1; + } // Initialize sdl2_mixer Mix_OpenAudio(44100, @@ -26,20 +25,13 @@ int main(int argc, char **argv) { 2048 ); - // create window - SDL_Window *win = SDL_CreateWindow( - "psp_win", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - SCREEN_WIDTH, - SCREEN_HEIGHT, - 0 - ); - - // Create Renderer - SDL_Renderer *renderer = SDL_CreateRenderer( - win, -1, 0 - ); + SDL_Window * window = NULL; + SDL_Renderer * renderer = NULL; + if (!SDL_CreateWindowAndRenderer("window", 480, 272, 0, &window, &renderer)) { + SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); + SDL_Quit(); + return 2; + } // Load ogg file Mix_Music *ogg_file = NULL; diff --git a/_includes/samples/sdl_ttf/main.c b/_includes/samples/sdl_ttf/main.c index 2fbc653..a111995 100644 --- a/_includes/samples/sdl_ttf/main.c +++ b/_includes/samples/sdl_ttf/main.c @@ -6,13 +6,13 @@ #define SCREEN_WIDTH 480 #define SCREEN_HEIGHT 272 -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { // This prevents compiler warnings // We don't actually need these variables, but they do need to be there so SDL_main works (void)argc; (void)argv; + // Initialize sdl if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) { SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); return 1; diff --git a/basic_programs.md b/basic_programs.md index 3091a1a..2f7f59a 100644 --- a/basic_programs.md +++ b/basic_programs.md @@ -117,7 +117,7 @@ psp-cmake .. make ``` -This will result in an EBOOT.PBP file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and add the grass image file, download it from here, to be able to run it on the PSP. +This will result in an EBOOT.PBP file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and add the grass image file, download it from here, to be able to run it on the PSP. More libgu examples can be found here. @@ -193,7 +193,7 @@ psp-cmake .. make ``` -

This will result in an EBOOT.PBP file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and the PSP can run it.

+This will result in an EBOOT.PBP file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and the PSP can run it. More audiolib examples can be found here. @@ -234,7 +234,7 @@ psp-cmake .. make ``` -

This will result in an EBOOT.PBP` file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and the PSP can run it.

+This will result in an EBOOT.PBP file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and the PSP can run it. If you have sdl dev package and a compiler installed this code will also build on Linux for Linux by running: @@ -248,7 +248,7 @@ More documentation on SDL can be found here. Put it in a directory in ms0:/MUSIC/ and then rename the audio file same as name on your *MUSIC_PATH* macro in your C code and the PSP can run it. +This will result in an EBOOT.PBP file in the build directory. Put it in a directory in ms0:/PSP/GAME/ and add the test audio file, download it from here, to be able to run it on the PSP. Documentation for SDL_mixer can be found here. @@ -340,7 +340,7 @@ Documentation for SDL_mixer can be found Date: Fri, 10 Jul 2026 21:53:24 +0200 Subject: [PATCH 5/5] Improve text about sdl sprite example --- basic_programs.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/basic_programs.md b/basic_programs.md index 2f7f59a..ea5b73c 100644 --- a/basic_programs.md +++ b/basic_programs.md @@ -253,7 +253,7 @@ More documentation on SDL can be found here, to be able to run it on the PSP. -If you have sdl sdl-image dev packages and a compiler installed this code will also build on Linux for Linux by running: +If you have the sdl dev package and a compiler installed this code will also build on Linux for Linux by running: ```shell mkdir build && cd build @@ -293,7 +293,9 @@ cmake .. make ``` -Documentation for SDL_image can be found here. +If you want support for more image formats than just PNG and BMP, you can use the SDL image library ins addition to SDL. This adds the `IMG_Load` function, which can be used in place of `SDL_LoadPNG` to load any image format. This would require adding an include and linking to SDL image. Documentation for SDL image can be found here. + +More documentation on SDL can be found here.