streaming-textures.c 4.5 KB

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