Use unique_ptr to manage SDL pointers

This commit is contained in:
Lexi / Zoe 2025-11-21 01:09:05 +01:00
parent 5c3e0e3a86
commit 4277f4c818
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
8 changed files with 128 additions and 129 deletions

View file

@ -8,6 +8,7 @@ export module game.game;
import core.engine;
import core.renderer;
import game.sprite;
import resources.texture;
export namespace game
{
@ -25,11 +26,9 @@ export namespace game
: engine_(engine)
{}
// No copy operations
// No copy or move operations because we have a reference to the engine
Game(const Game&) = delete;
Game& operator=(const Game&) = delete;
// No move operations - TODO?
Game(Game&&) = delete;
Game& operator=(Game&&) = delete;
@ -37,7 +36,12 @@ export namespace game
void initialize()
{
sprite_ = std::make_unique<Sprite>(engine_.get_renderer(), "assets/neocat.png", 100, 100);
auto texture = resources::Texture::load_from_file(
engine_.get_renderer().get_sdl_renderer(),
"assets/neocat.png"
);
sprite_ = std::make_unique<Sprite>(std::move(texture), 100, 100);
}
// Handles an SDL event. Returns true if the event has been handled.

View file

@ -2,87 +2,53 @@ module;
#include <string>
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
export module game.sprite;
import core.exceptions;
import core.renderer;
import resources.texture;
// 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};
// TODO: Texture should be a reference/pointer to an object managed by a ResourceManager or similar.
resources::Texture texture_;
SDL_FRect dest_rect_{0, 0, 0, 0};
public:
explicit Sprite(
core::Renderer& renderer,
const std::string& filename,
resources::Texture&& texture,
const int width,
const int height
)
: texture_{std::move(texture)}
{
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);
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;
}
// Default move operations
Sprite(Sprite&& other) = default;
Sprite& operator=(Sprite&& other) = default;
// 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);
}
}
~Sprite() = default;
void move(const float x, const float y)
{
dest_rect.x = x;
dest_rect.y = y;
dest_rect_.x = x;
dest_rect_.y = y;
}
void draw(const core::Renderer& renderer) const
{
renderer.render_texture(sdl_texture, nullptr, &dest_rect);
renderer.render_texture(texture_.get_sdl_texture(), nullptr, &dest_rect_);
}
};
}