Skip to content

Commit 92528db

Browse files
committed
Improve error handling and code formatting
1 parent 3b672e7 commit 92528db

5 files changed

Lines changed: 94 additions & 82 deletions

File tree

_includes/samples/sdl/main.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33

44
int main(int argc, char *argv[])
55
{
6-
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD);
7-
8-
SDL_Window * window = SDL_CreateWindow(
9-
"window",
10-
480,
11-
272,
12-
0
13-
);
6+
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
7+
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
8+
return 1;
9+
}
1410

15-
SDL_Renderer * renderer = SDL_CreateRenderer(window, NULL);
11+
SDL_Window * window = NULL;
12+
SDL_Renderer * renderer = NULL;
13+
if (!SDL_CreateWindowAndRenderer("window", 480, 272, 0, &window, &renderer)) {
14+
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
15+
SDL_Quit();
16+
return 2;
17+
}
1618

1719
SDL_FRect square = {216, 96, 34, 64};
1820

_includes/samples/sdl_image/main.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,33 @@
44

55
int main(int argc, char *argv[])
66
{
7-
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD);
8-
9-
SDL_Window * window = SDL_CreateWindow(
10-
"window",
11-
480,
12-
272,
13-
0
14-
);
7+
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
8+
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
9+
return 1;
10+
}
1511

16-
SDL_Renderer * renderer = SDL_CreateRenderer(window, NULL);
12+
SDL_Window * window = NULL;
13+
SDL_Renderer * renderer = NULL;
14+
if (!SDL_CreateWindowAndRenderer("window", 480, 272, 0, &window, &renderer)) {
15+
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
16+
SDL_Quit();
17+
return 2;
18+
}
1719

1820
// Load the texture
19-
SDL_Surface * pixels = IMG_Load("grass.png");
21+
SDL_Surface * pixels = IMG_Load("grass.png"); // For png you can also use SDL_LoadPNG instead, which does not require SDL image
22+
if (!pixels) {
23+
SDL_Log("Couldn't load grass.png: %s", SDL_GetError());
24+
SDL_Quit();
25+
return 3;
26+
}
2027
SDL_Texture * sprite = SDL_CreateTextureFromSurface(renderer, pixels);
2128
SDL_DestroySurface(pixels);
29+
if (!sprite) {
30+
SDL_Log("Couldn't create texture: %s", SDL_GetError());
31+
SDL_Quit();
32+
return 4;
33+
}
2234

2335
// Store the dimensions of the texture
2436
SDL_FRect sprite_rect;

_includes/samples/sdl_mixer/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_mixer)
3+
project(sdl-mixer)
44

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

7-
include(FindPkgConfig)
8-
pkg_search_module(SDL2 REQUIRED sdl2)
9-
pkg_search_module(SDL2_MIXER REQUIRED SDL2_mixer)
10-
11-
target_include_directories(${PROJECT_NAME} PRIVATE
12-
${SDL2_INCLUDE_DIRS}
13-
${SDL2_MIXER_INCLUDE_DIRS}
14-
)
7+
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
8+
find_package(SDL3_mixer REQUIRED)
159

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

2115
if(PSP)

_includes/samples/sdl_mixer/main.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include <SDL3/SDL.h>
2+
#include <SDL3/SDL_main.h>
23
#include <SDL3_mixer/SDL_mixer.h>
34

