test/teststreaming.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 12:24:59 -0800
changeset 5254 f650566b2f51
child 5255 b0f5108fda60
permissions -rw-r--r--
Added a very simple example of texture streaming
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
#include <string.h>
slouken@5254
    10
slouken@5254
    11
#include "SDL.h"
slouken@5254
    12
slouken@5254
    13
#define MOOSEPIC_W 64
slouken@5254
    14
#define MOOSEPIC_H 88
slouken@5254
    15
slouken@5254
    16
#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
slouken@5254
    17
#define MOOSEFRAMES_COUNT 10
slouken@5254
    18
slouken@5254
    19
SDL_Color MooseColors[84] = {
slouken@5254
    20
    {49, 49, 49}
slouken@5254
    21
    , {66, 24, 0}
slouken@5254
    22
    , {66, 33, 0}
slouken@5254
    23
    , {66, 66, 66}
slouken@5254
    24
    ,
slouken@5254
    25
    {66, 115, 49}
slouken@5254
    26
    , {74, 33, 0}
slouken@5254
    27
    , {74, 41, 16}
slouken@5254
    28
    , {82, 33, 8}
slouken@5254
    29
    ,
slouken@5254
    30
    {82, 41, 8}
slouken@5254
    31
    , {82, 49, 16}
slouken@5254
    32
    , {82, 82, 82}
slouken@5254
    33
    , {90, 41, 8}
slouken@5254
    34
    ,
slouken@5254
    35
    {90, 41, 16}
slouken@5254
    36
    , {90, 57, 24}
slouken@5254
    37
    , {99, 49, 16}
slouken@5254
    38
    , {99, 66, 24}
slouken@5254
    39
    ,
slouken@5254
    40
    {99, 66, 33}
slouken@5254
    41
    , {99, 74, 33}
slouken@5254
    42
    , {107, 57, 24}
slouken@5254
    43
    , {107, 82, 41}
slouken@5254
    44
    ,
slouken@5254
    45
    {115, 57, 33}
slouken@5254
    46
    , {115, 66, 33}
slouken@5254
    47
    , {115, 66, 41}
slouken@5254
    48
    , {115, 74, 0}
slouken@5254
    49
    ,
slouken@5254
    50
    {115, 90, 49}
slouken@5254
    51
    , {115, 115, 115}
slouken@5254
    52
    , {123, 82, 0}
slouken@5254
    53
    , {123, 99, 57}
slouken@5254
    54
    ,
slouken@5254
    55
    {132, 66, 41}
slouken@5254
    56
    , {132, 74, 41}
slouken@5254
    57
    , {132, 90, 8}
slouken@5254
    58
    , {132, 99, 33}
slouken@5254
    59
    ,
slouken@5254
    60
    {132, 99, 66}
slouken@5254
    61
    , {132, 107, 66}
slouken@5254
    62
    , {140, 74, 49}
slouken@5254
    63
    , {140, 99, 16}
slouken@5254
    64
    ,
slouken@5254
    65
    {140, 107, 74}
slouken@5254
    66
    , {140, 115, 74}
slouken@5254
    67
    , {148, 107, 24}
slouken@5254
    68
    , {148, 115, 82}
slouken@5254
    69
    ,
slouken@5254
    70
    {148, 123, 74}
slouken@5254
    71
    , {148, 123, 90}
slouken@5254
    72
    , {156, 115, 33}
slouken@5254
    73
    , {156, 115, 90}
slouken@5254
    74
    ,
slouken@5254
    75
    {156, 123, 82}
slouken@5254
    76
    , {156, 132, 82}
slouken@5254
    77
    , {156, 132, 99}
slouken@5254
    78
    , {156, 156, 156}
slouken@5254
    79
    ,
slouken@5254
    80
    {165, 123, 49}
slouken@5254
    81
    , {165, 123, 90}
slouken@5254
    82
    , {165, 132, 82}
slouken@5254
    83
    , {165, 132, 90}
slouken@5254
    84
    ,
slouken@5254
    85
    {165, 132, 99}
slouken@5254
    86
    , {165, 140, 90}
slouken@5254
    87
    , {173, 132, 57}
slouken@5254
    88
    , {173, 132, 99}
slouken@5254
    89
    ,
slouken@5254
    90
    {173, 140, 107}
slouken@5254
    91
    , {173, 140, 115}
slouken@5254
    92
    , {173, 148, 99}
slouken@5254
    93
    , {173, 173, 173}
slouken@5254
    94
    ,
slouken@5254
    95
    {181, 140, 74}
slouken@5254
    96
    , {181, 148, 115}
slouken@5254
    97
    , {181, 148, 123}
slouken@5254
    98
    , {181, 156, 107}
slouken@5254
    99
    ,
slouken@5254
   100
    {189, 148, 123}
slouken@5254
   101
    , {189, 156, 82}
slouken@5254
   102
    , {189, 156, 123}
slouken@5254
   103
    , {189, 156, 132}
slouken@5254
   104
    ,
slouken@5254
   105
    {189, 189, 189}
slouken@5254
   106
    , {198, 156, 123}
slouken@5254
   107
    , {198, 165, 132}
slouken@5254
   108
    , {206, 165, 99}
slouken@5254
   109
    ,
slouken@5254
   110
    {206, 165, 132}
slouken@5254
   111
    , {206, 173, 140}
slouken@5254
   112
    , {206, 206, 206}
slouken@5254
   113
    , {214, 173, 115}
slouken@5254
   114
    ,
slouken@5254
   115
    {214, 173, 140}
slouken@5254
   116
    , {222, 181, 148}
slouken@5254
   117
    , {222, 189, 132}
slouken@5254
   118
    , {222, 189, 156}
slouken@5254
   119
    ,
slouken@5254
   120
    {222, 222, 222}
slouken@5254
   121
    , {231, 198, 165}
slouken@5254
   122
    , {231, 231, 231}
slouken@5254
   123
    , {239, 206, 173}
slouken@5254
   124
};
slouken@5254
   125
