Move sprite implementation to sprite.cpp

This commit is contained in:
Jan Mrna 2025-09-27 13:18:01 +02:00
parent 1aebe47acf
commit f9b76687b3
5 changed files with 74 additions and 55 deletions

View File

@ -2,8 +2,8 @@ all: test pathfinding
# TODO add extra warnings
# TODO linter?
pathfinding: src/main.cpp src/array.hpp
g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp
pathfinding: src/main.cpp src/array.hpp src/sprite.cpp
g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp src/sprite.cpp
test: src/test.cpp src/array.hpp
g++ -Wall -Wextra -Wpedantic -ggdb3 -std=c++23 -lgtest -o test src/test.cpp

View File

@ -1,9 +1,9 @@
#include <memory>
#include "window.hpp"
#include "user_input.hpp"
#include "pathfindingdemo.hpp"
#include "gameloop.hpp"
#include "log.hpp"
#include "pathfindingdemo.hpp"
#include "user_input.hpp"
#include "window.hpp"
#include <memory>
int main(int argc, char **argv) {
constexpr int error = -1;

View File

@ -3,9 +3,9 @@
#include <cassert>
#include <cmath>
#include <concepts>
#include <initializer_list> // needed?
#include <initializer_list>
#include <iostream>
#include <utility> // TODO needed?
#include <utility>
template <typename T> struct Vec2D {

View File

@ -0,0 +1,52 @@
#include "sprite.hpp"
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <cassert>
#include <memory>
#include <stdexcept>
#include <string>
#include "log.hpp"
#include "math.hpp"
Sprite::Sprite() : m_Texture(nullptr, SDL_DestroyTexture) {}
Sprite::Sprite(std::string path, Vec2D<float> center) : Sprite() {
LoadImage(path, center);
}
Sprite::~Sprite() { LOG_DEBUG("."); }
void Sprite::LoadImage(std::string path, Vec2D<float> image_center) {
LOG_INFO("Loading image ", path);
assert(m_Renderer != nullptr);
auto surface = std::unique_ptr<SDL_Surface, decltype(&SDL_DestroySurface)>(
IMG_Load(path.c_str()), SDL_DestroySurface);
if (surface == nullptr) {
LOG_ERROR("IMG_Load failed to load ", path);
throw std::runtime_error("Failed to load resources");
}
m_Texture = std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)>(
SDL_CreateTextureFromSurface(m_Renderer.get(), surface.get()),
SDL_DestroyTexture);
if (m_Texture == nullptr) {
LOG_ERROR("SDL_CreateTextureFromSurface failed");
throw std::runtime_error("Failed to load resources");
}
float w, h;
SDL_GetTextureSize(m_Texture.get(), &w, &h);
m_Size = {w, h};
m_ImageCenter = image_center;
}
// Renderer is shared for all class instances - we need it in order
// to create textures from images
void Sprite::SetRenderer(std::shared_ptr<SDL_Renderer> renderer) {
m_Renderer = renderer;
}
std::shared_ptr<SDL_Renderer> Sprite::m_Renderer = nullptr;

View File

@ -7,67 +7,34 @@
#include <stdexcept>
#include <string>
#include "math.hpp"
#include "log.hpp"
#include "math.hpp"
class Sprite {
public:
Sprite() : m_Texture(nullptr, SDL_DestroyTexture) {}
Sprite(std::string path, Vec2D<float> center) : Sprite() {
LoadImage(path, center);
}
int m_R = 0;
int m_G = 0;
int m_B = 0;
int m_A = 0;
Sprite(const Sprite &x) = delete;
Sprite(Sprite &&x) = delete;
~Sprite() { LOG_DEBUG("."); }
void LoadImage(std::string path, Vec2D<float> image_center = {0.0, 0.0}) {
LOG_INFO("Loading image ", path);
assert(m_Renderer != nullptr);
Sprite();
~Sprite();
explicit Sprite(std::string path, WorldPos center = {0, 0});
auto surface = std::unique_ptr<SDL_Surface, decltype(&SDL_DestroySurface)>(
IMG_Load(path.c_str()), SDL_DestroySurface);
Sprite(const Sprite &) = delete;
Sprite &operator=(const Sprite &) = delete;
Sprite(Sprite &&) = delete;
Sprite &operator=(Sprite &&) = delete;
if (surface == nullptr) {
LOG_ERROR("IMG_Load failed to load ", path);
throw std::runtime_error("Failed to load resources");
}
m_Texture = std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)>(
SDL_CreateTextureFromSurface(m_Renderer.get(), surface.get()),
SDL_DestroyTexture);
if (m_Texture == nullptr) {
LOG_ERROR("SDL_CreateTextureFromSurface failed");
throw std::runtime_error("Failed to load resources");
}
float w, h;
SDL_GetTextureSize(m_Texture.get(), &w, &h);
m_Size = {w, h};
m_ImageCenter = image_center;
}
// Renderer is shared for all class instances - we need it in order
// to create textures from images
static void SetRenderer(std::shared_ptr<SDL_Renderer> renderer) {
m_Renderer = renderer;
}
static void SetRenderer(std::shared_ptr<SDL_Renderer> renderer);
// GetTexture cannot return pointer to const, as SDL_RenderTexture modifies it
SDL_Texture *GetTexture() { return m_Texture.get(); }
WorldPos GetSize() const { return m_Size; }
WorldPos GetCenter() const { return m_ImageCenter; }
Vec2D<float> GetSize() const { return m_Size; }
Vec2D<float> GetCenter() const { return m_ImageCenter; }
void LoadImage(std::string path, WorldPos image_center = {0.0, 0.0});
private:
static std::shared_ptr<SDL_Renderer> m_Renderer;
std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)> m_Texture;
Vec2D<float> m_Size;
Vec2D<float> m_ImageCenter;
WorldPos m_Size;
WorldPos m_ImageCenter;
float m_TextureWidth = 0;
float m_TextureHeight = 0;
};
std::shared_ptr<SDL_Renderer> Sprite::m_Renderer = nullptr;