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