testoverlay2.c (12936B)
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 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} 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 142 Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2]; 143 SDL_Texture *MooseTexture; 144 SDL_Rect displayrect; 145 int window_w; 146 int window_h; 147 SDL_Window *window; 148 SDL_Renderer *renderer; 149 int paused = 0; 150 int i; 151 SDL_bool done = SDL_FALSE; 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 180 loop() 181 { 182 SDL_Event event; 183 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; 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__ 218 SDL_Delay(fpsdelay); 219 #endif 220 221 if (!paused) { 222 i = (i + 1) % MOOSEFRAMES_COUNT; 223 224 SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W); 225 } 226 SDL_RenderClear(renderer); 227 SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect); 228 SDL_RenderPresent(renderer); 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; 242 SDL_Window *window; 243 int j; 244 int fps = 12; 245 int nodelay = 0; 246 int scale = 5; 247 248 /* Enable standard application logging */ 249 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); 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) { 261 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, 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)) { 266 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, 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 { 273 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, 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) { 285 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, 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)) { 290 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, 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 { 297 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, 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 326 SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT); 327 328 SDL_RWclose(handle); 329 330 /* Create the window and renderer */ 331 window_w = MOOSEPIC_W * scale; 332 window_h = MOOSEPIC_H * scale; 333 window = SDL_CreateWindow("Happy Moose", 334 SDL_WINDOWPOS_UNDEFINED, 335 SDL_WINDOWPOS_UNDEFINED, 336 window_w, window_h, 337 SDL_WINDOW_RESIZABLE); 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 344 renderer = SDL_CreateRenderer(window, -1, 0); 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 351 MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H); 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 } 373 ConvertRGBtoYUV(SDL_PIXELFORMAT_YV12, MooseFrameRGB, MOOSEPIC_W*3, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H, 374 SDL_GetYUVConversionModeForResolution(MOOSEPIC_W, MOOSEPIC_H), 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; 390 displayrect.w = window_w; 391 displayrect.h = window_h; 392 393 /* Ignore key up events, they don't even get filtered */ 394 SDL_EventState(SDL_KEYUP, SDL_IGNORE); 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 405 SDL_DestroyRenderer(renderer); 406 quit(0); 407 return 0; 408 } 409 410 /* vi: set ts=4 sw=4 expandtab: */