Skip to content

Commit b96bc7b

Browse files
authored
Merge pull request #111 from pspdev/switch-to-sdl3
Switch all examples using SDL to SDL3
2 parents 3432d21 + 7609395 commit b96bc7b

15 files changed

Lines changed: 430 additions & 403 deletions

File tree

_includes/samples/sdl2/CMakeLists.txt renamed to _includes/samples/sdl-sprite/CMakeLists.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
cmake_minimum_required(VERSION 3.11)
22

3-
project(sdl2)
3+
project(sdl-sprite)
44

55
add_executable(${PROJECT_NAME} main.c)
66

7-
include(FindPkgConfig)
8-
pkg_search_module(SDL2 REQUIRED sdl2)
9-
10-
target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_INCLUDE_DIRS})
7+
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
118

129
target_link_libraries(${PROJECT_NAME} PRIVATE
13-
${SDL2_LIBRARIES}
10+
SDL3::SDL3
1411
)
1512

1613
if(PSP)
14+
# Create an EBOOT.PBP file
1715
create_pbp_file(
1816
TARGET ${PROJECT_NAME}
1917
ICON_PATH NULL
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <SDL3/SDL.h>
2+
#include <SDL3/SDL_main.h>
3+
4+
int main(int argc, char *argv[]) {
5+
// This prevents compiler warnings
6+
// We don't actually need these variables, but they do need to be there so SDL_main works
7+
(void)argc;
8+
(void)argv;
9+
10+
// Initialize sdl
11+
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
12+
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
13+
return 1;
14+
}
15+
16+
SDL_Window * window = NULL;
17+
SDL_Renderer * renderer = NULL;
18+
if (!SDL_CreateWindowAndRenderer("window", 480, 272, 0, &window, &renderer)) {
19+
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
20+
SDL_Quit();
21+
return 2;
22+
}
23+
24+
// Load the texture
25+
SDL_Surface * pixels = SDL_LoadPNG("grass.png");
26+
if (!pixels) {
27+
SDL_Log("Couldn't load grass.png: %s", SDL_GetError());
28+
SDL_Quit();
29+
return 3;
30+
}
31+
SDL_Texture * sprite = SDL_CreateTextureFromSurface(renderer, pixels);
32+
SDL_DestroySurface(pixels);
33+
if (!sprite) {
34+
SDL_Log("Couldn't create texture: %s", SDL_GetError());
35+
SDL_Quit();
36+
return 4;
37+
}
38+
39+
// Store the dimensions of the texture
40+
SDL_FRect sprite_rect;
41+
SDL_GetTextureSize(sprite, &sprite_rect.w, &sprite_rect.h);
42+
43+
// Set the position to draw to in the middle of the screen
44+
sprite_rect.x = 480.0f / 2.0f - sprite_rect.w / 2.0f;
45+
sprite_rect.y = 272.0f / 2.0f - sprite_rect.h / 2.0f;
46+
47+
int running = 1;
48+
SDL_Event event;
49+
while (running) {
50+
// Process input
51+
if (SDL_PollEvent(&event)) {
52+
switch (event.type) {
53+
case SDL_EVENT_QUIT:
54+
// End the loop if the programs is being closed
55+
running = 0;
56+
break;
57+
case SDL_EVENT_GAMEPAD_ADDED:
58+
// Connect a controller when it is connected
59+
SDL_OpenGamepad(event.cdevice.which);
60+
break;
61+
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
62+
if(event.gbutton.button == SDL_GAMEPAD_BUTTON_START) {
63+
// Close the program if start is pressed
64+
running = 0;
65+
}
66+
break;
67+
}
68+
}
69+
70+
// Clear the screen
71+
SDL_RenderClear(renderer);
72+
73+
// Draw the 'grass' sprite
74+
SDL_RenderTexture(renderer, sprite, NULL, &sprite_rect);
75+
76+
// Draw everything on a white background
77+
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
78+
SDL_RenderPresent(renderer);
79+
}
80+
SDL_DestroyRenderer(renderer);
81+
SDL_DestroyWindow(window);
82+
SDL_Quit();
83+
84+
return 0;
85+
}
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
cmake_minimum_required(VERSION 3.11)
22

3-
project(sdl2_ttf)
3+
project(sdl)
44

55
add_executable(${PROJECT_NAME} main.c)
66

7-
include(FindPkgConfig)
8-
pkg_search_module(SDL2 REQUIRED sdl2)
9-
pkg_search_module(SDL2_TTF REQUIRED SDL2_ttf)
10-
11-
target_include_directories(${PROJECT_NAME} PRIVATE
12-
${SDL2_INCLUDE_DIRS}
13-
${SDL2_TTF_INCLUDE_DIRS}
14-
)
7+
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
158

169
target_link_libraries(${PROJECT_NAME} PRIVATE
17-
${SDL2_LIBRARIES}
18-
${SDL2_TTF_LIBRARIES}
10+
SDL3::SDL3
1911
)
2012

2113
if(PSP)
Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1-
#include <SDL.h>
1+
#include <SDL3/SDL.h>
2+
#include <SDL3/SDL_main.h>
23

3-
int main(int argc, char *argv[])
4-
{
5-
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
4+
int main(int argc, char *argv[]) {
5+
// This prevents compiler warnings
6+
// We don't actually need these variables, but they do need to be there so SDL_main works
7+
(void)argc;
8+
(void)argv;
69

7-
SDL_Window * window = SDL_CreateWindow(
8-
"window",
9-
SDL_WINDOWPOS_UNDEFINED,
10-
SDL_WINDOWPOS_UNDEFINED,
11-
480,
12-
272,
13-
0
14-
);
10+
// Initialize sdl
11+
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
12+
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
13+
return 1;
14+
}
1515

16-
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
16+
SDL_Window * window = NULL;
17+
SDL_Renderer * renderer = NULL;
18+
if (!SDL_CreateWindowAndRenderer("window", 480, 272, 0, &window, &renderer)) {
19+
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
20+
SDL_Quit();
21+
return 2;
22+
}
1723

18-
SDL_Rect square = {216, 96, 34, 64};
24+
SDL_FRect square = {216, 96, 34, 64};
1925

2026
int running = 1;
2127
SDL_Event event;
2228
while (running) {
2329
// Process input
2430
if (SDL_PollEvent(&event)) {
2531
switch (event.type) {
26-
case SDL_QUIT:
32+
case SDL_EVENT_QUIT:
2733
// End the loop if the programs is being closed
2834
running = 0;
2935
break;
30-
case SDL_CONTROLLERDEVICEADDED:
36+
case SDL_EVENT_GAMEPAD_ADDED:
3137
// Connect a controller when it is connected
32-
SDL_GameControllerOpen(event.cdevice.which);
38+
SDL_OpenGamepad(event.cdevice.which);
3339
break;
34-
case SDL_CONTROLLERBUTTONDOWN:
35-
if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
40+
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
41+
if(event.gbutton.button == SDL_GAMEPAD_BUTTON_START) {
3642
// Close the program if start is pressed
3743
running = 0;
3844
}

_includes/samples/sdl2_image/main.c

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)