Atlas - testcolorspace.c
Home / ext / SDL / test Lines: 1 | Size: 21612 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Copyright (C) 1997-2026 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#define SDL_MAIN_USE_CALLBACKS 1 14#include <SDL3/SDL.h> 15#include <SDL3/SDL_test.h> 16#include <SDL3/SDL_main.h> 17 18#define WINDOW_WIDTH 640 19#define WINDOW_HEIGHT 480 20 21#define TEXT_START_X 6.0f 22#define TEXT_START_Y 6.0f 23#define TEXT_LINE_ADVANCE FONT_CHARACTER_SIZE * 2 24 25static SDL_Window *window; 26static SDL_Renderer *renderer; 27static const char *renderer_name; 28static SDL_Colorspace colorspace = SDL_COLORSPACE_SRGB; 29static const char *colorspace_name = "sRGB"; 30static int renderer_count = 0; 31static int renderer_index = 0; 32static int stage_index = 0; 33static float HDR_headroom = 1.0f; 34 35enum 36{ 37 StageClearBackground, 38 StageDrawBackground, 39 StageTextureBackground, 40 StageTargetBackground, 41 StageBlendDrawing, 42 StageBlendTexture, 43 StageGradientDrawing, 44 StageGradientTexture, 45 StageCount 46}; 47 48static void FreeRenderer(void) 49{ 50 SDLTest_CleanupTextDrawing(); 51 SDL_DestroyRenderer(renderer); 52 renderer = NULL; 53} 54 55static void UpdateHDRState(void) 56{ 57 SDL_PropertiesID props; 58 bool HDR_enabled; 59 60 props = SDL_GetWindowProperties(window); 61 HDR_enabled = SDL_GetBooleanProperty(props, SDL_PROP_WINDOW_HDR_ENABLED_BOOLEAN, false); 62 63 SDL_Log("HDR %s", HDR_enabled ? "enabled" : "disabled"); 64 65 if (HDR_enabled) { 66 props = SDL_GetRendererProperties(renderer); 67 if (SDL_GetNumberProperty(props, SDL_PROP_RENDERER_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB) != SDL_COLORSPACE_SRGB_LINEAR) { 68 SDL_Log("Run with --colorspace linear to display HDR colors"); 69 } 70 HDR_headroom = SDL_GetFloatProperty(props, SDL_PROP_RENDERER_HDR_HEADROOM_FLOAT, 1.0f); 71 } 72} 73 74static void CreateRenderer(void) 75{ 76 SDL_PropertiesID props; 77 78 props = SDL_CreateProperties(); 79 SDL_SetPointerProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window); 80 SDL_SetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, SDL_GetRenderDriver(renderer_index)); 81 SDL_SetNumberProperty(props, SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER, colorspace); 82 renderer = SDL_CreateRendererWithProperties(props); 83 SDL_DestroyProperties(props); 84 if (!renderer) { 85 SDL_Log("Couldn't create renderer: %s", SDL_GetError()); 86 return; 87 } 88 89 renderer_name = SDL_GetRendererName(renderer); 90 SDL_Log("Created renderer %s", renderer_name); 91 92 UpdateHDRState(); 93} 94 95static void NextRenderer( void ) 96{ 97 if (renderer_count <= 0) { 98 return; 99 } 100 101 ++renderer_index; 102 if (renderer_index == renderer_count) { 103 renderer_index = 0; 104 } 105 FreeRenderer(); 106 CreateRenderer(); 107} 108 109static void PrevRenderer(void) 110{ 111 if (renderer_count <= 0) { 112 return; 113 } 114 115 --renderer_index; 116 if (renderer_index == -1) { 117 renderer_index += renderer_count; 118 } 119 FreeRenderer(); 120 CreateRenderer(); 121} 122 123static void NextStage(void) 124{ 125 ++stage_index; 126 if (stage_index == StageCount) { 127 stage_index = 0; 128 } 129} 130 131static void PrevStage(void) 132{ 133 --stage_index; 134 if (stage_index == -1) { 135 stage_index += StageCount; 136 } 137} 138 139static bool ReadPixel(int x, int y, SDL_Color *c) 140{ 141 SDL_Surface *surface; 142 SDL_Rect r; 143 bool result = false; 144 145 r.x = x; 146 r.y = y; 147 r.w = 1; 148 r.h = 1; 149 150 surface = SDL_RenderReadPixels(renderer, &r); 151 if (surface) { 152 /* Don't tonemap back to SDR, our source content was SDR */ 153 SDL_SetStringProperty(SDL_GetSurfaceProperties(surface), SDL_PROP_SURFACE_TONEMAP_OPERATOR_STRING, "*=1"); 154 155 if (SDL_ReadSurfacePixel(surface, 0, 0, &c->r, &c->g, &c->b, &c->a)) { 156 result = true; 157 } else { 158 SDL_Log("Couldn't read pixel: %s", SDL_GetError()); 159 } 160 SDL_DestroySurface(surface); 161 } else { 162 SDL_Log("Couldn't read back pixels: %s", SDL_GetError()); 163 } 164 return result; 165} 166 167static void DrawText(float x, float y, const char *fmt, ...) 168{ 169 char *text; 170 171 va_list ap; 172 va_start(ap, fmt); 173 SDL_vasprintf(&text, fmt, ap); 174 va_end(ap); 175 176 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 177 SDLTest_DrawString(renderer, x + 1.0f, y + 1.0f, text); 178 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 179 SDLTest_DrawString(renderer, x, y, text); 180 SDL_free(text); 181} 182 183static void RenderClearBackground(void) 184{ 185 /* Draw a 50% gray background. 186 * This will be darker when using sRGB colors and lighter using linear colors 187 */ 188 SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255); 189 SDL_RenderClear(renderer); 190 191 /* Check the rendered pixels */ 192 SDL_Color c; 193 if (!ReadPixel(0, 0, &c)) { 194 return; 195 } 196 197 float x = TEXT_START_X; 198 float y = TEXT_START_Y; 199 DrawText(x, y, "%s %s", renderer_name, colorspace_name); 200 y += TEXT_LINE_ADVANCE; 201 DrawText(x, y, "Test: Clear 50%% Gray Background"); 202 y += TEXT_LINE_ADVANCE; 203 DrawText(x, y, "Background color written: 0x808080, read: 0x%.2x%.2x%.2x", c.r, c.g, c.b); 204 y += TEXT_LINE_ADVANCE; 205 if (c.r != 128) { 206 DrawText(x, y, "Incorrect background color, unknown reason"); 207 y += TEXT_LINE_ADVANCE; 208 } 209} 210 211static void RenderDrawBackground(void) 212{ 213 /* Draw a 50% gray background. 214 * This will be darker when using sRGB colors and lighter using linear colors 215 */ 216 SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255); 217 SDL_RenderFillRect(renderer, NULL); 218 219 /* Check the rendered pixels */ 220 SDL_Color c; 221 if (!ReadPixel(0, 0, &c)) { 222 return; 223 } 224 225 float x = TEXT_START_X; 226 float y = TEXT_START_Y; 227 DrawText(x, y, "%s %s", renderer_name, colorspace_name); 228 y += TEXT_LINE_ADVANCE; 229 DrawText(x, y, "Test: Draw 50%% Gray Background"); 230 y += TEXT_LINE_ADVANCE; 231 DrawText(x, y, "Background color written: 0x808080, read: 0x%.2x%.2x%.2x", c.r, c.g, c.b); 232 y += TEXT_LINE_ADVANCE; 233 if (c.r != 128) { 234 DrawText(x, y, "Incorrect background color, unknown reason"); 235 y += TEXT_LINE_ADVANCE; 236 } 237} 238 239static SDL_Texture *CreateGrayTexture(void) 240{ 241 SDL_Texture *texture; 242 Uint8 pixels[4]; 243 244 /* Floating point textures are in the linear colorspace by default */ 245 texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 1, 1); 246 if (!texture) { 247 return NULL; 248 } 249 250 pixels[0] = 128; 251 pixels[1] = 128; 252 pixels[2] = 128; 253 pixels[3] = 255; 254 SDL_UpdateTexture(texture, NULL, pixels, sizeof(pixels)); 255 256 return texture; 257} 258 259static void RenderTextureBackground(void) 260{ 261 /* Fill the background with a 50% gray texture. 262 * This will be darker when using sRGB colors and lighter using linear colors 263 */ 264 SDL_Texture *texture = CreateGrayTexture(); 265 SDL_RenderTexture(renderer, texture, NULL, NULL); 266 SDL_DestroyTexture(texture); 267 268 /* Check the rendered pixels */ 269 SDL_Color c; 270 if (!ReadPixel(0, 0, &c)) { 271 return; 272 } 273 274 float x = TEXT_START_X; 275 float y = TEXT_START_Y; 276 DrawText(x, y, "%s %s", renderer_name, colorspace_name); 277 y += TEXT_LINE_ADVANCE; 278 DrawText(x, y, "Test: Fill 50%% Gray Texture"); 279 y += TEXT_LINE_ADVANCE; 280 DrawText(x, y, "Background color written: 0x808080, read: 0x%.2x%.2x%.2x", c.r, c.g, c.b); 281 y += TEXT_LINE_ADVANCE; 282 if (c.r != 128) { 283 DrawText(x, y, "Incorrect background color, unknown reason"); 284 y += TEXT_LINE_ADVANCE; 285 } 286} 287 288static void RenderTargetBackground(void) 289{ 290 /* Fill the background with a 50% gray texture. 291 * This will be darker when using sRGB colors and lighter using linear colors 292 */ 293 SDL_Texture *target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, 1, 1); 294 SDL_Texture *texture = CreateGrayTexture(); 295 296 /* Fill the render target with the gray texture */ 297 SDL_SetRenderTarget(renderer, target); 298 SDL_RenderTexture(renderer, texture, NULL, NULL); 299 SDL_DestroyTexture(texture); 300 301 /* Fill the output with the render target */ 302 SDL_SetRenderTarget(renderer, NULL); 303 SDL_RenderTexture(renderer, target, NULL, NULL); 304 SDL_DestroyTexture(target); 305 306 /* Check the rendered pixels */ 307 SDL_Color c; 308 if (!ReadPixel(0, 0, &c)) { 309 return; 310 } 311 312 float x = TEXT_START_X; 313 float y = TEXT_START_Y; 314 DrawText(x, y, "%s %s", renderer_name, colorspace_name); 315 y += TEXT_LINE_ADVANCE; 316 DrawText(x, y, "Test: Fill 50%% Gray Render Target"); 317 y += TEXT_LINE_ADVANCE; 318 DrawText(x, y, "Background color written: 0x808080, read: 0x%.2x%.2x%.2x", c.r, c.g, c.b); 319 y += TEXT_LINE_ADVANCE; 320 if (c.r != 128) { 321 DrawText(x, y, "Incorrect background color, unknown reason"); 322 y += TEXT_LINE_ADVANCE; 323 } 324} 325 326static void RenderBlendDrawing(void) 327{ 328 SDL_Color a = { 238, 70, 166, 255 }; /* red square */ 329 SDL_Color b = { 147, 255, 0, 255 }; /* green square */ 330 SDL_FRect rect; 331 332 /* Draw a green square blended over a red square 333 * This will have different effects based on whether sRGB colorspaces and sRGB vs linear blending is used. 334 */ 335 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 336 SDL_RenderClear(renderer); 337 rect.x = WINDOW_WIDTH / 3; 338 rect.y = 0; 339 rect.w = WINDOW_WIDTH / 3; 340 rect.h = WINDOW_HEIGHT; 341 SDL_SetRenderDrawColor(renderer, a.r, a.g, a.b, a.a); 342 SDL_RenderFillRect(renderer, &rect); 343 344 rect.x = 0; 345 rect.y = WINDOW_HEIGHT / 3; 346 rect.w = WINDOW_WIDTH; 347 rect.h = WINDOW_HEIGHT / 6; 348 SDL_SetRenderDrawColor(renderer, b.r, b.g, b.b, b.a); 349 SDL_RenderFillRect(renderer, &rect); 350 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); 351 SDL_SetRenderDrawColor(renderer, b.r, b.g, b.b, 128); 352 rect.y += WINDOW_HEIGHT / 6; 353 SDL_RenderFillRect(renderer, &rect); 354 355 SDL_Color ar, br, cr; 356 if (!ReadPixel(WINDOW_WIDTH / 2, 0, &ar) || 357 !ReadPixel(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 3, &br) || 358 !ReadPixel(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, &cr)) { 359 return; 360 } 361 362 float x = TEXT_START_X; 363 float y = TEXT_START_Y; 364 DrawText(x, y, "%s %s", renderer_name, colorspace_name); 365 y += TEXT_LINE_ADVANCE; 366 DrawText(x, y, "Test: Draw Blending"); 367 y += TEXT_LINE_ADVANCE; 368 if ((cr.r == 199 && cr.g == 193 && cr.b == 121) || 369 (cr.r == 199 && cr.g == 193 && cr.b == 120)) { 370 DrawText(x, y, "Correct blend color, blending in linear space"); 371 } else if ((cr.r == 192 && cr.g == 163 && cr.b == 83) || 372 (cr.r == 191 && cr.g == 162 && cr.b == 82)) { 373 DrawText(x, y, "Correct blend color, blending in sRGB space"); 374 } else if (cr.r == 214 && cr.g == 156 && cr.b == 113) { 375 DrawText(x, y, "Incorrect blend color, blending in PQ space"); 376 } else { 377 DrawText(x, y, "Incorrect blend color, unknown reason"); 378 } 379 y += TEXT_LINE_ADVANCE; 380} 381 382static void RenderBlendTexture(void) 383{ 384 SDL_Color color_a = { 238, 70, 166, 255 }; /* red square */ 385 SDL_Color color_b = { 147, 255, 0, 255 }; /* green square */ 386 SDL_Texture *a; 387 SDL_Texture *b; 388 SDL_FRect rect; 389 390 /* Draw a green square blended over a red square 391 * This will have different effects based on whether sRGB colorspaces and sRGB vs linear blending is used. 392 */ 393 a = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 1, 1); 394 SDL_UpdateTexture(a, NULL, &color_a, sizeof(color_a)); 395 b = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 1, 1); 396 SDL_UpdateTexture(b, NULL, &color_b, sizeof(color_b)); 397 398 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 399 SDL_RenderClear(renderer); 400 rect.x = WINDOW_WIDTH / 3; 401 rect.y = 0; 402 rect.w = WINDOW_WIDTH / 3; 403 rect.h = WINDOW_HEIGHT; 404 SDL_RenderTexture(renderer, a, NULL, &rect); 405 406 rect.x = 0; 407 rect.y = WINDOW_HEIGHT / 3; 408 rect.w = WINDOW_WIDTH; 409 rect.h = WINDOW_HEIGHT / 6; 410 SDL_RenderTexture(renderer, b, NULL, &rect); 411 rect.y += WINDOW_HEIGHT / 6; 412 SDL_SetTextureBlendMode(b, SDL_BLENDMODE_BLEND); 413 SDL_SetTextureAlphaModFloat(b, 128 / 255.0f); 414 SDL_RenderTexture(renderer, b, NULL, &rect); 415 416 SDL_Color ar, br, cr; 417 if (!ReadPixel(WINDOW_WIDTH / 2, 0, &ar) || 418 !ReadPixel(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 3, &br) || 419 !ReadPixel(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, &cr)) { 420 return; 421 } 422 423 float x = TEXT_START_X; 424 float y = TEXT_START_Y; 425 DrawText(x, y, "%s %s", renderer_name, colorspace_name); 426 y += TEXT_LINE_ADVANCE; 427 DrawText(x, y, "Test: Texture Blending"); 428 y += TEXT_LINE_ADVANCE; 429 if ((cr.r == 199 && cr.g == 193 && cr.b == 121) || 430 (cr.r == 199 && cr.g == 193 && cr.b == 120)) { 431 DrawText(x, y, "Correct blend color, blending in linear space"); 432 } else if ((cr.r == 192 && cr.g == 163 && cr.b == 83) || 433 (cr.r == 191 && cr.g == 162 && cr.b == 82)) { 434 DrawText(x, y, "Correct blend color, blending in sRGB space"); 435 } else { 436 DrawText(x, y, "Incorrect blend color, unknown reason"); 437 } 438 y += TEXT_LINE_ADVANCE; 439 440 SDL_DestroyTexture(a); 441 SDL_DestroyTexture(b); 442} 443 444static void DrawGradient(float x, float y, float width, float height, float start, float end) 445{ 446 float xy[8]; 447 const int xy_stride = 2 * sizeof(float); 448 SDL_FColor color[4]; 449 const int color_stride = sizeof(SDL_FColor); 450 const int num_vertices = 4; 451 const int indices[6] = { 0, 1, 2, 0, 2, 3 }; 452 const int num_indices = 6; 453 const int size_indices = 4; 454 float minx, miny, maxx, maxy; 455 SDL_FColor min_color = { start, start, start, 1.0f }; 456 SDL_FColor max_color = { end, end, end, 1.0f }; 457 458 minx = x; 459 miny = y; 460 maxx = minx + width; 461 maxy = miny + height; 462 463 xy[0] = minx; 464 xy[1] = miny; 465 xy[2] = maxx; 466 xy[3] = miny; 467 xy[4] = maxx; 468 xy[5] = maxy; 469 xy[6] = minx; 470 xy[7] = maxy; 471 472 color[0] = min_color; 473 color[1] = max_color; 474 color[2] = max_color; 475 color[3] = min_color; 476 477 SDL_RenderGeometryRaw(renderer, NULL, xy, xy_stride, color, color_stride, NULL, 0, num_vertices, indices, num_indices, size_indices); 478} 479 480static void RenderGradientDrawing(void) 481{ 482 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 483 SDL_RenderClear(renderer); 484 485 float x = TEXT_START_X; 486 float y = TEXT_START_Y; 487 DrawText(x, y, "%s %s", renderer_name, colorspace_name); 488 y += TEXT_LINE_ADVANCE; 489 DrawText(x, y, "Test: Draw SDR and HDR gradients"); 490 y += TEXT_LINE_ADVANCE; 491 492 y += TEXT_LINE_ADVANCE; 493 494 DrawText(x, y, "SDR gradient"); 495 y += TEXT_LINE_ADVANCE; 496 DrawGradient(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, 1.0f); 497 y += 64.0f; 498 499 y += TEXT_LINE_ADVANCE; 500 y += TEXT_LINE_ADVANCE; 501 502 if (HDR_headroom > 1.0f) { 503 DrawText(x, y, "HDR gradient"); 504 } else { 505 DrawText(x, y, "No HDR headroom, HDR and SDR gradient are the same"); 506 } 507 y += TEXT_LINE_ADVANCE; 508 /* Drawing is in the sRGB colorspace, so we need to use the color scale, which is applied in linear space, to get into high dynamic range */ 509 SDL_SetRenderColorScale(renderer, HDR_headroom); 510 DrawGradient(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, 1.0f); 511 SDL_SetRenderColorScale(renderer, 1.0f); 512 y += 64.0f; 513} 514 515static SDL_Texture *CreateGradientTexture(int width, float start, float end) 516{ 517 SDL_Texture *texture; 518 float *pixels; 519 520 /* Floating point textures are in the linear colorspace by default */ 521 texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA128_FLOAT, SDL_TEXTUREACCESS_STATIC, width, 1); 522 if (!texture) { 523 return NULL; 524 } 525 526 pixels = (float *)SDL_malloc(width * sizeof(float) * 4); 527 if (pixels) { 528 int i; 529 float length = (end - start); 530 531 for (i = 0; i < width; ++i) { 532 float v = (start + (length * i) / width); 533 pixels[i * 4 + 0] = v; 534 pixels[i * 4 + 1] = v; 535 pixels[i * 4 + 2] = v; 536 pixels[i * 4 + 3] = 1.0f; 537 } 538 SDL_UpdateTexture(texture, NULL, pixels, width * sizeof(float) * 4); 539 SDL_free(pixels); 540 } 541 return texture; 542} 543 544static void DrawGradientTexture(float x, float y, float width, float height, float start, float end) 545{ 546 SDL_FRect rect = { x, y, width, height }; 547 SDL_Texture *texture = CreateGradientTexture((int)width, start, end); 548 SDL_RenderTexture(renderer, texture, NULL, &rect); 549 SDL_DestroyTexture(texture); 550} 551 552static void RenderGradientTexture(void) 553{ 554 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 555 SDL_RenderClear(renderer); 556 557 float x = TEXT_START_X; 558 float y = TEXT_START_Y; 559 DrawText(x, y, "%s %s", renderer_name, colorspace_name); 560 y += TEXT_LINE_ADVANCE; 561 DrawText(x, y, "Test: Texture SDR and HDR gradients"); 562 y += TEXT_LINE_ADVANCE; 563 564 y += TEXT_LINE_ADVANCE; 565 566 DrawText(x, y, "SDR gradient"); 567 y += TEXT_LINE_ADVANCE; 568 DrawGradientTexture(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, 1.0f); 569 y += 64.0f; 570 571 y += TEXT_LINE_ADVANCE; 572 y += TEXT_LINE_ADVANCE; 573 574 if (HDR_headroom > 1.0f) { 575 DrawText(x, y, "HDR gradient"); 576 } else { 577 DrawText(x, y, "No HDR headroom, HDR and SDR gradient are the same"); 578 } 579 y += TEXT_LINE_ADVANCE; 580 /* The gradient texture is in the linear colorspace, so we can use the HDR_headroom value directly */ 581 DrawGradientTexture(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, HDR_headroom); 582 y += 64.0f; 583} 584 585SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 586{ 587 /* Check for events */ 588 if (event->type == SDL_EVENT_KEY_DOWN) { 589 switch (event->key.key) { 590 case SDLK_ESCAPE: 591 return SDL_APP_SUCCESS; 592 case SDLK_SPACE: 593 case SDLK_RIGHT: 594 NextStage(); 595 break; 596 case SDLK_LEFT: 597 PrevStage(); 598 break; 599 case SDLK_DOWN: 600 NextRenderer(); 601 break; 602 case SDLK_UP: 603 PrevRenderer(); 604 break; 605 default: 606 break; 607 } 608 } else if (event->type == SDL_EVENT_WINDOW_HDR_STATE_CHANGED) { 609 UpdateHDRState(); 610 } else if (event->type == SDL_EVENT_QUIT) { 611 return SDL_APP_SUCCESS; 612 } 613 return SDL_APP_CONTINUE; 614} 615 616SDL_AppResult SDL_AppIterate(void *appstate) 617{ 618 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 619 SDL_RenderClear(renderer); 620 621 switch (stage_index) { 622 case StageClearBackground: 623 RenderClearBackground(); 624 break; 625 case StageDrawBackground: 626 RenderDrawBackground(); 627 break; 628 case StageTextureBackground: 629 RenderTextureBackground(); 630 break; 631 case StageTargetBackground: 632 RenderTargetBackground(); 633 break; 634 case StageBlendDrawing: 635 RenderBlendDrawing(); 636 break; 637 case StageBlendTexture: 638 RenderBlendTexture(); 639 break; 640 case StageGradientDrawing: 641 RenderGradientDrawing(); 642 break; 643 case StageGradientTexture: 644 RenderGradientTexture(); 645 break; 646 } 647 648 SDL_RenderPresent(renderer); 649 SDL_Delay(100); 650 651 return SDL_APP_CONTINUE; 652} 653 654static void LogUsage(const char *argv0) 655{ 656 SDL_Log("Usage: %s [--renderer renderer] [--colorspace colorspace]", argv0); 657} 658 659SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 660{ 661 int i; 662 663 for (i = 1; i < argc; ++i) { 664 if (SDL_strcmp(argv[i], "--renderer") == 0) { 665 if (argv[i + 1]) { 666 renderer_name = argv[i + 1]; 667 ++i; 668 } else { 669 LogUsage(argv[0]); 670 return SDL_APP_FAILURE; 671 } 672 } else if (SDL_strcmp(argv[i], "--colorspace") == 0) { 673 if (argv[i + 1]) { 674 colorspace_name = argv[i + 1]; 675 if (SDL_strcasecmp(colorspace_name, "sRGB") == 0) { 676 colorspace = SDL_COLORSPACE_SRGB; 677 } else if (SDL_strcasecmp(colorspace_name, "linear") == 0) { 678 colorspace = SDL_COLORSPACE_SRGB_LINEAR; 679/* Not currently supported 680 } else if (SDL_strcasecmp(colorspace_name, "HDR10") == 0) { 681 colorspace = SDL_COLORSPACE_HDR10; 682*/ 683 } else { 684 SDL_Log("Unknown colorspace %s", argv[i + 1]); 685 return SDL_APP_FAILURE; 686 } 687 ++i; 688 } else { 689 LogUsage(argv[0]); 690 return SDL_APP_FAILURE; 691 } 692 } else { 693 LogUsage(argv[0]); 694 return SDL_APP_FAILURE; 695 } 696 } 697 698 window = SDL_CreateWindow("SDL colorspace test", WINDOW_WIDTH, WINDOW_HEIGHT, 0); 699 if (!window) { 700 SDL_Log("Couldn't create window: %s", SDL_GetError()); 701 return SDL_APP_FAILURE; 702 } 703 704 renderer_count = SDL_GetNumRenderDrivers(); 705 SDL_Log("There are %d render drivers:", renderer_count); 706 for (i = 0; i < renderer_count; ++i) { 707 const char *name = SDL_GetRenderDriver(i); 708 709 if (renderer_name && SDL_strcasecmp(renderer_name, name) == 0) { 710 renderer_index = i; 711 } 712 SDL_Log(" %s", name); 713 } 714 CreateRenderer(); 715 716 return SDL_APP_CONTINUE; 717} 718 719void SDL_AppQuit(void *appstate, SDL_AppResult result) 720{ 721 SDL_DestroyRenderer(renderer); 722 SDL_DestroyWindow(window); 723 SDL_Quit(); 724} 725[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.