slouken@5254
   126
Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
slouken@5254
   127
slouken@5254
   128
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@5254
   129
static void
slouken@5254
   130
quit(int rc)
slouken@5254
   131
{
slouken@5254
   132
    SDL_Quit();
slouken@5254
   133
    exit(rc);
slouken@5254
   134
}
slouken@5254
   135
slouken@5254
   136
static void UpdateTexture(SDL_Texture *texture, int frame)
slouken@5254
   137
{
slouken@5254
   138
    SDL_Color *color;
slouken@5254
   139
    Uint8 *src;
slouken@5254
   140
    Uint32 *dst;
slouken@5254
   141
    int row, col;
slouken@5254
   142
    void *pixels;
slouken@5254
   143
    int pitch;
slouken@5254
   144
slouken@5254
   145
    if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
slouken@5254
   146
        fprintf(stderr, "Couldn't lock texture: %s\n", SDL_GetError());
slouken@5254
   147
        quit(5);
slouken@5254
   148
    }
slouken@5254
   149
    src = MooseFrames[frame];
slouken@5254
   150
    for (row = 0; row < MOOSEPIC_H; ++row) {
slouken@5254
   151
        dst = (Uint32*)((Uint8*)pixels + row * pitch);
slouken@5254
   152
        for (col = 0; col < MOOSEPIC_W; ++col) {
slouken@5254
   153
            color = &MooseColors[*src++];
slouken@5254
   154
            *dst++ = (0xFF000000|(color->r<<16)|(color->g<<8)|color->b);
slouken@5254
   155
        }
slouken@5254
   156
    }
slouken@5254
   157
    SDL_UnlockTexture(texture);
slouken@5254
   158
}
slouken@5254
   159
slouken@5254
   160
int
slouken@5254
   161
