cliprect.c 5.1 KB

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