Added a very simple example of texture streaming
authorSam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 12:24:59 -0800
changeset 5254f650566b2f51
parent 5253 9e70b360f423
child 5255 b0f5108fda60
Added a very simple example of texture streaming
test/Makefile.in
test/teststreaming.c
     1.1 --- a/test/Makefile.in	Fri Feb 11 12:17:40 2011 -0800
     1.2 +++ b/test/Makefile.in	Fri Feb 11 12:24:59 2011 -0800
     1.3 @@ -49,6 +49,7 @@
     1.4  	testsprite$(EXE) \
     1.5  	testsprite2$(EXE) \
     1.6  	testspriteminimal$(EXE) \
     1.7 +	teststreaming$(EXE) \
     1.8  	testtimer$(EXE) \
     1.9  	testver$(EXE) \
    1.10  	testvidinfo$(EXE) \
    1.11 @@ -81,12 +82,12 @@
    1.12  testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c
    1.13  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.14  
    1.15 -testpower$(EXE): $(srcdir)/testpower.c
    1.16 -	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.17 -
    1.18  testalpha$(EXE): $(srcdir)/testalpha.c
    1.19  	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    1.20  
    1.21 +testatomic$(EXE): $(srcdir)/testatomic.c
    1.22 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.23 +
    1.24  testbitmap$(EXE): $(srcdir)/testbitmap.c
    1.25  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.26  
    1.27 @@ -117,9 +118,9 @@
    1.28  testfill$(EXE): $(srcdir)/testfill.c
    1.29  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.30  
    1.31 -testgamma$(EXE): $(srcdir)/testgamma.c
    1.32 +testgesture$(EXE): $(srcdir)/testgesture.c
    1.33  	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    1.34 -
    1.35 + 
    1.36  testgl$(EXE): $(srcdir)/testgl.c
    1.37  	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @GLLIB@ @MATHLIB@
    1.38  
    1.39 @@ -129,18 +130,27 @@
    1.40  testgles$(EXE): $(srcdir)/testgles.c $(srcdir)/common.c
    1.41  	$(CC) -o $@ $(srcdir)/testgles.c $(srcdir)/common.c $(CFLAGS) $(LIBS) @GLLIB@ @MATHLIB@
    1.42  
    1.43 +testhaptic$(EXE): $(srcdir)/testhaptic.c
    1.44 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.45 +
    1.46  testhread$(EXE): $(srcdir)/testhread.c
    1.47  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.48  
    1.49  testiconv$(EXE): $(srcdir)/testiconv.c
    1.50  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.51  
    1.52 +testime$(EXE): $(srcdir)/testime.c
    1.53 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @SDL_TTF_LIB@
    1.54 +
    1.55  testjoystick$(EXE): $(srcdir)/testjoystick.c
    1.56  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.57  
    1.58  testkeys$(EXE): $(srcdir)/testkeys.c
    1.59  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.60  
    1.61 +testloadso$(EXE): $(srcdir)/testloadso.c
    1.62 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.63 +
    1.64  testlock$(EXE): $(srcdir)/testlock.c
    1.65  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.66  
    1.67 @@ -153,6 +163,9 @@
    1.68  testplatform$(EXE): $(srcdir)/testplatform.c
    1.69  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.70  
    1.71 +testpower$(EXE): $(srcdir)/testpower.c
    1.72 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.73 +
    1.74  testscale$(EXE): $(srcdir)/testscale.c $(srcdir)/common.c
    1.75  	$(CC) -o $@ $(srcdir)/testscale.c $(srcdir)/common.c $(CFLAGS) $(LIBS)
    1.76  
    1.77 @@ -165,15 +178,18 @@
    1.78  testshape$(EXE): $(srcdir)/testshape.c
    1.79  	$(CC) -o $@ $? -std=c99 $(CFLAGS) $(LIBS)
    1.80  
    1.81 -testspriteminimal$(EXE): $(srcdir)/testspriteminimal.c
    1.82 -	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    1.83 -
    1.84  testsprite$(EXE): $(srcdir)/testsprite.c
    1.85  	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    1.86  
    1.87  testsprite2$(EXE): $(srcdir)/testsprite2.c $(srcdir)/common.c
    1.88  	$(CC) -o $@ $(srcdir)/testsprite2.c $(srcdir)/common.c $(CFLAGS) $(LIBS) @MATHLIB@
    1.89  
    1.90 +testspriteminimal$(EXE): $(srcdir)/testspriteminimal.c
    1.91 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    1.92 +
    1.93 +teststreaming$(EXE): $(srcdir)/teststreaming.c
    1.94 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    1.95 +
    1.96  testtimer$(EXE): $(srcdir)/testtimer.c
    1.97  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.98  
    1.99 @@ -198,21 +214,6 @@
   1.100  torturethread$(EXE): $(srcdir)/torturethread.c
   1.101  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
   1.102  
   1.103 -testloadso$(EXE): $(srcdir)/testloadso.c
   1.104 -	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
   1.105 -
   1.106 -testhaptic$(EXE): $(srcdir)/testhaptic.c
   1.107 -	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
   1.108 -
   1.109 -testatomic$(EXE): $(srcdir)/testatomic.c
   1.110 -	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
   1.111 -
   1.112 -testgesture$(EXE): $(srcdir)/testgesture.c
   1.113 -	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
   1.114 - 
   1.115 -testime$(EXE): $(srcdir)/testime.c
   1.116 -	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @SDL_TTF_LIB@
   1.117 -
   1.118  clean:
   1.119  	rm -f $(TARGETS)
   1.120  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/teststreaming.c	Fri Feb 11 12:24:59 2011 -0800
     2.3 @@ -0,0 +1,238 @@
     2.4 +/********************************************************************************
     2.5 + *                                                                              *
     2.6 + * Running moose :) Coded by Mike Gorchak.                                      *
     2.7 + *                                                                              *
     2.8 + ********************************************************************************/
     2.9 +
    2.10 +#include <stdlib.h>
    2.11 +#include <stdio.h>
    2.12 +#include <string.h>
    2.13 +
    2.14 +#include "SDL.h"
    2.15 +
    2.16 +#define MOOSEPIC_W 64
    2.17 +#define MOOSEPIC_H 88
    2.18 +
    2.19 +#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
    2.20 +#define MOOSEFRAMES_COUNT 10
    2.21 +
    2.22 +SDL_Color MooseColors[84] = {
    2.23 +    {49, 49, 49}
    2.24 +    , {66, 24, 0}
    2.25 +    , {66, 33, 0}
    2.26 +    , {66, 66, 66}
    2.27 +    ,
    2.28 +    {66, 115, 49}
    2.29 +    , {74, 33, 0}
    2.30 +    , {74, 41, 16}
    2.31 +    , {82, 33, 8}
    2.32 +    ,
    2.33 +    {82, 41, 8}
    2.34 +    , {82, 49, 16}
    2.35 +    , {82, 82, 82}
    2.36 +    , {90, 41, 8}
    2.37 +    ,
    2.38 +    {90, 41, 16}
    2.39 +    , {90, 57, 24}
    2.40 +    , {99, 49, 16}
    2.41 +    , {99, 66, 24}
    2.42 +    ,
    2.43 +    {99, 66, 33}
    2.44 +    , {99, 74, 33}
    2.45 +    , {107, 57, 24}
    2.46 +    , {107, 82, 41}
    2.47 +    ,
    2.48 +    {115, 57, 33}
    2.49 +    , {115, 66, 33}
    2.50 +    , {115, 66, 41}
    2.51 +    , {115, 74, 0}
    2.52 +    ,
    2.53 +    {115, 90, 49}
    2.54 +    , {115, 115, 115}
    2.55 +    , {123, 82, 0}
    2.56 +    , {123, 99, 57}
    2.57 +    ,
    2.58 +    {132, 66, 41}
    2.59 +    , {132, 74, 41}
    2.60 +    , {132, 90, 8}
    2.61 +    , {132, 99, 33}
    2.62 +    ,
    2.63 +    {132, 99, 66}
    2.64 +    , {132, 107, 66}
    2.65 +    , {140, 74, 49}
    2.66 +    , {140, 99, 16}
    2.67 +    ,
    2.68 +    {140, 107, 74}
    2.69 +    , {140, 115, 74}
    2.70 +    , {148, 107, 24}
    2.71 +    , {148, 115, 82}
    2.72 +    ,
    2.73 +    {148, 123, 74}
    2.74 +    , {148, 123, 90}
    2.75 +    , {156, 115, 33}
    2.76 +    , {156, 115, 90}
    2.77 +    ,
    2.78 +    {156, 123, 82}
    2.79 +    , {156, 132, 82}
    2.80 +    , {156, 132, 99}
    2.81 +    , {156, 156, 156}
    2.82 +    ,
    2.83 +    {165, 123, 49}
    2.84 +    , {165, 123, 90}
    2.85 +    , {165, 132, 82}
    2.86 +    , {165, 132, 90}
    2.87 +    ,
    2.88 +    {165, 132, 99}
    2.89 +    , {165, 140, 90}
    2.90 +    , {173, 132, 57}
    2.91 +    , {173, 132, 99}
    2.92 +    ,
    2.93 +    {173, 140, 107}
    2.94 +    , {173, 140, 115}
    2.95 +    , {173, 148, 99}
    2.96 +    , {173, 173, 173}
    2.97 +    ,
    2.98 +    {181, 140, 74}
    2.99 +    , {181, 148, 115}
   2.100 +    , {181, 148, 123}
   2.101 +    , {181, 156, 107}
   2.102 +    ,
   2.103 +    {189, 148, 123}
   2.104 +    , {189, 156, 82}
   2.105 +    , {189, 156, 123}
   2.106 +    , {189, 156, 132}
   2.107 +    ,
   2.108 +    {189, 189, 189}
   2.109 +    , {198, 156, 123}
   2.110 +    , {198, 165, 132}
   2.111 +    , {206, 165, 99}
   2.112 +    ,
   2.113 +    {206, 165, 132}
   2.114 +    , {206, 173, 140}
   2.115 +    , {206, 206, 206}
   2.116 +    , {214, 173, 115}
   2.117 +    ,
   2.118 +    {214, 173, 140}
   2.119 +    , {222, 181, 148}
   2.120 +    , {222, 189, 132}
   2.121 +    , {222, 189, 156}
   2.122 +    ,
   2.123 +    {222, 222, 222}
   2.124 +    , {231, 198, 165}
   2.125 +    , {231, 231, 231}
   2.126 +    , {239, 206, 173}
   2.127 +};
   2.128 +
   2.129 +Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
   2.130 +
   2.131 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   2.132 +static void
   2.133 +quit(int rc)
   2.134 +{
   2.135 +    SDL_Quit();
   2.136 +    exit(rc);
   2.137 +}
   2.138 +
   2.139 +static void UpdateTexture(SDL_Texture *texture, int frame)
   2.140 +{
   2.141 +    SDL_Color *color;
   2.142 +    Uint8 *src;
   2.143 +    Uint32 *dst;
   2.144 +    int row, col;
   2.145 +    void *pixels;
   2.146 +    int pitch;
   2.147 +
   2.148 +    if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
   2.149 +        fprintf(stderr, "Couldn't lock texture: %s\n", SDL_GetError());
   2.150 +        quit(5);
   2.151 +    }
   2.152 +    src = MooseFrames[frame];
   2.153 +    for (row = 0; row < MOOSEPIC_H; ++row) {
   2.154 +        dst = (Uint32*)((Uint8*)pixels + row * pitch);
   2.155 +        for (col = 0; col < MOOSEPIC_W; ++col) {
   2.156 +            color = &MooseColors[*src++];
   2.157 +            *dst++ = (0xFF000000|(color->r<<16)|(color->g<<8)|color->b);
   2.158 +        }
   2.159 +    }
   2.160 +    SDL_UnlockTexture(texture);
   2.161 +}
   2.162 +
   2.163 +int
   2.164 +main(int argc, char **argv)
   2.165 +{
   2.166 +    SDL_Window *window;
   2.167 +    SDL_Renderer *renderer;
   2.168 +    SDL_RWops *handle;
   2.169 +    SDL_Texture *MooseTexture;
   2.170 +    SDL_Event event;
   2.171 +    SDL_bool done = SDL_FALSE;
   2.172 +    int frame;
   2.173 +
   2.174 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   2.175 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   2.176 +        return 1;
   2.177 +    }
   2.178 +
   2.179 +    /* load the moose images */
   2.180 +    handle = SDL_RWFromFile("moose.dat", "rb");
   2.181 +    if (handle == NULL) {
   2.182 +        fprintf(stderr, "Can't find the file moose.dat !\n");
   2.183 +        quit(2);
   2.184 +    }
   2.185 +    SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
   2.186 +    SDL_RWclose(handle);
   2.187 +
   2.188 +
   2.189 +    /* Create the window and renderer */
   2.190 +    window = SDL_CreateWindow("Happy Moose",
   2.191 +                              SDL_WINDOWPOS_UNDEFINED,
   2.192 +                              SDL_WINDOWPOS_UNDEFINED,
   2.193 +                              MOOSEPIC_W*4, MOOSEPIC_H*4,
   2.194 +                              SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
   2.195 +    if (!window) {
   2.196 +        fprintf(stderr, "Couldn't set create window: %s\n", SDL_GetError());
   2.197 +        quit(3);
   2.198 +    }
   2.199 +
   2.200 +    renderer = SDL_CreateRenderer(window, -1, 0);
   2.201 +    if (!renderer) {
   2.202 +        fprintf(stderr, "Couldn't set create renderer: %s\n", SDL_GetError());
   2.203 +        quit(4);
   2.204 +    }
   2.205 +
   2.206 +    MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
   2.207 +    if (!MooseTexture) {
   2.208 +        fprintf(stderr, "Couldn't set create texture: %s\n", SDL_GetError());
   2.209 +        quit(5);
   2.210 +    }
   2.211 +
   2.212 +    /* Loop, waiting for QUIT or the escape key */
   2.213 +    frame = 0;
   2.214 +    while (!done) {
   2.215 +        while (SDL_PollEvent(&event)) {
   2.216 +            switch (event.type) {
   2.217 +            case SDL_KEYDOWN:
   2.218 +                if (event.key.keysym.sym == SDLK_ESCAPE) {
   2.219 +                    done = SDL_TRUE;
   2.220 +                }
   2.221 +                break;
   2.222 +            case SDL_QUIT:
   2.223 +                done = SDL_TRUE;
   2.224 +                break;
   2.225 +            }
   2.226 +        }
   2.227 +
   2.228 +        frame = (frame + 1) % MOOSEFRAMES_COUNT;
   2.229 +        UpdateTexture(MooseTexture, frame);
   2.230 +
   2.231 +        SDL_RenderClear(renderer);
   2.232 +        SDL_RenderCopy(renderer, MooseTexture, NULL, NULL);
   2.233 +        SDL_RenderPresent(renderer);
   2.234 +    }
   2.235 +    SDL_DestroyRenderer(renderer);
   2.236 +
   2.237 +    quit(0);
   2.238 +    return 0;
   2.239 +}
   2.240 +
   2.241 +/* vi: set ts=4 sw=4 expandtab: */