SDL: examples: Added renderer/07-streaming-textures

From 0c7334cce39703b0b5a8e3754a4fd3f377fccbf4 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Sat, 21 Sep 2024 20:39:48 -0400
Subject: [PATCH] examples: Added renderer/07-streaming-textures

---
 examples/CMakeLists.txt                       |   1 +
 .../renderer/07-streaming-textures/README.txt |   3 +
 .../renderer-streaming-textures.c             | 106 ++++++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100644 examples/renderer/07-streaming-textures/README.txt
 create mode 100644 examples/renderer/07-streaming-textures/renderer-streaming-textures.c

diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index ec38246fab16d..78bbd78d09428 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -126,6 +126,7 @@ add_sdl_example_executable(renderer-lines SOURCES renderer/03-lines/renderer-lin
 add_sdl_example_executable(renderer-points SOURCES renderer/04-points/renderer-points.c)
 add_sdl_example_executable(renderer-rectangles SOURCES renderer/05-rectangles/renderer-rectangles.c)
 add_sdl_example_executable(renderer-textures SOURCES renderer/06-textures/renderer-textures.c)
+add_sdl_example_executable(renderer-streaming-textures SOURCES renderer/07-streaming-textures/renderer-streaming-textures.c)
 add_sdl_example_executable(audio-simple-playback SOURCES audio/01-simple-playback/simple-playback.c)
 add_sdl_example_executable(audio-simple-playback-callback SOURCES audio/02-simple-playback-callback/simple-playback-callback.c)
 add_sdl_example_executable(audio-load-wav SOURCES audio/03-load-wav/load-wav.c DATAFILES ${CMAKE_CURRENT_SOURCE_DIR}/../test/sample.wav)
diff --git a/examples/renderer/07-streaming-textures/README.txt b/examples/renderer/07-streaming-textures/README.txt
new file mode 100644
index 0000000000000..c2505719af3a0
--- /dev/null
+++ b/examples/renderer/07-streaming-textures/README.txt
@@ -0,0 +1,3 @@
+This example creates an SDL window and renderer, then a streaming texture that
+it will update every frame before drawing it to the screen.
+
diff --git a/examples/renderer/07-streaming-textures/renderer-streaming-textures.c b/examples/renderer/07-streaming-textures/renderer-streaming-textures.c
new file mode 100644
index 0000000000000..994efd7718cab
--- /dev/null
+++ b/examples/renderer/07-streaming-textures/renderer-streaming-textures.c
@@ -0,0 +1,106 @@
+/*
+ * This example creates an SDL window and renderer, and then draws a streaming
+ * texture to it every frame.
+ *
+ * This code is public domain. Feel free to use it for any purpose!
+ */
+
+#define SDL_MAIN_USE_CALLBACKS 1  /* use the callbacks instead of main() */
+#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;
+static SDL_Texture *texture = NULL;
+
+#define TEXTURE_SIZE 150
+
+#define WINDOW_WIDTH 640
+#define WINDOW_HEIGHT 480
+
+/* This function runs once at startup. */
+SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
+{
+    if (!SDL_Init(SDL_INIT_VIDEO)) {
+        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
+        return SDL_APP_FAILURE;
+    }
+
+    if (!SDL_CreateWindowAndRenderer("examples/renderer/streaming-textures", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
+        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
+        return SDL_APP_FAILURE;
+    }
+
+    texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, TEXTURE_SIZE, TEXTURE_SIZE);
+    if (!texture) {
+        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create streaming texture!", SDL_GetError(), NULL);
+        return SDL_APP_FAILURE;
+    }
+
+    return SDL_APP_CONTINUE;  /* carry on with the program! */
+}
+
+/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
+SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
+{
+    if (event->type == SDL_EVENT_QUIT) {
+        return SDL_APP_SUCCESS;  /* end the program, reporting success to the OS. */
+    }
+    return SDL_APP_CONTINUE;  /* carry on with the program! */
+}
+
+/* This function runs once per frame, and is the heart of the program. */
+SDL_AppResult SDL_AppIterate(void *appstate)
+{
+    SDL_FRect dst_rect;
+    const Uint64 now = SDL_GetTicks();
+    SDL_Surface *surface = NULL;
+
+    /* we'll have some color move around over a few seconds. */
+    const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f;
+    const float scale = ((float) (((int) (now % 1000)) - 500) / 500.0f) * direction;
+
+    /* To update a streaming texture, you need to lock it first. This gets you access to the pixels.
+       Note that this is considered a _write-only_ operation: the buffer you get from locking
+       might not acutally have the existing contents of the texture, and you have to write to every
+       locked pixel! */
+
+    /* You can use SDL_LockTexture() to get an array of raw pixels, but we're going to use
+       SDL_LockTextureToSurface() here, because it wraps that array in a temporary SDL_Surface,
+       letting us use the surface drawing functions instead of lighting up individual pixels. */
+    if (SDL_LockTextureToSurface(texture, NULL, &surface)) {
+        SDL_Rect r;
+        SDL_FillSurfaceRect(surface, NULL, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, 0, 0, 0));  /* make the whole surface black */
+        r.w = TEXTURE_SIZE;
+        r.h = TEXTURE_SIZE / 10;
+        r.x = 0;
+        r.y = (int) (((float) (TEXTURE_SIZE - r.h)) * ((scale + 1.0f) / 2.0f));
+        SDL_FillSurfaceRect(surface, &r, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, 0, 255, 0));  /* make a strip of the surface green */
+        SDL_UnlockTexture(texture);  /* upload the changes (and frees the temporary surface)! */
+    }
+
+    /* as you can see from this, rendering draws over whatever was drawn before it. */
+    SDL_SetRenderDrawColor(renderer, 66, 66, 66, 255);  /* grey, full alpha */
+    SDL_RenderClear(renderer);  /* start with a blank canvas. */
+
+    /* Just draw the static texture a few times. You can think of it like a
+       stamp, there isn't a limit to the number of times you can draw with it. */
+
+    /* Center this one. It'll draw the latest version of the texture we drew while it was locked. */
+    dst_rect.x = ((float) (WINDOW_WIDTH - TEXTURE_SIZE)) / 2.0f;
+    dst_rect.y = ((float) (WINDOW_HEIGHT - TEXTURE_SIZE)) / 2.0f;
+    dst_rect.w = dst_rect.h = (float) TEXTURE_SIZE;
+    SDL_RenderTexture(renderer, texture, NULL, &dst_rect);
+
+    SDL_RenderPresent(renderer);  /* put it all on the screen! */
+
+    return SDL_APP_CONTINUE;  /* carry on with the program! */
+}
+
+/* This function runs once at shutdown. */
+void SDL_AppQuit(void *appstate)
+{
+    /* SDL will clean up the window/renderer for us. */
+}
+