Guest User

Untitled

a guest
Feb 18th, 2026
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | Source Code | 0 0
  1. #include <format>
  2. #include <stdexcept>
  3.  
  4. #define SDL_MAIN_USE_CALLBACKS 1
  5. #include "SDL3/SDL.h"
  6. #include "SDL3/SDL_main.h"
  7.  
  8. class Application
  9. {
  10.     SDL_Window *window = nullptr;
  11.     SDL_Renderer *renderer = nullptr;
  12.  
  13. public:
  14.     Application(int argc, char **argv)
  15.     {
  16.         SDL_SetAppMetadata("Example Renderer Clear", "1.0", "com.example.renderer-clear");
  17.  
  18.         if (!SDL_Init(SDL_INIT_VIDEO))
  19.         {
  20.             auto message = std::format("Couldn't initialize SDL: {}", SDL_GetError());
  21.             throw std::runtime_error(message);
  22.         }
  23.  
  24.         if (!SDL_CreateWindowAndRenderer("examples/renderer/clear", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer))
  25.         {
  26.             auto message = std::format("Couldn't create window/renderer: {}", SDL_GetError());
  27.             throw std::runtime_error(message);
  28.         }
  29.  
  30.         SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
  31.     }
  32.  
  33.     SDL_AppResult processEvent(SDL_Event *event) noexcept
  34.     {
  35.         if (event->type == SDL_EVENT_QUIT)
  36.         {
  37.             return SDL_APP_SUCCESS;
  38.         }
  39.  
  40.         return SDL_APP_CONTINUE;
  41.     }
  42.  
  43.     SDL_AppResult renderFrame() noexcept
  44.     {
  45.         const double now = ((double)SDL_GetTicks()) / 1000.0;
  46.         const float red = (float) (0.5 + 0.5 * SDL_sin(now));
  47.         const float green = (float) (0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 2 / 3));
  48.         const float blue = (float) (0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 4 / 3));
  49.         SDL_SetRenderDrawColorFloat(renderer, red, green, blue, SDL_ALPHA_OPAQUE_FLOAT);
  50.  
  51.         SDL_RenderClear(renderer);
  52.  
  53.         SDL_RenderPresent(renderer);
  54.  
  55.         return SDL_APP_CONTINUE;
  56.     }
  57. };
  58.  
  59. extern "C"
  60. {
  61.  
  62. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  63. {
  64.     try
  65.     {
  66.         *appstate = nullptr;
  67.         *appstate = reinterpret_cast<void *>(new Application(argc, argv));
  68.  
  69.         return SDL_APP_CONTINUE;
  70.     }
  71.     catch (...)
  72.     {
  73.         return SDL_APP_FAILURE;
  74.     }
  75. }
  76.  
  77. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  78. {
  79.     return reinterpret_cast<Application *>(appstate)->processEvent(event);
  80. }
  81.  
  82. SDL_AppResult SDL_AppIterate(void *appstate)
  83. {
  84.     return reinterpret_cast<Application *>(appstate)->renderFrame();
  85. }
  86.  
  87. /* This function runs once at shutdown. */
  88. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  89. {
  90.     delete reinterpret_cast<Application *>(appstate);
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment