Atlas - streaming-textures.c

Home / ext / SDL / examples / renderer / 07-streaming-textures Lines: 1 | Size: 4740 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
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, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 33 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 34 return SDL_APP_FAILURE; 35 } 36 SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX); 37 38 texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, TEXTURE_SIZE, TEXTURE_SIZE); 39 if (!texture) { 40 SDL_Log("Couldn't create streaming texture: %s", SDL_GetError()); 41 return SDL_APP_FAILURE; 42 } 43 44 return SDL_APP_CONTINUE; /* carry on with the program! */ 45} 46 47/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 48SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 49{ 50 if (event->type == SDL_EVENT_QUIT) { 51 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 52 } 53 return SDL_APP_CONTINUE; /* carry on with the program! */ 54} 55 56/* This function runs once per frame, and is the heart of the program. */ 57SDL_AppResult SDL_AppIterate(void *appstate) 58{ 59 SDL_FRect dst_rect; 60 const Uint64 now = SDL_GetTicks(); 61 SDL_Surface *surface = NULL; 62 63 /* we'll have some color move around over a few seconds. */ 64 const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f; 65 const float scale = ((float) (((int) (now % 1000)) - 500) / 500.0f) * direction; 66 67 /* To update a streaming texture, you need to lock it first. This gets you access to the pixels. 68 Note that this is considered a _write-only_ operation: the buffer you get from locking 69 might not acutally have the existing contents of the texture, and you have to write to every 70 locked pixel! */ 71 72 /* You can use SDL_LockTexture() to get an array of raw pixels, but we're going to use 73 SDL_LockTextureToSurface() here, because it wraps that array in a temporary SDL_Surface, 74 letting us use the surface drawing functions instead of lighting up individual pixels. */ 75 if (SDL_LockTextureToSurface(texture, NULL, &surface)) { 76 SDL_Rect r; 77 SDL_FillSurfaceRect(surface, NULL, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, 0, 0, 0)); /* make the whole surface black */ 78 r.w = TEXTURE_SIZE; 79 r.h = TEXTURE_SIZE / 10; 80 r.x = 0; 81 r.y = (int) (((float) (TEXTURE_SIZE - r.h)) * ((scale + 1.0f) / 2.0f)); 82 SDL_FillSurfaceRect(surface, &r, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, 0, 255, 0)); /* make a strip of the surface green */ 83 SDL_UnlockTexture(texture); /* upload the changes (and frees the temporary surface)! */ 84 } 85 86 /* as you can see from this, rendering draws over whatever was drawn before it. */ 87 SDL_SetRenderDrawColor(renderer, 66, 66, 66, SDL_ALPHA_OPAQUE); /* grey, full alpha */ 88 SDL_RenderClear(renderer); /* start with a blank canvas. */ 89 90 /* Just draw the static texture a few times. You can think of it like a 91 stamp, there isn't a limit to the number of times you can draw with it. */ 92 93 /* Center this one. It'll draw the latest version of the texture we drew while it was locked. */ 94 dst_rect.x = ((float) (WINDOW_WIDTH - TEXTURE_SIZE)) / 2.0f; 95 dst_rect.y = ((float) (WINDOW_HEIGHT - TEXTURE_SIZE)) / 2.0f; 96 dst_rect.w = dst_rect.h = (float) TEXTURE_SIZE; 97 SDL_RenderTexture(renderer, texture, NULL, &dst_rect); 98 99 SDL_RenderPresent(renderer); /* put it all on the screen! */ 100 101 return SDL_APP_CONTINUE; /* carry on with the program! */ 102} 103 104/* This function runs once at shutdown. */ 105void SDL_AppQuit(void *appstate, SDL_AppResult result) 106{ 107 SDL_DestroyTexture(texture); 108 /* SDL will clean up the window/renderer for us. */ 109} 110 111
[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.