breakout

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 1eab2bb6c1f7643e331a0f141ea633db252531fa
parent e95bd82deff056b86640f0815a6e20f56837432a
Author: Chris Langford <chris@hellocld.com>
Date:   Mon, 26 Aug 2019 21:14:47 -0400

Did some cleanup

- Initialization of SDL, the window and the renderer in their own
function
- myquit() renamed to Shutdown
- int temp in main renamed to status, 'cause that's what it's for

Diffstat:
Msrc/main.c | 108++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 66 insertions(+), 42 deletions(-)

diff --git a/src/main.c b/src/main.c @@ -4,9 +4,10 @@ #define WINDOW_W 320 #define WINDOW_H 240 +int Initialize(void); int CreateSprite(void); void MoveSprite(void); -void myquit(int); +void Shutdown(int); SDL_Window *window; SDL_Renderer *renderer; @@ -23,46 +24,15 @@ struct sprite ball; int main(int argc, char *argv[]) { - if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { - SDL_Log("Failed to initialize SDL: %s\n", SDL_GetError()); - return 1; - } - - SDL_Log("SDL initialized successfully\n"); - - data_path = SDL_GetBasePath(); - SDL_Log("Data path: %s\n", data_path); - - SDL_version compiled, linked; - SDL_VERSION(&compiled); - SDL_GetVersion(&linked); - SDL_Log("Compiled with %d.%d.%d\n", compiled.major, compiled.minor, compiled.patch); - SDL_Log("Linked against %d.%d.%d\n", linked.major, linked.minor, linked.patch); - - window = SDL_CreateWindow( - "BREAKOUT", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - WINDOW_W, - WINDOW_H, - 0 - ); - if(window == NULL) { - SDL_Log("Failed to create window: %s\n", SDL_GetError()); - return 1; - } - - renderer = SDL_CreateRenderer(window, -1, 0); - if(renderer == NULL) { - SDL_Log("Failed to create renderer: %s\n", SDL_GetError()); - return 1; - } + + int status; + status = Initialize(); + if(status != 0) + Shutdown(status); - int temp; - temp = CreateSprite(); - if(temp != 0) { - myquit(temp); - } + status = CreateSprite(); + if(status != 0) + Shutdown(status); SDL_Event event; int done = 0; @@ -75,12 +45,13 @@ int main(int argc, char *argv[]) SDL_Delay(10); } - myquit(0); + Shutdown(0); return 0; } -void myquit(int i) +void Shutdown(int i) { + SDL_Log("Shutting down...\n"); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_free(data_path); @@ -148,3 +119,56 @@ void MoveSprite() SDL_RenderPresent(renderer); } + +/* Init SDL, set up the window and renderer */ +int Initialize() +{ + if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { + SDL_Log("Failed to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_Log("SDL initialized successfully\n"); + + data_path = SDL_GetBasePath(); + SDL_Log("Data path: %s\n", data_path); + + SDL_version compiled, linked; + SDL_VERSION(&compiled); + SDL_GetVersion(&linked); + SDL_Log( + "Compiled with %d.%d.%d\n", + compiled.major, + compiled.minor, + compiled.patch + ); + SDL_Log( + "Linked against %d.%d.%d\n", + linked.major, + linked.minor, + linked.patch + ); + + window = SDL_CreateWindow( + "BREAKOUT", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + WINDOW_W, + WINDOW_H, + 0 + ); + + if(window == NULL) { + SDL_Log("Failed to create window: %s\n", SDL_GetError()); + return 1; + } + + renderer = SDL_CreateRenderer(window, -1, 0); + if(renderer == NULL) { + SDL_Log("Failed to create renderer: %s\n", SDL_GetError()); + return 1; + } + + /* Success! */ + return 0; +}