SDL.cxx 974 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "SDL.hxx"
  2. #include <cassert>
  3. #include <sstream>
  4. SDLError::SDLError(std::string_view const context, std::source_location location)
  5. :std::runtime_error{(std::ostringstream{}
  6. << context << " at "
  7. << location.file_name() << ":" << location.line() << ":" << location.column()
  8. << " - " << SDL_GetError()).str()}
  9. {
  10. }
  11. SDL* SDL::instance_ = nullptr;
  12. SDL::SDL(std::uint32_t const flags)
  13. {
  14. assert(instance_==nullptr);
  15. if (SDL_Init(flags)!=0) {
  16. throw SDLError("Failed to initialize SDL");
  17. }
  18. SDL_Log("Initialized SDL successfully.");
  19. instance_ = this;
  20. }
  21. SDL::~SDL() noexcept
  22. {
  23. assert(instance_!=nullptr);
  24. SDL_Quit();
  25. SDL_Log("Shut down SDL successfully.");
  26. instance_ = nullptr;
  27. }
  28. SDL& SDL::instance() noexcept
  29. {
  30. assert(instance_!=nullptr);
  31. return *instance_;
  32. }
  33. SDL& SDL::require(std::uint32_t const flags) noexcept
  34. {
  35. assert((SDL_WasInit(flags) | SDL_INIT_NOPARACHUTE)==(flags | SDL_INIT_NOPARACHUTE));
  36. return instance();
  37. }