SDL 2.0
testoverlay2.c
Go to the documentation of this file.
1/*
2 Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
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#ifdef __EMSCRIPTEN__
20#include <emscripten/emscripten.h>
21#endif
22
23#include "SDL.h"
24
25#include "testyuv_cvt.h"
26
27#define MOOSEPIC_W 64
28#define MOOSEPIC_H 88
29
30#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
31#define MOOSEFRAMES_COUNT 10
32
34 {49, 49, 49, SDL_ALPHA_OPAQUE}
35 , {66, 24, 0, SDL_ALPHA_OPAQUE}
36 , {66, 33, 0, SDL_ALPHA_OPAQUE}
37 , {66, 66, 66, SDL_ALPHA_OPAQUE}
38 ,
39 {66, 115, 49, SDL_ALPHA_OPAQUE}
40 , {74, 33, 0, SDL_ALPHA_OPAQUE}
41 , {74, 41, 16, SDL_ALPHA_OPAQUE}
42 , {82, 33, 8, SDL_ALPHA_OPAQUE}
43 ,
44 {82, 41, 8, SDL_ALPHA_OPAQUE}
45 , {82, 49, 16, SDL_ALPHA_OPAQUE}
46 , {82, 82, 82, SDL_ALPHA_OPAQUE}
47 , {90, 41, 8, SDL_ALPHA_OPAQUE}
48 ,
49 {90, 41, 16, SDL_ALPHA_OPAQUE}
50 , {90, 57, 24, SDL_ALPHA_OPAQUE}
51 , {99, 49, 16, SDL_ALPHA_OPAQUE}
52 , {99, 66, 24, SDL_ALPHA_OPAQUE}
53 ,
54 {99, 66, 33, SDL_ALPHA_OPAQUE}
55 , {99, 74, 33, SDL_ALPHA_OPAQUE}
56 , {107, 57, 24, SDL_ALPHA_OPAQUE}
57 , {107, 82, 41, SDL_ALPHA_OPAQUE}
58 ,
59 {115, 57, 33, SDL_ALPHA_OPAQUE}
60 , {115, 66, 33, SDL_ALPHA_OPAQUE}
61 , {115, 66, 41, SDL_ALPHA_OPAQUE}
62 , {115, 74, 0, SDL_ALPHA_OPAQUE}
63 ,
64 {115, 90, 49, SDL_ALPHA_OPAQUE}
65 , {115, 115, 115, SDL_ALPHA_OPAQUE}
66 , {123, 82, 0, SDL_ALPHA_OPAQUE}
67 , {123, 99, 57, SDL_ALPHA_OPAQUE}
68 ,
69 {132, 66, 41, SDL_ALPHA_OPAQUE}
70 , {132, 74, 41, SDL_ALPHA_OPAQUE}
71 , {132, 90, 8, SDL_ALPHA_OPAQUE}
72 , {132, 99, 33, SDL_ALPHA_OPAQUE}
73 ,
74 {132, 99, 66, SDL_ALPHA_OPAQUE}
75 , {132, 107, 66, SDL_ALPHA_OPAQUE}
76 , {140, 74, 49, SDL_ALPHA_OPAQUE}
77 , {140, 99, 16, SDL_ALPHA_OPAQUE}
78 ,
79 {140, 107, 74, SDL_ALPHA_OPAQUE}
80 , {140, 115, 74, SDL_ALPHA_OPAQUE}
81 , {148, 107, 24, SDL_ALPHA_OPAQUE}
82 , {148, 115, 82, SDL_ALPHA_OPAQUE}
83 ,
84 {148, 123, 74, SDL_ALPHA_OPAQUE}
85 , {148, 123, 90, SDL_ALPHA_OPAQUE}
86 , {156, 115, 33, SDL_ALPHA_OPAQUE}
87 , {156, 115, 90, SDL_ALPHA_OPAQUE}
88 ,
89 {156, 123, 82, SDL_ALPHA_OPAQUE}
90 , {156, 132, 82, SDL_ALPHA_OPAQUE}
91 , {156, 132, 99, SDL_ALPHA_OPAQUE}
92 , {156, 156, 156, SDL_ALPHA_OPAQUE}
93 ,
94 {165, 123, 49, SDL_ALPHA_OPAQUE}
95 , {165, 123, 90, SDL_ALPHA_OPAQUE}
96 , {165, 132, 82, SDL_ALPHA_OPAQUE}
97 , {165, 132, 90, SDL_ALPHA_OPAQUE}
98 ,
99 {165, 132, 99, SDL_ALPHA_OPAQUE}
100 , {165, 140, 90, SDL_ALPHA_OPAQUE}
101 , {173, 132, 57, SDL_ALPHA_OPAQUE}
102 , {173, 132, 99, SDL_ALPHA_OPAQUE}
103 ,
104 {173, 140, 107, SDL_ALPHA_OPAQUE}
105 , {173, 140, 115, SDL_ALPHA_OPAQUE}
106 , {173, 148, 99, SDL_ALPHA_OPAQUE}
107 , {173, 173, 173, SDL_ALPHA_OPAQUE}
108 ,
109 {181, 140, 74, SDL_ALPHA_OPAQUE}
110 , {181, 148, 115, SDL_ALPHA_OPAQUE}
111 , {181, 148, 123, SDL_ALPHA_OPAQUE}
112 , {181, 156, 107, SDL_ALPHA_OPAQUE}
113 ,
114 {189, 148, 123, SDL_ALPHA_OPAQUE}
115 , {189, 156, 82, SDL_ALPHA_OPAQUE}
116 , {189, 156, 123, SDL_ALPHA_OPAQUE}
117 , {189, 156, 132, SDL_ALPHA_OPAQUE}
118 ,
119 {189, 189, 189, SDL_ALPHA_OPAQUE}
120 , {198, 156, 123, SDL_ALPHA_OPAQUE}
121 , {198, 165, 132, SDL_ALPHA_OPAQUE}
122 , {206, 165, 99, SDL_ALPHA_OPAQUE}
123 ,
124 {206, 165, 132, SDL_ALPHA_OPAQUE}
125 , {206, 173, 140, SDL_ALPHA_OPAQUE}
126 , {206, 206, 206, SDL_ALPHA_OPAQUE}
127 , {214, 173, 115, SDL_ALPHA_OPAQUE}
128 ,
129 {214, 173, 140, SDL_ALPHA_OPAQUE}
130 , {222, 181, 148, SDL_ALPHA_OPAQUE}
131 , {222, 189, 132, SDL_ALPHA_OPAQUE}
132 , {222, 189, 156, SDL_ALPHA_OPAQUE}
133 ,
134 {222, 222, 222, SDL_ALPHA_OPAQUE}
135 , {231, 198, 165, SDL_ALPHA_OPAQUE}
136 , {231, 231, 231, SDL_ALPHA_OPAQUE}
137 , {239, 206, 173, SDL_ALPHA_OPAQUE}
138};
139
147int paused = 0;
148int i;
150static int fpsdelay;
151
152/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
153static void
154quit(int rc)
155{
156 SDL_Quit();
157 exit(rc);
158}
159
160static void
161PrintUsage(char *argv0)
162{
163 SDL_Log("Usage: %s [arg] [arg] [arg] ...\n", argv0);
164 SDL_Log("\n");
165 SDL_Log("Where 'arg' is any of the following options:\n");
166 SDL_Log("\n");
167 SDL_Log(" -fps <frames per second>\n");
168 SDL_Log(" -nodelay\n");
169 SDL_Log(" -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
170 SDL_Log(" -scale <scale factor> (initial scale of the overlay)\n");
171 SDL_Log(" -help (shows this help)\n");
172 SDL_Log("\n");
173 SDL_Log("Press ESC to exit, or SPACE to freeze the movie while application running.\n");
174 SDL_Log("\n");
175}
176
177void
179{
181
182 while (SDL_PollEvent(&event)) {
183 switch (event.type) {
184 case SDL_WINDOWEVENT:
185 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
187 displayrect.w = window_w = event.window.data1;
188 displayrect.h = window_h = event.window.data2;
189 }
190 break;
192 displayrect.x = event.button.x - window_w / 2;
193 displayrect.y = event.button.y - window_h / 2;
194 break;
195 case SDL_MOUSEMOTION:
196 if (event.motion.state) {
197 displayrect.x = event.motion.x - window_w / 2;
198 displayrect.y = event.motion.y - window_h / 2;
199 }
200 break;
201 case SDL_KEYDOWN:
202 if (event.key.keysym.sym == SDLK_SPACE) {
203 paused = !paused;
204 break;
205 }
206 if (event.key.keysym.sym != SDLK_ESCAPE) {
207 break;
208 }
209 case SDL_QUIT:
210 done = SDL_TRUE;
211 break;
212 }
213 }
214
215#ifndef __EMSCRIPTEN__
217#endif
218
219 if (!paused) {
220 i = (i + 1) % MOOSEFRAMES_COUNT;
221
223 }
227
228#ifdef __EMSCRIPTEN__
229 if (done) {
230 emscripten_cancel_main_loop();
231 }
232#endif
233}
234
235int
236main(int argc, char **argv)
237{
238 Uint8 *RawMooseData;
241 int j;
242 int fps = 12;
243 int nodelay = 0;
244 int scale = 5;
245
246 /* Enable standard application logging */
248
249 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
250 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
251 return 3;
252 }
253
254 while (argc > 1) {
255 if (SDL_strcmp(argv[1], "-fps") == 0) {
256 if (argv[2]) {
257 fps = SDL_atoi(argv[2]);
258 if (fps == 0) {
260 "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
261 quit(10);
262 }
263 if ((fps < 0) || (fps > 1000)) {
265 "The -fps option must be in range from 1 to 1000, default is 12.\n");
266 quit(10);
267 }
268 argv += 2;
269 argc -= 2;
270 } else {
272 "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
273 quit(10);
274 }
275 } else if (SDL_strcmp(argv[1], "-nodelay") == 0) {
276 nodelay = 1;
277 argv += 1;
278 argc -= 1;
279 } else if (SDL_strcmp(argv[1], "-scale") == 0) {
280 if (argv[2]) {
281 scale = SDL_atoi(argv[2]);
282 if (scale == 0) {
284 "The -scale option requires an argument [from 1 to 50], default is 5.\n");
285 quit(10);
286 }
287 if ((scale < 0) || (scale > 50)) {
289 "The -scale option must be in range from 1 to 50, default is 5.\n");
290 quit(10);
291 }
292 argv += 2;
293 argc -= 2;
294 } else {
296 "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
297 quit(10);
298 }
299 } else if ((SDL_strcmp(argv[1], "-help") == 0)
300 || (SDL_strcmp(argv[1], "-h") == 0)) {
301 PrintUsage(argv[0]);
302 quit(0);
303 } else {
304 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unrecognized option: %s.\n", argv[1]);
305 quit(10);
306 }
307 break;
308 }
309
311 if (RawMooseData == NULL) {
312 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !\n");
313 quit(1);
314 }
315
316 /* load the trojan moose images */
317 handle = SDL_RWFromFile("moose.dat", "rb");
318 if (handle == NULL) {
319 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
320 SDL_free(RawMooseData);
321 quit(2);
322 }
323
325
327
328 /* Create the window and renderer */
331 window = SDL_CreateWindow("Happy Moose",
336 if (!window) {
337 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
338 SDL_free(RawMooseData);
339 quit(4);
340 }
341
343 if (!renderer) {
344 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
345 SDL_free(RawMooseData);
346 quit(4);
347 }
348
350 if (!MooseTexture) {
351 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
352 SDL_free(RawMooseData);
353 quit(5);
354 }
355 /* Uncomment this to check vertex color with a YUV texture */
356 /* SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80); */
357
358 for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
359 Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
360 Uint8 *rgb;
361 Uint8 *frame;
362
363 rgb = MooseFrameRGB;
364 frame = RawMooseData + i * MOOSEFRAME_SIZE;
365 for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
366 rgb[0] = MooseColors[frame[j]].r;
367 rgb[1] = MooseColors[frame[j]].g;
368 rgb[2] = MooseColors[frame[j]].b;
369 rgb += 3;
370 }
373 0, 100);
374 }
375
376 SDL_free(RawMooseData);
377
378 /* set the start frame */
379 i = 0;
380 if (nodelay) {
381 fpsdelay = 0;
382 } else {
383 fpsdelay = 1000 / fps;
384 }
385
386 displayrect.x = 0;
387 displayrect.y = 0;
390
391 /* Ignore key up events, they don't even get filtered */
393
394 /* Loop, waiting for QUIT or RESIZE */
395#ifdef __EMSCRIPTEN__
396 emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1);
397#else
398 while (!done) {
399 loop();
400 }
401#endif
402
404 quit(0);
405 return 0;
406}
407
408/* vi: set ts=4 sw=4 expandtab: */
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_RenderPresent
#define SDL_GetError
#define SDL_PollEvent
#define SDL_DestroyRenderer
#define SDL_CreateWindow
#define SDL_CreateTexture
#define SDL_GetYUVConversionModeForResolution
#define SDL_CreateRenderer
#define SDL_RWread
#define SDL_malloc
#define SDL_EventState
#define SDL_UpdateTexture
#define SDL_LogSetPriority
#define SDL_RenderClear
#define SDL_LogError
#define SDL_free
#define SDL_strcmp
#define SDL_RWFromFile
#define SDL_Delay
#define SDL_RenderCopy
#define SDL_Quit
#define SDL_Init
#define SDL_atoi
#define SDL_Log
#define SDL_RWclose
#define SDL_RenderSetViewport
@ SDL_MOUSEMOTION
Definition: SDL_events.h:105
@ SDL_QUIT
Definition: SDL_events.h:60
@ SDL_WINDOWEVENT
Definition: SDL_events.h:92
@ SDL_MOUSEBUTTONDOWN
Definition: SDL_events.h:106
@ SDL_KEYDOWN
Definition: SDL_events.h:96
@ SDL_KEYUP
Definition: SDL_events.h:97
#define SDL_IGNORE
Definition: SDL_events.h:757
@ SDLK_ESCAPE
Definition: SDL_keycode.h:55
@ SDLK_SPACE
Definition: SDL_keycode.h:58
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
struct _cl_event * event
GLenum GLenum GLenum GLenum GLenum scale
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
@ SDL_PIXELFORMAT_YV12
Definition: SDL_pixels.h:277
@ SDL_TEXTUREACCESS_STREAMING
Definition: SDL_render.h:94
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
uint8_t Uint8
Definition: SDL_stdinc.h:179
#define SDL_WINDOWPOS_UNDEFINED
Definition: SDL_video.h:130
@ SDL_WINDOW_RESIZABLE
Definition: SDL_video.h:105
@ SDL_WINDOWEVENT_RESIZED
Definition: SDL_video.h:155
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:167
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
Uint8 r
Definition: SDL_pixels.h:297
Uint8 b
Definition: SDL_pixels.h:299
Uint8 g
Definition: SDL_pixels.h:298
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
int y
Definition: SDL_rect.h:79
int x
Definition: SDL_rect.h:79
The type used to identify a window.
Definition: SDL_sysvideo.h:74
#define MOOSEPIC_H
Definition: testoverlay2.c:28
#define MOOSEFRAMES_COUNT
Definition: testoverlay2.c:31
SDL_Rect displayrect
Definition: testoverlay2.c:142
static void quit(int rc)
Definition: testoverlay2.c:154
static void PrintUsage(char *argv0)
Definition: testoverlay2.c:161
int main(int argc, char **argv)
Definition: testoverlay2.c:236
#define MOOSEPIC_W
Definition: testoverlay2.c:27
Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE *2]
Definition: testoverlay2.c:140
SDL_Texture * MooseTexture
Definition: testoverlay2.c:141
int window_w
Definition: testoverlay2.c:143
SDL_Color MooseColors[84]
Definition: testoverlay2.c:33
int window_h
Definition: testoverlay2.c:144
int paused
Definition: testoverlay2.c:147
SDL_Renderer * renderer
Definition: testoverlay2.c:146
SDL_bool done
Definition: testoverlay2.c:149
SDL_Window * window
Definition: testoverlay2.c:145
static int fpsdelay
Definition: testoverlay2.c:150
int i
Definition: testoverlay2.c:148
#define MOOSEFRAME_SIZE
Definition: testoverlay2.c:30
void loop()
Definition: testoverlay2.c:178
int frame
Definition: teststreaming.c:60
SDL_bool ConvertRGBtoYUV(Uint32 format, Uint8 *src, int pitch, Uint8 *out, int w, int h, SDL_YUV_CONVERSION_MODE mode, int monochrome, int luminance)
Definition: testyuv_cvt.c:262
General event structure.
Definition: SDL_events.h:558