Add wrappers for SDL functions and structs
This commit is contained in:
parent
4277f4c818
commit
d180b8a5b4
18 changed files with 410 additions and 206 deletions
11
src/wrappers/sdl.cppm
Normal file
11
src/wrappers/sdl.cppm
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
module;
|
||||
|
||||
export module wrappers.sdl;
|
||||
|
||||
// Export submodules
|
||||
export import wrappers.sdl.error;
|
||||
export import wrappers.sdl.events;
|
||||
export import wrappers.sdl.init;
|
||||
export import wrappers.sdl.rect;
|
||||
export import wrappers.sdl.render;
|
||||
export import wrappers.sdl.video;
|
||||
24
src/wrappers/sdl/error.cppm
Normal file
24
src/wrappers/sdl/error.cppm
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
module;
|
||||
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module wrappers.sdl.error;
|
||||
|
||||
export namespace sdl
|
||||
{
|
||||
// Exception that wraps an SDL error (which SDL function caused the error, what's the error).
|
||||
// SDL_GetError() is used in the constructor to get the error message unless specified explicitly.
|
||||
class SDLException final : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
explicit SDLException(const std::string& sdl_func, const std::string& sdl_error)
|
||||
: runtime_error(std::format("Error in SDL function {}: {}", sdl_func, sdl_error))
|
||||
{}
|
||||
|
||||
explicit SDLException(const std::string& sdl_func)
|
||||
: SDLException(sdl_func, SDL_GetError())
|
||||
{}
|
||||
};
|
||||
}
|
||||
11
src/wrappers/sdl/events.cppm
Normal file
11
src/wrappers/sdl/events.cppm
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
module;
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module wrappers.sdl.events;
|
||||
|
||||
export namespace sdl
|
||||
{
|
||||
// Simple alias for SDL_Event union
|
||||
using Event = SDL_Event;
|
||||
}
|
||||
62
src/wrappers/sdl/init.cppm
Normal file
62
src/wrappers/sdl/init.cppm
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
module;
|
||||
|
||||
#include <type_traits>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module wrappers.sdl.init;
|
||||
|
||||
import wrappers.sdl.error;
|
||||
|
||||
export namespace sdl
|
||||
{
|
||||
using InitFlags_t = SDL_InitFlags;
|
||||
|
||||
/**
|
||||
* Wrapper for the SDL_InitFlags enum/constants.
|
||||
*
|
||||
* We're using a namespace here to emulate an enum-like interface. If we used an actual enum, we would have to
|
||||
* explicitly define bit-wise operations for it (as well as for every other kind of bit flag enum.
|
||||
* (Maybe in the future this can be replaced with a more elegant template-based solution or something.)
|
||||
*/
|
||||
namespace InitFlags
|
||||
{
|
||||
constexpr InitFlags_t Audio = SDL_INIT_AUDIO;
|
||||
constexpr InitFlags_t Video = SDL_INIT_VIDEO;
|
||||
constexpr InitFlags_t Joystick = SDL_INIT_JOYSTICK;
|
||||
constexpr InitFlags_t Haptic = SDL_INIT_HAPTIC;
|
||||
constexpr InitFlags_t Gamepad = SDL_INIT_GAMEPAD;
|
||||
constexpr InitFlags_t Events = SDL_INIT_EVENTS;
|
||||
constexpr InitFlags_t Sensor = SDL_INIT_SENSOR;
|
||||
constexpr InitFlags_t Camera = SDL_INIT_CAMERA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper around the SDL_AppResult enum.
|
||||
*/
|
||||
enum class AppResult : std::underlying_type_t<SDL_AppResult>
|
||||
{
|
||||
Continue = SDL_APP_CONTINUE,
|
||||
Success = SDL_APP_SUCCESS,
|
||||
Failure = SDL_APP_FAILURE,
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrapper around SDL_Init().
|
||||
*/
|
||||
constexpr void Init(const InitFlags_t flags)
|
||||
{
|
||||
if (!SDL_Init(flags)) {
|
||||
throw SDLException("SDL_Init");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper around SDL_SetAppMetadataProperty().
|
||||
*/
|
||||
constexpr void SetAppMetadataProperty(const char* property_name, const char* value)
|
||||
{
|
||||
if (!SDL_SetAppMetadataProperty(property_name, value)) {
|
||||
throw SDLException("SDL_SetAppMetadataProperty");
|
||||
}
|
||||
}
|
||||
}
|
||||
42
src/wrappers/sdl/rect.cppm
Normal file
42
src/wrappers/sdl/rect.cppm
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
module;
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module wrappers.sdl.rect;
|
||||
|
||||
export namespace sdl
|
||||
{
|
||||
// Wrappers around SDL_Point and SDL_FPoint (change to wrapper classes when needed)
|
||||
using FPoint = SDL_FPoint;
|
||||
using Point = SDL_Point;
|
||||
|
||||
/**
|
||||
* Wrapper around SDL_FRect using class inheritance.
|
||||
*/
|
||||
class FRect : public SDL_FRect
|
||||
{
|
||||
/**
|
||||
* Wrapper around SDL_RectEmptyFloat().
|
||||
* @return True if the floating point rectangle takes no space.
|
||||
*/
|
||||
constexpr bool is_empty() const
|
||||
{
|
||||
return SDL_RectEmptyFloat(this);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrapper around SDL_Rect using class inheritance.
|
||||
*/
|
||||
class Rect : public SDL_Rect
|
||||
{
|
||||
/**
|
||||
* Wrapper around SDL_RectEmpty().
|
||||
* @return True if the rectangle takes no space.
|
||||
*/
|
||||
constexpr bool is_empty() const
|
||||
{
|
||||
return SDL_RectEmpty(this);
|
||||
}
|
||||
};
|
||||
}
|
||||
125
src/wrappers/sdl/render.cppm
Normal file
125
src/wrappers/sdl/render.cppm
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
module;
|
||||
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module wrappers.sdl.render;
|
||||
|
||||
import utils.memory;
|
||||
import wrappers.sdl.error;
|
||||
import wrappers.sdl.rect;
|
||||
import wrappers.sdl.video;
|
||||
|
||||
export namespace sdl
|
||||
{
|
||||
/**
|
||||
* Wrapper around SDL_Texture that manages its lifecycle with a unique_ptr.
|
||||
*/
|
||||
class Texture
|
||||
{
|
||||
std::unique_ptr<
|
||||
SDL_Texture,
|
||||
utils::FuncDeleter<SDL_DestroyTexture>
|
||||
> raw_texture_ = nullptr;
|
||||
|
||||
public:
|
||||
Texture() = default;
|
||||
|
||||
explicit Texture(SDL_Texture* raw_texture)
|
||||
: raw_texture_{raw_texture}
|
||||
{}
|
||||
|
||||
constexpr SDL_Texture* get_raw() const
|
||||
{
|
||||
assert(raw_texture_);
|
||||
return raw_texture_.get();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrapper around SDL_Renderer that manages its lifecycle with a unique_ptr.
|
||||
*/
|
||||
class Renderer
|
||||
{
|
||||
std::unique_ptr<
|
||||
SDL_Renderer,
|
||||
utils::FuncDeleter<SDL_DestroyRenderer>
|
||||
> raw_renderer_ = nullptr;
|
||||
|
||||
public:
|
||||
Renderer() = default;
|
||||
|
||||
explicit Renderer(SDL_Renderer* raw_renderer)
|
||||
: raw_renderer_{raw_renderer}
|
||||
{}
|
||||
|
||||
constexpr SDL_Renderer* get_raw() const
|
||||
{
|
||||
assert(raw_renderer_);
|
||||
return raw_renderer_.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper around SDL_RenderClear().
|
||||
* Uses assert to verify the function returned true.
|
||||
*/
|
||||
constexpr void clear() const
|
||||
{
|
||||
// TODO: These functions probably should never fail? Change this to a runtime exception if necessary.
|
||||
if (!SDL_RenderClear(get_raw())) {
|
||||
assert(!"SDL_RenderClear returned false");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper around SDL_RenderPresent().
|
||||
* Uses assert to verify the function returned true.
|
||||
*/
|
||||
constexpr void present() const
|
||||
{
|
||||
if (!SDL_RenderPresent(get_raw())) {
|
||||
assert(!"SDL_RenderPresent returned false");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper around SDL_RenderTexture().
|
||||
* Uses assert to verify the function returned true.
|
||||
*/
|
||||
constexpr void render_texture(
|
||||
const Texture& texture,
|
||||
const FRect* src_rect,
|
||||
const FRect* dest_rect
|
||||
) const
|
||||
{
|
||||
if (!SDL_RenderTexture(get_raw(), texture.get_raw(), src_rect, dest_rect)) {
|
||||
assert(!"SDL_RenderTexture returned false");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrapper around SDL_CreateWindowAndRenderer().
|
||||
* Returns a pair of a Window and a Renderer object.
|
||||
* Throws an SDLException on failure.
|
||||
*/
|
||||
std::pair<Window, Renderer> CreateWindowAndRenderer(
|
||||
const std::string& title,
|
||||
const int width,
|
||||
const int height,
|
||||
const SDL_WindowFlags window_flags
|
||||
)
|
||||
{
|
||||
SDL_Window* raw_window = nullptr;
|
||||
SDL_Renderer* raw_renderer = nullptr;
|
||||
|
||||
if (!SDL_CreateWindowAndRenderer(
|
||||
title.c_str(), width, height, window_flags, &raw_window, &raw_renderer
|
||||
)) {
|
||||
throw SDLException("SDL_CreateWindowAndRenderer");
|
||||
}
|
||||
|
||||
return {Window{raw_window}, Renderer{raw_renderer}};
|
||||
}
|
||||
}
|
||||
36
src/wrappers/sdl/video.cppm
Normal file
36
src/wrappers/sdl/video.cppm
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
module;
|
||||
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module wrappers.sdl.video;
|
||||
|
||||
import utils.memory;
|
||||
|
||||
export namespace sdl
|
||||
{
|
||||
/**
|
||||
* Wrapper around SDL_Window that manages its lifecycle with a unique_ptr.
|
||||
*/
|
||||
class Window
|
||||
{
|
||||
std::unique_ptr<
|
||||
SDL_Window,
|
||||
utils::FuncDeleter<SDL_DestroyWindow>
|
||||
> raw_window_ = nullptr;
|
||||
|
||||
public:
|
||||
Window() = default;
|
||||
|
||||
explicit Window(SDL_Window* raw_window)
|
||||
: raw_window_{raw_window}
|
||||
{}
|
||||
|
||||
constexpr SDL_Window* get_raw() const
|
||||
{
|
||||
assert(raw_window_);
|
||||
return raw_window_.get();
|
||||
}
|
||||
};
|
||||
}
|
||||
28
src/wrappers/sdl_image.cppm
Normal file
28
src/wrappers/sdl_image.cppm
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
module;
|
||||
|
||||
#include <string>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
|
||||
export module wrappers.sdl_image;
|
||||
|
||||
import utils.memory;
|
||||
import wrappers.sdl.error;
|
||||
import wrappers.sdl.render;
|
||||
|
||||
export namespace sdl_image
|
||||
{
|
||||
/**
|
||||
* Wrapper around IMG_LoadTexture().
|
||||
*/
|
||||
sdl::Texture LoadTexture(const sdl::Renderer& renderer, const std::string& filename)
|
||||
{
|
||||
SDL_Texture* sdl_texture = IMG_LoadTexture(renderer.get_raw(), filename.c_str());
|
||||
|
||||
if (sdl_texture == nullptr) {
|
||||
throw sdl::SDLException("IMG_LoadTexture");
|
||||
}
|
||||
|
||||
return sdl::Texture{sdl_texture};
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue