Refactor code into classes
This commit is contained in:
parent
0743e6dc62
commit
5c3e0e3a86
|
|
@ -8,3 +8,4 @@ default_target: linux
|
|||
targets:
|
||||
linux:
|
||||
output_file: rutile_game
|
||||
cpp_flags_extra: -DDEBUG
|
||||
|
|
|
|||
|
|
@ -0,0 +1,109 @@
|
|||
module;
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module app;
|
||||
|
||||
import config;
|
||||
import core.engine;
|
||||
import core.exceptions;
|
||||
import game.game;
|
||||
|
||||
export
|
||||
{
|
||||
class App
|
||||
{
|
||||
std::unique_ptr<core::Engine> engine_ = nullptr;
|
||||
std::unique_ptr<game::Game> game_ = nullptr;
|
||||
|
||||
public:
|
||||
App() = default;
|
||||
|
||||
// No copy operations
|
||||
App(const App&) = delete;
|
||||
App& operator=(const App&) = delete;
|
||||
|
||||
// Default move operations
|
||||
App(App&&) = default;
|
||||
App& operator=(App&&) = default;
|
||||
|
||||
~App() = default;
|
||||
|
||||
SDL_AppResult initialize()
|
||||
{
|
||||
try {
|
||||
// Set SDL application metadata
|
||||
set_app_metadata(SDL_PROP_APP_METADATA_NAME_STRING, config::app_name);
|
||||
set_app_metadata(SDL_PROP_APP_METADATA_VERSION_STRING, config::app_version);
|
||||
set_app_metadata(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, config::app_identifier);
|
||||
set_app_metadata(SDL_PROP_APP_METADATA_CREATOR_STRING, config::app_creator);
|
||||
set_app_metadata(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, config::app_copyright);
|
||||
set_app_metadata(SDL_PROP_APP_METADATA_URL_STRING, config::app_url);
|
||||
set_app_metadata(SDL_PROP_APP_METADATA_TYPE_STRING, "game");
|
||||
|
||||
// Initialize SDL subsystems
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
|
||||
throw core::SDLException("SDL_Init");
|
||||
}
|
||||
|
||||
engine_ = std::make_unique<core::Engine>();
|
||||
engine_->initialize();
|
||||
|
||||
game_ = std::make_unique<game::Game>(*engine_);
|
||||
game_->initialize();
|
||||
}
|
||||
catch (const std::runtime_error& e) {
|
||||
std::cerr << "Unhandled exception during initialization: " << e.what() << '\n';
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
static void set_app_metadata(const char* property_name, const char* value)
|
||||
{
|
||||
if (!SDL_SetAppMetadataProperty(property_name, value)) {
|
||||
throw core::SDLException("SDL_SetAppMetadataProperty");
|
||||
}
|
||||
}
|
||||
|
||||
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_APP_FAILURE;
|
||||
}
|
||||
|
||||
return engine_->keep_running() ? SDL_APP_CONTINUE : SDL_APP_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_APP_FAILURE;
|
||||
}
|
||||
|
||||
return engine_->keep_running() ? SDL_APP_CONTINUE : SDL_APP_SUCCESS;
|
||||
}
|
||||
|
||||
void shutdown() const
|
||||
{
|
||||
engine_->shutdown();
|
||||
game_->shutdown();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
module;
|
||||
|
||||
#include <format>
|
||||
|
||||
#ifdef DEBUG
|
||||
#define DEBUG_BOOL true
|
||||
#else
|
||||
#define DEBUG_BOOL false
|
||||
#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,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;
|
||||
}
|
||||
|
|
@ -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())
|
||||
{}
|
||||
};
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -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}};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
module;
|
||||
|
||||
#include <memory>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
export module game.game;
|
||||
|
||||
import core.engine;
|
||||
import core.renderer;
|
||||
import game.sprite;
|
||||
|
||||
export namespace game
|
||||
{
|
||||
class Game
|
||||
{
|
||||
core::Engine& engine_;
|
||||
|
||||
// Sprite for testing
|
||||
std::unique_ptr<Sprite> sprite_ = nullptr;
|
||||
|
||||
public:
|
||||
Game() = delete;
|
||||
|
||||
explicit Game(core::Engine& engine)
|
||||
: engine_(engine)
|
||||
{}
|
||||
|
||||
// No copy operations
|
||||
Game(const Game&) = delete;
|
||||
Game& operator=(const Game&) = delete;
|
||||
|
||||
// No move operations - TODO?
|
||||
Game(Game&&) = delete;
|
||||
Game& operator=(Game&&) = delete;
|
||||
|
||||
~Game() = default;
|
||||
|
||||
void initialize()
|
||||
{
|
||||
sprite_ = std::make_unique<Sprite>(engine_.get_renderer(), "assets/neocat.png", 100, 100);
|
||||
}
|
||||
|
||||
// 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.clear();
|
||||
sprite_->draw(renderer);
|
||||
renderer.present();
|
||||
}
|
||||
|
||||
void shutdown()
|
||||
{
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
module;
|
||||
|
||||
#include <string>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
|
||||
export module game.sprite;
|
||||
|
||||
import core.exceptions;
|
||||
import core.renderer;
|
||||
|
||||
// TODO: Move this to a different namespace (core, drawing, ...?)
|
||||
export namespace game
|
||||
{
|
||||
class Sprite
|
||||
{
|
||||
// TODO: Move texture to separate class
|
||||
SDL_Texture* sdl_texture;
|
||||
SDL_FRect dest_rect{0, 0, 0, 0};
|
||||
|
||||
public:
|
||||
explicit Sprite(
|
||||
core::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 core::SDLException("IMG_Load");
|
||||
}
|
||||
|
||||
sdl_texture = SDL_CreateTextureFromSurface(renderer.get_sdl_renderer(), texture_surface);
|
||||
SDL_DestroySurface(texture_surface);
|
||||
|
||||
if (sdl_texture == nullptr) {
|
||||
throw core::SDLException("SDL_CreateTextureFromSurface");
|
||||
}
|
||||
|
||||
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 draw(const core::Renderer& renderer) const
|
||||
{
|
||||
renderer.render_texture(sdl_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
|
||||
#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;
|
||||
return sdl_app_init(static_cast<AppState*>(*appstate));
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppIterate(void* appstate)
|
||||
{
|
||||
return sdl_app_iterate(static_cast<AppState*>(appstate));
|
||||
auto* app = new App;
|
||||
*appstate = app;
|
||||
return app->initialize();
|
||||
}
|
||||
|
||||
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 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);
|
||||
sdl_app_shutdown(game_app_state);
|
||||
delete game_app_state;
|
||||
auto* app = static_cast<App*>(appstate);
|
||||
return app->iterate();
|
||||
}
|
||||
|
||||
void SDL_AppQuit(void* appstate, const SDL_AppResult /*result*/)
|
||||
{
|
||||
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);
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue