From 59df950e11bf889fa0d87257d37b71028a11c66b Mon Sep 17 00:00:00 2001 From: Redacted Date: Tue, 18 Feb 2025 14:10:59 -0500 Subject: [PATCH] Add draw commands to support the texture atlas. --- include/JGL/JGL.h | 5 +++++ include/JGL/types/TextureAtlas.h | 9 ++++----- src/renderer/OpenGL/J2D.cpp | 8 ++++++++ src/types/TextureAtlas.cpp | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/JGL/JGL.h b/include/JGL/JGL.h index 01d3342..2c5918c 100644 --- a/include/JGL/JGL.h +++ b/include/JGL/JGL.h @@ -29,6 +29,7 @@ #include #include #include +#include // Fonts that are included by default. namespace JGL::Fonts { @@ -230,6 +231,10 @@ namespace JGL::J2D { void DrawSprite(const RenderTarget* render_target, const Vector2& position, float rad_rotation = 0, const Vector2& origin = Vector2(0 , 0), const Vector2& scale = Vector2(1, 1), const Color4& color = Colors::White, Direction inversion = Direction::None); + void DrawSprite(const TextureAtlas* texture_atlas, const AtlasRegion& atlas_region, const Vector2& position, float rad_rotation, const Vector2& origin = Vector2(0, 0), + const Vector2& scale = Vector2(1, 1), const Color4& color = Colors::White); + void DrawSprite(const TextureAtlas& texture_atlas, const AtlasRegion& atlas_region, const Vector2& position, float rad_rotation, const Vector2& origin = Vector2(0, 0), + const Vector2& scale = Vector2(1, 1), const Color4& color = Colors::White); /// Draws a sprite to the screen by passing a G̶L̶u̶i̶n̶t̶ JGL Texture that represents a handle to a loaded texture. /// @param texture A texture instance to be displayed. /// @param position The point at which to draw the sprite (from the top-left down). diff --git a/include/JGL/types/TextureAtlas.h b/include/JGL/types/TextureAtlas.h index dd1254c..0f6e714 100644 --- a/include/JGL/types/TextureAtlas.h +++ b/include/JGL/types/TextureAtlas.h @@ -2,18 +2,16 @@ #include -// A texture which actually contains multiple such that you can pass it in to draw sprite or draw partial sprite. -// It also makes for more efficient sprite batch because you don't have to swap textures between. - namespace JGL { struct AtlasRegion { Vector2i position = { 0, 0 }; Vector2i size = { 0, 0 }; }; - class TextureAtlas; } +// A texture which actually contains multiple such that you can pass it in to draw sprite or draw partial sprite. +// It also makes for more efficient sprite batch because you don't have to swap textures between. class JGL::TextureAtlas : public Texture { protected: std::vector regions; @@ -32,5 +30,6 @@ public: /// @param size The size of the image. /// @param regions The individual regions that will make up your atlas. /// @param region_count The number of regions there are. - TextureAtlas(const Color4* pixels, const Vector2i& size, AtlasRegion** regions, unsigned int region_count, FilteringMode filtering_mode = FilteringMode::NEAREST); + TextureAtlas(const Color4* pixels, const Vector2i& size, AtlasRegion** regions, unsigned int region_count, FilteringMode filtering_mode = FilteringMode::NEAREST, SampleRate anisotropy = SampleRate::NONE); + ~TextureAtlas() = default; }; \ No newline at end of file diff --git a/src/renderer/OpenGL/J2D.cpp b/src/renderer/OpenGL/J2D.cpp index a5577cb..6e200d7 100644 --- a/src/renderer/OpenGL/J2D.cpp +++ b/src/renderer/OpenGL/J2D.cpp @@ -1075,3 +1075,11 @@ void J2D::FillEllipse(const Color4& color, const Vector2& position, float radius glDrawArrays(GL_TRIANGLE_FAN, 0, (int) vertices.size()); glColor4fv(default_state.draw_color); } + +void J2D::DrawSprite(const TextureAtlas* texture_atlas, const AtlasRegion& atlas_region, const Vector2& position, float rad_rotation, const Vector2& origin, const Vector2& scale, const Color4& color) { + J2D::DrawPartialSprite(texture_atlas, position, Vector2(atlas_region.position), Vector2(atlas_region.size), rad_rotation, origin, scale, color); +} + +void J2D::DrawSprite(const TextureAtlas& texture_atlas, const AtlasRegion& atlas_region, const Vector2& position, float rad_rotation, const Vector2& origin, const Vector2& scale, const Color4& color) { + J2D::DrawSprite(&texture_atlas, atlas_region, position, rad_rotation, origin, scale, color); +} diff --git a/src/types/TextureAtlas.cpp b/src/types/TextureAtlas.cpp index be6e330..8047df3 100644 --- a/src/types/TextureAtlas.cpp +++ b/src/types/TextureAtlas.cpp @@ -55,7 +55,7 @@ TextureAtlas::TextureAtlas(const Color4** pixels, const Vector2i** sizes, unsign operator delete(result); } -TextureAtlas::TextureAtlas(const Color4* pixels, const Vector2i& size, AtlasRegion** regions, unsigned int region_count, FilteringMode filtering_mode) : Texture(pixels, size, filtering_mode) { +TextureAtlas::TextureAtlas(const Color4* pixels, const Vector2i& size, AtlasRegion** regions, unsigned int region_count, FilteringMode filtering_mode, SampleRate anisotropy) : Texture(pixels, size, filtering_mode, anisotropy) { this->regions.resize(region_count); for (unsigned int i = 0; i < region_count; ++i) this->regions[i] = *regions[i];