SDL.cxx 999 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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()
  9. ).str()}
  10. {
  11. }
  12. SDL* SDL::instance_ = nullptr;
  13. SDL::SDL(std::uint32_t const flags)
  14. {
  15. assert(instance_==nullptr);
  16. if (SDL_Init(flags)!=0) {
  17. throw SDLError("Failed to initialize SDL");
  18. }
  19. SDL_Log("Initialized SDL successfully.");
  20. instance_ = this;
  21. }
  22. SDL::~SDL() noexcept
  23. {
  24. assert(instance_!=nullptr);
  25. SDL_Quit();
  26. SDL_Log("Shut down SDL successfully.");
  27. instance_ = nullptr;
  28. }
  29. SDL& SDL::instance() noexcept
  30. {
  31. assert(instance_!=nullptr);
  32. return *instance_;
  33. }
  34. SDL& SDL::require(std::uint32_t const flags) noexcept
  35. {
  36. assert((SDL_WasInit(flags) | SDL_INIT_NOPARACHUTE)==(flags | SDL_INIT_NOPARACHUTE));
  37. return instance();
  38. }