Atlas - testoverlay.c

Home / ext / SDL / test Lines: 1 | Size: 14973 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 <SDL3/SDL_test.h> 20#include <SDL3/SDL_test_common.h> 21#include <SDL3/SDL_main.h> 22#include "testutils.h" 23 24#ifdef SDL_PLATFORM_EMSCRIPTEN 25#include <emscripten/emscripten.h> 26#endif 27 28#include <stdlib.h> 29 30#define MOOSEPIC_W 64 31#define MOOSEPIC_H 88 32 33#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H) 34#define MOOSEFRAME_PITCH MOOSEPIC_W 35#define MOOSEFRAMES_COUNT 10 36 37/* *INDENT-OFF* */ /* clang-format off */ 38static const SDL_Color MooseColors[84] = { 39 {49, 49, 49, SDL_ALPHA_OPAQUE} 40 , {66, 24, 0, SDL_ALPHA_OPAQUE} 41 , {66, 33, 0, SDL_ALPHA_OPAQUE} 42 , {66, 66, 66, SDL_ALPHA_OPAQUE} 43 , 44 {66, 115, 49, SDL_ALPHA_OPAQUE} 45 , {74, 33, 0, SDL_ALPHA_OPAQUE} 46 , {74, 41, 16, SDL_ALPHA_OPAQUE} 47 , {82, 33, 8, SDL_ALPHA_OPAQUE} 48 , 49 {82, 41, 8, SDL_ALPHA_OPAQUE} 50 , {82, 49, 16, SDL_ALPHA_OPAQUE} 51 , {82, 82, 82, SDL_ALPHA_OPAQUE} 52 , {90, 41, 8, SDL_ALPHA_OPAQUE} 53 , 54 {90, 41, 16, SDL_ALPHA_OPAQUE} 55 , {90, 57, 24, SDL_ALPHA_OPAQUE} 56 , {99, 49, 16, SDL_ALPHA_OPAQUE} 57 , {99, 66, 24, SDL_ALPHA_OPAQUE} 58 , 59 {99, 66, 33, SDL_ALPHA_OPAQUE} 60 , {99, 74, 33, SDL_ALPHA_OPAQUE} 61 , {107, 57, 24, SDL_ALPHA_OPAQUE} 62 , {107, 82, 41, SDL_ALPHA_OPAQUE} 63 , 64 {115, 57, 33, SDL_ALPHA_OPAQUE} 65 , {115, 66, 33, SDL_ALPHA_OPAQUE} 66 , {115, 66, 41, SDL_ALPHA_OPAQUE} 67 , {115, 74, 0, SDL_ALPHA_OPAQUE} 68 , 69 {115, 90, 49, SDL_ALPHA_OPAQUE} 70 , {115, 115, 115, SDL_ALPHA_OPAQUE} 71 , {123, 82, 0, SDL_ALPHA_OPAQUE} 72 , {123, 99, 57, SDL_ALPHA_OPAQUE} 73 , 74 {132, 66, 41, SDL_ALPHA_OPAQUE} 75 , {132, 74, 41, SDL_ALPHA_OPAQUE} 76 , {132, 90, 8, SDL_ALPHA_OPAQUE} 77 , {132, 99, 33, SDL_ALPHA_OPAQUE} 78 , 79 {132, 99, 66, SDL_ALPHA_OPAQUE} 80 , {132, 107, 66, SDL_ALPHA_OPAQUE} 81 , {140, 74, 49, SDL_ALPHA_OPAQUE} 82 , {140, 99, 16, SDL_ALPHA_OPAQUE} 83 , 84 {140, 107, 74, SDL_ALPHA_OPAQUE} 85 , {140, 115, 74, SDL_ALPHA_OPAQUE} 86 , {148, 107, 24, SDL_ALPHA_OPAQUE} 87 , {148, 115, 82, SDL_ALPHA_OPAQUE} 88 , 89 {148, 123, 74, SDL_ALPHA_OPAQUE} 90 , {148, 123, 90, SDL_ALPHA_OPAQUE} 91 , {156, 115, 33, SDL_ALPHA_OPAQUE} 92 , {156, 115, 90, SDL_ALPHA_OPAQUE} 93 , 94 {156, 123, 82, SDL_ALPHA_OPAQUE} 95 , {156, 132, 82, SDL_ALPHA_OPAQUE} 96 , {156, 132, 99, SDL_ALPHA_OPAQUE} 97 , {156, 156, 156, SDL_ALPHA_OPAQUE} 98 , 99 {165, 123, 49, SDL_ALPHA_OPAQUE} 100 , {165, 123, 90, SDL_ALPHA_OPAQUE} 101 , {165, 132, 82, SDL_ALPHA_OPAQUE} 102 , {165, 132, 90, SDL_ALPHA_OPAQUE} 103 , 104 {165, 132, 99, SDL_ALPHA_OPAQUE} 105 , {165, 140, 90, SDL_ALPHA_OPAQUE} 106 , {173, 132, 57, SDL_ALPHA_OPAQUE} 107 , {173, 132, 99, SDL_ALPHA_OPAQUE} 108 , 109 {173, 140, 107, SDL_ALPHA_OPAQUE} 110 , {173, 140, 115, SDL_ALPHA_OPAQUE} 111 , {173, 148, 99, SDL_ALPHA_OPAQUE} 112 , {173, 173, 173, SDL_ALPHA_OPAQUE} 113 , 114 {181, 140, 74, SDL_ALPHA_OPAQUE} 115 , {181, 148, 115, SDL_ALPHA_OPAQUE} 116 , {181, 148, 123, SDL_ALPHA_OPAQUE} 117 , {181, 156, 107, SDL_ALPHA_OPAQUE} 118 , 119 {189, 148, 123, SDL_ALPHA_OPAQUE} 120 , {189, 156, 82, SDL_ALPHA_OPAQUE} 121 , {189, 156, 123, SDL_ALPHA_OPAQUE} 122 , {189, 156, 132, SDL_ALPHA_OPAQUE} 123 , 124 {189, 189, 189, SDL_ALPHA_OPAQUE} 125 , {198, 156, 123, SDL_ALPHA_OPAQUE} 126 , {198, 165, 132, SDL_ALPHA_OPAQUE} 127 , {206, 165, 99, SDL_ALPHA_OPAQUE} 128 , 129 {206, 165, 132, SDL_ALPHA_OPAQUE} 130 , {206, 173, 140, SDL_ALPHA_OPAQUE} 131 , {206, 206, 206, SDL_ALPHA_OPAQUE} 132 , {214, 173, 115, SDL_ALPHA_OPAQUE} 133 , 134 {214, 173, 140, SDL_ALPHA_OPAQUE} 135 , {222, 181, 148, SDL_ALPHA_OPAQUE} 136 , {222, 189, 132, SDL_ALPHA_OPAQUE} 137 , {222, 189, 156, SDL_ALPHA_OPAQUE} 138 , 139 {222, 222, 222, SDL_ALPHA_OPAQUE} 140 , {231, 198, 165, SDL_ALPHA_OPAQUE} 141 , {231, 231, 231, SDL_ALPHA_OPAQUE} 142 , {239, 206, 173, SDL_ALPHA_OPAQUE} 143}; 144/* *INDENT-ON* */ /* clang-format on */ 145 146static SDLTest_CommonState *state; 147static Uint64 next_fps_check; 148static Uint32 frames; 149static const Uint32 fps_check_delay = 5000; 150 151static SDL_Surface *MooseSurfaces[MOOSEFRAMES_COUNT]; 152static SDL_Texture *MooseTexture = NULL; 153static SDL_Palette *MoosePalette = NULL; 154static SDL_FRect displayrect; 155static int window_w; 156static int window_h; 157static int paused = 0; 158static int done = 0; 159static int fpsdelay; 160static bool streaming = true; 161static Uint8 *RawMooseData = NULL; 162 163/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 164static void 165quit(int rc) 166{ 167 int i; 168 169 /* If rc is 0, just let main return normally rather than calling exit. 170 * This allows testing of platforms where SDL_main is required and does meaningful cleanup. 171 */ 172 173 for (i = 0; i < MOOSEFRAMES_COUNT; i++) { 174 SDL_DestroySurface(MooseSurfaces[i]); 175 } 176 177 SDL_DestroyPalette(MoosePalette); 178 179 SDL_free(RawMooseData); 180 181 SDLTest_CommonQuit(state); 182 183 /* Let 'main()' return normally */ 184 if (rc != 0) { 185 exit(rc); 186 } 187} 188 189static void MoveSprites(SDL_Renderer *renderer) 190{ 191 static int i = 0; 192 193 if (streaming) { 194 if (!paused) { 195 i = (i + 1) % MOOSEFRAMES_COUNT; 196 SDL_UpdateTexture(MooseTexture, NULL, RawMooseData + i * MOOSEFRAME_SIZE, MOOSEFRAME_PITCH); 197 } 198 SDL_RenderClear(renderer); 199 SDL_RenderTexture(renderer, MooseTexture, NULL, &displayrect); 200 SDL_RenderPresent(renderer); 201 } else { 202 SDL_Texture *tmp; 203 204 /* Test SDL_CreateTextureFromSurface */ 205 if (!paused) { 206 i = (i + 1) % MOOSEFRAMES_COUNT; 207 } 208 209 tmp = SDL_CreateTextureFromSurface(renderer, MooseSurfaces[i]); 210 if (!tmp) { 211 SDL_Log("Error %s", SDL_GetError()); 212 quit(7); 213 } 214 215 SDL_RenderClear(renderer); 216 SDL_RenderTexture(renderer, tmp, NULL, &displayrect); 217 SDL_RenderPresent(renderer); 218 SDL_DestroyTexture(tmp); 219 } 220} 221 222 223static void loop(void) 224{ 225 Uint64 now; 226 int i; 227 SDL_Event event; 228 229 SDL_Renderer *renderer = state->renderers[0]; /* only 1 window */ 230 231 /* Check for events */ 232 while (SDL_PollEvent(&event)) { 233 SDLTest_CommonEvent(state, &event, &done); 234 SDL_ConvertEventToRenderCoordinates(SDL_GetRenderer(SDL_GetWindowFromEvent(&event)), &event); 235 236 switch (event.type) { 237 case SDL_EVENT_WINDOW_RESIZED: 238 SDL_SetRenderViewport(renderer, NULL); 239 window_w = event.window.data1; 240 window_h = event.window.data2; 241 displayrect.w = (float)window_w; 242 displayrect.h = (float)window_h; 243 break; 244 case SDL_EVENT_MOUSE_BUTTON_DOWN: 245 displayrect.x = event.button.x - window_w / 2; 246 displayrect.y = event.button.y - window_h / 2; 247 break; 248 case SDL_EVENT_MOUSE_MOTION: 249 if (event.motion.state) { 250 displayrect.x = event.motion.x - window_w / 2; 251 displayrect.y = event.motion.y - window_h / 2; 252 } 253 break; 254 case SDL_EVENT_KEY_DOWN: 255 if (event.key.key == SDLK_SPACE) { 256 paused = !paused; 257 break; 258 } 259 if (event.key.key != SDLK_ESCAPE) { 260 break; 261 } 262 break; 263 default: 264 break; 265 } 266 } 267 268#ifndef SDL_PLATFORM_EMSCRIPTEN 269 SDL_Delay(fpsdelay); 270#endif 271 272 for (i = 0; i < state->num_windows; ++i) { 273 if (state->windows[i] == NULL) { 274 continue; 275 } 276 MoveSprites(state->renderers[i]); 277 } 278#ifdef SDL_PLATFORM_EMSCRIPTEN 279 if (done) { 280 emscripten_cancel_main_loop(); 281 } 282#endif 283 284 frames++; 285 now = SDL_GetTicks(); 286 if (now >= next_fps_check) { 287 /* Print out some timing information */ 288 const Uint64 then = next_fps_check - fps_check_delay; 289 const double fps = ((double)frames * 1000) / (now - then); 290 SDL_Log("%2.2f frames per second", fps); 291 next_fps_check = now + fps_check_delay; 292 frames = 0; 293 } 294} 295 296 297int main(int argc, char **argv) 298{ 299 SDL_IOStream *handle; 300 int i; 301 int fps = 12; 302 int nodelay = 0; 303 int scale = 5; 304 char *filename = NULL; 305 306 /* Initialize test framework */ 307 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); 308 if (!state) { 309 return 1; 310 } 311 312 for (i = 1; i < argc;) { 313 int consumed; 314 315 consumed = SDLTest_CommonArg(state, i); 316 if (consumed == 0) { 317 consumed = -1; 318 if (SDL_strcmp(argv[i], "--fps") == 0) { 319 if (argv[i + 1]) { 320 consumed = 2; 321 fps = SDL_atoi(argv[i + 1]); 322 if (fps == 0) { 323 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --fps option requires an argument [from 1 to 1000], default is 12."); 324 quit(10); 325 } 326 if ((fps < 0) || (fps > 1000)) { 327 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --fps option must be in range from 1 to 1000, default is 12."); 328 quit(10); 329 } 330 } else { 331 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --fps option requires an argument [from 1 to 1000], default is 12."); 332 quit(10); 333 } 334 } else if (SDL_strcmp(argv[i], "--nodelay") == 0) { 335 consumed = 1; 336 nodelay = 1; 337 } else if (SDL_strcmp(argv[i], "--nostreaming") == 0) { 338 consumed = 1; 339 streaming = false; 340 } else if (SDL_strcmp(argv[i], "--scale") == 0) { 341 consumed = 2; 342 if (argv[i + 1]) { 343 scale = SDL_atoi(argv[i + 1]); 344 if (scale == 0) { 345 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --scale option requires an argument [from 1 to 50], default is 5."); 346 quit(10); 347 } 348 if ((scale < 0) || (scale > 50)) { 349 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --scale option must be in range from 1 to 50, default is 5."); 350 quit(10); 351 } 352 } else { 353 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --fps option requires an argument [from 1 to 1000], default is 12."); 354 quit(10); 355 } 356 } 357 358 } 359 if (consumed < 0) { 360 static const char *options[] = { 361 "[--fps <frames per second>]", 362 "[--nodelay]", 363 "[--scale <scale factor>] (initial scale of the overlay)", 364 "[--nostreaming] path that use SDL_CreateTextureFromSurface() not STREAMING texture", 365 NULL 366 }; 367 SDLTest_CommonLogUsage(state, argv[0], options); 368 quit(1); 369 } 370 i += consumed; 371 } 372 373 /* Force window size */ 374 state->window_w = MOOSEPIC_W * scale; 375 state->window_h = MOOSEPIC_H * scale; 376 377 if (!SDLTest_CommonInit(state)) { 378 quit(2); 379 } 380 381 RawMooseData = (Uint8 *)SDL_malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT); 382 if (!RawMooseData) { 383 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !"); 384 quit(1); 385 } 386 387 /* load the trojan moose images */ 388 filename = GetResourceFilename(NULL, "moose.dat"); 389 if (!filename) { 390 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory"); 391 quit(2); 392 } 393 handle = SDL_IOFromFile(filename, "rb"); 394 SDL_free(filename); 395 if (!handle) { 396 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !"); 397 quit(2); 398 } 399 400 SDL_ReadIO(handle, RawMooseData, MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT); 401 402 SDL_CloseIO(handle); 403 404 MoosePalette = SDL_CreatePalette(SDL_arraysize(MooseColors)); 405 if (!MoosePalette) { 406 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create palette: %s", SDL_GetError()); 407 quit(3); 408 } 409 SDL_SetPaletteColors(MoosePalette, MooseColors, 0, SDL_arraysize(MooseColors)); 410 411 /* Create the window and renderer */ 412 window_w = MOOSEPIC_W * scale; 413 window_h = MOOSEPIC_H * scale; 414 415 if (state->num_windows != 1) { 416 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Only one window allowed"); 417 quit(1); 418 } 419 420 for (i = 0; i < state->num_windows; ++i) { 421 SDL_Renderer *renderer = state->renderers[i]; 422 423 if (streaming) { 424 MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_INDEX8, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H); 425 if (!MooseTexture) { 426 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s", SDL_GetError()); 427 quit(5); 428 } 429 SDL_SetTexturePalette(MooseTexture, MoosePalette); 430 } 431 } 432 433 /* Uncomment this to check vertex color with a paletted texture */ 434 /* SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80); */ 435 436 for (i = 0; i < MOOSEFRAMES_COUNT; i++) { 437 MooseSurfaces[i] = SDL_CreateSurfaceFrom(MOOSEPIC_W, MOOSEPIC_H, SDL_PIXELFORMAT_INDEX8, 438 RawMooseData + i * MOOSEFRAME_SIZE, MOOSEFRAME_PITCH); 439 if (!MooseSurfaces[i]) { 440 quit(6); 441 } 442 SDL_SetSurfacePalette(MooseSurfaces[i], MoosePalette); 443 } 444 445 /* set the start frame */ 446 i = 0; 447 if (nodelay) { 448 fpsdelay = 0; 449 } else { 450 fpsdelay = 1000 / fps; 451 } 452 453 displayrect.x = 0; 454 displayrect.y = 0; 455 displayrect.w = (float)window_w; 456 displayrect.h = (float)window_h; 457 458 /* Ignore key up events, they don't even get filtered */ 459 SDL_SetEventEnabled(SDL_EVENT_KEY_UP, false); 460 461 /* Main render loop */ 462 frames = 0; 463 next_fps_check = SDL_GetTicks() + fps_check_delay; 464 done = 0; 465 466 /* Loop, waiting for QUIT or RESIZE */ 467#ifdef SDL_PLATFORM_EMSCRIPTEN 468 emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1); 469#else 470 while (!done) { 471 loop(); 472 } 473#endif 474 475 quit(0); 476 return 0; 477} 478
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.