Skip to content

Commit 3b672e7

Browse files
committed
Initial work porting examples to SDL3
1 parent 1376287 commit 3b672e7

12 files changed

Lines changed: 175 additions & 197 deletions

File tree

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

3-
project(sdl2)
3+
project(sdl)
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)
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
1-
#include <SDL.h>
1+
#include <SDL3/SDL.h>
2+
#include <SDL3/SDL_main.h>
23

34
int main(int argc, char *argv[])
45
{
5-
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
6+
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD);
67

78
SDL_Window * window = SDL_CreateWindow(
89
"window",
9-
SDL_WINDOWPOS_UNDEFINED,
10-
SDL_WINDOWPOS_UNDEFINED,
1110
480,
1211
272,
1312
0
1413
);
1514

16-
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
15+
SDL_Renderer * renderer = SDL_CreateRenderer(window, NULL);
1716

18-
SDL_Rect square = {216, 96, 34, 64};
17+
SDL_FRect square = {216, 96, 34, 64};
1918

2019
int running = 1;
2120
SDL_Event event;
2221
while (running) {
2322
// Process input
2423
if (SDL_PollEvent(&event)) {
2524
switch (event.type) {
26-
case SDL_QUIT:
25+
case SDL_EVENT_QUIT:
2726
// End the loop if the programs is being closed
2827
running = 0;
2928
break;
30-
case SDL_CONTROLLERDEVICEADDED:
29+
case SDL_EVENT_GAMEPAD_ADDED:
3130
// Connect a controller when it is connected
32-
SDL_GameControllerOpen(event.cdevice.which);
31+
SDL_OpenGamepad(event.cdevice.which);
3332
break;
34-
case SDL_CONTROLLERBUTTONDOWN:
35-
if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
33+
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
34+
if(event.gbutton.button == SDL_GAMEPAD_BUTTON_START) {
3635
// Close the program if start is pressed
3736
running = 0;
3837
}

_includes/samples/sdl2_ttf/main.c

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

_includes/samples/sdl2_image/CMakeLists.txt renamed to _includes/samples/sdl_image/CMakeLists.txt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
cmake_minimum_required(VERSION 3.11)
22

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

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

7-
include(FindPkgConfig)
8-
pkg_search_module(SDL2 REQUIRED sdl2)
9-
pkg_search_module(SDL2_IMAGE REQUIRED SDL2_image)
10-
11-
target_include_directories(${PROJECT_NAME} PRIVATE
12-
${SDL2_INCLUDE_DIRS}
13-
${SDL2_IMAGE_INCLUDE_DIRS}
14-
)
7+
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
8+
find_package(SDL3_image REQUIRED)
159

1610
target_link_libraries(${PROJECT_NAME} PRIVATE
17-
${SDL2_LIBRARIES}
18-
${SDL2_IMAGE_LIBRARIES}
11+
SDL3::SDL3
12+
SDL3_image::SDL3_image
1913
)
2014

2115
if(PSP)
Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,49 @@
1-
#include <SDL.h>
2-
#include <SDL_image.h>
1+
#include <SDL3/SDL.h>
2+
#include <SDL3/SDL_main.h>
3+
#include <SDL3_image/SDL_image.h>
34

45
int main(int argc, char *argv[])
56
{
6-
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
7-
8-
// Enable png support for SDL2_image
9-
IMG_Init(IMG_INIT_PNG);
7+
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD);
108

119
SDL_Window * window = SDL_CreateWindow(
1210
"window",
13-
SDL_WINDOWPOS_UNDEFINED,
14-
SDL_WINDOWPOS_UNDEFINED,
1511
480,
1612
272,
1713
0
1814
);
1915

20-
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
16+
SDL_Renderer * renderer = SDL_CreateRenderer(window, NULL);
2117

2218
// Load the texture
2319
SDL_Surface * pixels = IMG_Load("grass.png");
2420
SDL_Texture * sprite = SDL_CreateTextureFromSurface(renderer, pixels);
25-
SDL_FreeSurface(pixels);
21+
SDL_DestroySurface(pixels);
2622

2723
// Store the dimensions of the texture
28-
SDL_Rect sprite_rect;
29-
SDL_QueryTexture(sprite, NULL, NULL, &sprite_rect.w, &sprite_rect.h);
24+
SDL_FRect sprite_rect;
25+
SDL_GetTextureSize(sprite, &sprite_rect.w, &sprite_rect.h);
3026

3127
// Set the position to draw to in the middle of the screen
32-
sprite_rect.x = 480/2 - sprite_rect.w/2;
33-
sprite_rect.y = 272/2 - sprite_rect.h/2;
28+
sprite_rect.x = 480.0f / 2.0f - sprite_rect.w / 2.0f;
29+
sprite_rect.y = 272.0f / 2.0f - sprite_rect.h / 2.0f;
3430

3531
int running = 1;
3632
SDL_Event event;
3733
while (running) {
3834
// Process input
3935
if (SDL_PollEvent(&event)) {
4036
switch (event.type) {
41-
case SDL_QUIT:
37+
case SDL_EVENT_QUIT:
4238
// End the loop if the programs is being closed
4339
running = 0;
4440
break;
45-
case SDL_CONTROLLERDEVICEADDED:
41+
case SDL_EVENT_GAMEPAD_ADDED:
4642
// Connect a controller when it is connected
47-
SDL_GameControllerOpen(event.cdevice.which);
43+
SDL_OpenGamepad(event.cdevice.which);
4844
break;
49-
case SDL_CONTROLLERBUTTONDOWN:
50-
if(event.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
45+
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
46+
if(event.gbutton.button == SDL_GAMEPAD_BUTTON_START) {
5147
// Close the program if start is pressed
5248
running = 0;
5349
}
@@ -59,7 +55,7 @@ int main(int argc, char *argv[])
5955
SDL_RenderClear(renderer);
6056

6157
// Draw the 'grass' sprite
62-
SDL_RenderCopy(renderer, sprite, NULL, &sprite_rect);
58+
SDL_RenderTexture(renderer, sprite, NULL, &sprite_rect);
6359

6460
// Draw everything on a white background
6561
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
File renamed without changes.
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <SDL2/SDL.h>
2-
#include <SDL2/SDL_mixer.h>
1+
#include <SDL3/SDL.h>
2+
#include <SDL3_mixer/SDL_mixer.h>
33

44
// Define MIN macro
55
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
@@ -18,7 +18,7 @@ int main(int argc, char **argv) {
1818
// Initialize sdl
1919
SDL_Init(SDL_INIT_VIDEO |
2020
SDL_INIT_AUDIO |
21-
SDL_INIT_GAMECONTROLLER
21+
SDL_INIT_GAMEPAD
2222
);
2323

2424
// Initialize sdl2_mixer
@@ -81,22 +81,22 @@ int main(int argc, char **argv) {
8181
while (running) {
8282
if(SDL_PollEvent(&e)) {
8383
switch(e.type) {
84-
case SDL_QUIT:
84+
case SDL_EVENT_QUIT:
8585
running = 0;
8686
break;
87-
case SDL_CONTROLLERDEVICEADDED:
88-
SDL_GameControllerOpen(e.cdevice.which);
87+
case SDL_EVENT_GAMEPAD_ADDED:
88+
SDL_OpenGamepad(e.cdevice.which);
8989
break;
90-
case SDL_CONTROLLERBUTTONDOWN:
90+
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
9191
// pause using cross button
92-
if (e.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
92+
if (e.cbutton.button == SDL_GAMEPAD_BUTTON_SOUTH) {
9393
Mix_PauseMusic();
9494
// resume using circle button
95-
} else if (e.cbutton.button == SDL_CONTROLLER_BUTTON_B) {
95+
} else if (e.cbutton.button == SDL_GAMEPAD_BUTTON_EAST) {
9696
Mix_ResumeMusic();
9797
}
9898
// press start button to exit
99-
if (e.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
99+
if (e.cbutton.button == SDL_GAMEPAD_BUTTON_START) {
100100
running = 0;
101101
}
102102
break;
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
cmake_minimum_required(VERSION 3.11)
22

3-
project(sdl2_ttf)
3+
project(sdl-ttf)
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)
8+
find_package(SDL3_ttf REQUIRED)
159

1610
target_link_libraries(${PROJECT_NAME} PRIVATE
17-
${SDL2_LIBRARIES}
18-
${SDL2_TTF_LIBRARIES}
11+
SDL3::SDL3
12+
SDL3_ttf::SDL3_ttf
1913
)
2014

2115
if(PSP)

0 commit comments

Comments
 (0)