cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

main.c (2001B)


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