Xcode-iOS/Template/SDL iOS Application/main.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 21 Jul 2013 22:09:00 +0200
changeset 7497 109f37bca7cc
parent 7191 75360622e65f
child 10943 729e39fc0175
permissions -rw-r--r--
Removed not needed SDL_WINDOW_SHOWN from iOS example programs.
See bug #1667.
ewing@5277
     1
/*
slouken@7191
     2
 *  rectangles.c
slouken@7191
     3
 *  written by Holmes Futrell
slouken@7191
     4
 *  use however you want
ewing@5277
     5
 */
ewing@5277
     6
ewing@5277
     7
#include "SDL.h"
ewing@5277
     8
#include <time.h>
ewing@5277
     9
ewing@5277
    10
#define SCREEN_WIDTH 320
ewing@5277
    11
#define SCREEN_HEIGHT 480
ewing@5277
    12
ewing@5277
    13
int
ewing@5277
    14
randomInt(int min, int max)
ewing@5277
    15
{
ewing@5277
    16
    return min + rand() % (max - min + 1);
ewing@5277
    17
}
ewing@5277
    18
ewing@5277
    19
void
slouken@5366
    20
render(SDL_Renderer *renderer)
ewing@5277
    21
{
ewing@5277
    22
ewing@5277
    23
    Uint8 r, g, b;
slouken@5366
    24
slouken@5366
    25
    /* Clear the screen */
slouken@5412
    26
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
slouken@5511
    27
    SDL_RenderClear(renderer);
slouken@5366
    28
ewing@5277
    29
    /*  Come up with a random rectangle */
ewing@5277
    30
    SDL_Rect rect;
ewing@5277
    31
    rect.w = randomInt(64, 128);
ewing@5277
    32
    rect.h = randomInt(64, 128);
ewing@5277
    33
    rect.x = randomInt(0, SCREEN_WIDTH);
ewing@5277
    34
    rect.y = randomInt(0, SCREEN_HEIGHT);
ewing@5277
    35
ewing@5277
    36
    /* Come up with a random color */
ewing@5277
    37
    r = randomInt(50, 255);
ewing@5277
    38
    g = randomInt(50, 255);
ewing@5277
    39
    b = randomInt(50, 255);
slouken@5366
    40
    SDL_SetRenderDrawColor(renderer, r, g, b, 255);
ewing@5277
    41
ewing@5277
    42
    /*  Fill the rectangle in the color */
slouken@5366
    43
    SDL_RenderFillRect(renderer, &rect);
ewing@5277
    44
ewing@5277
    45
    /* update screen */
slouken@5366
    46
    SDL_RenderPresent(renderer);
ewing@5277
    47
}
ewing@5277
    48
ewing@5277
    49
int
ewing@5277
    50
main(int argc, char *argv[])
ewing@5277
    51
{
ewing@5277
    52
slouken@5366
    53
    SDL_Window *window;
slouken@5366
    54
    SDL_Renderer *renderer;
ewing@5277
    55
    int done;
ewing@5277
    56
    SDL_Event event;
ewing@5277
    57
ewing@5277
    58
    /* initialize SDL */
ewing@5277
    59
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
ewing@5277
    60
        printf("Could not initialize SDL\n");
slouken@5366
    61
        return 1;
ewing@5277
    62
    }
ewing@5277
    63
ewing@5277
    64
    /* seed random number generator */
ewing@5277
    65
    srand(time(NULL));
ewing@5277
    66
ewing@5277
    67
    /* create window and renderer */
slouken@5366
    68
    window =
ewing@5277
    69
        SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
philipp@7497
    70
                         SDL_WINDOW_OPENGL);
slouken@5366
    71
    if (!window) {
ewing@5277
    72
        printf("Could not initialize Window\n");
slouken@5366
    73
        return 1;
ewing@5277
    74
    }
ewing@5277
    75
slouken@5366
    76
    renderer = SDL_CreateRenderer(window, -1, 0);
slouken@5511
    77
    if (!renderer) {
slouken@5366
    78
        printf("Could not create renderer\n");
slouken@5366
    79
        return 1;
slouken@5366
    80
    }
ewing@5277
    81
ewing@5277
    82
    /* Enter render loop, waiting for user to quit */
ewing@5277
    83
    done = 0;
ewing@5277
    84
    while (!done) {
ewing@5277
    85
        while (SDL_PollEvent(&event)) {
ewing@5277
    86
            if (event.type == SDL_QUIT) {
ewing@5277
    87
                done = 1;
ewing@5277
    88
            }
ewing@5277
    89
        }
slouken@5366
    90
        render(renderer);
ewing@5277
    91
        SDL_Delay(1);
ewing@5277
    92
    }
ewing@5277
    93
ewing@5277
    94
    /* shutdown SDL */
ewing@5277
    95
    SDL_Quit();
ewing@5277
    96
ewing@5277
    97
    return 0;
ewing@5277
    98
}