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