Debian Codegraph @

test/testoverlay2.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11 */
12 /********************************************************************************
13 * *
14 * Test of the overlay used for moved pictures, test more closed to real life. *
15 * Running trojan moose :) Coded by Mike Gorchak. *
16 * *
17 ********************************************************************************/
18
19 #include <stdlib.h>
20
21 #ifdef __EMSCRIPTEN__
22 #include <emscripten/emscripten.h>
23 #endif
24
25 #include "SDL.h"
26
27 #include "testyuv_cvt.h"
28 #include "testutils.h"
29
30 #define MOOSEPIC_W 64
31 #define MOOSEPIC_H 88
32
33 #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
34 #define MOOSEFRAMES_COUNT 10
35
36 /* *INDENT-OFF* */ /* clang-format off */
37 SDL_Color MooseColors[84] = {
38 {49, 49, 49, SDL_ALPHA_OPAQUE}
39 , {66, 24, 0, SDL_ALPHA_OPAQUE}
40 , {66, 33, 0, SDL_ALPHA_OPAQUE}
41 , {66, 66, 66, SDL_ALPHA_OPAQUE}
42 ,
43 {66, 115, 49, SDL_ALPHA_OPAQUE}
44 , {74, 33, 0, SDL_ALPHA_OPAQUE}
45 , {74, 41, 16, SDL_ALPHA_OPAQUE}
46 , {82, 33, 8, SDL_ALPHA_OPAQUE}
47 ,
48 {82, 41, 8, SDL_ALPHA_OPAQUE}
49 , {82, 49, 16, SDL_ALPHA_OPAQUE}
50 , {82, 82, 82, SDL_ALPHA_OPAQUE}
51 , {90, 41, 8, SDL_ALPHA_OPAQUE}
52 ,
53 {90, 41, 16, SDL_ALPHA_OPAQUE}
54 , {90, 57, 24, SDL_ALPHA_OPAQUE}
55 , {99, 49, 16, SDL_ALPHA_OPAQUE}
56 , {99, 66, 24, SDL_ALPHA_OPAQUE}
57 ,
58 {99, 66, 33, SDL_ALPHA_OPAQUE}
59 , {99, 74, 33, SDL_ALPHA_OPAQUE}
60 , {107, 57, 24, SDL_ALPHA_OPAQUE}
61 , {107, 82, 41, SDL_ALPHA_OPAQUE}
62 ,
63 {115, 57, 33, SDL_ALPHA_OPAQUE}
64 , {115, 66, 33, SDL_ALPHA_OPAQUE}
65 , {115, 66, 41, SDL_ALPHA_OPAQUE}
66 , {115, 74, 0, SDL_ALPHA_OPAQUE}
67 ,
68 {115, 90, 49, SDL_ALPHA_OPAQUE}
69 , {115, 115, 115, SDL_ALPHA_OPAQUE}
70 , {123, 82, 0, SDL_ALPHA_OPAQUE}
71 , {123, 99, 57, SDL_ALPHA_OPAQUE}
72 ,
73 {132, 66, 41, SDL_ALPHA_OPAQUE}
74 , {132, 74, 41, SDL_ALPHA_OPAQUE}
75 , {132, 90, 8, SDL_ALPHA_OPAQUE}
76 , {132, 99, 33, SDL_ALPHA_OPAQUE}
77 ,
78 {132, 99, 66, SDL_ALPHA_OPAQUE}
79 , {132, 107, 66, SDL_ALPHA_OPAQUE}
80 , {140, 74, 49, SDL_ALPHA_OPAQUE}
81 , {140, 99, 16, SDL_ALPHA_OPAQUE}
82 ,
83 {140, 107, 74, SDL_ALPHA_OPAQUE}
84 , {140, 115, 74, SDL_ALPHA_OPAQUE}
85 , {148, 107, 24, SDL_ALPHA_OPAQUE}
86 , {148, 115, 82, SDL_ALPHA_OPAQUE}
87 ,
88 {148, 123, 74, SDL_ALPHA_OPAQUE}
89 , {148, 123, 90, SDL_ALPHA_OPAQUE}
90 , {156, 115, 33, SDL_ALPHA_OPAQUE}
91 , {156, 115, 90, SDL_ALPHA_OPAQUE}
92 ,
93 {156, 123, 82, SDL_ALPHA_OPAQUE}
94 , {156, 132, 82, SDL_ALPHA_OPAQUE}
95 , {156, 132, 99, SDL_ALPHA_OPAQUE}
96 , {156, 156, 156, SDL_ALPHA_OPAQUE}
97 ,
98 {165, 123, 49, SDL_ALPHA_OPAQUE}
99 , {165, 123, 90, SDL_ALPHA_OPAQUE}
100 , {165, 132, 82, SDL_ALPHA_OPAQUE}
101 , {165, 132, 90, SDL_ALPHA_OPAQUE}
102 ,
103 {165, 132, 99, SDL_ALPHA_OPAQUE}
104 , {165, 140, 90, SDL_ALPHA_OPAQUE}
105 , {173, 132, 57, SDL_ALPHA_OPAQUE}
106 , {173, 132, 99, SDL_ALPHA_OPAQUE}
107 ,
108 {173, 140, 107, SDL_ALPHA_OPAQUE}
109 , {173, 140, 115, SDL_ALPHA_OPAQUE}
110 , {173, 148, 99, SDL_ALPHA_OPAQUE}
111 , {173, 173, 173, SDL_ALPHA_OPAQUE}
112 ,
113 {181, 140, 74, SDL_ALPHA_OPAQUE}
114 , {181, 148, 115, SDL_ALPHA_OPAQUE}
115 , {181, 148, 123, SDL_ALPHA_OPAQUE}
116 , {181, 156, 107, SDL_ALPHA_OPAQUE}
117 ,
118 {189, 148, 123, SDL_ALPHA_OPAQUE}
119 , {189, 156, 82, SDL_ALPHA_OPAQUE}
120 , {189, 156, 123, SDL_ALPHA_OPAQUE}
121 , {189, 156, 132, SDL_ALPHA_OPAQUE}
122 ,
123 {189, 189, 189, SDL_ALPHA_OPAQUE}
124 , {198, 156, 123, SDL_ALPHA_OPAQUE}
125 , {198, 165, 132, SDL_ALPHA_OPAQUE}
126 , {206, 165, 99, SDL_ALPHA_OPAQUE}
127 ,
128 {206, 165, 132, SDL_ALPHA_OPAQUE}
129 , {206, 173, 140, SDL_ALPHA_OPAQUE}
130 , {206, 206, 206, SDL_ALPHA_OPAQUE}
131 , {214, 173, 115, SDL_ALPHA_OPAQUE}
132 ,
133 {214, 173, 140, SDL_ALPHA_OPAQUE}
134 , {222, 181, 148, SDL_ALPHA_OPAQUE}
135 , {222, 189, 132, SDL_ALPHA_OPAQUE}
136 , {222, 189, 156, SDL_ALPHA_OPAQUE}
137 ,
138 {222, 222, 222, SDL_ALPHA_OPAQUE}
139 , {231, 198, 165, SDL_ALPHA_OPAQUE}
140 , {231, 231, 231, SDL_ALPHA_OPAQUE}
141 , {239, 206, 173, SDL_ALPHA_OPAQUE}
142 };
143 /* *INDENT-ON* */ /* clang-format on */
144
145 Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE * 2];
146 SDL_Texture *MooseTexture;
147 SDL_Rect displayrect;
148 int window_w;
149 int window_h;
150 SDL_Renderer *renderer;
151 int paused = 0;
152 int i;
153 SDL_bool done = SDL_FALSE;
154 static int fpsdelay;
155
156 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
157 static void
158 quit(int rc)
159 {
160 SDL_Quit();
161 exit(rc);
162 }
163
164 static void
165 PrintUsage(char *argv0)
166 {
167 SDL_Log("Usage: %s [arg] [arg] [arg] ...\n", argv0);
168 SDL_Log("\n");
169 SDL_Log("Where 'arg' is any of the following options:\n");
170 SDL_Log("\n");
171 SDL_Log(" -fps <frames per second>\n");
172 SDL_Log(" -nodelay\n");
173 SDL_Log(" -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
174 SDL_Log(" -scale <scale factor> (initial scale of the overlay)\n");
175 SDL_Log(" -help (shows this help)\n");
176 SDL_Log("\n");
177 SDL_Log("Press ESC to exit, or SPACE to freeze the movie while application running.\n");
178 SDL_Log("\n");
179 }
180
181 void loop(void)
182 {
183 SDL_Event event;
184
185 while (SDL_PollEvent(&event)) {
186 switch (event.type) {
187 case SDL_WINDOWEVENT:
188 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
189 SDL_RenderSetViewport(renderer, NULL);
190 displayrect.w = window_w = event.window.data1;
191 displayrect.h = window_h = event.window.data2;
192 }
193 break;
194 case SDL_MOUSEBUTTONDOWN:
195 displayrect.x = event.button.x - window_w / 2;
196 displayrect.y = event.button.y - window_h / 2;
197 break;
198 case SDL_MOUSEMOTION:
199 if (event.motion.state) {
200 displayrect.x = event.motion.x - window_w / 2;
201 displayrect.y = event.motion.y - window_h / 2;
202 }
203 break;
204 case SDL_KEYDOWN:
205 if (event.key.keysym.sym == SDLK_SPACE) {
206 paused = !paused;
207 break;
208 }
209 if (event.key.keysym.sym != SDLK_ESCAPE) {
210 break;
211 }
212 SDL_FALLTHROUGH;
213 case SDL_QUIT:
214 done = SDL_TRUE;
215 break;
216 }
217 }
218
219 #ifndef __EMSCRIPTEN__
220 SDL_Delay(fpsdelay);
221 #endif
222
223 if (!paused) {
224 i = (i + 1) % MOOSEFRAMES_COUNT;
225
226 SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W);
227 }
228 SDL_RenderClear(renderer);
229 SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
230 SDL_RenderPresent(renderer);
231
232 #ifdef __EMSCRIPTEN__
233 if (done) {
234 emscripten_cancel_main_loop();
235 }
236 #endif
237 }
238
239 int main(int argc, char **argv)
240 {
241 Uint8 *RawMooseData;
242 SDL_RWops *handle;
243 SDL_Window *window;
244 int j;
245 int fps = 12;
246 int nodelay = 0;
247 int scale = 5;
248 char *filename = NULL;
249
250 /* Enable standard application logging */
251 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
252
253 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
254 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
255 return 3;
256 }
257
258 while (argc > 1) {
259 if (SDL_strcmp(argv[1], "-fps") == 0) {
260 if (argv[2]) {
261 fps = SDL_atoi(argv[2]);
262 if (fps == 0) {
263 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
264 "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
265 quit(10);
266 }
267 if ((fps < 0) || (fps > 1000)) {
268 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
269 "The -fps option must be in range from 1 to 1000, default is 12.\n");
270 quit(10);
271 }
272 argv += 2;
273 argc -= 2;
274 } else {
275 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
276 "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
277 quit(10);
278 }
279 } else if (SDL_strcmp(argv[1], "-nodelay") == 0) {
280 nodelay = 1;
281 argv += 1;
282 argc -= 1;
283 } else if (SDL_strcmp(argv[1], "-scale") == 0) {
284 if (argv[2]) {
285 scale = SDL_atoi(argv[2]);
286 if (scale == 0) {
287 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
288 "The -scale option requires an argument [from 1 to 50], default is 5.\n");
289 quit(10);
290 }
291 if ((scale < 0) || (scale > 50)) {
292 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
293 "The -scale option must be in range from 1 to 50, default is 5.\n");
294 quit(10);
295 }
296 argv += 2;
297 argc -= 2;
298 } else {
299 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
300 "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
301 quit(10);
302 }
303 } else if ((SDL_strcmp(argv[1], "-help") == 0) || (SDL_strcmp(argv[1], "-h") == 0)) {
304 PrintUsage(argv[0]);
305 quit(0);
306 } else {
307 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unrecognized option: %s.\n", argv[1]);
308 quit(10);
309 }
310 break;
311 }
312
313 RawMooseData = (Uint8 *)SDL_malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
314 if (!RawMooseData) {
315 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !\n");
316 quit(1);
317 }
318
319 /* load the trojan moose images */
320 filename = GetResourceFilename(NULL, "moose.dat");
321 if (!filename) {
322 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
323 SDL_free(RawMooseData);
324 return -1;
325 }
326 handle = SDL_RWFromFile(filename, "rb");
327 SDL_free(filename);
328 if (!handle) {
329 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
330 SDL_free(RawMooseData);
331 quit(2);
332 }
333
334 SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
335
336 SDL_RWclose(handle);
337
338 /* Create the window and renderer */
339 window_w = MOOSEPIC_W * scale;
340 window_h = MOOSEPIC_H * scale;
341 window = SDL_CreateWindow("Happy Moose",
342 SDL_WINDOWPOS_UNDEFINED,
343 SDL_WINDOWPOS_UNDEFINED,
344 window_w, window_h,
345 SDL_WINDOW_RESIZABLE);
346 if (!window) {
347 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
348 SDL_free(RawMooseData);
349 quit(4);
350 }
351
352 renderer = SDL_CreateRenderer(window, -1, 0);
353 if (!renderer) {
354 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
355 SDL_free(RawMooseData);
356 quit(4);
357 }
358
359 MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
360 if (!MooseTexture) {
361 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
362 SDL_free(RawMooseData);
363 quit(5);
364 }
365 /* Uncomment this to check vertex color with a YUV texture */
366 /* SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80); */
367
368 for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
369 Uint8 MooseFrameRGB[MOOSEFRAME_SIZE * 3];
370 Uint8 *rgb;
371 Uint8 *frame;
372
373 rgb = MooseFrameRGB;
374 frame = RawMooseData + i * MOOSEFRAME_SIZE;
375 for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
376 rgb[0] = MooseColors[frame[j]].r;
377 rgb[1] = MooseColors[frame[j]].g;
378 rgb[2] = MooseColors[frame[j]].b;
379 rgb += 3;
380 }
381 ConvertRGBtoYUV(SDL_PIXELFORMAT_YV12, MooseFrameRGB, MOOSEPIC_W * 3, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H,
382 SDL_GetYUVConversionModeForResolution(MOOSEPIC_W, MOOSEPIC_H),
383 0, 100);
384 }
385
386 SDL_free(RawMooseData);
387
388 /* set the start frame */
389 i = 0;
390 if (nodelay) {
391 fpsdelay = 0;
392 } else {
393 fpsdelay = 1000 / fps;
394 }
395
396 displayrect.x = 0;
397 displayrect.y = 0;
398 displayrect.w = window_w;
399 displayrect.h = window_h;
400
401 /* Ignore key up events, they don't even get filtered */
402 SDL_EventState(SDL_KEYUP, SDL_IGNORE);
403
404 /* Loop, waiting for QUIT or RESIZE */
405 #ifdef __EMSCRIPTEN__
406 emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1);
407 #else
408 while (!done) {
409 loop();
410 }
411 #endif
412
413 SDL_DestroyRenderer(renderer);
414 quit(0);
415 return 0;
416 }
417
418 /* vi: set ts=4 sw=4 expandtab: */