streaming-textures.c 4.5 KB

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