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
|
|
@ -1,17 +1,19 @@
|
|||
module;
|
||||
|
||||
#include <cassert>
|
||||
#include <tuple>
|
||||
#include <memory>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module core.engine;
|
||||
|
||||
import config;
|
||||
import core.renderer;
|
||||
import core.window;
|
||||
import wrappers.sdl;
|
||||
|
||||
export namespace core
|
||||
{
|
||||
using Window = sdl::Window;
|
||||
|
||||
class Engine
|
||||
{
|
||||
// Whether this class is currently instantiated (to prevent multiple instances)
|
||||
|
|
@ -23,13 +25,16 @@ export namespace core
|
|||
Window window_;
|
||||
Renderer renderer_;
|
||||
|
||||
// Private constructor
|
||||
Engine(Window&& window, Renderer&& renderer)
|
||||
: window_{std::move(window)},
|
||||
renderer_{std::move(renderer)}
|
||||
{
|
||||
instantiated_ = true;
|
||||
}
|
||||
|
||||
public:
|
||||
Engine()
|
||||
{
|
||||
// Prevent the class from being instantiated multiple times
|
||||
assert(!instantiated_);
|
||||
instantiated_ = true;
|
||||
}
|
||||
Engine() = delete;
|
||||
|
||||
// No copy or move operations
|
||||
Engine(const Engine&) = delete;
|
||||
|
|
@ -42,6 +47,26 @@ export namespace core
|
|||
instantiated_ = false;
|
||||
}
|
||||
|
||||
static std::unique_ptr<Engine> create()
|
||||
{
|
||||
// Prevent the class from being instantiated multiple times
|
||||
assert(!instantiated_);
|
||||
|
||||
auto [sdl_window, sdl_renderer] = sdl::CreateWindowAndRenderer(
|
||||
config::get_window_title(),
|
||||
config::window_width,
|
||||
config::window_height,
|
||||
0
|
||||
);
|
||||
|
||||
return std::unique_ptr<Engine>(
|
||||
new Engine{
|
||||
std::move(sdl_window),
|
||||
Renderer{std::move(sdl_renderer)}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
bool keep_running() const
|
||||
{
|
||||
return keep_running_;
|
||||
|
|
@ -57,19 +82,8 @@ export namespace core
|
|||
return renderer_;
|
||||
}
|
||||
|
||||
// TODO: Should this be moved to the constructor?
|
||||
void initialize()
|
||||
{
|
||||
std::tie(window_, renderer_) = Window::create_window_and_renderer(
|
||||
config::get_window_title(),
|
||||
config::window_width,
|
||||
config::window_height,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
// Handles an SDL event. Returns true if the event has been handled.
|
||||
bool handle_event(const SDL_Event* event)
|
||||
bool handle_event(const sdl::Event* event)
|
||||
{
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
// Exit the application
|
||||
|
|
|
|||
|
|
@ -1,24 +0,0 @@
|
|||
module;
|
||||
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module core.exceptions;
|
||||
|
||||
export namespace core
|
||||
{
|
||||
// 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())
|
||||
{}
|
||||
};
|
||||
}
|
||||
|
|
@ -1,51 +1,47 @@
|
|||
module;
|
||||
|
||||
#include <memory>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <utility>
|
||||
|
||||
export module core.renderer;
|
||||
|
||||
import utils.memory;
|
||||
import wrappers.sdl;
|
||||
|
||||
export namespace core
|
||||
{
|
||||
// TODO: Rename this class to RenderServer or something to distinguish it from sdl::Renderer?
|
||||
class Renderer
|
||||
{
|
||||
std::unique_ptr<
|
||||
SDL_Renderer,
|
||||
utils::FuncDeleter<SDL_DestroyRenderer>
|
||||
> sdl_renderer_ = nullptr;
|
||||
sdl::Renderer sdl_renderer_;
|
||||
|
||||
public:
|
||||
Renderer() = default;
|
||||
Renderer() = delete;
|
||||
|
||||
explicit Renderer(SDL_Renderer* sdl_renderer)
|
||||
: sdl_renderer_(sdl_renderer)
|
||||
explicit Renderer(sdl::Renderer&& sdl_renderer)
|
||||
: sdl_renderer_{std::move(sdl_renderer)}
|
||||
{}
|
||||
|
||||
// TODO: Remove this when not needed anymore
|
||||
constexpr SDL_Renderer* get_sdl_renderer() const
|
||||
constexpr sdl::Renderer& get_sdl_renderer()
|
||||
{
|
||||
return sdl_renderer_.get();
|
||||
return sdl_renderer_;
|
||||
}
|
||||
|
||||
// TODO: Rename clear/present to start_render/finish_render or similar?
|
||||
void clear() const
|
||||
void start_frame() const
|
||||
{
|
||||
SDL_RenderClear(sdl_renderer_.get());
|
||||
sdl_renderer_.clear();
|
||||
}
|
||||
|
||||
void present() const
|
||||
void finish_frame() const
|
||||
{
|
||||
SDL_RenderPresent(sdl_renderer_.get());
|
||||
sdl_renderer_.present();
|
||||
}
|
||||
|
||||
// TODO: Replace SDL_Texture pointer with Texture class
|
||||
// TODO: Also replace SDL_FRect with something SDL-independent (although for performance it might make sense
|
||||
// to just type-alias it?)
|
||||
void render_texture(SDL_Texture* texture, const SDL_FRect* src_rect, const SDL_FRect* dest_rect) const
|
||||
void render_texture(
|
||||
const sdl::Texture& texture,
|
||||
const sdl::FRect* src_rect,
|
||||
const sdl::FRect* dest_rect
|
||||
) const
|
||||
{
|
||||
SDL_RenderTexture(sdl_renderer_.get(), texture, src_rect, dest_rect);
|
||||
sdl_renderer_.render_texture(texture, src_rect, dest_rect);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,48 +0,0 @@
|
|||
module;
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module core.window;
|
||||
|
||||
import core.exceptions;
|
||||
import core.renderer;
|
||||
import utils.memory;
|
||||
|
||||
export namespace core
|
||||
{
|
||||
class Window
|
||||
{
|
||||
std::unique_ptr<
|
||||
SDL_Window,
|
||||
utils::FuncDeleter<SDL_DestroyWindow>
|
||||
> sdl_window_ = nullptr;
|
||||
|
||||
public:
|
||||
Window() = default;
|
||||
|
||||
explicit Window(SDL_Window* sdl_window)
|
||||
: sdl_window_(sdl_window)
|
||||
{}
|
||||
|
||||
static std::pair<Window, Renderer> create_window_and_renderer(
|
||||
const std::string& title,
|
||||
const int width,
|
||||
const int height,
|
||||
const SDL_WindowFlags window_flags
|
||||
)
|
||||
{
|
||||
SDL_Window* sdl_window = nullptr;
|
||||
SDL_Renderer* sdl_renderer = nullptr;
|
||||
|
||||
if (!SDL_CreateWindowAndRenderer(
|
||||
title.c_str(), width, height, window_flags, &sdl_window, &sdl_renderer
|
||||
)) {
|
||||
throw SDLException("SDL_CreateWindowAndRenderer");
|
||||
}
|
||||
|
||||
return {Window{sdl_window}, Renderer{sdl_renderer}};
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue