SDL: examples/renderer/01-clear: Use the color-cycle code from testvulkan.c

From 1828bde49f7cca0782686dabe6abe7c52bd5b201 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Sun, 22 Sep 2024 16:10:57 -0400
Subject: [PATCH] examples/renderer/01-clear: Use the color-cycle code from
 testvulkan.c

(and testgpu_simple_clear.c, of course!)
---
 examples/renderer/01-clear/clear.c | 31 ++++++------------------------
 test/testgpu_simple_clear.c        |  2 +-
 2 files changed, 7 insertions(+), 26 deletions(-)

diff --git a/examples/renderer/01-clear/clear.c b/examples/renderer/01-clear/clear.c
index e678f1ccb4321..322c28d46423c 100644
--- a/examples/renderer/01-clear/clear.c
+++ b/examples/renderer/01-clear/clear.c
@@ -10,18 +10,10 @@
 #include <SDL3/SDL.h>
 #include <SDL3/SDL_main.h>
 
-
 /* We will use this renderer to draw into this window every frame. */
 static SDL_Window *window = NULL;
 static SDL_Renderer *renderer = NULL;
 
-/* the current red color we're clearing to. */
-static Uint8 red = 0;
-
-/* When fading up, this is 1, when fading down, it's -1. */
-static int fade_direction = 1;
-
-
 /* This function runs once at startup. */
 SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
 {
@@ -50,9 +42,12 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
 /* This function runs once per frame, and is the heart of the program. */
 SDL_AppResult SDL_AppIterate(void *appstate)
 {
-    /* since we're always fading red, we leave green and blue at zero.
-       alpha doesn't mean much here, so leave it at full (255, no transparency). */
-    SDL_SetRenderDrawColor(renderer, red, 0, 0, 255);
+    const double now = ((double)SDL_GetTicks()) / 1000.0;  /* convert from milliseconds to seconds. */
+    /* choose the color for the frame we will draw. The sine wave trick makes it fade between colors smoothly. */
+    const float red = (float) (0.5 + 0.5 * SDL_sin(now));
+    const float green = (float) (0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 2 / 3));
+    const float blue = (float) (0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 4 / 3));
+    SDL_SetRenderDrawColorFloat(renderer, red, green, blue, 1.0f);  /* new color, full alpha. */
 
     /* clear the window to the draw color. */
     SDL_RenderClear(renderer);
@@ -60,20 +55,6 @@ SDL_AppResult SDL_AppIterate(void *appstate)
     /* put the newly-cleared rendering on the screen. */
     SDL_RenderPresent(renderer);
 
-    /* update the color for the next frame we will draw. */
-    if (fade_direction > 0) {
-        if (red == 255) {
-            fade_direction = -1;
-        } else {
-            red++;
-        }
-    } else if (fade_direction < 0) {
-        if (red == 0) {
-            fade_direction = 1;
-        } else {
-            red--;
-        }
-    }
     return SDL_APP_CONTINUE;  /* carry on with the program! */
 }
 
diff --git a/test/testgpu_simple_clear.c b/test/testgpu_simple_clear.c
index 21e71b79b9117..19017b92f1f06 100644
--- a/test/testgpu_simple_clear.c
+++ b/test/testgpu_simple_clear.c
@@ -91,7 +91,7 @@ SDL_AppResult SDL_AppIterate(void *appstate)
 		color_target_info.texture = swapchainTexture;
 		color_target_info.clear_color.r = (float)(0.5 + 0.5 * SDL_sin(currentTime));
 		color_target_info.clear_color.g = (float)(0.5 + 0.5 * SDL_sin(currentTime + SDL_PI_D * 2 / 3));
-		color_target_info.clear_color.b = (float)(0.5 + 0.5 * SDL_sin(currentTime + SDL_PI_D * 4 / 3));;
+		color_target_info.clear_color.b = (float)(0.5 + 0.5 * SDL_sin(currentTime + SDL_PI_D * 4 / 3));
 		color_target_info.clear_color.a = 1.0f;
 		color_target_info.load_op = SDL_GPU_LOADOP_CLEAR;
 		color_target_info.store_op = SDL_GPU_STOREOP_STORE;