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