2 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
12 /********************************************************************************
14 * Test of the overlay used for moved pictures, test more closed to real life. *
15 * Running trojan moose :) Coded by Mike Gorchak. *
17 ********************************************************************************/
22 #include <emscripten/emscripten.h>
27 #include "testyuv_cvt.h"
32 #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
33 #define MOOSEFRAMES_COUNT 10
35 SDL_Color MooseColors[84] = {
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
142 Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
143 SDL_Texture *MooseTexture;
144 SDL_Rect displayrect;
148 SDL_Renderer *renderer;
151 SDL_bool done = SDL_FALSE;
154 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
163 PrintUsage(char *argv0)
165 SDL_Log("Usage: %s [arg] [arg] [arg] ...\n", argv0);
167 SDL_Log("Where 'arg' is any of the following options:\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");
175 SDL_Log("Press ESC to exit, or SPACE to freeze the movie while application running.\n");
184 while (SDL_PollEvent(&event)) {
185 switch (event.type) {
186 case SDL_WINDOWEVENT:
187 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
188 SDL_RenderSetViewport(renderer, NULL);
189 displayrect.w = window_w = event.window.data1;
190 displayrect.h = window_h = event.window.data2;
193 case SDL_MOUSEBUTTONDOWN:
194 displayrect.x = event.button.x - window_w / 2;
195 displayrect.y = event.button.y - window_h / 2;
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;
204 if (event.key.keysym.sym == SDLK_SPACE) {
208 if (event.key.keysym.sym != SDLK_ESCAPE) {
211 if(event.key.keysym.sym == 0)
222 #ifndef __EMSCRIPTEN__
227 i = (i + 1) % MOOSEFRAMES_COUNT;
229 SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W);
231 SDL_RenderClear(renderer);
232 SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
233 SDL_RenderPresent(renderer);
235 #ifdef __EMSCRIPTEN__
237 emscripten_cancel_main_loop();
242 main(int argc, char **argv)
252 /* Enable standard application logging */
253 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
255 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
256 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
261 if (SDL_strcmp(argv[1], "-fps") == 0) {
263 fps = SDL_atoi(argv[2]);
265 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
266 "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
269 if ((fps < 0) || (fps > 1000)) {
270 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
271 "The -fps option must be in range from 1 to 1000, default is 12.\n");
277 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
278 "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
281 } else if (SDL_strcmp(argv[1], "-nodelay") == 0) {
285 } else if (SDL_strcmp(argv[1], "-scale") == 0) {
287 scale = SDL_atoi(argv[2]);
289 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
290 "The -scale option requires an argument [from 1 to 50], default is 5.\n");
293 if ((scale < 0) || (scale > 50)) {
294 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
295 "The -scale option must be in range from 1 to 50, default is 5.\n");
301 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
302 "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
305 } else if ((SDL_strcmp(argv[1], "-help") == 0)
306 || (SDL_strcmp(argv[1], "-h") == 0)) {
310 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unrecognized option: %s.\n", argv[1]);
316 RawMooseData = (Uint8 *) SDL_malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
317 if (RawMooseData == NULL) {
318 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !\n");
322 /* load the trojan moose images */
323 handle = SDL_RWFromFile("res/moose.dat", "rb");
324 if (handle == NULL) {
325 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
326 SDL_free(RawMooseData);
330 SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
334 /* Create the window and renderer */
335 window_w = MOOSEPIC_W * scale;
336 window_h = MOOSEPIC_H * scale;
337 window = SDL_CreateWindow("Happy Moose",
338 SDL_WINDOWPOS_UNDEFINED,
339 SDL_WINDOWPOS_UNDEFINED,
341 SDL_WINDOW_RESIZABLE);
343 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
344 SDL_free(RawMooseData);
348 renderer = SDL_CreateRenderer(window, -1, 0);
350 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
351 SDL_free(RawMooseData);
355 MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
357 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
358 SDL_free(RawMooseData);
361 /* Uncomment this to check vertex color with a YUV texture */
362 /* SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80); */
364 for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
365 Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
370 frame = RawMooseData + i * MOOSEFRAME_SIZE;
371 for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
372 rgb[0] = MooseColors[frame[j]].r;
373 rgb[1] = MooseColors[frame[j]].g;
374 rgb[2] = MooseColors[frame[j]].b;
377 ConvertRGBtoYUV(SDL_PIXELFORMAT_YV12, MooseFrameRGB, MOOSEPIC_W*3, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H,
378 SDL_GetYUVConversionModeForResolution(MOOSEPIC_W, MOOSEPIC_H),
382 SDL_free(RawMooseData);
384 /* set the start frame */
389 fpsdelay = 1000 / fps;
394 displayrect.w = window_w;
395 displayrect.h = window_h;
397 /* Ignore key up events, they don't even get filtered */
398 SDL_EventState(SDL_KEYUP, SDL_IGNORE);
400 /* Loop, waiting for QUIT or RESIZE */
401 #ifdef __EMSCRIPTEN__
402 emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1);
409 SDL_DestroyRenderer(renderer);
414 /* vi: set ts=4 sw=4 expandtab: */