cliprect.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * This example creates an SDL window and renderer, and then draws a scene
  3. * to it every frame, while sliding around a clipping rectangle.
  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. #define WINDOW_WIDTH 640
  11. #define WINDOW_HEIGHT 480
  12. #define CLIPRECT_SIZE 250
  13. #define CLIPRECT_SPEED 200 /* pixels per second */
  14. /* We will use this renderer to draw into this window every frame. */
  15. static SDL_Window *window = NULL;
  16. static SDL_Renderer *renderer = NULL;
  17. static SDL_Texture *texture = NULL;
  18. static SDL_FPoint cliprect_position;
  19. static SDL_FPoint cliprect_direction;
  20. static Uint64 last_time = 0;
  21. /* A lot of this program is examples/renderer/02-primitives, so we have a good
  22. visual that we can slide a clip rect around. The actual new magic in here
  23. is the SDL_SetRenderClipRect() function. */
  24. /* This function runs once at startup. */
  25. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  26. {
  27. SDL_Surface *surface = NULL;
  28. char *png_path = NULL;
  29. SDL_SetAppMetadata("Example Renderer Clipping Rectangle", "1.0", "com.example.renderer-cliprect");
  30. if (!SDL_Init(SDL_INIT_VIDEO)) {
  31. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  32. return SDL_APP_FAILURE;
  33. }
  34. if (!SDL_CreateWindowAndRenderer("examples/renderer/cliprect", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
  35. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  36. return SDL_APP_FAILURE;
  37. }
  38. SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX);
  39. cliprect_direction.x = cliprect_direction.y = 1.0f;
  40. last_time = SDL_GetTicks();
  41. /* Textures are pixel data that we upload to the video hardware for fast drawing. Lots of 2D
  42. engines refer to these as "sprites." We'll do a static texture (upload once, draw many
  43. times) with data from a bitmap file. */
  44. /* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
  45. Load a .png into a surface, move it to a texture from there. */
  46. SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */
  47. surface = SDL_LoadPNG(png_path);
  48. if (!surface) {
  49. SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
  50. return SDL_APP_FAILURE;
  51. }
  52. SDL_free(png_path); /* done with this, the file is loaded. */
  53. texture = SDL_CreateTextureFromSurface(renderer, surface);
  54. if (!texture) {
  55. SDL_Log("Couldn't create static texture: %s", SDL_GetError());
  56. return SDL_APP_FAILURE;
  57. }
  58. SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
  59. return SDL_APP_CONTINUE; /* carry on with the program! */
  60. }
  61. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  62. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  63. {
  64. if (event->type == SDL_EVENT_QUIT) {
  65. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  66. }
  67. return SDL_APP_CONTINUE; /* carry on with the program! */
  68. }
  69. /* This function runs once per frame, and is the heart of the program. */
  70. SDL_AppResult SDL_AppIterate(void *appstate)
  71. {
  72. const SDL_Rect cliprect = { (int) SDL_roundf(cliprect_position.x), (int) SDL_roundf(cliprect_position.y), CLIPRECT_SIZE, CLIPRECT_SIZE };
  73. const Uint64 now = SDL_GetTicks();
  74. const float elapsed = ((float) (now - last_time)) / 1000.0f; /* seconds since last iteration */
  75. const float distance = elapsed * CLIPRECT_SPEED;
  76. /* Set a new clipping rectangle position */
  77. cliprect_position.x += distance * cliprect_direction.x;
  78. if (cliprect_position.x < -CLIPRECT_SIZE) {
  79. cliprect_position.x = -CLIPRECT_SIZE;
  80. cliprect_direction.x = 1.0f;
  81. } else if (cliprect_position.x >= WINDOW_WIDTH) {
  82. cliprect_position.x = WINDOW_WIDTH - 1;
  83. cliprect_direction.x = -1.0f;
  84. }
  85. cliprect_position.y += distance * cliprect_direction.y;
  86. if (cliprect_position.y < -CLIPRECT_SIZE) {
  87. cliprect_position.y = -CLIPRECT_SIZE;
  88. cliprect_direction.y = 1.0f;
  89. } else if (cliprect_position.y >= WINDOW_HEIGHT) {
  90. cliprect_position.y = WINDOW_HEIGHT - 1;
  91. cliprect_direction.y = -1.0f;
  92. }
  93. SDL_SetRenderClipRect(renderer, &cliprect);
  94. last_time = now;
  95. /* okay, now draw! */
  96. /* Note that SDL_RenderClear is _not_ affected by the clipping rectangle! */
  97. SDL_SetRenderDrawColor(renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); /* grey, full alpha */
  98. SDL_RenderClear(renderer); /* start with a blank canvas. */
  99. /* stretch the texture across the entire window. Only the piece in the
  100. clipping rectangle will actually render, though! */
  101. SDL_RenderTexture(renderer, texture, NULL, NULL);
  102. SDL_RenderPresent(renderer); /* put it all on the screen! */
  103. return SDL_APP_CONTINUE; /* carry on with the program! */
  104. }
  105. /* This function runs once at shutdown. */
  106. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  107. {
  108. SDL_DestroyTexture(texture);
  109. /* SDL will clean up the window/renderer for us. */
  110. }