summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/examples/renderer/07-streaming-textures/streaming-textures.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/examples/renderer/07-streaming-textures/streaming-textures.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/examples/renderer/07-streaming-textures/streaming-textures.c')
-rw-r--r--src/contrib/SDL-3.2.20/examples/renderer/07-streaming-textures/streaming-textures.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/examples/renderer/07-streaming-textures/streaming-textures.c b/src/contrib/SDL-3.2.20/examples/renderer/07-streaming-textures/streaming-textures.c
new file mode 100644
index 0000000..bf309bc
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/examples/renderer/07-streaming-textures/streaming-textures.c
@@ -0,0 +1,109 @@
1/*
2 * This example creates an SDL window and renderer, and then draws a streaming
3 * texture to it every frame.
4 *
5 * This code is public domain. Feel free to use it for any purpose!
6 */
7
8#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
9#include <SDL3/SDL.h>
10#include <SDL3/SDL_main.h>
11
12/* We will use this renderer to draw into this window every frame. */
13static SDL_Window *window = NULL;
14static SDL_Renderer *renderer = NULL;
15static SDL_Texture *texture = NULL;
16
17#define TEXTURE_SIZE 150
18
19#define WINDOW_WIDTH 640
20#define WINDOW_HEIGHT 480
21
22/* This function runs once at startup. */
23SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
24{
25 SDL_SetAppMetadata("Example Renderer Streaming Textures", "1.0", "com.example.renderer-streaming-textures");
26
27 if (!SDL_Init(SDL_INIT_VIDEO)) {
28 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
29 return SDL_APP_FAILURE;
30 }
31
32 if (!SDL_CreateWindowAndRenderer("examples/renderer/streaming-textures", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
33 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
34 return SDL_APP_FAILURE;
35 }
36
37 texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, TEXTURE_SIZE, TEXTURE_SIZE);
38 if (!texture) {
39 SDL_Log("Couldn't create streaming texture: %s", SDL_GetError());
40 return SDL_APP_FAILURE;
41 }
42
43 return SDL_APP_CONTINUE; /* carry on with the program! */
44}
45
46/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
47SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
48{
49 if (event->type == SDL_EVENT_QUIT) {
50 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
51 }
52 return SDL_APP_CONTINUE; /* carry on with the program! */
53}
54
55/* This function runs once per frame, and is the heart of the program. */
56SDL_AppResult SDL_AppIterate(void *appstate)
57{
58 SDL_FRect dst_rect;
59 const Uint64 now = SDL_GetTicks();
60 SDL_Surface *surface = NULL;
61
62 /* we'll have some color move around over a few seconds. */
63 const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f;
64 const float scale = ((float) (((int) (now % 1000)) - 500) / 500.0f) * direction;
65
66 /* To update a streaming texture, you need to lock it first. This gets you access to the pixels.
67 Note that this is considered a _write-only_ operation: the buffer you get from locking
68 might not acutally have the existing contents of the texture, and you have to write to every
69 locked pixel! */
70
71 /* You can use SDL_LockTexture() to get an array of raw pixels, but we're going to use
72 SDL_LockTextureToSurface() here, because it wraps that array in a temporary SDL_Surface,
73 letting us use the surface drawing functions instead of lighting up individual pixels. */
74 if (SDL_LockTextureToSurface(texture, NULL, &surface)) {
75 SDL_Rect r;
76 SDL_FillSurfaceRect(surface, NULL, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, 0, 0, 0)); /* make the whole surface black */
77 r.w = TEXTURE_SIZE;
78 r.h = TEXTURE_SIZE / 10;
79 r.x = 0;
80 r.y = (int) (((float) (TEXTURE_SIZE - r.h)) * ((scale + 1.0f) / 2.0f));
81 SDL_FillSurfaceRect(surface, &r, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, 0, 255, 0)); /* make a strip of the surface green */
82 SDL_UnlockTexture(texture); /* upload the changes (and frees the temporary surface)! */
83 }
84
85 /* as you can see from this, rendering draws over whatever was drawn before it. */
86 SDL_SetRenderDrawColor(renderer, 66, 66, 66, SDL_ALPHA_OPAQUE); /* grey, full alpha */
87 SDL_RenderClear(renderer); /* start with a blank canvas. */
88
89 /* Just draw the static texture a few times. You can think of it like a
90 stamp, there isn't a limit to the number of times you can draw with it. */
91
92 /* Center this one. It'll draw the latest version of the texture we drew while it was locked. */
93 dst_rect.x = ((float) (WINDOW_WIDTH - TEXTURE_SIZE)) / 2.0f;
94 dst_rect.y = ((float) (WINDOW_HEIGHT - TEXTURE_SIZE)) / 2.0f;
95 dst_rect.w = dst_rect.h = (float) TEXTURE_SIZE;
96 SDL_RenderTexture(renderer, texture, NULL, &dst_rect);
97
98 SDL_RenderPresent(renderer); /* put it all on the screen! */
99
100 return SDL_APP_CONTINUE; /* carry on with the program! */
101}
102
103/* This function runs once at shutdown. */
104void SDL_AppQuit(void *appstate, SDL_AppResult result)
105{
106 SDL_DestroyTexture(texture);
107 /* SDL will clean up the window/renderer for us. */
108}
109