SDL.hxx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #pragma once
  2. #ifndef SNAKE_SDL_HXX
  3. #define SNAKE_SDL_HXX
  4. #include <SDL.h>
  5. #include <cstdint>
  6. #include <source_location>
  7. #include <stdexcept>
  8. #include <string_view>
  9. #include <vector>
  10. #include "NonCopyable.hxx"
  11. /**
  12. * @class SDLError
  13. * @brief Represents an SDL error.
  14. *
  15. * This class is a subclass of std::runtime_error and represents an error that occurred
  16. * during an SDL operation. It provides additional information such as the context where
  17. * the error occurred and the source location.
  18. */
  19. class SDLError final : public std::runtime_error {
  20. public:
  21. explicit SDLError(
  22. std::string_view message,
  23. std::source_location location = std::source_location::current()
  24. );
  25. };
  26. /**
  27. * @class SDL
  28. * @brief The SDL class is used to initialize and clean up the SDL library.
  29. *
  30. * This class provides a convenient way to initialize SDL and clean up resources when they are no longer needed.
  31. * Only one instance of this class can exist at a time.
  32. */
  33. class SDL final : private NonCopyable {
  34. public:
  35. explicit SDL(std::uint32_t flags = SDL_INIT_EVERYTHING);
  36. ~SDL() noexcept;
  37. static SDL& instance() noexcept;
  38. static SDL& require(std::uint32_t flags) noexcept;
  39. void add_controller(int which);
  40. void remove_controller(int which);
  41. private:
  42. static SDL* instance_;
  43. std::vector<SDL_GameController*> controllers_;
  44. };
  45. #endif // SNAKE_SDL_HXX