cscg22-gearboy

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

teststreaming.c (6164B)


      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 *                                                                              *
     14 * Running moose :) Coded by Mike Gorchak.                                      *
     15 *                                                                              *
     16 ********************************************************************************/
     17
     18#include <stdlib.h>
     19#include <stdio.h>
     20
     21#include "SDL.h"
     22
     23#define MOOSEPIC_W 64
     24#define MOOSEPIC_H 88
     25
     26#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
     27#define MOOSEFRAMES_COUNT 10
     28
     29SDL_Color MooseColors[84] = {
     30    {49, 49, 49, 255}, {66, 24, 0, 255}, {66, 33, 0, 255}, {66, 66, 66, 255},
     31    {66, 115, 49, 255}, {74, 33, 0, 255}, {74, 41, 16, 255}, {82, 33, 8, 255},
     32    {82, 41, 8, 255}, {82, 49, 16, 255}, {82, 82, 82, 255}, {90, 41, 8, 255},
     33    {90, 41, 16, 255}, {90, 57, 24, 255}, {99, 49, 16, 255}, {99, 66, 24, 255},
     34    {99, 66, 33, 255}, {99, 74, 33, 255}, {107, 57, 24, 255}, {107, 82, 41, 255},
     35    {115, 57, 33, 255}, {115, 66, 33, 255}, {115, 66, 41, 255}, {115, 74, 0, 255},
     36    {115, 90, 49, 255}, {115, 115, 115, 255}, {123, 82, 0, 255}, {123, 99, 57, 255},
     37    {132, 66, 41, 255}, {132, 74, 41, 255}, {132, 90, 8, 255}, {132, 99, 33, 255},
     38    {132, 99, 66, 255}, {132, 107, 66, 255}, {140, 74, 49, 255}, {140, 99, 16, 255},
     39    {140, 107, 74, 255}, {140, 115, 74, 255}, {148, 107, 24, 255}, {148, 115, 82, 255},
     40    {148, 123, 74, 255}, {148, 123, 90, 255}, {156, 115, 33, 255}, {156, 115, 90, 255},
     41    {156, 123, 82, 255}, {156, 132, 82, 255}, {156, 132, 99, 255}, {156, 156, 156, 255},
     42    {165, 123, 49, 255}, {165, 123, 90, 255}, {165, 132, 82, 255}, {165, 132, 90, 255},
     43    {165, 132, 99, 255}, {165, 140, 90, 255}, {173, 132, 57, 255}, {173, 132, 99, 255},
     44    {173, 140, 107, 255}, {173, 140, 115, 255}, {173, 148, 99, 255}, {173, 173, 173, 255},
     45    {181, 140, 74, 255}, {181, 148, 115, 255}, {181, 148, 123, 255}, {181, 156, 107, 255},
     46    {189, 148, 123, 255}, {189, 156, 82, 255}, {189, 156, 123, 255}, {189, 156, 132, 255},
     47    {189, 189, 189, 255}, {198, 156, 123, 255}, {198, 165, 132, 255}, {206, 165, 99, 255},
     48    {206, 165, 132, 255}, {206, 173, 140, 255}, {206, 206, 206, 255}, {214, 173, 115, 255},
     49    {214, 173, 140, 255}, {222, 181, 148, 255}, {222, 189, 132, 255}, {222, 189, 156, 255},
     50    {222, 222, 222, 255}, {231, 198, 165, 255}, {231, 231, 231, 255}, {239, 206, 173, 255}
     51};
     52
     53Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
     54
     55void quit(int rc)
     56{
     57    SDL_Quit();
     58    exit(rc);
     59}
     60
     61void UpdateTexture(SDL_Texture *texture, int frame)
     62{
     63    SDL_Color *color;
     64    Uint8 *src;
     65    Uint32 *dst;
     66    int row, col;
     67    void *pixels;
     68    int pitch;
     69
     70    if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
     71        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError());
     72        quit(5);
     73    }
     74    src = MooseFrames[frame];
     75    for (row = 0; row < MOOSEPIC_H; ++row) {
     76        dst = (Uint32*)((Uint8*)pixels + row * pitch);
     77        for (col = 0; col < MOOSEPIC_W; ++col) {
     78            color = &MooseColors[*src++];
     79            *dst++ = (0xFF000000|(color->r<<16)|(color->g<<8)|color->b);
     80        }
     81    }
     82    SDL_UnlockTexture(texture);
     83}
     84
     85int
     86main(int argc, char **argv)
     87{
     88    SDL_Window *window;
     89    SDL_Renderer *renderer;
     90    SDL_RWops *handle;
     91    SDL_Texture *MooseTexture;
     92    SDL_Event event;
     93    SDL_bool done = SDL_FALSE;
     94    int frame;
     95
     96	/* Enable standard application logging */
     97    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
     98
     99    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    100        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    101        return 1;
    102    }
    103
    104    /* load the moose images */
    105    handle = SDL_RWFromFile("moose.dat", "rb");
    106    if (handle == NULL) {
    107        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
    108        quit(2);
    109    }
    110    SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
    111    SDL_RWclose(handle);
    112
    113
    114    /* Create the window and renderer */
    115    window = SDL_CreateWindow("Happy Moose",
    116                              SDL_WINDOWPOS_UNDEFINED,
    117                              SDL_WINDOWPOS_UNDEFINED,
    118                              MOOSEPIC_W*4, MOOSEPIC_H*4,
    119                              SDL_WINDOW_RESIZABLE);
    120    if (!window) {
    121        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
    122        quit(3);
    123    }
    124
    125    renderer = SDL_CreateRenderer(window, -1, 0);
    126    if (!renderer) {
    127        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
    128        quit(4);
    129    }
    130
    131    MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
    132    if (!MooseTexture) {
    133        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
    134        quit(5);
    135    }
    136
    137    /* Loop, waiting for QUIT or the escape key */
    138    frame = 0;
    139    while (!done) {
    140        while (SDL_PollEvent(&event)) {
    141            switch (event.type) {
    142            case SDL_KEYDOWN:
    143                if (event.key.keysym.sym == SDLK_ESCAPE) {
    144                    done = SDL_TRUE;
    145                }
    146                break;
    147            case SDL_QUIT:
    148                done = SDL_TRUE;
    149                break;
    150            }
    151        }
    152
    153        frame = (frame + 1) % MOOSEFRAMES_COUNT;
    154        UpdateTexture(MooseTexture, frame);
    155
    156        SDL_RenderClear(renderer);
    157        SDL_RenderCopy(renderer, MooseTexture, NULL, NULL);
    158        SDL_RenderPresent(renderer);
    159    }
    160    SDL_DestroyRenderer(renderer);
    161
    162    quit(0);
    163    return 0;
    164}
    165
    166/* vi: set ts=4 sw=4 expandtab: */