Refactor code into classes

This commit is contained in:
Lexi / Zoe 2025-11-17 21:17:45 +01:00
parent 0743e6dc62
commit 5c3e0e3a86
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
12 changed files with 584 additions and 190 deletions

72
src/game/game.cppm Normal file
View file

@ -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()
{
}
};
}

88
src/game/sprite.cppm Normal file
View file

@ -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);
}
};
}