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