SDL  2.0
testoverlay2.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2018 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 
147 int paused = 0;
148 int i;
150 static int fpsdelay;
151 
152 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
153 static void
154 quit(int rc)
155 {
156  SDL_Quit();
157  exit(rc);
158 }
159 
160 static void
161 PrintUsage(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 
177 void
179 {
181 
182  while (SDL_PollEvent(&event)) {
183  switch (event.type) {
184  case SDL_WINDOWEVENT:
185  if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
186  SDL_RenderSetViewport(renderer, NULL);
187  displayrect.w = window_w = event.window.data1;
188  displayrect.h = window_h = event.window.data2;
189  }
190  break;
191  case SDL_MOUSEBUTTONDOWN:
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 
222  SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W);
223  }
224  SDL_RenderClear(renderer);
225  SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
226  SDL_RenderPresent(renderer);
227 
228 #ifdef __EMSCRIPTEN__
229  if (done) {
230  emscripten_cancel_main_loop();
231  }
232 #endif
233 }
234 
235 int
236 main(int argc, char **argv)
237 {
238  Uint8 *RawMooseData;
239  SDL_RWops *handle;
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 (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 (strcmp(argv[1], "-nodelay") == 0) {
276  nodelay = 1;
277  argv += 1;
278  argc -= 1;
279  } else if (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 ((strcmp(argv[1], "-help") == 0)
300  || (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 
310  RawMooseData = (Uint8 *) malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
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  free(RawMooseData);
321  quit(2);
322  }
323 
324  SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
325 
326  SDL_RWclose(handle);
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  free(RawMooseData);
339  quit(4);
340  }
341 
342  renderer = SDL_CreateRenderer(window, -1, 0);
343  if (!renderer) {
344  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
345  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  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  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;
388  displayrect.w = window_w;
389  displayrect.h = window_h;
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 
403  SDL_DestroyRenderer(renderer);
404  quit(0);
405  return 0;
406 }
407 
408 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_CreateTexture
GLenum GLenum GLenum GLenum GLenum scale
#define SDL_RenderSetViewport
SDL_MouseMotionEvent motion
Definition: SDL_events.h:533
#define MOOSEPIC_W
Definition: testoverlay2.c:27
void loop()
Definition: testoverlay2.c:178
#define SDL_PollEvent
#define SDL_GetError
Uint8 g
Definition: SDL_pixels.h:298
static void quit(int rc)
Definition: testoverlay2.c:154
#define MOOSEFRAME_SIZE
Definition: testoverlay2.c:30
SDL_EventEntry * free
Definition: SDL_events.c:84
SDL_Texture * MooseTexture
Definition: testoverlay2.c:141
SDL_bool done
Definition: testoverlay2.c:149
#define SDL_RWread(ctx, ptr, size, n)
Definition: SDL_rwops.h:187
Uint8 b
Definition: SDL_pixels.h:299
#define SDL_CreateWindow
#define SDL_WINDOWPOS_UNDEFINED
Definition: SDL_video.h:130
#define SDL_UpdateTexture
#define SDL_LogError
#define MOOSEPIC_H
Definition: testoverlay2.c:28
SDL_WindowEvent window
Definition: SDL_events.h:529
#define SDL_RenderCopy
Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE *2]
Definition: testoverlay2.c:140
#define SDL_Log
#define SDL_RWFromFile
SDL_Renderer * renderer
Definition: testoverlay2.c:146
#define MOOSEFRAMES_COUNT
Definition: testoverlay2.c:31
Uint8 r
Definition: SDL_pixels.h:297
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
uint8_t Uint8
Definition: SDL_stdinc.h:157
struct _cl_event * event
int frame
Definition: teststreaming.c:60
#define SDL_Quit
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
static int fpsdelay
Definition: testoverlay2.c:150
int paused
Definition: testoverlay2.c:147
int x
Definition: SDL_rect.h:66
SDL_Keysym keysym
Definition: SDL_events.h:199
int w
Definition: SDL_rect.h:67
#define SDL_atoi
#define SDL_Delay
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:139
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
#define SDL_RenderClear
SDL_KeyboardEvent key
Definition: SDL_events.h:530
int h
Definition: SDL_rect.h:67
The type used to identify a window.
Definition: SDL_sysvideo.h:73
#define SDL_EventState
SDL_Window * window
Definition: testoverlay2.c:145
SDL_Keycode sym
Definition: SDL_keyboard.h:50
int window_h
Definition: testoverlay2.c:144
int i
Definition: testoverlay2.c:148
SDL_Color MooseColors[84]
Definition: testoverlay2.c:33
#define SDL_Init
General event structure.
Definition: SDL_events.h:525
SDL_Rect displayrect
Definition: testoverlay2.c:142
#define SDL_DestroyRenderer
int main(int argc, char **argv)
Definition: testoverlay2.c:236
#define SDL_GetYUVConversionModeForResolution
static void PrintUsage(char *argv0)
Definition: testoverlay2.c:161
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
int y
Definition: SDL_rect.h:66
int window_w
Definition: testoverlay2.c:143
#define SDL_INIT_VIDEO
Definition: SDL.h:78
#define SDL_CreateRenderer
#define malloc
Definition: SDL_qsort.c:47
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:527
#define SDL_IGNORE
Definition: SDL_events.h:720
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