4-
// Define MIN macro
5-
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
6-
75
// Define screen dimensions
86
#define SCREEN_WIDTH 480
97
#define SCREEN_HEIGHT 272
@@ -53,8 +51,8 @@ int main(int argc, char **argv) {
5351
SDL_Rect rect;
5452

5553
// Square dimensions: Half of the min(SCREEN_WIDTH, SCREEN_HEIGHT)
56-
rect.w = MIN(SCREEN_WIDTH, SCREEN_HEIGHT) / 2;
57-
rect.h = MIN(SCREEN_WIDTH, SCREEN_HEIGHT) / 2;
54+
rect.w = SDL_min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2;
55+
rect.h = SDL_min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2;
5856

5957
// Square position: In the middle of the screen
6058
rect.x = SCREEN_WIDTH / 2 - rect.w / 2;

_includes/samples/sdl_ttf/main.c

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,58 @@
88

99
int main(int argc, char **argv)
1010
{
11+
// This prevents compiler warnings
12+
// We don't actually need these variables, but they do need to be there so SDL_main works
1113
(void)argc;
1214
(void)argv;
1315

14-
// Initialize SDL
15-
if (!SDL_Init(SDL_INIT_VIDEO))
16-
{
17-
SDL_Log("SDL2 could not be initialized!\n"
18-
"SDL2 Error: %s",
19-
SDL_GetError());
16+
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
17+
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
2018
return 1;
2119
}
2220

23-
// Initialize SDL_ttf
24-
if (!TTF_Init())
25-
{
26-
SDL_Log("SDL2_ttf could not be initialized!\n"
27-
"SDL2_ttf Error: %s",
28-
SDL_GetError());
21+
// Initialize SDL ttf
22+
if (!TTF_Init()) {
23+
SDL_Log("Couldn't initialize SDL ttf: %s", SDL_GetError());
24+
SDL_Quit();
2925
return 2;
3026
}
3127

32-
SDL_Window *win = SDL_CreateWindow(
33-
"window",
34-
SCREEN_WIDTH,
35-
SCREEN_HEIGHT,
36-
0);
37-
if (!win)
38-
{
39-
SDL_Log("Window could not be created!"
40-
"SDL_Error: %s\n",
41-
SDL_GetError());
28+
SDL_Window * window = NULL;
29+
SDL_Renderer * renderer = NULL;
30+
if (!SDL_CreateWindowAndRenderer("window", 480, 272, 0, &window, &renderer)) {
31+
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
32+
SDL_Quit();
33+
return 3;
4234
}
4335

44-
SDL_Renderer *renderer = SDL_CreateRenderer(win, NULL);
36+
// Load the font file, make sure it is in the directory the eboot file is in
4537
TTF_Font *font = TTF_OpenFont("Pacifico.ttf", 40);
38+
if (!font) {
39+
SDL_Log("Couldn't load font: %s", SDL_GetError());
40+
SDL_DestroyRenderer(renderer);
41+
SDL_DestroyWindow(window);
42+
TTF_Quit();
43+
SDL_Quit();
44+
return 4;
45+
}
4646

4747
// Set the text and background color
48-
SDL_Color text_color = {0xff, 0xff, 0xff, 0xff};
49-
SDL_Color bg_color = {0x00, 0x00, 0x00, 0xff};
48+
SDL_Color text_color = {0x00, 0x00, 0x00, 0xff};
49+
SDL_Color bg_color = {0xff, 0xff, 0xff, 0xff};
5050

51+
// Create a texture using a string and SDL ttf
5152
const char * text = "Hello World!";
52-
SDL_FRect text_rect;
53-
SDL_Surface *surface = TTF_RenderText_Blended(font, text, SDL_strlen(text), text_color);
54-
text_rect.w = (float) surface->w;
55-
text_rect.h = (float) surface->h;
53+
SDL_FRect text_rect = {0, 0, 0, 0};
54+
SDL_Surface *surface = TTF_RenderText_Solid(font, text, SDL_strlen(text), text_color);
55+
if (!surface) {
56+
SDL_Log("Couldn't render text: %s", SDL_GetError());
57+
SDL_DestroyRenderer(renderer);
58+
SDL_DestroyWindow(window);
59+
TTF_Quit();
60+
SDL_Quit();
61+
return 5;
62+
}
5663
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
5764

5865
// Get text dimensions
@@ -66,35 +73,34 @@ int main(int argc, char **argv)
6673

6774
int running = 1;
6875
SDL_Event e;
69-
while (running)
70-
{
71-
if (SDL_PollEvent(&e))
72-
{
73-
switch (e.type)
74-
{
75-
case SDL_EVENT_QUIT:
76-
running = 0;
77-
break;
78-
case SDL_EVENT_GAMEPAD_ADDED:
79-
SDL_OpenGamepad(e.cdevice.which);
80-
break;
81-
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
82-
if (e.gbutton.button == SDL_GAMEPAD_BUTTON_START)
83-
{
76+
while (running) {
77+
if (SDL_PollEvent(&e)) {
78+
switch (e.type) {
79+
case SDL_EVENT_QUIT: // Event that is passed when the user closes the program
8480
running = 0;
85-
}
86-
break;
81+
break;
82+
case SDL_EVENT_GAMEPAD_ADDED: // Activate any gamepad that is connected
83+
SDL_OpenGamepad(e.cdevice.which);
84+
break;
85+
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
86+
if (e.gbutton.button == SDL_GAMEPAD_BUTTON_START) {
87+
running = 0;
88+
}
89+
break;
8790
}
8891
}
8992

93+
// Refresh the screen and redraw the text every frame
9094
SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
9195
SDL_RenderClear(renderer);
9296
SDL_RenderTexture(renderer, texture, NULL, &text_rect);
9397
SDL_RenderPresent(renderer);
9498
}
9599

100+
// Clean up
101+
SDL_DestroyTexture(texture);
96102
SDL_DestroyRenderer(renderer);
97-
SDL_DestroyWindow(win);
103+
SDL_DestroyWindow(window);
98104
TTF_Quit();
99105
SDL_Quit();
100106

0 commit comments

Comments
 (0)