main(int argc, char **argv)
slouken@5254
   162
{
slouken@5254
   163
    SDL_Window *window;
slouken@5254
   164
    SDL_Renderer *renderer;
slouken@5254
   165
    SDL_RWops *handle;
slouken@5254
   166
    SDL_Texture *MooseTexture;
slouken@5254
   167
    SDL_Event event;
slouken@5254
   168
    SDL_bool done = SDL_FALSE;
slouken@5254
   169
    int frame;
slouken@5254
   170
slouken@5254
   171
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@5254
   172
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@5254
   173
        return 1;
slouken@5254
   174
    }
slouken@5254
   175
slouken@5254
   176
    /* load the moose images */
slouken@5254
   177
    handle = SDL_RWFromFile("moose.dat", "rb");
slouken@5254
   178
    if (handle == NULL) {
slouken@5254
   179
        fprintf(stderr, "Can't find the file moose.dat !\n");
slouken@5254
   180
        quit(2);
slouken@5254
   181
    }
slouken@5254
   182
    SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
slouken@5254
   183
    SDL_RWclose(handle);
slouken@5254
   184
slouken@5254
   185
slouken@5254
   186
    /* Create the window and renderer */
slouken@5254
   187
    window = SDL_CreateWindow("Happy Moose",
slouken@5254
   188
                              SDL_WINDOWPOS_UNDEFINED,
slouken@5254
   189
                              SDL_WINDOWPOS_UNDEFINED,
slouken@5254
   190
                              MOOSEPIC_W*4, MOOSEPIC_H*4,
slouken@5254
   191
                              SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
slouken@5254
   192
    if (!window) {
slouken@5254
   193
        fprintf(stderr, "Couldn't set create window: %s\n", SDL_GetError());
slouken@5254
   194
        quit(3);
slouken@5254
   195
    }
slouken@5254
   196
slouken@5254
   197
    renderer = SDL_CreateRenderer(window, -1, 0);
slouken@5254
   198
    if (!renderer) {
slouken@5254
   199
        fprintf(stderr, "Couldn't set create renderer: %s\n", SDL_GetError());
slouken@5254
   200
        quit(4);
slouken@5254
   201
    }
slouken@5254
   202
slouken@5254
   203
    MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
slouken@5254
   204
    if (!MooseTexture) {
slouken@5254
   205
        fprintf(stderr, "Couldn't set create texture: %s\n", SDL_GetError());
slouken@5254
   206
        quit(5);
slouken@5254
   207
    }
slouken@5254
   208
slouken@5254
   209
    /* Loop, waiting for QUIT or the escape key */
slouken@5254
   210
    frame = 0;
slouken@5254
   211
    while (!done) {
slouken@5254
   212
        while (SDL_PollEvent(&event)) {
slouken@5254
   213
            switch (event.type) {
slouken@5254
   214
            case SDL_KEYDOWN:
slouken@5254
   215
                if (event.key.keysym.sym == SDLK_ESCAPE) {
slouken@5254
   216
                    done = SDL_TRUE;
slouken@5254
   217
                }
slouken@5254
   218
                break;
slouken@5254
   219
            case SDL_QUIT:
slouken@5254
   220
                done = SDL_TRUE;
slouken@5254
   221
                break;
slouken@5254
   222
            }
slouken@5254
   223
        }
slouken@5254
   224
slouken@5254
   225
        frame = (frame + 1) % MOOSEFRAMES_COUNT;
slouken@5254
   226
        UpdateTexture(MooseTexture, frame);
slouken@5254
   227
slouken@5254
   228
        SDL_RenderClear(renderer);
slouken@5254
   229
        SDL_RenderCopy(renderer, MooseTexture, NULL, NULL);
slouken@5254
   230
        SDL_RenderPresent(renderer);
slouken@5254
   231
    }
slouken@5254
   232
    SDL_DestroyRenderer(renderer);
slouken@5254
   233
slouken@5254
   234
    quit(0);
slouken@5254
   235
    return 0;
slouken@5254
   236
}
slouken@5254
   237
slouken@5254
   238
/* vi: set ts=4 sw=4 expandtab: */