From 5e5bac697662d923726cee56bd2761930ae453ba Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 20 Feb 2011 19:15:00 -0800 Subject: [PATCH] Updated the template code --- .../Template/SDL iOS Application/main.c | 33 ++++++---- .../SDL OpenGL Application/main.c | 62 +++++++++---------- 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/Xcode-iPhoneOS/Template/SDL iOS Application/main.c b/Xcode-iPhoneOS/Template/SDL iOS Application/main.c index 8b20954b7..c9cd557b3 100644 --- a/Xcode-iPhoneOS/Template/SDL iOS Application/main.c +++ b/Xcode-iPhoneOS/Template/SDL iOS Application/main.c @@ -17,10 +17,15 @@ randomInt(int min, int max) } void -render(void) +render(SDL_Renderer *renderer) { Uint8 r, g, b; + + /* Clear the screen */ + SDL_SetRenderDrawColor(0, 0, 0, 255); + SDL_SetRenderClear(renderer); + /* Come up with a random rectangle */ SDL_Rect rect; rect.w = randomInt(64, 128); @@ -32,46 +37,48 @@ render(void) r = randomInt(50, 255); g = randomInt(50, 255); b = randomInt(50, 255); - SDL_SetRenderDrawColor(r, g, b, 255); + SDL_SetRenderDrawColor(renderer, r, g, b, 255); /* Fill the rectangle in the color */ - SDL_RenderFillRect(&rect); + SDL_RenderFillRect(renderer, &rect); /* update screen */ - SDL_RenderPresent(); - + SDL_RenderPresent(renderer); } int main(int argc, char *argv[]) { - SDL_WindowID windowID; + SDL_Window *window; + SDL_Renderer *renderer; int done; SDL_Event event; /* initialize SDL */ if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("Could not initialize SDL\n"); + return 1; } /* seed random number generator */ srand(time(NULL)); /* create window and renderer */ - windowID = + window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); - if (windowID == 0) { + if (!window) { printf("Could not initialize Window\n"); + return 1; } - if (SDL_CreateRenderer(windowID, -1, 0) != 0) { + + renderer = SDL_CreateRenderer(window, -1, 0); + if (renderer) { printf("Could not create renderer\n"); + return 1; } - /* Fill screen with black */ - SDL_RenderClear(); - /* Enter render loop, waiting for user to quit */ done = 0; while (!done) { @@ -80,7 +87,7 @@ main(int argc, char *argv[]) done = 1; } } - render(); + render(renderer); SDL_Delay(1); } diff --git a/Xcode/TemplatesForXcodeSnowLeopard/SDL OpenGL Application/main.c b/Xcode/TemplatesForXcodeSnowLeopard/SDL OpenGL Application/main.c index b7794b3b6..1ef49c0c9 100644 --- a/Xcode/TemplatesForXcodeSnowLeopard/SDL OpenGL Application/main.c +++ b/Xcode/TemplatesForXcodeSnowLeopard/SDL OpenGL Application/main.c @@ -75,12 +75,12 @@ static void createSurface (int fullscreen) // Create window gScreen = SDL_SetVideoMode (640, 480, 0, flags); if (gScreen == NULL) { - + fprintf (stderr, "Couldn't set 640x480 OpenGL video mode: %s\n", SDL_GetError()); - SDL_Quit(); - exit(2); - } + SDL_Quit(); + exit(2); + } } static void initGL () @@ -100,29 +100,29 @@ static void mainLoop () SDL_Event event; int done = 0; int fps = 24; - int delay = 1000/fps; + int delay = 1000/fps; int thenTicks = -1; int nowTicks; while ( !done ) { - /* Check for events */ - while ( SDL_PollEvent (&event) ) { - switch (event.type) { - - case SDL_MOUSEMOTION: - break; - case SDL_MOUSEBUTTONDOWN: - break; - case SDL_KEYDOWN: - /* Any keypress quits the app... */ - case SDL_QUIT: - done = 1; - break; - default: - break; - } - } + /* Check for events */ + while ( SDL_PollEvent (&event) ) { + switch (event.type) { + + case SDL_MOUSEMOTION: + break; + case SDL_MOUSEBUTTONDOWN: + break; + case SDL_KEYDOWN: + /* Any keypress quits the app... */ + case SDL_QUIT: + done = 1; + break; + default: + break; + } + } // Draw at 24 hz // This approach is not normally recommended - it is better to @@ -144,18 +144,18 @@ static void mainLoop () } SDL_Delay (delay); - } + } } int main(int argc, char *argv[]) { - // Init SDL video subsystem - if ( SDL_Init (SDL_INIT_VIDEO) < 0 ) { - + // Init SDL video subsystem + if ( SDL_Init (SDL_INIT_VIDEO) < 0 ) { + fprintf(stderr, "Couldn't initialize SDL: %s\n", - SDL_GetError()); - exit(1); - } + SDL_GetError()); + exit(1); + } // Set GL context attributes initAttributes (); @@ -173,7 +173,7 @@ int main(int argc, char *argv[]) mainLoop (); // Cleanup - SDL_Quit(); - + SDL_Quit(); + return 0; }