streaming-textures.c 4.6 KB

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