diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-10 16:21:05 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-10 16:21:05 +0100 |
commit | 7b8de90699aea153e73b5f2cee05c69b966b81be (patch) | |
tree | 123f8b71b59a933b2bfcffb9e43e5bea66086d8f /src/crepe/facade | |
parent | 33cd5566909ac089cdf56db38a3d1daf0cb7dd10 (diff) |
implemented feedback wouter, improved animator. however if spritesheet aspect_ratio is not the same as the single frame then the scaling is wrong
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 4 | ||||
-rw-r--r-- | src/crepe/facade/Texture.cpp | 34 | ||||
-rw-r--r-- | src/crepe/facade/Texture.h | 72 |
4 files changed, 110 insertions, 2 deletions
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index 4cc53bc..0598e16 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -1,5 +1,6 @@ target_sources(crepe PUBLIC Sound.cpp + Texture.cpp SoundContext.cpp SDLContext.cpp DB.cpp @@ -7,6 +8,7 @@ target_sources(crepe PUBLIC target_sources(crepe PUBLIC FILE_SET HEADERS FILES Sound.h + Texture.h SoundContext.h SDLContext.h DB.h diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index ac21d15..a92ec8b 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -19,12 +19,12 @@ #include "../api/Color.h" #include "../api/Config.h" #include "../api/Sprite.h" -#include "../api/Texture.h" #include "../util/Log.h" #include "manager/Manager.h" #include "manager/Mediator.h" #include "SDLContext.h" +#include "Texture.h" #include "types.h" using namespace crepe; @@ -273,7 +273,7 @@ void SDLContext::draw(const RenderContext & ctx) { .img_scale = ctx.scale, }); - cout << dstrect.w << " " << dstrect.h << " " << dstrect.x << " " << dstrect.y << endl; + cout << srcrect->w << " " << srcrect->h << " " << srcrect->x << " " << srcrect->y << endl; double angle = ctx.angle + data.angle_offset; this->set_color_texture(ctx.texture, ctx.sprite.data.color); diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp new file mode 100644 index 0000000..7224cb8 --- /dev/null +++ b/src/crepe/facade/Texture.cpp @@ -0,0 +1,34 @@ +#include "../util/Log.h" +#include "manager/Mediator.h" +#include "facade/SDLContext.h" + +#include "Resource.h" +#include "Texture.h" +#include "types.h" + +using namespace crepe; +using namespace std; + +Texture::Texture(const Asset & src, Mediator & mediator) : Resource(src, mediator){ + dbg_trace(); + SDLContext & ctx = mediator.sdl_context; + this->texture = ctx.texture_from_path(src.get_path()); + this->size = ctx.get_size(*this); + this->aspect_ratio = static_cast<float>(this->size.x) / this->size.y; +} + +Texture::~Texture() { + dbg_trace(); + this->texture.reset(); +} + +const ivec2 & Texture::get_size() const noexcept{ + return this->size; +} +const float & Texture::get_ratio() const noexcept{ + return this->aspect_ratio; +} + +SDL_Texture * Texture::get_img() const noexcept{ + return this->texture.get(); +} diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h new file mode 100644 index 0000000..255e14b --- /dev/null +++ b/src/crepe/facade/Texture.h @@ -0,0 +1,72 @@ +#pragma once + +// FIXME: this header can't be included because this is an API header, and SDL2 development +// headers won't be bundled with crepe. Why is this facade in the API namespace? + +#include <SDL2/SDL_render.h> +#include <memory> + +#include "../Resource.h" + +#include "types.h" + +namespace crepe { + +class Mediator; +class Asset; + +/** + * \class Texture + * \brief Manages texture loading and properties. + * + * The Texture class is responsible for loading an image from a source and providing access to + * its dimensions. Textures can be used for rendering. + */ +class Texture : public Resource { + +public: + /** + * \brief Constructs a Texture from an Asset resource. + * \param src Asset with texture data to load. + * \param mediator use the SDLContext reference to load the image + */ + Texture(const Asset & src, Mediator & mediator); + + /** + * \brief Destroys the Texture instance + */ + ~Texture(); + + /** + * \brief get width and height of image in pixels + * \return pixel size width and height + * + */ + const ivec2 & get_size() const noexcept; + + /** + * \brief aspect_ratio of image + * \return ratio + * + */ + const float & get_ratio() const noexcept; + + /** + * \brief get the image texture + * \return SDL_Texture + * + */ + SDL_Texture * get_img() const noexcept; + +private: + //! The texture of the class from the library + std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> texture; + + // texture size in pixel + ivec2 size; + + //! ratio of image + float aspect_ratio; +}; + +} // namespace crepe |