Refactor code into classes
This commit is contained in:
parent
0743e6dc62
commit
5c3e0e3a86
12 changed files with 584 additions and 190 deletions
95
src/core/engine.cppm
Normal file
95
src/core/engine.cppm
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
module;
|
||||
|
||||
#include <cassert>
|
||||
#include <tuple>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module core.engine;
|
||||
|
||||
import config;
|
||||
import core.renderer;
|
||||
import core.window;
|
||||
|
||||
export namespace core
|
||||
{
|
||||
class Engine
|
||||
{
|
||||
// Whether this class is currently instantiated (to prevent multiple instances)
|
||||
static bool instantiated_;
|
||||
|
||||
// If this is set to false, the application will exit
|
||||
bool keep_running_ = true;
|
||||
|
||||
Window window_;
|
||||
Renderer renderer_;
|
||||
|
||||
public:
|
||||
Engine()
|
||||
{
|
||||
// Prevent the class from being instantiated multiple times
|
||||
assert(!instantiated_);
|
||||
instantiated_ = true;
|
||||
}
|
||||
|
||||
// No copy operations
|
||||
Engine(const Engine&) = delete;
|
||||
Engine& operator=(const Engine&) = delete;
|
||||
|
||||
// Default move operations
|
||||
Engine(Engine&&) = default;
|
||||
Engine& operator=(Engine&&) = default;
|
||||
|
||||
~Engine()
|
||||
{
|
||||
instantiated_ = false;
|
||||
}
|
||||
|
||||
bool keep_running() const
|
||||
{
|
||||
return keep_running_;
|
||||
}
|
||||
|
||||
Window& get_window()
|
||||
{
|
||||
return window_;
|
||||
}
|
||||
|
||||
Renderer& get_renderer()
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
// Exit the application
|
||||
keep_running_ = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void update()
|
||||
{
|
||||
}
|
||||
|
||||
void shutdown()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
bool Engine::instantiated_ = false;
|
||||
}
|
||||
24
src/core/exceptions.cppm
Normal file
24
src/core/exceptions.cppm
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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())
|
||||
{}
|
||||
};
|
||||
}
|
||||
72
src/core/renderer.cppm
Normal file
72
src/core/renderer.cppm
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
module;
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module core.renderer;
|
||||
|
||||
export namespace core
|
||||
{
|
||||
class Renderer
|
||||
{
|
||||
// TODO: Use unique_ptr with custom deleters
|
||||
SDL_Renderer* sdl_renderer_ = nullptr;
|
||||
|
||||
public:
|
||||
Renderer() = default;
|
||||
|
||||
explicit Renderer(SDL_Renderer* sdl_renderer)
|
||||
: sdl_renderer_(sdl_renderer)
|
||||
{}
|
||||
|
||||
// No copy operations
|
||||
Renderer(const Renderer&) = delete;
|
||||
Renderer& operator=(const Renderer&) = delete;
|
||||
|
||||
// Move constructor
|
||||
Renderer(Renderer&& other) noexcept
|
||||
: sdl_renderer_(other.sdl_renderer_)
|
||||
{
|
||||
other.sdl_renderer_ = nullptr;
|
||||
}
|
||||
|
||||
// Move assignment
|
||||
Renderer& operator=(Renderer&& other) noexcept
|
||||
{
|
||||
sdl_renderer_ = other.sdl_renderer_;
|
||||
other.sdl_renderer_ = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Renderer()
|
||||
{
|
||||
if (sdl_renderer_ != nullptr) {
|
||||
SDL_DestroyRenderer(sdl_renderer_);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Remove this when not needed anymore
|
||||
SDL_Renderer* get_sdl_renderer() const
|
||||
{
|
||||
return sdl_renderer_;
|
||||
}
|
||||
|
||||
// TODO: Rename clear/present to start_render/finish_render or similar?
|
||||
void clear() const
|
||||
{
|
||||
SDL_RenderClear(sdl_renderer_);
|
||||
}
|
||||
|
||||
void present() const
|
||||
{
|
||||
SDL_RenderPresent(sdl_renderer_);
|
||||
}
|
||||
|
||||
// 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
|
||||
{
|
||||
SDL_RenderTexture(sdl_renderer_, texture, src_rect, dest_rect);
|
||||
}
|
||||
};
|
||||
}
|
||||
70
src/core/window.cppm
Normal file
70
src/core/window.cppm
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
module;
|
||||
|
||||
#include <string>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module core.window;
|
||||
|
||||
import core.exceptions;
|
||||
import core.renderer;
|
||||
|
||||
export namespace core
|
||||
{
|
||||
class Window
|
||||
{
|
||||
// TODO: Use unique_ptr with custom deleters
|
||||
SDL_Window* sdl_window_ = nullptr;
|
||||
|
||||
public:
|
||||
Window() = default;
|
||||
|
||||
explicit Window(SDL_Window* sdl_window)
|
||||
: sdl_window_(sdl_window)
|
||||
{}
|
||||
|
||||
// No copy operations
|
||||
Window(const Window&) = delete;
|
||||
Window& operator=(const Window&) = delete;
|
||||
|
||||
// Move constructor
|
||||
Window(Window&& other) noexcept
|
||||
: sdl_window_(other.sdl_window_)
|
||||
{
|
||||
other.sdl_window_ = nullptr;
|
||||
}
|
||||
|
||||
// Move assignment
|
||||
Window& operator=(Window&& other) noexcept
|
||||
{
|
||||
sdl_window_ = other.sdl_window_;
|
||||
other.sdl_window_ = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Window()
|
||||
{
|
||||
if (sdl_window_ != nullptr) {
|
||||
SDL_DestroyWindow(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