/**
* @file 01_hello_SDL.cpp
* @author Sam4uk (sam4uk.site@gmail.com)
* @date 2025-05-03
*
* @copyright Copyright © Sam4uk 2025 (Sam4uk.site@gmail.com)
*
*/
#if defined(__linux)
#include <SDL2/SDL.h>
#elif defined(WIN32)
#include <SDL.h>
#endif
#include <exception>
#include <iostream>
#include <memory>
#include <stdexcept>
/** Screen dimension constants */
const int SCREEN_WIDTH{640}, SCREEN_HEIGHT{480};
int main(int argc, char* args[]) try {
/** Initialize SDL */
struct SDL_Guard {
SDL_Guard(Uint32 flags) {
if (0 > SDL_Init(flags))
throw std::runtime_error("SDL could not initialize! SDL_Error:" +
std::string(SDL_GetError()));
}
~SDL_Guard() { SDL_Quit(); }
} sdl_guart(SDL_INIT_VIDEO);
/** The window we'll be rendering to */
std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> window{
SDL_CreateWindow("C++ SDL blank by Sam4uk", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN),
[](SDL_Window* w) {
if (w) SDL_DestroyWindow(w);
}};
if (!window)
throw std::runtime_error("Window could not be created! SDL_Error:\n" +
std::string(SDL_GetError()));
/** The surface contained by the window */
SDL_Surface* screenSurface{SDL_GetWindowSurface(window.get())};
/** Fill the surface white */
SDL_FillRect(screenSurface, NULL,
SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
SDL_Event e;
bool quit{false};
do {
// Update the surface
SDL_UpdateWindowSurface(window.get());
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
quit = true;
break;
default:
break;
}
}
} while (!quit);
return EXIT_SUCCESS;
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
}
catch (...) {
std::cout << "Something wrong\n";
return -EXIT_FAILURE;
}