changeset 5259:f650566b2f51

Added a very simple example of texture streaming
author Sam Lantinga <slouken@libsdl.org>
date Fri, 11 Feb 2011 12:24:59 -0800
parents 9e70b360f423
children b0f5108fda60
files test/Makefile.in test/teststreaming.c
diffstat 2 files changed, 262 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/test/Makefile.in	Fri Feb 11 12:17:40 2011 -0800
+++ b/test/Makefile.in	Fri Feb 11 12:24:59 2011 -0800
@@ -49,6 +49,7 @@
 	testsprite$(EXE) \
 	testsprite2$(EXE) \
 	testspriteminimal$(EXE) \
+	teststreaming$(EXE) \
 	testtimer$(EXE) \
 	testver$(EXE) \
 	testvidinfo$(EXE) \
@@ -81,12 +82,12 @@
 testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
-testpower$(EXE): $(srcdir)/testpower.c
-	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
-
 testalpha$(EXE): $(srcdir)/testalpha.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
 
+testatomic$(EXE): $(srcdir)/testatomic.c
+	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
+
 testbitmap$(EXE): $(srcdir)/testbitmap.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
@@ -117,9 +118,9 @@
 testfill$(EXE): $(srcdir)/testfill.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
-testgamma$(EXE): $(srcdir)/testgamma.c
+testgesture$(EXE): $(srcdir)/testgesture.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
-
+ 
 testgl$(EXE): $(srcdir)/testgl.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @GLLIB@ @MATHLIB@
 
@@ -129,18 +130,27 @@
 testgles$(EXE): $(srcdir)/testgles.c $(srcdir)/common.c
 	$(CC) -o $@ $(srcdir)/testgles.c $(srcdir)/common.c $(CFLAGS) $(LIBS) @GLLIB@ @MATHLIB@
 
+testhaptic$(EXE): $(srcdir)/testhaptic.c
+	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
+
 testhread$(EXE): $(srcdir)/testhread.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
 testiconv$(EXE): $(srcdir)/testiconv.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
+testime$(EXE): $(srcdir)/testime.c
+	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @SDL_TTF_LIB@
+
 testjoystick$(EXE): $(srcdir)/testjoystick.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
 testkeys$(EXE): $(srcdir)/testkeys.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
+testloadso$(EXE): $(srcdir)/testloadso.c
+	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
+
 testlock$(EXE): $(srcdir)/testlock.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
@@ -153,6 +163,9 @@
 testplatform$(EXE): $(srcdir)/testplatform.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
+testpower$(EXE): $(srcdir)/testpower.c
+	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
+
 testscale$(EXE): $(srcdir)/testscale.c $(srcdir)/common.c
 	$(CC) -o $@ $(srcdir)/testscale.c $(srcdir)/common.c $(CFLAGS) $(LIBS)
 
@@ -165,15 +178,18 @@
 testshape$(EXE): $(srcdir)/testshape.c
 	$(CC) -o $@ $? -std=c99 $(CFLAGS) $(LIBS)
 
-testspriteminimal$(EXE): $(srcdir)/testspriteminimal.c
-	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
-
 testsprite$(EXE): $(srcdir)/testsprite.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
 
 testsprite2$(EXE): $(srcdir)/testsprite2.c $(srcdir)/common.c
 	$(CC) -o $@ $(srcdir)/testsprite2.c $(srcdir)/common.c $(CFLAGS) $(LIBS) @MATHLIB@
 
+testspriteminimal$(EXE): $(srcdir)/testspriteminimal.c
+	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
+
+teststreaming$(EXE): $(srcdir)/teststreaming.c
+	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
+
 testtimer$(EXE): $(srcdir)/testtimer.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
@@ -198,21 +214,6 @@
 torturethread$(EXE): $(srcdir)/torturethread.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
-testloadso$(EXE): $(srcdir)/testloadso.c
-	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
-
-testhaptic$(EXE): $(srcdir)/testhaptic.c
-	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
-
-testatomic$(EXE): $(srcdir)/testatomic.c
-	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
-
-testgesture$(EXE): $(srcdir)/testgesture.c
-	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
- 
-testime$(EXE): $(srcdir)/testime.c
-	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @SDL_TTF_LIB@
-
 clean:
 	rm -f $(TARGETS)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/teststreaming.c	Fri Feb 11 12:24:59 2011 -0800
