test/teststreaming.c
changeset 5254 f650566b2f51
child 5255 b0f5108fda60
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/teststreaming.c	Fri Feb 11 12:24:59 2011 -0800
     1.3 @@ -0,0 +1,238 @@
     1.4 +/********************************************************************************
     1.5 + *                                                                              *
     1.6 + * Running moose :) Coded by Mike Gorchak.                                      *
     1.7 + *                                                                              *
     1.8 + ********************************************************************************/
     1.9 +
    1.10 +#include <stdlib.h>
    1.11 +#include <stdio.h>
    1.12 +#include <string.h>
    1.13 +
    1.14 +#include "SDL.h"
    1.15 +
    1.16 +#define MOOSEPIC_W 64
    1.17 +#define MOOSEPIC_H 88
    1.18 +
    1.19 +#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
    1.20 +#define MOOSEFRAMES_COUNT 10
    1.21 +
    1.22 +SDL_Color MooseColors[84] = {
    1.23 +    {49, 49, 49}
    1.24 +    , {66, 24, 0}
    1.25 +    , {66, 33, 0}
    1.26 +    , {66, 66, 66}
    1.27 +    ,
    1.28 +    {66, 115, 49}
    1.29 +    , {74, 33, 0}
    1.30 +    , {74, 41, 16}
    1.31 +    , {82, 33, 8}
    1.32 +    ,
    1.33 +    {82, 41, 8}
    1.34 +    , {82, 49, 16}
    1.35 +    , {82, 82, 82}
    1.36 +    , {90, 41, 8}
    1.37 +    ,
    1.38 +    {90, 41, 16}
    1.39 +    , {90, 57, 24}
    1.40 +    , {99, 49, 16}
    1.41 +    , {99, 66, 24}
    1.42 +    ,
    1.43 +    {99, 66, 33}
    1.44 +    , {99, 74, 33}
    1.45 +    , {107, 57, 24}
    1.46 +    , {107, 82, 41}
    1.47 +    ,
    1.48 +    {115, 57, 33}
    1.49 +    , {115, 66, 33}
    1.50 +    , {115, 66, 41}
    1.51 +    , {115, 74, 0}
    1.52 +    ,
    1.53 +    {115, 90, 49}
    1.54 +    , {115, 115, 115}
    1.55 +    , {123, 82, 0}
    1.56 +    , {123, 99, 57}
    1.57 +    ,
    1.58 +    {132, 66, 41}
    1.59 +    , {132, 74, 41}
    1.60 +    , {132, 90, 8}
    1.61 +    , {132, 99, 33}
    1.62 +    ,
    1.63 +    {132, 99, 66}
    1.64 +    , {132, 107, 66}
    1.65 +    , {140, 74, 49}
    1.66 +    , {140, 99, 16}
    1.67 +    ,
    1.68 +    {140, 107, 74}
    1.69 +    , {140, 115, 74}
    1.70 +    , {148, 107, 24}
    1.71 +    , {148, 115, 82}
    1.72 +    ,
    1.73 +    {148, 123, 74}
    1.74 +    , {148, 123, 90}
    1.75 +    , {156, 115, 33}
    1.76 +    , {156, 115, 90}
    1.77 +    ,
    1.78 +    {156, 123, 82}
    1.79 +    , {156, 132, 82}
    1.80 +    , {156, 132, 99}
    1.81 +    , {156, 156, 156}
    1.82 +    ,
    1.83 +    {165, 123, 49}
    1.84 +    , {165, 123, 90}
    1.85 +    , {165, 132, 82}
    1.86 +    , {165, 132, 90}
    1.87 +    ,
    1.88 +    {165, 132, 99}
    1.89 +    , {165, 140, 90}
    1.90 +    , {173, 132, 57}
    1.91 +    , {173, 132, 99}
    1.92 +    ,
    1.93 +    {173, 140, 107}
    1.94 +    , {173, 140, 115}
    1.95 +    , {173, 148, 99}
    1.96 +    , {173, 173, 173}
    1.97 +    ,
    1.98 +    {181, 140, 74}
    1.99 +    , {181, 148, 115}
   1.100 +    , {181, 148, 123}
   1.101 +    , {181, 156, 107}
   1.102 +    ,
   1.103 +    {189, 148, 123}
   1.104 +    , {189, 156, 82}
   1.105 +    , {189, 156, 123}
   1.106 +    , {189, 156, 132}
   1.107 +    ,
   1.108 +    {189, 189, 189}
   1.109 +    , {198, 156, 123}
   1.110 +    , {198, 165, 132}
   1.111 +    , {206, 165, 99}
   1.112 +    ,
   1.113 +    {206, 165, 132}
   1.114 +    , {206, 173, 140}
   1.115 +    , {206, 206, 206}
   1.116 +    , {214, 173, 115}
   1.117 +    ,
   1.118 +    {214, 173, 140}
   1.119 +    , {222, 181, 148}
   1.120 +    , {222, 189, 132}
   1.121 +    , {222, 189, 156}
   1.122 +    ,
   1.123 +    {222, 222, 222}
   1.124 +    , {231, 198, 165}
   1.125 +    , {231, 231, 231}
   1.126 +    , {239, 206, 173}
   1.127 +};
   1.128 +
   1.129 +Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
   1.130 +
   1.131 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   1.132 +static void
   1.133 +quit(int rc)
   1.134 +{
   1.135 +    SDL_Quit();
   1.136 +    exit(rc);
   1.137 +}
   1.138 +
   1.139 +static void UpdateTexture(SDL_Texture *texture, int frame)
   1.140 +{
   1.141 +    SDL_Color *color;
   1.142 +    Uint8 *src;
   1.143 +    Uint32 *dst;
   1.144 +    int row, col;
   1.145 +    void *pixels;
   1.146 +    int pitch;
   1.147 +
   1.148 +    if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
   1.149 +        fprintf(stderr, "Couldn't lock texture: %s\n", SDL_GetError());
   1.150 +        quit(5);
   1.151 +    }
   1.152 +    src = MooseFrames[frame];
   1.153 +    for (row = 0; row < MOOSEPIC_H; ++row) {
   1.154 +        dst = (Uint32*)((Uint8*)pixels + row * pitch);
   1.155 +        for (col = 0; col < MOOSEPIC_W; ++col) {
   1.156 +            color = &MooseColors[*src++];
   1.157 +            *dst++ = (0xFF000000|(color->r<<16)|(color->g<<8)|color->b);
   1.158 +        }
   1.159 +    }
   1.160 +    SDL_UnlockTexture(texture);
   1.161 +}
   1.162 +
   1.163 +int
   1.164 +main(int argc, char **argv)
   1.165 +{
   1.166 +    SDL_Window *window;
   1.167 +    SDL_Renderer *renderer;
   1.168 +    SDL_RWops *handle;
   1.169 +    SDL_Texture *MooseTexture;
   1.170 +    SDL_Event event;
   1.171 +    SDL_bool done = SDL_FALSE;
   1.172 +    int frame;
   1.173 +
   1.174 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   1.175 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.176 +        return 1;
   1.177 +    }
   1.178 +
   1.179 +    /* load the moose images */
   1.180 +    handle = SDL_RWFromFile("moose.dat", "rb");
   1.181 +    if (handle == NULL) {
   1.182 +        fprintf(stderr, "Can't find the file moose.dat !\n");
   1.183 +        quit(2);
   1.184 +    }
   1.185 +    SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
   1.186 +    SDL_RWclose(handle);
   1.187 +
   1.188 +
   1.189 +    /* Create the window and renderer */
   1.190 +    window = SDL_CreateWindow("Happy Moose",
   1.191 +                              SDL_WINDOWPOS_UNDEFINED,
   1.192 +                              SDL_WINDOWPOS_UNDEFINED,
   1.193 +                              MOOSEPIC_W*4, MOOSEPIC_H*4,
   1.194 +                              SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
   1.195 +    if (!window) {
   1.196 +        fprintf(stderr, "Couldn't set create window: %s\n", SDL_GetError());
   1.197 +        quit(3);
   1.198 +    }
   1.199 +
   1.200 +    renderer = SDL_CreateRenderer(window, -1, 0);
   1.201 +    if (!renderer) {
   1.202 +        fprintf(stderr, "Couldn't set create renderer: %s\n", SDL_GetError());
   1.203 +        quit(4);
   1.204 +    }
   1.205 +
   1.206 +    MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
   1.207 +    if (!MooseTexture) {
   1.208 +        fprintf(stderr, "Couldn't set create texture: %s\n", SDL_GetError());
   1.209 +        quit(5);
   1.210 +    }
   1.211 +
   1.212 +    /* Loop, waiting for QUIT or the escape key */
   1.213 +    frame = 0;
   1.214 +    while (!done) {
   1.215 +        while (SDL_PollEvent(&event)) {
   1.216 +            switch (event.type) {
   1.217 +            case SDL_KEYDOWN:
   1.218 +                if (event.key.keysym.sym == SDLK_ESCAPE) {
   1.219 +                    done = SDL_TRUE;
   1.220 +                }
   1.221 +                break;
   1.222 +            case SDL_QUIT:
   1.223 +                done = SDL_TRUE;
   1.224 +                break;
   1.225 +            }
   1.226 +        }
   1.227 +
   1.228 +        frame = (frame + 1) % MOOSEFRAMES_COUNT;
   1.229 +        UpdateTexture(MooseTexture, frame);
   1.230 +
   1.231 +        SDL_RenderClear(renderer);
   1.232 +        SDL_RenderCopy(renderer, MooseTexture, NULL, NULL);
   1.233 +        SDL_RenderPresent(renderer);
   1.234 +    }
   1.235 +    SDL_DestroyRenderer(renderer);
   1.236 +
   1.237 +    quit(0);
   1.238 +    return 0;
   1.239 +}
   1.240 +
   1.241 +/* vi: set ts=4 sw=4 expandtab: */