Struct SDL2¶
Defined in File SDL2.hpp
Struct Documentation¶
-
struct SDL2¶
SDL2 wrapper class for managing SDL2 window, OpenGL context, and events.
This struct encapsulates SDL2 initialization, window/context management, event polling, and image loading functionalities. It provides utility methods for error handling and OpenGL attribute configuration.
It also handles OpenGL context initialization using GLEW.
Public Functions
-
SDL2() = default¶
Constructs the SDL2 renderer context.
Initializes SDL2 with video support, sets OpenGL context attributes, creates the SDL window and OpenGL context, and initializes GLEW. Enables depth testing, face culling, and alpha blending for rendering. Also initializes SDL_image with PNG and JPG support.
- Throws:
std::runtime_error – if any initialization step fails, including:
SDL initialization
Window creation
OpenGL context creation
GLEW initialization
SDL_image initialization
-
~SDL2()¶
-
bool Init(size_t width = 800, size_t height = 600)¶
-
inline void SetWindowSize(size_t width, size_t height)¶
-
inline size_t GetWidth() const¶
-
inline size_t GetHeight() const¶
-
inline std::string GetError() const¶
-
inline std::string GetIMGError() const¶
-
inline void SwapWindow() const¶
Refreshes the screen by swapping the OpenGL buffers.
This function updates the display with the rendered image by swapping the front and back buffers of the SDL window.
-
inline void Clear(GLclampf r, GLclampf g, GLclampf b, GLclampf a) const¶
Clears the color and depth buffers with the specified color.
Sets the clear color using the provided RGBA values and clears both the color and depth buffers.
- Parameters:
r – Red component of the clear color (range: 0.0f to 1.0f).
g – Green component of the clear color (range: 0.0f to 1.0f).
b – Blue component of the clear color (range: 0.0f to 1.0f).
a – Alpha component of the clear color (range: 0.0f to 1.0f).
-
inline Uint64 GetTicks64() const¶
Retrieves the number of milliseconds since the SDL library initialization.
This function returns the number of milliseconds, as a 64-bit unsigned integer, that have elapsed since the SDL library was initialized. It is a wrapper around SDL_GetTicks64(), providing higher precision and avoiding overflow issues present in the 32-bit version.
- Returns:
The number of milliseconds since SDL initialization as a Uint64.
-
inline bool PollEvent()¶
Polls for currently pending events.
This function checks the event queue for pending events and updates the internal SDL_Event structure. It returns true if there is an event available, false otherwise.
Note
This function does not remove the event from the queue.
- Returns:
true if there is a pending event, false otherwise.
-
inline const SDL_Event &GetEvent() const¶
Retrieves the current SDL event.
This function returns a constant reference to the internally stored SDL_Event object, allowing read-only access to the most recent event polled from the SDL event queue.
- Returns:
const SDL_Event& A constant reference to the current SDL_Event.
-
inline std::unique_ptr<SDL_Surface> IMGLoad(const std::string &s) const¶
Loads an image from the specified file path into an SDL_Surface.
This function uses SDL_image’s IMG_Load to load an image file and returns a unique_ptr managing the resulting SDL_Surface. The caller is responsible for ensuring the file path is valid and that SDL_image is properly initialized.
- Parameters:
s – The file path to the image to load.
- Returns:
std::unique_ptr<SDL_Surface> A unique pointer to the loaded SDL_Surface, or nullptr if loading fails.
-
SDL2() = default¶