88
99int 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