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