Updated the template code
authorSam Lantinga <slouken@libsdl.org>
Sun, 20 Feb 2011 19:15:00 -0800
changeset 5366d032ec40e7c1
parent 5365 db3c6ebcc954
child 5367 71fe70373e58
Updated the template code
Xcode-iPhoneOS/Template/SDL iOS Application/main.c
Xcode/TemplatesForXcodeSnowLeopard/SDL OpenGL Application/main.c
     1.1 --- a/Xcode-iPhoneOS/Template/SDL iOS Application/main.c	Sun Feb 20 14:59:36 2011 -0800
     1.2 +++ b/Xcode-iPhoneOS/Template/SDL iOS Application/main.c	Sun Feb 20 19:15:00 2011 -0800
     1.3 @@ -17,10 +17,15 @@
     1.4  }
     1.5  
     1.6  void
     1.7 -render(void)
     1.8 +render(SDL_Renderer *renderer)
     1.9  {
    1.10  
    1.11      Uint8 r, g, b;
    1.12 +
    1.13 +    /* Clear the screen */
    1.14 +    SDL_SetRenderDrawColor(0, 0, 0, 255);
    1.15 +    SDL_SetRenderClear(renderer);
    1.16 +
    1.17      /*  Come up with a random rectangle */
    1.18      SDL_Rect rect;
    1.19      rect.w = randomInt(64, 128);
    1.20 @@ -32,45 +37,47 @@
    1.21      r = randomInt(50, 255);
    1.22      g = randomInt(50, 255);
    1.23      b = randomInt(50, 255);
    1.24 -    SDL_SetRenderDrawColor(r, g, b, 255);
    1.25 +    SDL_SetRenderDrawColor(renderer, r, g, b, 255);
    1.26  
    1.27      /*  Fill the rectangle in the color */
    1.28 -    SDL_RenderFillRect(&rect);
    1.29 +    SDL_RenderFillRect(renderer, &rect);
    1.30  
    1.31      /* update screen */
    1.32 -    SDL_RenderPresent();
    1.33 -
    1.34 +    SDL_RenderPresent(renderer);
    1.35  }
    1.36  
    1.37  int
    1.38  main(int argc, char *argv[])
    1.39  {
    1.40  
    1.41 -    SDL_WindowID windowID;
    1.42 +    SDL_Window *window;
    1.43 +    SDL_Renderer *renderer;
    1.44      int done;
    1.45      SDL_Event event;
    1.46  
    1.47      /* initialize SDL */
    1.48      if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    1.49          printf("Could not initialize SDL\n");
    1.50 +        return 1;
    1.51      }
    1.52  
    1.53      /* seed random number generator */
    1.54      srand(time(NULL));
    1.55  
    1.56      /* create window and renderer */
    1.57 -    windowID =
    1.58 +    window =
    1.59          SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
    1.60                           SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    1.61 -    if (windowID == 0) {
    1.62 +    if (!window) {
    1.63          printf("Could not initialize Window\n");
    1.64 -    }
    1.65 -    if (SDL_CreateRenderer(windowID, -1, 0) != 0) {
    1.66 -        printf("Could not create renderer\n");
    1.67 +        return 1;
    1.68      }
    1.69  
    1.70 -    /* Fill screen with black */
    1.71 -    SDL_RenderClear();
    1.72 +    renderer = SDL_CreateRenderer(window, -1, 0);
    1.73 +    if (renderer) {
    1.74 +        printf("Could not create renderer\n");
    1.75 +        return 1;
    1.76 +    }
    1.77  
    1.78      /* Enter render loop, waiting for user to quit */
    1.79      done = 0;
    1.80 @@ -80,7 +87,7 @@
    1.81                  done = 1;
    1.82              }
    1.83          }
    1.84 -        render();
    1.85 +        render(renderer);
    1.86          SDL_Delay(1);
    1.87      }
    1.88  
     2.1 --- a/Xcode/TemplatesForXcodeSnowLeopard/SDL OpenGL Application/main.c	Sun Feb 20 14:59:36 2011 -0800
     2.2 +++ b/Xcode/TemplatesForXcodeSnowLeopard/SDL OpenGL Application/main.c	Sun Feb 20 19:15:00 2011 -0800
     2.3 @@ -75,12 +75,12 @@
     2.4      // Create window
     2.5      gScreen = SDL_SetVideoMode (640, 480, 0, flags);
     2.6      if (gScreen == NULL) {
     2.7 -		
     2.8 +        
     2.9          fprintf (stderr, "Couldn't set 640x480 OpenGL video mode: %s\n",
    2.10                   SDL_GetError());
    2.11 -		SDL_Quit();
    2.12 -		exit(2);
    2.13 -	}
    2.14 +        SDL_Quit();
    2.15 +        exit(2);
    2.16 +    }
    2.17  }
    2.18  
    2.19  static void initGL ()
    2.20 @@ -100,29 +100,29 @@
    2.21      SDL_Event event;
    2.22      int done = 0;
    2.23      int fps = 24;
    2.24 -	int delay = 1000/fps;
    2.25 +    int delay = 1000/fps;
    2.26      int thenTicks = -1;
    2.27      int nowTicks;
    2.28      
    2.29      while ( !done ) {
    2.30  
    2.31 -		/* Check for events */
    2.32 -		while ( SDL_PollEvent (&event) ) {
    2.33 -			switch (event.type) {
    2.34 +        /* Check for events */
    2.35 +        while ( SDL_PollEvent (&event) ) {
    2.36 +            switch (event.type) {
    2.37  
    2.38 -				case SDL_MOUSEMOTION:
    2.39 -					break;
    2.40 -				case SDL_MOUSEBUTTONDOWN:
    2.41 -					break;
    2.42 -				case SDL_KEYDOWN:
    2.43 -					/* Any keypress quits the app... */
    2.44 -				case SDL_QUIT:
    2.45 -					done = 1;
    2.46 -					break;
    2.47 -				default:
    2.48 -					break;
    2.49 -			}
    2.50 -		}
    2.51 +                case SDL_MOUSEMOTION:
    2.52 +                    break;
    2.53 +                case SDL_MOUSEBUTTONDOWN:
    2.54 +                    break;
    2.55 +                case SDL_KEYDOWN:
    2.56 +                    /* Any keypress quits the app... */
    2.57 +                case SDL_QUIT:
    2.58 +                    done = 1;
    2.59 +                    break;
    2.60 +                default:
    2.61 +                    break;
    2.62 +            }
    2.63 +        }
    2.64      
    2.65          // Draw at 24 hz
    2.66          //     This approach is not normally recommended - it is better to
    2.67 @@ -144,18 +144,18 @@
    2.68          }
    2.69  
    2.70          SDL_Delay (delay);
    2.71 -	}
    2.72 +    }
    2.73  }
    2.74  
    2.75  int main(int argc, char *argv[])
    2.76  {
    2.77 -	// Init SDL video subsystem
    2.78 -	if ( SDL_Init (SDL_INIT_VIDEO) < 0 ) {
    2.79 -		
    2.80 +    // Init SDL video subsystem
    2.81 +    if ( SDL_Init (SDL_INIT_VIDEO) < 0 ) {
    2.82 +        
    2.83          fprintf(stderr, "Couldn't initialize SDL: %s\n",
    2.84 -			SDL_GetError());
    2.85 -		exit(1);
    2.86 -	}
    2.87 +            SDL_GetError());
    2.88 +        exit(1);
    2.89 +    }
    2.90  
    2.91      // Set GL context attributes
    2.92      initAttributes ();
    2.93 @@ -173,7 +173,7 @@
    2.94      mainLoop ();
    2.95      
    2.96      // Cleanup
    2.97 -	SDL_Quit();
    2.98 -	
    2.99 +    SDL_Quit();
   2.100 +
   2.101      return 0;
   2.102  }