@@ -0,0 +1,238 @@
+/********************************************************************************
+ *                                                                              *
+ * Running moose :) Coded by Mike Gorchak.                                      *
+ *                                                                              *
+ ********************************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "SDL.h"
+
+#define MOOSEPIC_W 64
+#define MOOSEPIC_H 88
+
+#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
+#define MOOSEFRAMES_COUNT 10
+
+SDL_Color MooseColors[84] = {
+    {49, 49, 49}
+    , {66, 24, 0}
+    , {66, 33, 0}
+    , {66, 66, 66}
+    ,
+    {66, 115, 49}
+    , {74, 33, 0}
+    , {74, 41, 16}
+    , {82, 33, 8}
+    ,
+    {82, 41, 8}
+    , {82, 49, 16}
+    , {82, 82, 82}
+    , {90, 41, 8}
+    ,
+    {90, 41, 16}
+    , {90, 57, 24}
+    , {99, 49, 16}
+    , {99, 66, 24}
+    ,
+    {99, 66, 33}
+    , {99, 74, 33}
+    , {107, 57, 24}
+    , {107, 82, 41}
+    ,
+    {115, 57, 33}
+    , {115, 66, 33}
+    , {115, 66, 41}
+    , {115, 74, 0}
+    ,
+    {115, 90, 49}
+    , {115, 115, 115}
+    , {123, 82, 0}
+    , {123, 99, 57}
+    ,
+    {132, 66, 41}
+    , {132, 74, 41}
+    , {132, 90, 8}
+    , {132, 99, 33}
+    ,
+    {132, 99, 66}
+    , {132, 107, 66}
+    , {140, 74, 49}
+    , {140, 99, 16}
+    ,
+    {140, 107, 74}
+    , {140, 115, 74}
+    , {148, 107, 24}
+    , {148, 115, 82}
+    ,
+    {148, 123, 74}
+    , {148, 123, 90}
+    , {156, 115, 33}
+    , {156, 115, 90}
+    ,
+    {156, 123, 82}
+    , {156, 132, 82}
+    , {156, 132, 99}
+    , {156, 156, 156}
+    ,
+    {165, 123, 49}
+    , {165, 123, 90}
+    , {165, 132, 82}
+    , {165, 132, 90}
+    ,
+    {165, 132, 99}
+    , {165, 140, 90}
+    , {173, 132, 57}
+    , {173, 132, 99}
+    ,
+    {173, 140, 107}
+    , {173, 140, 115}
+    , {173, 148, 99}
+    , {173, 173, 173}
+    ,
+    {181, 140, 74}
+    , {181, 148, 115}
+    , {181, 148, 123}
+    , {181, 156, 107}
+    ,
+    {189, 148, 123}
+    , {189, 156, 82}
+    , {189, 156, 123}
+    , {189, 156, 132}
+    ,
+    {189, 189, 189}
+    , {198, 156, 123}
+    , {198, 165, 132}
+    , {206, 165, 99}
+    ,
+    {206, 165, 132}
+    , {206, 173, 140}
+    , {206, 206, 206}
+    , {214, 173, 115}
+    ,
+    {214, 173, 140}
+    , {222, 181, 148}
+    , {222, 189, 132}
+    , {222, 189, 156}
+    ,
+    {222, 222, 222}
+    , {231, 198, 165}
+    , {231, 231, 231}
+    , {239, 206, 173}
+};
+
+Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
+
+/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
+static void
+quit(int rc)
+{
+    SDL_Quit();
+    exit(rc);
+}
+
+static void UpdateTexture(SDL_Texture *texture, int frame)
+{
+    SDL_Color *color;
+    Uint8 *src;
+    Uint32 *dst;
+    int row, col;
+    void *pixels;
+    int pitch;
+
+    if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
+        fprintf(stderr, "Couldn't lock texture: %s\n", SDL_GetError());
+        quit(5);
+    }
+    src = MooseFrames[frame];
+    for (row = 0; row < MOOSEPIC_H; ++row) {
+        dst = (Uint32*)((Uint8*)pixels + row * pitch);
+        for (col = 0; col < MOOSEPIC_W; ++col) {
+            color = &MooseColors[*src++];
+            *dst++ = (0xFF000000|(color->r<<16)|(color->g<<8)|color->b);
+        }
+    }
+    SDL_UnlockTexture(texture);
+}
+
+int
+main(int argc, char **argv)
+{
+    SDL_Window *window;
+    SDL_Renderer *renderer;
+    SDL_RWops *handle;
+    SDL_Texture *MooseTexture;
+    SDL_Event event;
+    SDL_bool done = SDL_FALSE;
+    int frame;
+
+    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
+        return 1;
+    }
+
+    /* load the moose images */
+    handle = SDL_RWFromFile("moose.dat", "rb");
+    if (handle == NULL) {
+        fprintf(stderr, "Can't find the file moose.dat !\n");
+        quit(2);
+    }
+    SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
+    SDL_RWclose(handle);
+
+
+    /* Create the window and renderer */
+    window = SDL_CreateWindow("Happy Moose",
+                              SDL_WINDOWPOS_UNDEFINED,
+                              SDL_WINDOWPOS_UNDEFINED,
+                              MOOSEPIC_W*4, MOOSEPIC_H*4,
+                              SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
+    if (!window) {
+        fprintf(stderr, "Couldn't set create window: %s\n", SDL_GetError());
+        quit(3);
+    }
+
+    renderer = SDL_CreateRenderer(window, -1, 0);
+    if (!renderer) {
+        fprintf(stderr, "Couldn't set create renderer: %s\n", SDL_GetError());
+        quit(4);
+    }
+
+    MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
+    if (!MooseTexture) {
+        fprintf(stderr, "Couldn't set create texture: %s\n", SDL_GetError());
+        quit(5);
+    }
+
+    /* Loop, waiting for QUIT or the escape key */
+    frame = 0;
+    while (!done) {
+        while (SDL_PollEvent(&event)) {
+            switch (event.type) {
+            case SDL_KEYDOWN:
+                if (event.key.keysym.sym == SDLK_ESCAPE) {
+                    done = SDL_TRUE;
+                }
+                break;
+            case SDL_QUIT:
+                done = SDL_TRUE;
+                break;
+            }
+        }
+
+        frame = (frame + 1) % MOOSEFRAMES_COUNT;
+        UpdateTexture(MooseTexture, frame);
+
+        SDL_RenderClear(renderer);
+        SDL_RenderCopy(renderer, MooseTexture, NULL, NULL);
+        SDL_RenderPresent(renderer);
+    }
+    SDL_DestroyRenderer(renderer);
+
+    quit(0);
+    return 0;
+}
+
+/* vi: set ts=4 sw=4 expandtab: */