diff --git a/cpp/Makefile b/cpp/Makefile index 286c981..305c39a 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -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 diff --git a/cpp/src/main.cpp b/cpp/src/main.cpp index 95acd2d..06da93a 100644 --- a/cpp/src/main.cpp +++ b/cpp/src/main.cpp @@ -1,9 +1,9 @@ -#include -#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 int main(int argc, char **argv) { constexpr int error = -1; diff --git a/cpp/src/math.hpp b/cpp/src/math.hpp index 70bff57..6a0c6cb 100644 --- a/cpp/src/math.hpp +++ b/cpp/src/math.hpp @@ -3,9 +3,9 @@ #include #include #include -#include // needed? +#include #include -#include // TODO needed? +#include template struct Vec2D { diff --git a/cpp/src/sprite.cpp b/cpp/src/sprite.cpp index e69de29..41b40ca 100644 --- a/cpp/src/sprite.cpp +++ b/cpp/src/sprite.cpp @@ -0,0 +1,52 @@ +#include "sprite.hpp" + +#include +#include +#include +#include +#include +#include + +#include "log.hpp" +#include "math.hpp" + +Sprite::Sprite() : m_Texture(nullptr, SDL_DestroyTexture) {} + +Sprite::Sprite(std::string path, Vec2D center) : Sprite() { + LoadImage(path, center); +} + +Sprite::~Sprite() { LOG_DEBUG("."); } + +void Sprite::LoadImage(std::string path, Vec2D image_center) { + LOG_INFO("Loading image ", path); + assert(m_Renderer != nullptr); + + auto surface = std::unique_ptr( + 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_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 renderer) { + m_Renderer = renderer; +} + +std::shared_ptr Sprite::m_Renderer = nullptr; diff --git a/cpp/src/sprite.hpp b/cpp/src/sprite.hpp index f503163..fd86167 100644 --- a/cpp/src/sprite.hpp +++ b/cpp/src/sprite.hpp @@ -7,67 +7,34 @@ #include #include -#include "math.hpp" #include "log.hpp" +#include "math.hpp" class Sprite { public: - Sprite() : m_Texture(nullptr, SDL_DestroyTexture) {} - Sprite(std::string path, Vec2D 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 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( - 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_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 renderer) { - m_Renderer = renderer; - } + static void SetRenderer(std::shared_ptr 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 GetSize() const { return m_Size; } - Vec2D GetCenter() const { return m_ImageCenter; } + void LoadImage(std::string path, WorldPos image_center = {0.0, 0.0}); private: static std::shared_ptr m_Renderer; std::unique_ptr m_Texture; - Vec2D m_Size; - Vec2D m_ImageCenter; + WorldPos m_Size; + WorldPos m_ImageCenter; float m_TextureWidth = 0; float m_TextureHeight = 0; }; -std::shared_ptr Sprite::m_Renderer = nullptr; - -