Xcode-iOS/Template/SDL iOS Application/main.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 13 Feb 2018 08:13:29 -0800
changeset 11869 68864895c386
parent 10943 729e39fc0175
permissions -rw-r--r--
Fixed bug 3947 - replace strlcpy with memcpy in SDL_strdup()
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"
philipp@10943
     8
#include <stdio.h>
philipp@10943
     9
#include <stdlib.h>
ewing@5277
    10
#include <time.h>
ewing@5277
    11
ewing@5277
    12
#define SCREEN_WIDTH 320
ewing@5277
    13
#define SCREEN_HEIGHT 480
ewing@5277
    14
ewing@5277
    15
int
ewing@5277
    16
randomInt(int min, int max)
ewing@5277
    17
{
ewing@5277
    18
    return min + rand() % (max - min + 1);
ewing@5277
    19
}
ewing@5277
    20
ewing@5277
    21
void
slouken@5366
    22
render(SDL_Renderer *renderer)
ewing@5277
    23
{
ewing@5277
    24
philipp@10943
    25
    SDL_Rect rect;
ewing@5277
    26
    Uint8 r, g, b;
slouken@5366
    27
slouken@5366
    28
    /* Clear the screen */
slouken@5412
    29
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
slouken@5511
    30
    SDL_RenderClear(renderer);
slouken@5366
    31
ewing@5277
    32
    /*  Come up with a random rectangle */
ewing@5277
    33
    rect.w = randomInt(64, 128);
ewing@5277
    34
    rect.h = randomInt(64, 128);
ewing@5277
    35
    rect.x = randomInt(0, SCREEN_WIDTH);
ewing@5277
    36
    rect.y = randomInt(0, SCREEN_HEIGHT);
ewing@5277
    37
ewing@5277
    38
    /* Come up with a random color */
ewing@5277
    39
    r = randomInt(50, 255);
ewing@5277
    40
    g = randomInt(50, 255);
ewing@5277
    41
    b = randomInt(50, 255);
slouken@5366
    42
    SDL_SetRenderDrawColor(renderer, r, g, b, 255);
ewing@5277
    43
ewing@5277
    44
    /*  Fill the rectangle in the color */
slouken@5366
    45
    SDL_RenderFillRect(renderer, &rect);
ewing@5277
    46
ewing@5277
    47
    /* update screen */
slouken@5366
    48
    SDL_RenderPresent(renderer);
ewing@5277
    49
}
ewing@5277
    50
ewing@5277
    51
int
ewing@5277
    52
main(int argc, char *argv[])
ewing@5277
    53
{
ewing@5277
    54
slouken@5366
    55
    SDL_Window *window;
slouken@5366
    56
    SDL_Renderer *renderer;
ewing@5277
    57
    int done;
ewing@5277
    58
    SDL_Event event;
ewing@5277
    59
ewing@5277
    60
    /* initialize SDL */
ewing@5277
    61
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
ewing@5277
    62
        printf("Could not initialize SDL\n");
slouken@5366
    63
        return 1;
ewing@5277
    64
    }
ewing@5277
    65
ewing@5277
    66
    /* seed random number generator */
ewing@5277
    67
    srand(time(NULL));
ewing@5277
    68
ewing@5277
    69
    /* create window and renderer */
slouken@5366
    70
    window =
ewing@5277
    71
        SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
philipp@7497
    72
                         SDL_WINDOW_OPENGL);
slouken@5366
    73
    if (!window) {
ewing@5277
    74
        printf("Could not initialize Window\n");
slouken@5366
    75
        return 1;
ewing@5277
    76
    }
ewing@5277
    77
slouken@5366
    78
    renderer = SDL_CreateRenderer(window, -1, 0);
slouken@5511
    79
    if (!renderer) {
slouken@5366
    80
        printf("Could not create renderer\n");
slouken@5366
    81
        return 1;
slouken@5366
    82
    }
ewing@5277
    83
ewing@5277
    84
    /* Enter render loop, waiting for user to quit */
ewing@5277
    85
    done = 0;
ewing@5277
    86
    while (!done) {
ewing@5277
    87
        while (SDL_PollEvent(&event)) {
ewing@5277
    88
            if (event.type == SDL_QUIT) {
ewing@5277
    89
                done = 1;
ewing@5277
    90
            }
ewing@5277
    91
        }
slouken@5366
    92
        render(renderer);
ewing@5277
    93
        SDL_Delay(1);
ewing@5277
    94
    }
ewing@5277
    95
ewing@5277
    96
    /* shutdown SDL */
ewing@5277
    97
    SDL_Quit();
ewing@5277
    98
ewing@5277
    99
    return 0;
ewing@5277
   100
}