Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <format>
- #include <stdexcept>
- #define SDL_MAIN_USE_CALLBACKS 1
- #include "SDL3/SDL.h"
- #include "SDL3/SDL_main.h"
- class Application
- {
- SDL_Window *window = nullptr;
- SDL_Renderer *renderer = nullptr;
- public:
- Application(int argc, char **argv)
- {
- SDL_SetAppMetadata("Example Renderer Clear", "1.0", "com.example.renderer-clear");
- if (!SDL_Init(SDL_INIT_VIDEO))
- {
- auto message = std::format("Couldn't initialize SDL: {}", SDL_GetError());
- throw std::runtime_error(message);
- }
- if (!SDL_CreateWindowAndRenderer("examples/renderer/clear", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer))
- {
- auto message = std::format("Couldn't create window/renderer: {}", SDL_GetError());
- throw std::runtime_error(message);
- }
- SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
- }
- SDL_AppResult processEvent(SDL_Event *event) noexcept
- {
- if (event->type == SDL_EVENT_QUIT)
- {
- return SDL_APP_SUCCESS;
- }
- return SDL_APP_CONTINUE;
- }
- SDL_AppResult renderFrame() noexcept
- {
- const double now = ((double)SDL_GetTicks()) / 1000.0;
- const float red = (float) (0.5 + 0.5 * SDL_sin(now));
- const float green = (float) (0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 2 / 3));
- const float blue = (float) (0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 4 / 3));
- SDL_SetRenderDrawColorFloat(renderer, red, green, blue, SDL_ALPHA_OPAQUE_FLOAT);
- SDL_RenderClear(renderer);
- SDL_RenderPresent(renderer);
- return SDL_APP_CONTINUE;
- }
- };
- extern "C"
- {
- SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
- {
- try
- {
- *appstate = nullptr;
- *appstate = reinterpret_cast<void *>(new Application(argc, argv));
- return SDL_APP_CONTINUE;
- }
- catch (...)
- {
- return SDL_APP_FAILURE;
- }
- }
- SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
- {
- return reinterpret_cast<Application *>(appstate)->processEvent(event);
- }
- SDL_AppResult SDL_AppIterate(void *appstate)
- {
- return reinterpret_cast<Application *>(appstate)->renderFrame();
- }
- /* This function runs once at shutdown. */
- void SDL_AppQuit(void *appstate, SDL_AppResult result)
- {
- delete reinterpret_cast<Application *>(appstate);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment