cliprect.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 *bmp_path = NULL;
  29. if (!SDL_Init(SDL_INIT_VIDEO)) {
  30. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
  31. return SDL_APP_FAILURE;
  32. }
  33. if (!SDL_CreateWindowAndRenderer("examples/renderer/cliprect", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
  34. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
  35. return SDL_APP_FAILURE;
  36. }
  37. cliprect_direction.x = cliprect_direction.y = 1.0f;
  38. last_time = SDL_GetTicks();
  39. /* Textures are pixel data that we upload to the video hardware for fast drawing. Lots of 2D
  40. engines refer to these as "sprites." We'll do a static texture (upload once, draw many
  41. times) with data from a bitmap file. */
  42. /* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
  43. Load a .bmp into a surface, move it to a texture from there. */
  44. SDL_asprintf(&bmp_path, "%ssample.bmp", SDL_GetBasePath()); /* allocate a string of the full file path */
  45. surface = SDL_LoadBMP(bmp_path);
  46. if (!surface) {
  47. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't load bitmap!", SDL_GetError(), NULL);
  48. return SDL_APP_FAILURE;
  49. }
  50. SDL_free(bmp_path); /* done with this, the file is loaded. */
  51. texture = SDL_CreateTextureFromSurface(renderer, surface);
  52. if (!texture) {
  53. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create static texture!", SDL_GetError(), NULL);
  54. return SDL_APP_FAILURE;
  55. }
  56. SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
  57. return SDL_APP_CONTINUE; /* carry on with the program! */
  58. }
  59. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  60. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  61. {
  62. if (event->type == SDL_EVENT_QUIT) {
  63. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  64. }
  65. return SDL_APP_CONTINUE; /* carry on with the program! */
  66. }
  67. /* This function runs once per frame, and is the heart of the program. */
  68. SDL_AppResult SDL_AppIterate(void *appstate)
  69. {
  70. const SDL_Rect cliprect = { (int) SDL_roundf(cliprect_position.x), (int) SDL_roundf(cliprect_position.y), CLIPRECT_SIZE, CLIPRECT_SIZE };
  71. const Uint64 now = SDL_GetTicks();
  72. const float elapsed = ((float) (now - last_time)) / 1000.0f; /* seconds since last iteration */
  73. const float distance = elapsed * CLIPRECT_SPEED;
  74. /* Set a new clipping rectangle position */
  75. cliprect_position.x += distance * cliprect_direction.x;
  76. if (cliprect_position.x < 0.0f) {
  77. cliprect_position.x = 0.0f;
  78. cliprect_direction.x = 1.0f;
  79. } else if (cliprect_position.x >= (WINDOW_WIDTH - CLIPRECT_SIZE)) {
  80. cliprect_position.x = (WINDOW_WIDTH - CLIPRECT_SIZE) - 1;
  81. cliprect_direction.x = -1.0f;
  82. }
  83. cliprect_position.y += distance * cliprect_direction.y;
  84. if (cliprect_position.y < 0.0f) {
  85. cliprect_position.y = 0.0f;
  86. cliprect_direction.y = 1.0f;
  87. } else if (cliprect_position.y >= (WINDOW_HEIGHT - CLIPRECT_SIZE)) {
  88. cliprect_position.y = (WINDOW_HEIGHT - CLIPRECT_SIZE) - 1;
  89. cliprect_direction.y = -1.0f;
  90. }
  91. SDL_SetRenderClipRect(renderer, &cliprect);
  92. last_time = now;
  93. /* okay, now draw! */
  94. /* Note that SDL_RenderClear is _not_ affected by the clipping rectangle! */
  95. SDL_SetRenderDrawColor(renderer, 33, 33, 33, 255); /* grey, full alpha */
  96. SDL_RenderClear(renderer); /* start with a blank canvas. */
  97. /* stretch the texture across the entire window. Only the piece in the
  98. clipping rectangle will actually render, though! */
  99. SDL_RenderTexture(renderer, texture, NULL, NULL);
  100. SDL_RenderPresent(renderer); /* put it all on the screen! */
  101. return SDL_APP_CONTINUE; /* carry on with the program! */
  102. }
  103. /* This function runs once at shutdown. */
  104. void SDL_AppQuit(void *appstate)
  105. {
  106. SDL_DestroyTexture(texture);
  107. /* SDL will clean up the window/renderer for us. */
  108. }