Xcode-iPhoneOS/Demos/src/rectangles.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 13 Mar 2011 11:17:11 -0700
changeset 5483 ccec742c9b7d
parent 5208 78db79f5a4e2
permissions -rw-r--r--
Removed unreferenced variable
slouken@3277
     1
/*
slouken@3277
     2
 *	rectangles.c
slouken@3277
     3
 *	written by Holmes Futrell
slouken@3277
     4
 *	use however you want
slouken@3277
     5
*/
slouken@3277
     6
slouken@3277
     7
#include "SDL.h"
slouken@3277
     8
#include <time.h>
slouken@3277
     9
#include "common.h"
slouken@3277
    10
slouken@3277
    11
void
slouken@5208
    12
render(SDL_Renderer *renderer)
slouken@3277
    13
{
slouken@3277
    14
slouken@3277
    15
    Uint8 r, g, b;
slouken@3277
    16
    /*  Come up with a random rectangle */
slouken@3277
    17
    SDL_Rect rect;
slouken@3277
    18
    rect.w = randomInt(64, 128);
slouken@3277
    19
    rect.h = randomInt(64, 128);
slouken@3277
    20
    rect.x = randomInt(0, SCREEN_WIDTH);
slouken@3277
    21
    rect.y = randomInt(0, SCREEN_HEIGHT);
slouken@3277
    22
slouken@3277
    23
    /* Come up with a random color */
slouken@3277
    24
    r = randomInt(50, 255);
slouken@3277
    25
    g = randomInt(50, 255);
slouken@3277
    26
    b = randomInt(50, 255);
slouken@3277
    27
slouken@3277
    28
    /*  Fill the rectangle in the color */
slouken@5208
    29
    SDL_SetRenderDrawColor(renderer, r, g, b, 255);
slouken@5208
    30
    SDL_RenderFillRect(renderer, &rect);
slouken@3277
    31
slouken@3277
    32
    /* update screen */
slouken@5208
    33
    SDL_RenderPresent(renderer);
slouken@3277
    34
slouken@3277
    35
}
slouken@3277
    36
slouken@3277
    37
int
slouken@3277
    38
main(int argc, char *argv[])
slouken@3277
    39
{
slouken@3277
    40
slouken@3685
    41
    SDL_Window *window;
slouken@5208
    42
	SDL_Renderer *renderer;
slouken@3277
    43
    int done;
slouken@3277
    44
    SDL_Event event;
slouken@3277
    45
slouken@3277
    46
    /* initialize SDL */
slouken@3277
    47
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@3277
    48
        fatalError("Could not initialize SDL");
slouken@3277
    49
    }
slouken@3277
    50
slouken@3277
    51
    /* seed random number generator */
slouken@3277
    52
    srand(time(NULL));
slouken@3277
    53
slouken@3277
    54
    /* create window and renderer */
slouken@3685
    55
    window =
slouken@3277
    56
        SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
slouken@3277
    57
                         SDL_WINDOW_SHOWN);
slouken@3685
    58
    if (window == 0) {
slouken@3277
    59
        fatalError("Could not initialize Window");
slouken@3277
    60
    }
slouken@5208
    61
    renderer = SDL_CreateRenderer(window, -1, 0);
slouken@5208
    62
	if (!renderer) {
slouken@3277
    63
        fatalError("Could not create renderer");
slouken@3277
    64
    }
slouken@3277
    65
slouken@3277
    66
    /* Fill screen with black */
slouken@5208
    67
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
slouken@5208
    68
    SDL_RenderClear(renderer);
slouken@3277
    69
slouken@3277
    70
    /* Enter render loop, waiting for user to quit */
slouken@3277
    71
    done = 0;
slouken@3277
    72
    while (!done) {
slouken@3277
    73
        while (SDL_PollEvent(&event)) {
slouken@3277
    74
            if (event.type == SDL_QUIT) {
slouken@3277
    75
                done = 1;
slouken@3277
    76
            }
slouken@3277
    77
        }
slouken@5208
    78
        render(renderer);
slouken@3277
    79
        SDL_Delay(1);
slouken@3277
    80
    }
slouken@3277
    81
slouken@3277
    82
    /* shutdown SDL */
slouken@3277
    83
    SDL_Quit();
slouken@3277
    84
slouken@3277
    85
    return 0;
slouken@3277
    86
}