Xcode-iOS/Template/SDL iOS Application/main.c
changeset 6159 fb6f363032fd
parent 5511 be843abc1f6c
child 7191 75360622e65f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Xcode-iOS/Template/SDL iOS Application/main.c	Thu Jan 05 21:41:55 2012 -0500
     1.3 @@ -0,0 +1,98 @@
     1.4 +/*
     1.5 + *	rectangles.c
     1.6 + *	written by Holmes Futrell
     1.7 + *	use however you want
     1.8 + */
     1.9 +
    1.10 +#include "SDL.h"
    1.11 +#include <time.h>
    1.12 +
    1.13 +#define SCREEN_WIDTH 320
    1.14 +#define SCREEN_HEIGHT 480
    1.15 +
    1.16 +int
    1.17 +randomInt(int min, int max)
    1.18 +{
    1.19 +    return min + rand() % (max - min + 1);
    1.20 +}
    1.21 +
    1.22 +void
    1.23 +render(SDL_Renderer *renderer)
    1.24 +{
    1.25 +
    1.26 +    Uint8 r, g, b;
    1.27 +
    1.28 +    /* Clear the screen */
    1.29 +    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    1.30 +    SDL_RenderClear(renderer);
    1.31 +
    1.32 +    /*  Come up with a random rectangle */
    1.33 +    SDL_Rect rect;
    1.34 +    rect.w = randomInt(64, 128);
    1.35 +    rect.h = randomInt(64, 128);
    1.36 +    rect.x = randomInt(0, SCREEN_WIDTH);
    1.37 +    rect.y = randomInt(0, SCREEN_HEIGHT);
    1.38 +
    1.39 +    /* Come up with a random color */
    1.40 +    r = randomInt(50, 255);
    1.41 +    g = randomInt(50, 255);
    1.42 +    b = randomInt(50, 255);
    1.43 +    SDL_SetRenderDrawColor(renderer, r, g, b, 255);
    1.44 +
    1.45 +    /*  Fill the rectangle in the color */
    1.46 +    SDL_RenderFillRect(renderer, &rect);
    1.47 +
    1.48 +    /* update screen */
    1.49 +    SDL_RenderPresent(renderer);
    1.50 +}
    1.51 +
    1.52 +int
    1.53 +main(int argc, char *argv[])
    1.54 +{
    1.55 +
    1.56 +    SDL_Window *window;
    1.57 +    SDL_Renderer *renderer;
    1.58 +    int done;
    1.59 +    SDL_Event event;
    1.60 +
    1.61 +    /* initialize SDL */
    1.62 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    1.63 +        printf("Could not initialize SDL\n");
    1.64 +        return 1;
    1.65 +    }
    1.66 +
    1.67 +    /* seed random number generator */
    1.68 +    srand(time(NULL));
    1.69 +
    1.70 +    /* create window and renderer */
    1.71 +    window =
    1.72 +        SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
    1.73 +                         SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    1.74 +    if (!window) {
    1.75 +        printf("Could not initialize Window\n");
    1.76 +        return 1;
    1.77 +    }
    1.78 +
    1.79 +    renderer = SDL_CreateRenderer(window, -1, 0);
    1.80 +    if (!renderer) {
    1.81 +        printf("Could not create renderer\n");
    1.82 +        return 1;
    1.83 +    }
    1.84 +
    1.85 +    /* Enter render loop, waiting for user to quit */
    1.86 +    done = 0;
    1.87 +    while (!done) {
    1.88 +        while (SDL_PollEvent(&event)) {
    1.89 +            if (event.type == SDL_QUIT) {
    1.90 +                done = 1;
    1.91 +            }
    1.92 +        }
    1.93 +        render(renderer);
    1.94 +        SDL_Delay(1);
    1.95 +    }
    1.96 +
    1.97 +    /* shutdown SDL */
    1.98 +    SDL_Quit();
    1.99 +
   1.100 +    return 0;
   1.101 +}