diff --git a/_includes/samples/sdl2/CMakeLists.txt b/_includes/samples/sdl-sprite/CMakeLists.txt similarity index 66% rename from _includes/samples/sdl2/CMakeLists.txt rename to _includes/samples/sdl-sprite/CMakeLists.txt index 3383735..f561983 100644 --- a/_includes/samples/sdl2/CMakeLists.txt +++ b/_includes/samples/sdl-sprite/CMakeLists.txt @@ -1,19 +1,17 @@ cmake_minimum_required(VERSION 3.11) -project(sdl2) +project(sdl-sprite) 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) + # Create an EBOOT.PBP file create_pbp_file( TARGET ${PROJECT_NAME} ICON_PATH NULL diff --git a/_includes/samples/sdl-sprite/main.c b/_includes/samples/sdl-sprite/main.c new file mode 100644 index 0000000..c5f36d4 --- /dev/null +++ b/_includes/samples/sdl-sprite/main.c @@ -0,0 +1,85 @@ +#include +#include + +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; + } + + 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 = SDL_LoadPNG("grass.png"); + 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; + 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.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; + while (running) { + // Process input + if (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_EVENT_QUIT: + // End the loop if the programs is being closed + running = 0; + break; + case SDL_EVENT_GAMEPAD_ADDED: + // Connect a controller when it is connected + SDL_OpenGamepad(event.cdevice.which); + break; + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: + if(event.gbutton.button == SDL_GAMEPAD_BUTTON_START) { + // Close the program if start is pressed + running = 0; + } + break; + } + } + + // Clear the screen + SDL_RenderClear(renderer); + + // Draw the 'grass' sprite + SDL_RenderTexture(renderer, sprite, NULL, &sprite_rect); + + // Draw everything on a white background + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderPresent(renderer); + } + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; +} \ No newline at end of file diff --git a/_includes/samples/sdl2_ttf/CMakeLists.txt b/_includes/samples/sdl/CMakeLists.txt similarity index 54% rename from _includes/samples/sdl2_ttf/CMakeLists.txt rename to _includes/samples/sdl/CMakeLists.txt index 4dc0986..520f844 100644 --- a/_includes/samples/sdl2_ttf/CMakeLists.txt +++ b/_includes/samples/sdl/CMakeLists.txt @@ -1,21 +1,13 @@ cmake_minimum_required(VERSION 3.11) -project(sdl2_ttf) +project(sdl) 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) target_link_libraries(${PROJECT_NAME} PRIVATE - ${SDL2_LIBRARIES} - ${SDL2_TTF_LIBRARIES} + SDL3::SDL3 ) if(PSP) diff --git a/_includes/samples/sdl2/main.c b/_includes/samples/sdl/main.c similarity index 50% rename from _includes/samples/sdl2/main.c rename to _includes/samples/sdl/main.c index fc7b6f4..5289d93 100644 --- a/_includes/samples/sdl2/main.c +++ b/_includes/samples/sdl/main.c @@ -1,21 +1,27 @@ -#include +#include +#include -int main(int argc, char *argv[]) -{ - SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER); +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; - SDL_Window * window = SDL_CreateWindow( - "window", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - 480, - 272, - 0 - ); + // Initialize sdl + 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, -1, SDL_RENDERER_ACCELERATED); + 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_Rect square = {216, 96, 34, 64}; + SDL_FRect square = {216, 96, 34, 64}; int running = 1; SDL_Event event; @@ -23,16 +29,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_image/main.c b/_includes/samples/sdl2_image/main.c deleted file mode 100644 index eafecc9..0000000 --- a/_includes/samples/sdl2_image/main.c +++ /dev/null @@ -1,73 +0,0 @@ -#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_Window * window = SDL_CreateWindow( - "window", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - 480, - 272, - 0 - ); - - SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); - - // Load the texture - SDL_Surface * pixels = IMG_Load("grass.png"); - SDL_Texture * sprite = SDL_CreateTextureFromSurface(renderer, pixels); - SDL_FreeSurface(pixels); - - // Store the dimensions of the texture - SDL_Rect sprite_rect; - SDL_QueryTexture(sprite, NULL, NULL, &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; - - int running = 1; - SDL_Event event; - while (running) { - // Process input - if (SDL_PollEvent(&event)) { - switch (event.type) { - case SDL_QUIT: - // End the loop if the programs is being closed - 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) { - // Close the program if start is pressed - running = 0; - } - break; - } - } - - // Clear the screen - SDL_RenderClear(renderer); - - // Draw the 'grass' sprite - SDL_RenderCopy(renderer, sprite, NULL, &sprite_rect); - - // Draw everything on a white background - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_RenderPresent(renderer); - } - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); - - return 0; -} \ No newline at end of file diff --git a/_includes/samples/sdl2_mixer/main.c b/_includes/samples/sdl2_mixer/main.c deleted file mode 100644 index 95ef8a0..0000000 --- a/_includes/samples/sdl2_mixer/main.c +++ /dev/null @@ -1,139 +0,0 @@ -#include -#include - -// Define MIN macro -#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) - -// Define screen dimensions -#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) { - (void)argc; - (void)argv; - - // Initialize sdl - SDL_Init(SDL_INIT_VIDEO | - SDL_INIT_AUDIO | - SDL_INIT_GAMECONTROLLER - ); - - // Initialize sdl2_mixer - Mix_OpenAudio(44100, - MIX_DEFAULT_FORMAT, - MIX_DEFAULT_CHANNELS, - 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 - ); - - // Load ogg file - Mix_Music *ogg_file = NULL; - ogg_file = Mix_LoadMUS(MUSIC_PATH); - if (!ogg_file) { - return 0; - } - - 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; - - // Square position: In the middle of the screen - rect.x = SCREEN_WIDTH / 2 - rect.w / 2; - rect.y = SCREEN_HEIGHT / 2 - rect.h / 2; - - - // Declare rects of pause symbol - SDL_Rect pause_rect1, pause_rect2; - - pause_rect1.h = rect.h / 2; - pause_rect1.w = 40; - pause_rect1.x = rect.x + (rect.w - pause_rect1.w * 3) / 2; - pause_rect1.y = rect.y + rect.h / 4; - pause_rect2 = pause_rect1; - pause_rect2.x += pause_rect1.w * 2; - - // play the music 8 times - if (Mix_PlayMusic(ogg_file, 8) == -1) { - return 0; - } - - 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: - // pause using cross button - if (e.cbutton.button == SDL_CONTROLLER_BUTTON_A) { - Mix_PauseMusic(); - // resume using circle button - } else if (e.cbutton.button == SDL_CONTROLLER_BUTTON_B) { - Mix_ResumeMusic(); - } - // press start button to exit - if (e.cbutton.button == SDL_CONTROLLER_BUTTON_START) { - running = 0; - } - break; - } - } - - // Initialize renderer color black for the background - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); - - // Clear screen - SDL_RenderClear(renderer); - - // Set renderer color green to draw the square - SDL_SetRenderDrawColor(renderer, 0, 0xFF, 0, 0xFF); - - // Draw filled square - SDL_RenderFillRect(renderer, &rect); - - // Check pause status - if(Mix_PausedMusic()) { - // Set renderer color black to draw the pause symbol - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); - - // Draw pause symbol - SDL_RenderFillRect(renderer, &pause_rect1); - SDL_RenderFillRect(renderer, &pause_rect2); - } - - // Update screen - SDL_RenderPresent(renderer); - } - - Mix_FreeMusic(ogg_file); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(win); - Mix_CloseAudio(); - SDL_Quit(); - - return 0; -} \ No newline at end of file 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_mixer/CMakeLists.txt b/_includes/samples/sdl_mixer/CMakeLists.txt similarity index 53% rename from _includes/samples/sdl2_mixer/CMakeLists.txt rename to _includes/samples/sdl_mixer/CMakeLists.txt index 6b4651c..1149244 100644 --- a/_includes/samples/sdl2_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 new file mode 100644 index 0000000..d8f3b62 --- /dev/null +++ b/_includes/samples/sdl_mixer/main.c @@ -0,0 +1,165 @@ +#include +#include +#include + +// Define screen dimensions +#define SCREEN_WIDTH 480 +#define SCREEN_HEIGHT 272 + +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_AUDIO | SDL_INIT_GAMEPAD)) { + SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); + return 1; + } + + // Initialize SDL mixer + if(!MIX_Init()) { + SDL_Log("Couldn't initialize SDL mixer: %s", SDL_GetError()); + SDL_Quit(); + return 2; + } + + // Initialise audio device + MIX_Mixer * mixer = MIX_CreateMixerDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL); + if (!mixer) { + SDL_Log("Couldn't open default audio device: %s", SDL_GetError()); + MIX_Quit(); + SDL_Quit(); + return 3; + } + + // Initialize channel to play music on, called a track in SDL mixer + MIX_Track * music_track = MIX_CreateTrack(mixer); + if (!music_track) { + SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); + MIX_DestroyMixer(mixer); + MIX_Quit(); + SDL_Quit(); + return 3; + } + + + 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()); + MIX_DestroyMixer(mixer); + MIX_Quit(); + SDL_Quit(); + return 4; + } + + // Load ogg file + MIX_Audio *ogg_file = MIX_LoadAudio(mixer, "test.ogg", false); + if (!ogg_file) { + SDL_Log("Couldn't load audio file: %s", SDL_GetError()); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + MIX_DestroyMixer(mixer); + MIX_Quit(); + SDL_Quit(); + return 5; + } + + SDL_FRect rect; + + // Square dimensions: Half of the min(SCREEN_WIDTH, SCREEN_HEIGHT) + rect.w = SDL_min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2.0f; + rect.h = SDL_min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2.0f; + + // Square position: In the middle of the screen + rect.x = SCREEN_WIDTH / 2 - rect.w / 2; + rect.y = SCREEN_HEIGHT / 2 - rect.h / 2; + + + // Declare rects of pause symbol + SDL_FRect pause_rect1, pause_rect2; + + pause_rect1.h = rect.h / 2.0f; + pause_rect1.w = 40.0f; + pause_rect1.x = rect.x + (rect.w - pause_rect1.w * 3.0f) / 2.0f; + pause_rect1.y = rect.y + rect.h / 4.0f; + pause_rect2 = pause_rect1; + pause_rect2.x += pause_rect1.w * 2.0f; + + // play the music 8 times + MIX_SetTrackAudio(music_track, ogg_file); + MIX_SetTrackGain(music_track, 1.0f); + if (!MIX_PlayTrack(music_track, 8)) { + SDL_Log("Couldn't play audio: %s", SDL_GetError()); + MIX_DestroyAudio(ogg_file); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + MIX_DestroyMixer(mixer); + MIX_Quit(); + SDL_Quit(); + return 6; + } + + 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: + // pause using cross button + if (e.gbutton.button == SDL_GAMEPAD_BUTTON_SOUTH) { + MIX_PauseTrack(music_track); + // resume using circle button + } else if (e.gbutton.button == SDL_GAMEPAD_BUTTON_EAST) { + MIX_ResumeTrack(music_track); + // press start button to exit + } else if (e.gbutton.button == SDL_GAMEPAD_BUTTON_START) { + running = 0; + } + break; + } + } + + // Initialize renderer color black for the background + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + + // Clear screen + SDL_RenderClear(renderer); + + // Set renderer color green to draw the square + SDL_SetRenderDrawColor(renderer, 0, 0xFF, 0, 0xFF); + + // Draw filled square + SDL_RenderFillRect(renderer, &rect); + + // Check pause status + if(!MIX_TrackPlaying(music_track)) { + // Set renderer color black to draw the pause symbol + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + + // Draw pause symbol + SDL_RenderFillRect(renderer, &pause_rect1); + SDL_RenderFillRect(renderer, &pause_rect2); + } + + // Update screen + SDL_RenderPresent(renderer); + } + + MIX_DestroyAudio(ogg_file); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + MIX_DestroyMixer(mixer); + MIX_Quit(); + SDL_Quit(); + + return 0; +} \ No newline at end of file diff --git a/_includes/samples/sdl2_image/CMakeLists.txt b/_includes/samples/sdl_ttf/CMakeLists.txt similarity index 50% rename from _includes/samples/sdl2_image/CMakeLists.txt rename to _includes/samples/sdl_ttf/CMakeLists.txt index a2c75b5..fa43626 100644 --- a/_includes/samples/sdl2_image/CMakeLists.txt +++ b/_includes/samples/sdl_ttf/CMakeLists.txt @@ -1,25 +1,18 @@ cmake_minimum_required(VERSION 3.11) -project(sdl2-image) +project(sdl-ttf) 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_ttf REQUIRED) target_link_libraries(${PROJECT_NAME} PRIVATE - ${SDL2_LIBRARIES} - ${SDL2_IMAGE_LIBRARIES} + SDL3::SDL3 + SDL3_ttf::SDL3_ttf ) if(PSP) - # Create an EBOOT.PBP file create_pbp_file( TARGET ${PROJECT_NAME} ICON_PATH NULL diff --git a/_includes/samples/sdl_ttf/main.c b/_includes/samples/sdl_ttf/main.c new file mode 100644 index 0000000..a111995 --- /dev/null +++ b/_includes/samples/sdl_ttf/main.c @@ -0,0 +1,108 @@ +#include +#include +#include + +// Define screen dimensions +#define SCREEN_WIDTH 480 +#define SCREEN_HEIGHT 272 + +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; + } + + // Initialize SDL ttf + if (!TTF_Init()) { + SDL_Log("Couldn't initialize SDL ttf: %s", SDL_GetError()); + SDL_Quit(); + return 2; + } + + 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; + } + + // 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 = {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 = {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 + 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: // Event that is passed when the user closes the program + running = 0; + 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(window); + TTF_Quit(); + SDL_Quit(); + + return 0; +} \ No newline at end of file diff --git a/basic_programs.md b/basic_programs.md index fa2b39c..ea5b73c 100644 --- a/basic_programs.md +++ b/basic_programs.md @@ -89,7 +89,7 @@ More libgu examples can be found 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,18 +193,18 @@ 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. -## 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: @@ -234,9 +234,9 @@ 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 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 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,17 +293,19 @@ cmake .. make ``` -Documentation for SDL2_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. -## 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 +316,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: @@ -331,18 +333,18 @@ 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 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. +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 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.png) -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 +355,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 +374,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_ttf.jpg b/images/sdl2_ttf.jpg deleted file mode 100644 index 38a76c9..0000000 Binary files a/images/sdl2_ttf.jpg and /dev/null differ 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/sdl_ttf.png b/images/sdl_ttf.png new file mode 100644 index 0000000..0e28615 Binary files /dev/null and b/images/sdl_ttf.png differ