Move sprite implementation to sprite.cpp
This commit is contained in:
parent
1aebe47acf
commit
f9b76687b3
@ -2,8 +2,8 @@ all: test pathfinding
|
|||||||
# TODO add extra warnings
|
# TODO add extra warnings
|
||||||
# TODO linter?
|
# TODO linter?
|
||||||
|
|
||||||
pathfinding: src/main.cpp src/array.hpp
|
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
|
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
|
test: src/test.cpp src/array.hpp
|
||||||
g++ -Wall -Wextra -Wpedantic -ggdb3 -std=c++23 -lgtest -o test src/test.cpp
|
g++ -Wall -Wextra -Wpedantic -ggdb3 -std=c++23 -lgtest -o test src/test.cpp
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
#include <memory>
|
|
||||||
#include "window.hpp"
|
|
||||||
#include "user_input.hpp"
|
|
||||||
#include "pathfindingdemo.hpp"
|
|
||||||
#include "gameloop.hpp"
|
#include "gameloop.hpp"
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
|
#include "pathfindingdemo.hpp"
|
||||||
|
#include "user_input.hpp"
|
||||||
|
#include "window.hpp"
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
constexpr int error = -1;
|
constexpr int error = -1;
|
||||||
|
|||||||
@ -3,9 +3,9 @@
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <concepts>
|
#include <concepts>
|
||||||
#include <initializer_list> // needed?
|
#include <initializer_list>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <utility> // TODO needed?
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
template <typename T> struct Vec2D {
|
template <typename T> struct Vec2D {
|
||||||
|
|||||||
@ -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;
|
||||||
@ -7,67 +7,34 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "math.hpp"
|
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
|
#include "math.hpp"
|
||||||
|
|
||||||
class Sprite {
|
class Sprite {
|
||||||
public:
|
public:
|
||||||
Sprite() : m_Texture(nullptr, SDL_DestroyTexture) {}
|
Sprite();
|
||||||
Sprite(std::string path, Vec2D<float> center) : Sprite() {
|
~Sprite();
|
||||||
LoadImage(path, center);
|
explicit Sprite(std::string path, WorldPos center = {0, 0});
|
||||||
}
|
|
||||||
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);
|
|
||||||
|
|
||||||
auto surface = std::unique_ptr<SDL_Surface, decltype(&SDL_DestroySurface)>(
|
Sprite(const Sprite &) = delete;
|
||||||
IMG_Load(path.c_str()), SDL_DestroySurface);
|
Sprite &operator=(const Sprite &) = delete;
|
||||||
|
Sprite(Sprite &&) = delete;
|
||||||
|
Sprite &operator=(Sprite &&) = delete;
|
||||||
|
|
||||||
if (surface == nullptr) {
|
static void SetRenderer(std::shared_ptr<SDL_Renderer> renderer);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetTexture cannot return pointer to const, as SDL_RenderTexture modifies it
|
// GetTexture cannot return pointer to const, as SDL_RenderTexture modifies it
|
||||||
SDL_Texture *GetTexture() { return m_Texture.get(); }
|
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; }
|
void LoadImage(std::string path, WorldPos image_center = {0.0, 0.0});
|
||||||
Vec2D<float> GetCenter() const { return m_ImageCenter; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::shared_ptr<SDL_Renderer> m_Renderer;
|
static std::shared_ptr<SDL_Renderer> m_Renderer;
|
||||||
std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)> m_Texture;
|
std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)> m_Texture;
|
||||||
Vec2D<float> m_Size;
|
WorldPos m_Size;
|
||||||
Vec2D<float> m_ImageCenter;
|
WorldPos m_ImageCenter;
|
||||||
float m_TextureWidth = 0;
|
float m_TextureWidth = 0;
|
||||||
float m_TextureHeight = 0;
|
float m_TextureHeight = 0;
|
||||||
};
|
};
|
||||||
std::shared_ptr<SDL_Renderer> Sprite::m_Renderer = nullptr;
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user