cscg22-gearboy

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

testdraw2.c (8864B)


      1/*
      2  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
      3
      4  This software is provided 'as-is', without any express or implied
      5  warranty.  In no event will the authors be held liable for any damages
      6  arising from the use of this software.
      7
      8  Permission is granted to anyone to use this software for any purpose,
      9  including commercial applications, and to alter it and redistribute it
     10  freely.
     11*/
     12
     13/* Simple program:  draw as many random objects on the screen as possible */
     14
     15#include <stdlib.h>
     16#include <stdio.h>
     17#include <time.h>
     18
     19#include "SDL_test_common.h"
     20
     21#define NUM_OBJECTS 100
     22
     23static SDLTest_CommonState *state;
     24static int num_objects;
     25static SDL_bool cycle_color;
     26static SDL_bool cycle_alpha;
     27static int cycle_direction = 1;
     28static int current_alpha = 255;
     29static int current_color = 255;
     30static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
     31
     32void
     33DrawPoints(SDL_Renderer * renderer)
     34{
     35    int i;
     36    int x, y;
     37    SDL_Rect viewport;
     38
     39    /* Query the sizes */
     40    SDL_RenderGetViewport(renderer, &viewport);
     41
     42    for (i = 0; i < num_objects * 4; ++i) {
     43        /* Cycle the color and alpha, if desired */
     44        if (cycle_color) {
     45            current_color += cycle_direction;
     46            if (current_color < 0) {
     47                current_color = 0;
     48                cycle_direction = -cycle_direction;
     49            }
     50            if (current_color > 255) {
     51                current_color = 255;
     52                cycle_direction = -cycle_direction;
     53            }
     54        }
     55        if (cycle_alpha) {
     56            current_alpha += cycle_direction;
     57            if (current_alpha < 0) {
     58                current_alpha = 0;
     59                cycle_direction = -cycle_direction;
     60            }
     61            if (current_alpha > 255) {
     62                current_alpha = 255;
     63                cycle_direction = -cycle_direction;
     64            }
     65        }
     66        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
     67                               (Uint8) current_color, (Uint8) current_alpha);
     68
     69        x = rand() % viewport.w;
     70        y = rand() % viewport.h;
     71        SDL_RenderDrawPoint(renderer, x, y);
     72    }
     73}
     74
     75void
     76DrawLines(SDL_Renderer * renderer)
     77{
     78    int i;
     79    int x1, y1, x2, y2;
     80    SDL_Rect viewport;
     81
     82    /* Query the sizes */
     83    SDL_RenderGetViewport(renderer, &viewport);
     84
     85    for (i = 0; i < num_objects; ++i) {
     86        /* Cycle the color and alpha, if desired */
     87        if (cycle_color) {
     88            current_color += cycle_direction;
     89            if (current_color < 0) {
     90                current_color = 0;
     91                cycle_direction = -cycle_direction;
     92            }
     93            if (current_color > 255) {
     94                current_color = 255;
     95                cycle_direction = -cycle_direction;
     96            }
     97        }
     98        if (cycle_alpha) {
     99            current_alpha += cycle_direction;
    100            if (current_alpha < 0) {
    101                current_alpha = 0;
    102                cycle_direction = -cycle_direction;
    103            }
    104            if (current_alpha > 255) {
    105                current_alpha = 255;
    106                cycle_direction = -cycle_direction;
    107            }
    108        }
    109        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
    110                               (Uint8) current_color, (Uint8) current_alpha);
    111
    112        if (i == 0) {
    113            SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
    114            SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
    115            SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
    116            SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
    117        } else {
    118            x1 = (rand() % (viewport.w*2)) - viewport.w;
    119            x2 = (rand() % (viewport.w*2)) - viewport.w;
    120            y1 = (rand() % (viewport.h*2)) - viewport.h;
    121            y2 = (rand() % (viewport.h*2)) - viewport.h;
    122            SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
    123        }
    124    }
    125}
    126
    127void
    128DrawRects(SDL_Renderer * renderer)
    129{
    130    int i;
    131    SDL_Rect rect;
    132    SDL_Rect viewport;
    133
    134    /* Query the sizes */
    135    SDL_RenderGetViewport(renderer, &viewport);
    136
    137    for (i = 0; i < num_objects / 4; ++i) {
    138        /* Cycle the color and alpha, if desired */
    139        if (cycle_color) {
    140            current_color += cycle_direction;
    141            if (current_color < 0) {
    142                current_color = 0;
    143                cycle_direction = -cycle_direction;
    144            }
    145            if (current_color > 255) {
    146                current_color = 255;
    147                cycle_direction = -cycle_direction;
    148            }
    149        }
    150        if (cycle_alpha) {
    151            current_alpha += cycle_direction;
    152            if (current_alpha < 0) {
    153                current_alpha = 0;
    154                cycle_direction = -cycle_direction;
    155            }
    156            if (current_alpha > 255) {
    157                current_alpha = 255;
    158                cycle_direction = -cycle_direction;
    159            }
    160        }
    161        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
    162                               (Uint8) current_color, (Uint8) current_alpha);
    163
    164        rect.w = rand() % (viewport.h / 2);
    165        rect.h = rand() % (viewport.h / 2);
    166        rect.x = (rand() % (viewport.w*2) - viewport.w) - (rect.w / 2);
    167        rect.y = (rand() % (viewport.h*2) - viewport.h) - (rect.h / 2);
    168        SDL_RenderFillRect(renderer, &rect);
    169    }
    170}
    171
    172int
    173main(int argc, char *argv[])
    174{
    175    int i, done;
    176    SDL_Event event;
    177    Uint32 then, now, frames;
    178
    179	/* Enable standard application logging */
    180	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    181
    182    /* Initialize parameters */
    183    num_objects = NUM_OBJECTS;
    184
    185    /* Initialize test framework */
    186    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
    187    if (!state) {
    188        return 1;
    189    }
    190    for (i = 1; i < argc;) {
    191        int consumed;
    192
    193        consumed = SDLTest_CommonArg(state, i);
    194        if (consumed == 0) {
    195            consumed = -1;
    196            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
    197                if (argv[i + 1]) {
    198                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
    199                        blendMode = SDL_BLENDMODE_NONE;
    200                        consumed = 2;
    201                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
    202                        blendMode = SDL_BLENDMODE_BLEND;
    203                        consumed = 2;
    204                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
    205                        blendMode = SDL_BLENDMODE_ADD;
    206                        consumed = 2;
    207                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
    208                        blendMode = SDL_BLENDMODE_MOD;
    209                        consumed = 2;
    210                    }
    211                }
    212            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
    213                cycle_color = SDL_TRUE;
    214                consumed = 1;
    215            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
    216                cycle_alpha = SDL_TRUE;
    217                consumed = 1;
    218            } else if (SDL_isdigit(*argv[i])) {
    219                num_objects = SDL_atoi(argv[i]);
    220                consumed = 1;
    221            }
    222        }
    223        if (consumed < 0) {
    224            SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
    225                    argv[0], SDLTest_CommonUsage(state));
    226            return 1;
    227        }
    228        i += consumed;
    229    }
    230    if (!SDLTest_CommonInit(state)) {
    231        return 2;
    232    }
    233
    234    /* Create the windows and initialize the renderers */
    235    for (i = 0; i < state->num_windows; ++i) {
    236        SDL_Renderer *renderer = state->renderers[i];
    237        SDL_SetRenderDrawBlendMode(renderer, blendMode);
    238        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
    239        SDL_RenderClear(renderer);
    240    }
    241
    242    srand((unsigned int)time(NULL));
    243
    244    /* Main render loop */
    245    frames = 0;
    246    then = SDL_GetTicks();
    247    done = 0;
    248    while (!done) {
    249        /* Check for events */
    250        ++frames;
    251        while (SDL_PollEvent(&event)) {
    252            SDLTest_CommonEvent(state, &event, &done);
    253        }
    254        for (i = 0; i < state->num_windows; ++i) {
    255            SDL_Renderer *renderer = state->renderers[i];
    256            if (state->windows[i] == NULL)
    257                continue;
    258            SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
    259            SDL_RenderClear(renderer);
    260
    261            DrawRects(renderer);
    262            DrawLines(renderer);
    263            DrawPoints(renderer);
    264
    265            SDL_RenderPresent(renderer);
    266        }
    267    }
    268
    269    SDLTest_CommonQuit(state);
    270
    271    /* Print out some timing information */
    272    now = SDL_GetTicks();
    273    if (now > then) {
    274        double fps = ((double) frames * 1000) / (now - then);
    275        SDL_Log("%2.2f frames per second\n", fps);
    276    }
    277    return 0;
    278}
    279
    280/* vi: set ts=4 sw=4 expandtab: */