SDL.hxx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. std::vector<SDL_GameController *> const& get_controllers() const;
  42. private:
  43. static SDL* instance_;
  44. std::vector<SDL_GameController*> controllers_;
  45. };
  46. #endif // SNAKE_SDL_HXX