Compare commits
4 Commits
0743e6dc62
...
e37eb0b03f
| Author | SHA1 | Date |
|---|---|---|
|
|
e37eb0b03f | |
|
|
d180b8a5b4 | |
|
|
4277f4c818 | |
|
|
5c3e0e3a86 |
|
|
@ -8,3 +8,4 @@ default_target: linux
|
||||||
targets:
|
targets:
|
||||||
linux:
|
linux:
|
||||||
output_file: rutile_game
|
output_file: rutile_game
|
||||||
|
# TODO: In a release build, set -DNDEBUG
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
export module app;
|
||||||
|
|
||||||
|
import config;
|
||||||
|
import core.engine;
|
||||||
|
import game.game;
|
||||||
|
import wrappers.sdl;
|
||||||
|
|
||||||
|
export
|
||||||
|
{
|
||||||
|
class App
|
||||||
|
{
|
||||||
|
std::unique_ptr<core::Engine> engine_{nullptr};
|
||||||
|
std::unique_ptr<game::Game> game_{nullptr};
|
||||||
|
|
||||||
|
public:
|
||||||
|
App() = default;
|
||||||
|
|
||||||
|
sdl::AppResult initialize()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
// Set SDL application metadata
|
||||||
|
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING, config::app_name);
|
||||||
|
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_VERSION_STRING, config::app_version);
|
||||||
|
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, config::app_identifier);
|
||||||
|
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_CREATOR_STRING, config::app_creator);
|
||||||
|
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, config::app_copyright);
|
||||||
|
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_URL_STRING, config::app_url);
|
||||||
|
sdl::SetAppMetadataProperty(SDL_PROP_APP_METADATA_TYPE_STRING, "game");
|
||||||
|
|
||||||
|
// Initialize SDL subsystems
|
||||||
|
sdl::Init(sdl::InitFlags::Video | sdl::InitFlags::Events);
|
||||||
|
|
||||||
|
// Create engine (includes window and renderer) and game state
|
||||||
|
engine_ = core::Engine::create();
|
||||||
|
game_ = game::Game::create(*engine_);
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error& e) {
|
||||||
|
std::cerr << "Unhandled exception during initialization: " << e.what() << '\n';
|
||||||
|
return sdl::AppResult::Failure;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sdl::AppResult::Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
sdl::AppResult handle_event(const sdl::Event* event)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (!engine_->handle_event(event)) {
|
||||||
|
game_->handle_event(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error& e) {
|
||||||
|
std::cerr << "Unhandled exception during event handling: " << e.what() << '\n';
|
||||||
|
return sdl::AppResult::Failure;
|
||||||
|
}
|
||||||
|
|
||||||
|
return engine_->keep_running() ? sdl::AppResult::Continue : sdl::AppResult::Success;
|
||||||
|
}
|
||||||
|
|
||||||
|
sdl::AppResult iterate()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
engine_->update();
|
||||||
|
game_->update();
|
||||||
|
game_->render();
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error& e) {
|
||||||
|
std::cerr << "Unhandled exception during updating: " << e.what() << '\n';
|
||||||
|
return sdl::AppResult::Failure;
|
||||||
|
}
|
||||||
|
|
||||||
|
return engine_->keep_running() ? sdl::AppResult::Continue : sdl::AppResult::Success;
|
||||||
|
}
|
||||||
|
|
||||||
|
void shutdown() const
|
||||||
|
{
|
||||||
|
engine_->shutdown();
|
||||||
|
game_->shutdown();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
#include <format>
|
||||||
|
|
||||||
|
#ifdef NDEBUG
|
||||||
|
#define DEBUG_BOOL false
|
||||||
|
#else
|
||||||
|
#define DEBUG_BOOL true
|
||||||
|
#endif
|
||||||
|
|
||||||
|
export module config;
|
||||||
|
|
||||||
|
export namespace config
|
||||||
|
{
|
||||||
|
constexpr auto debug = DEBUG_BOOL;
|
||||||
|
|
||||||
|
constexpr auto app_name = "Rutile Game Prototype";
|
||||||
|
constexpr auto app_version = "0.0.1-dev";
|
||||||
|
constexpr auto app_identifier = "dev.binarydiv.rutile_game";
|
||||||
|
constexpr auto app_creator = "binaryDiv";
|
||||||
|
constexpr auto app_copyright = "Copyright (c) 2025 binaryDiv";
|
||||||
|
constexpr auto app_url = "https://git.0xbd.space/binaryDiv/rutile-game";
|
||||||
|
|
||||||
|
constexpr auto window_width = 640;
|
||||||
|
constexpr auto window_height = 480;
|
||||||
|
|
||||||
|
constexpr auto get_window_title()
|
||||||
|
{
|
||||||
|
if constexpr (debug) {
|
||||||
|
return std::format("{} ({}) [DEBUG]", app_name, app_version);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return std::format("{} ({})", app_name, app_version);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,107 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <memory>
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
export module core.engine;
|
||||||
|
|
||||||
|
import config;
|
||||||
|
import core.renderer;
|
||||||
|
import wrappers.sdl;
|
||||||
|
|
||||||
|
export namespace core
|
||||||
|
{
|
||||||
|
using Window = sdl::Window;
|
||||||
|
|
||||||
|
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_;
|
||||||
|
|
||||||
|
// Private constructor
|
||||||
|
Engine(Window&& window, Renderer&& renderer)
|
||||||
|
: window_{std::move(window)},
|
||||||
|
renderer_{std::move(renderer)}
|
||||||
|
{
|
||||||
|
instantiated_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
Engine() = delete;
|
||||||
|
|
||||||
|
// No copy or move operations
|
||||||
|
Engine(const Engine&) = delete;
|
||||||
|
Engine& operator=(const Engine&) = delete;
|
||||||
|
Engine(Engine&&) = delete;
|
||||||
|
Engine& operator=(Engine&&) = delete;
|
||||||
|
|
||||||
|
~Engine()
|
||||||
|
{
|
||||||
|
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_;
|
||||||
|
}
|
||||||
|
|
||||||
|
Window& get_window()
|
||||||
|
{
|
||||||
|
return window_;
|
||||||
|
}
|
||||||
|
|
||||||
|
Renderer& get_renderer()
|
||||||
|
{
|
||||||
|
return renderer_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
export module core.renderer;
|
||||||
|
|
||||||
|
import wrappers.sdl;
|
||||||
|
|
||||||
|
export namespace core
|
||||||
|
{
|
||||||
|
// TODO: Rename this class to RenderServer or something to distinguish it from sdl::Renderer?
|
||||||
|
class Renderer
|
||||||
|
{
|
||||||
|
sdl::Renderer sdl_renderer_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Renderer() = delete;
|
||||||
|
|
||||||
|
explicit Renderer(sdl::Renderer&& sdl_renderer)
|
||||||
|
: sdl_renderer_{std::move(sdl_renderer)}
|
||||||
|
{}
|
||||||
|
|
||||||
|
constexpr sdl::Renderer& get_sdl_renderer()
|
||||||
|
{
|
||||||
|
return sdl_renderer_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void start_frame() const
|
||||||
|
{
|
||||||
|
sdl_renderer_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void finish_frame() const
|
||||||
|
{
|
||||||
|
sdl_renderer_.present();
|
||||||
|
}
|
||||||
|
|
||||||
|
void render_texture(
|
||||||
|
const sdl::Texture& texture,
|
||||||
|
const sdl::FRect* src_rect,
|
||||||
|
const sdl::FRect* dest_rect
|
||||||
|
) const
|
||||||
|
{
|
||||||
|
sdl_renderer_.render_texture(texture, src_rect, dest_rect);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,95 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <memory>
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
export module game.game;
|
||||||
|
|
||||||
|
import core.engine;
|
||||||
|
import core.renderer;
|
||||||
|
import game.sprite;
|
||||||
|
import wrappers.sdl;
|
||||||
|
import wrappers.sdl_image;
|
||||||
|
|
||||||
|
export namespace game
|
||||||
|
{
|
||||||
|
class Game
|
||||||
|
{
|
||||||
|
// Whether this class is currently instantiated (to prevent multiple instances)
|
||||||
|
static bool instantiated_;
|
||||||
|
|
||||||
|
// Reference to the engine
|
||||||
|
core::Engine& engine_;
|
||||||
|
|
||||||
|
// Sprite for testing
|
||||||
|
std::unique_ptr<Sprite> sprite_{nullptr};
|
||||||
|
|
||||||
|
// Private constructor
|
||||||
|
explicit Game(core::Engine& engine)
|
||||||
|
: engine_(engine)
|
||||||
|
{
|
||||||
|
// TODO: Texture should be a reference/pointer to an object managed by a ResourceManager or similar.
|
||||||
|
auto texture = sdl_image::LoadTexture(
|
||||||
|
engine_.get_renderer().get_sdl_renderer(),
|
||||||
|
"assets/neocat.png"
|
||||||
|
);
|
||||||
|
|
||||||
|
sprite_ = std::make_unique<Sprite>(std::move(texture), 100, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
Game() = delete;
|
||||||
|
|
||||||
|
// No copy or move operations because we have a reference to the engine
|
||||||
|
Game(const Game&) = delete;
|
||||||
|
Game& operator=(const Game&) = delete;
|
||||||
|
Game(Game&&) = delete;
|
||||||
|
Game& operator=(Game&&) = delete;
|
||||||
|
|
||||||
|
~Game()
|
||||||
|
{
|
||||||
|
instantiated_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::unique_ptr<Game> create(core::Engine& engine)
|
||||||
|
{
|
||||||
|
// Prevent the class from being instantiated multiple times
|
||||||
|
assert(!instantiated_);
|
||||||
|
return std::unique_ptr<Game>{
|
||||||
|
new Game(engine)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handles an SDL event. Returns true if the event has been handled.
|
||||||
|
bool handle_event(const sdl::Event* event) const
|
||||||
|
{
|
||||||
|
if (event->type == SDL_EVENT_MOUSE_MOTION) {
|
||||||
|
sprite_->move(
|
||||||
|
event->motion.x - 50,
|
||||||
|
event->motion.y - 50
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void render() const
|
||||||
|
{
|
||||||
|
const auto& renderer = engine_.get_renderer();
|
||||||
|
renderer.start_frame();
|
||||||
|
sprite_->draw(renderer);
|
||||||
|
renderer.finish_frame();
|
||||||
|
}
|
||||||
|
|
||||||
|
void shutdown()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bool Game::instantiated_ = false;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
export module game.sprite;
|
||||||
|
|
||||||
|
import core.renderer;
|
||||||
|
import wrappers.sdl;
|
||||||
|
|
||||||
|
// TODO: Move this to a different namespace (core, drawing, ...?)
|
||||||
|
export namespace game
|
||||||
|
{
|
||||||
|
class Sprite
|
||||||
|
{
|
||||||
|
// TODO: Texture should be a reference/pointer to an object managed by a ResourceManager or similar.
|
||||||
|
sdl::Texture texture_;
|
||||||
|
sdl::FRect dest_rect_{0, 0, 0, 0};
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit Sprite(
|
||||||
|
sdl::Texture&& texture,
|
||||||
|
const int width,
|
||||||
|
const int height
|
||||||
|
)
|
||||||
|
: texture_{std::move(texture)}
|
||||||
|
{
|
||||||
|
dest_rect_.w = static_cast<float>(width);
|
||||||
|
dest_rect_.h = static_cast<float>(height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void move(const float x, const float y)
|
||||||
|
{
|
||||||
|
dest_rect_.x = x;
|
||||||
|
dest_rect_.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(const core::Renderer& renderer) const
|
||||||
|
{
|
||||||
|
renderer.render_texture(texture_, nullptr, &dest_rect_);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
31
src/main.cpp
31
src/main.cpp
|
|
@ -1,27 +1,30 @@
|
||||||
import sdl_app;
|
import app;
|
||||||
|
|
||||||
#define SDL_MAIN_USE_CALLBACKS
|
#define SDL_MAIN_USE_CALLBACKS
|
||||||
#include <SDL3/SDL_main.h>
|
#include <SDL3/SDL_main.h>
|
||||||
|
|
||||||
SDL_AppResult SDL_AppInit(void** appstate, int /*argc*/, char** /*argv*/)
|
SDL_AppResult SDL_AppInit(void** appstate, const int /*argc*/, char** /*argv*/)
|
||||||
{
|
{
|
||||||
*appstate = new AppState;
|
auto* app = new App;
|
||||||
return sdl_app_init(static_cast<AppState*>(*appstate));
|
*appstate = app;
|
||||||
}
|
return static_cast<SDL_AppResult>(app->initialize());
|
||||||
|
|
||||||
SDL_AppResult SDL_AppIterate(void* appstate)
|
|
||||||
{
|
|
||||||
return sdl_app_iterate(static_cast<AppState*>(appstate));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event)
|
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event)
|
||||||
{
|
{
|
||||||
return sdl_app_event(static_cast<AppState*>(appstate), event);
|
auto* app = static_cast<App*>(appstate);
|
||||||
|
return static_cast<SDL_AppResult>(app->handle_event(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDL_AppQuit(void* appstate, SDL_AppResult /*result*/)
|
SDL_AppResult SDL_AppIterate(void* appstate)
|
||||||
{
|
{
|
||||||
const auto* game_app_state = static_cast<AppState*>(appstate);
|
auto* app = static_cast<App*>(appstate);
|
||||||
sdl_app_shutdown(game_app_state);
|
return static_cast<SDL_AppResult>(app->iterate());
|
||||||
delete game_app_state;
|
}
|
||||||
|
|
||||||
|
void SDL_AppQuit(void* appstate, const SDL_AppResult /*result*/)
|
||||||
|
{
|
||||||
|
const auto* app = static_cast<App*>(appstate);
|
||||||
|
app->shutdown();
|
||||||
|
delete app;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,95 +0,0 @@
|
||||||
module;
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
#include <SDL3/SDL.h>
|
|
||||||
|
|
||||||
export module sdl_app;
|
|
||||||
|
|
||||||
import sprite;
|
|
||||||
|
|
||||||
export {
|
|
||||||
struct AppState
|
|
||||||
{
|
|
||||||
SDL_Window* window = nullptr;
|
|
||||||
SDL_Renderer* renderer = nullptr;
|
|
||||||
Sprite* sprite = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
SDL_AppResult sdl_panic(
|
|
||||||
const std::string& error_prefix,
|
|
||||||
SDL_Window* window = nullptr,
|
|
||||||
SDL_Renderer* renderer = nullptr
|
|
||||||
)
|
|
||||||
{
|
|
||||||
std::cerr << error_prefix << ": " << SDL_GetError() << '\n';
|
|
||||||
|
|
||||||
if (renderer != nullptr) {
|
|
||||||
SDL_DestroyRenderer(renderer);
|
|
||||||
}
|
|
||||||
if (window != nullptr) {
|
|
||||||
SDL_DestroyWindow(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Quit();
|
|
||||||
return SDL_APP_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_AppResult sdl_app_init(AppState* app_state)
|
|
||||||
{
|
|
||||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
|
|
||||||
return sdl_panic("SDL_Init error");
|
|
||||||
}
|
|
||||||
|
|
||||||
app_state->window = SDL_CreateWindow("BuildSystemTest", 640, 480, 0);
|
|
||||||
if (app_state->window == nullptr) {
|
|
||||||
return sdl_panic("SDL_CreateWindow error");
|
|
||||||
}
|
|
||||||
|
|
||||||
app_state->renderer = SDL_CreateRenderer(app_state->window, nullptr);
|
|
||||||
if (app_state->renderer == nullptr) {
|
|
||||||
return sdl_panic("SDL_CreateRenderer error", app_state->window);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
app_state->sprite = new Sprite(app_state->renderer, "assets/neocat.png", 100, 100);
|
|
||||||
}
|
|
||||||
catch (const std::runtime_error& e) {
|
|
||||||
return sdl_panic(e.what(), app_state->window, app_state->renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
return SDL_APP_CONTINUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_AppResult sdl_app_event(const AppState* app_state, const SDL_Event* event)
|
|
||||||
{
|
|
||||||
if (event->type == SDL_EVENT_QUIT) {
|
|
||||||
return SDL_APP_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event->type == SDL_EVENT_MOUSE_MOTION) {
|
|
||||||
app_state->sprite->move(
|
|
||||||
event->motion.x - 50,
|
|
||||||
event->motion.y - 50
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return SDL_APP_CONTINUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_AppResult sdl_app_iterate(const AppState* app_state)
|
|
||||||
{
|
|
||||||
SDL_RenderClear(app_state->renderer);
|
|
||||||
app_state->sprite->render(app_state->renderer);
|
|
||||||
SDL_RenderPresent(app_state->renderer);
|
|
||||||
|
|
||||||
return SDL_APP_CONTINUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
void sdl_app_shutdown(const AppState* app_state)
|
|
||||||
{
|
|
||||||
delete app_state->sprite;
|
|
||||||
SDL_DestroyRenderer(app_state->renderer);
|
|
||||||
SDL_DestroyWindow(app_state->window);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,81 +0,0 @@
|
||||||
module;
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <string>
|
|
||||||
#include <SDL3/SDL.h>
|
|
||||||
#include <SDL3_image/SDL_image.h>
|
|
||||||
|
|
||||||
export module sprite;
|
|
||||||
|
|
||||||
export class Sprite
|
|
||||||
{
|
|
||||||
SDL_Texture* sdl_texture;
|
|
||||||
SDL_FRect dest_rect{0, 0, 0, 0};
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit Sprite(
|
|
||||||
SDL_Renderer* renderer,
|
|
||||||
const std::string& filename,
|
|
||||||
const int width,
|
|
||||||
const int height
|
|
||||||
)
|
|
||||||
{
|
|
||||||
SDL_Surface* texture_surface = IMG_Load(filename.c_str());
|
|
||||||
if (texture_surface == nullptr) {
|
|
||||||
throw std::runtime_error("IMG_Load error");
|
|
||||||
}
|
|
||||||
|
|
||||||
sdl_texture = SDL_CreateTextureFromSurface(renderer, texture_surface);
|
|
||||||
SDL_DestroySurface(texture_surface);
|
|
||||||
|
|
||||||
if (sdl_texture == nullptr) {
|
|
||||||
throw std::runtime_error("SDL_CreateTextureFromSurface error");
|
|
||||||
}
|
|
||||||
|
|
||||||
dest_rect.w = static_cast<float>(width);
|
|
||||||
dest_rect.h = static_cast<float>(height);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't allow copy operations
|
|
||||||
Sprite(const Sprite&) = delete;
|
|
||||||
Sprite& operator=(const Sprite&) = delete;
|
|
||||||
|
|
||||||
// Move constructor
|
|
||||||
Sprite(Sprite&& other) noexcept
|
|
||||||
: sdl_texture(other.sdl_texture),
|
|
||||||
dest_rect(other.dest_rect)
|
|
||||||
{
|
|
||||||
other.sdl_texture = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move assignment
|
|
||||||
Sprite& operator=(Sprite&& other) noexcept
|
|
||||||
{
|
|
||||||
// Move inner resources from other
|
|
||||||
sdl_texture = other.sdl_texture;
|
|
||||||
dest_rect = other.dest_rect;
|
|
||||||
|
|
||||||
// Reset other to make it safe for deletion
|
|
||||||
other.sdl_texture = nullptr;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
~Sprite()
|
|
||||||
{
|
|
||||||
if (sdl_texture != nullptr) {
|
|
||||||
SDL_DestroyTexture(sdl_texture);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void move(const float x, const float y)
|
|
||||||
{
|
|
||||||
dest_rect.x = x;
|
|
||||||
dest_rect.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
void render(SDL_Renderer* renderer) const
|
|
||||||
{
|
|
||||||
SDL_RenderTexture(renderer, sdl_texture, nullptr, &dest_rect);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
export module utils.memory;
|
||||||
|
|
||||||
|
export namespace utils
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Template to generate deleters for `std::unique_ptr` from functions, e.g. to free SDL resources.
|
||||||
|
*
|
||||||
|
* @tparam delete_func Function that takes a pointer to a resource and deletes the resource.
|
||||||
|
*/
|
||||||
|
template <auto delete_func>
|
||||||
|
struct FuncDeleter
|
||||||
|
{
|
||||||
|
template <typename T>
|
||||||
|
constexpr void operator()(T* ptr) const noexcept
|
||||||
|
{
|
||||||
|
delete_func(ptr);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
|
@ -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())
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -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_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Texture() = delete;
|
||||||
|
|
||||||
|
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_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Renderer() = delete;
|
||||||
|
|
||||||
|
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}};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Window() = delete;
|
||||||
|
|
||||||
|
explicit Window(SDL_Window* raw_window)
|
||||||
|
: raw_window_{raw_window}
|
||||||
|
{}
|
||||||
|
|
||||||
|
constexpr SDL_Window* get_raw() const
|
||||||
|
{
|
||||||
|
assert(raw_window_);
|
||||||
|
return raw_window_.get();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -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…
Reference in New Issue