cscg22-gearboy

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

testscale.c (5609B)


      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/* Simple program:  Move N sprites around on the screen as fast as possible */
     13
     14#include <stdlib.h>
     15#include <stdio.h>
     16#include <time.h>
     17
     18#include "SDL_test_common.h"
     19
     20#define WINDOW_WIDTH    640
     21#define WINDOW_HEIGHT   480
     22
     23static SDLTest_CommonState *state;
     24
     25typedef struct {
     26    SDL_Window *window;
     27    SDL_Renderer *renderer;
     28    SDL_Texture *background;
     29    SDL_Texture *sprite;
     30    SDL_Rect sprite_rect;
     31    int scale_direction;
     32} DrawState;
     33
     34/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     35static void
     36quit(int rc)
     37{
     38    SDLTest_CommonQuit(state);
     39    exit(rc);
     40}
     41
     42SDL_Texture *
     43LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
     44{
     45    SDL_Surface *temp;
     46    SDL_Texture *texture;
     47
     48    /* Load the sprite image */
     49    temp = SDL_LoadBMP(file);
     50    if (temp == NULL) {
     51        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
     52        return NULL;
     53    }
     54
     55    /* Set transparent pixel as the pixel at (0,0) */
     56    if (transparent) {
     57        if (temp->format->palette) {
     58            SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
     59        } else {
     60            switch (temp->format->BitsPerPixel) {
     61            case 15:
     62                SDL_SetColorKey(temp, SDL_TRUE,
     63                                (*(Uint16 *) temp->pixels) & 0x00007FFF);
     64                break;
     65            case 16:
     66                SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
     67                break;
     68            case 24:
     69                SDL_SetColorKey(temp, SDL_TRUE,
     70                                (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
     71                break;
     72            case 32:
     73                SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
     74                break;
     75            }
     76        }
     77    }
     78
     79    /* Create textures from the image */
     80    texture = SDL_CreateTextureFromSurface(renderer, temp);
     81    if (!texture) {
     82        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
     83        SDL_FreeSurface(temp);
     84        return NULL;
     85    }
     86    SDL_FreeSurface(temp);
     87
     88    /* We're ready to roll. :) */
     89    return texture;
     90}
     91
     92void
     93Draw(DrawState *s)
     94{
     95    SDL_Rect viewport;
     96
     97    SDL_RenderGetViewport(s->renderer, &viewport);
     98
     99    /* Draw the background */
    100    SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
    101
    102    /* Scale and draw the sprite */
    103    s->sprite_rect.w += s->scale_direction;
    104    s->sprite_rect.h += s->scale_direction;
    105    if (s->scale_direction > 0) {
    106        if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
    107            s->scale_direction = -1;
    108        }
    109    } else {
    110        if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
    111            s->scale_direction = 1;
    112        }
    113    }
    114    s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
    115    s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
    116
    117    SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
    118
    119    /* Update the screen! */
    120    SDL_RenderPresent(s->renderer);
    121}
    122
    123int
    124main(int argc, char *argv[])
    125{
    126    DrawState *drawstates;
    127    int i, done;
    128    SDL_Event event;
    129    int frames;
    130    Uint32 then, now;
    131
    132    /* Enable standard application logging */
    133    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    134
    135    /* Initialize test framework */
    136    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
    137    if (!state) {
    138        return 1;
    139    }
    140    for (i = 1; i < argc;) {
    141        int consumed;
    142
    143        consumed = SDLTest_CommonArg(state, i);
    144        if (consumed == 0) {
    145            SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
    146            return 1;
    147        }
    148        i += consumed;
    149    }
    150    if (!SDLTest_CommonInit(state)) {
    151        quit(2);
    152    }
    153
    154    drawstates = SDL_stack_alloc(DrawState, state->num_windows);
    155    for (i = 0; i < state->num_windows; ++i) {
    156        DrawState *drawstate = &drawstates[i];
    157
    158        drawstate->window = state->windows[i];
    159        drawstate->renderer = state->renderers[i];
    160        drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
    161        drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
    162        if (!drawstate->sprite || !drawstate->background) {
    163            quit(2);
    164        }
    165        SDL_QueryTexture(drawstate->sprite, NULL, NULL,
    166                         &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
    167        drawstate->scale_direction = 1;
    168    }
    169
    170    /* Main render loop */
    171    frames = 0;
    172    then = SDL_GetTicks();
    173    done = 0;
    174    while (!done) {
    175        /* Check for events */
    176        ++frames;
    177        while (SDL_PollEvent(&event)) {
    178            SDLTest_CommonEvent(state, &event, &done);
    179        }
    180        for (i = 0; i < state->num_windows; ++i) {
    181            if (state->windows[i] == NULL)
    182                continue;
    183            Draw(&drawstates[i]);
    184        }
    185    }
    186
    187    /* Print out some timing information */
    188    now = SDL_GetTicks();
    189    if (now > then) {
    190        double fps = ((double) frames * 1000) / (now - then);
    191        SDL_Log("%2.2f frames per second\n", fps);
    192    }
    193
    194    SDL_stack_free(drawstates);
    195
    196    quit(0);
    197    return 0;
    198}
    199
    200/* vi: set ts=4 sw=4 expandtab: */