diff --git a/examples/Sprite/main.cpp b/examples/Sprite/main.cpp index 7a400592..8bb35743 100644 --- a/examples/Sprite/main.cpp +++ b/examples/Sprite/main.cpp @@ -75,6 +75,7 @@ int main(int argv, char ** argc) {"sAtlas3", jGL::Transform(ax, ay+as, 0.0f, scale32Pixels*0.5f)}, {"sAtlas4", jGL::Transform(ax+as, ay+as, 0.0f, scale32Pixels*0.5f)}, {"sAtlasFull", jGL::Transform(ax, ay+as+scale32Pixels, 0.0f, scale32Pixels)}, + {"sAtlasAnimated", jGL::Transform(ax+as, ay-scale32Pixels, 0.0f, scale32Pixels*0.5f)}, {"sJerboa", jGL::Transform(0.1f, 0.9f, 0.0f, 0.1f)}, {"sPi", jGL::Transform(0.1f, 0.1f, 0.0f, 0.1f)}, {"sHeart", jGL::Transform(0.5f, 0.5f, 0.0f, 0.1f)}, @@ -90,7 +91,7 @@ int main(int argv, char ** argc) ( { trans["sJerboa"], - jGL::TextureOffset(), + jGL::TextureRegion(), jerboa }, "sJerboa" @@ -100,7 +101,7 @@ int main(int argv, char ** argc) ( { trans["sAtlas1"], - jGL::TextureOffset(0.0, 0.0, 16.0, 16.0), + jGL::TextureRegion(0, 0, 16, 16), atlas }, "sAtlas1" @@ -110,7 +111,7 @@ int main(int argv, char ** argc) ( { trans["sAtlas2"], - jGL::TextureOffset(16.0, 0.0, 16.0, 16.0), + jGL::TextureRegion(16, 0, 16, 16), atlas }, "sAtlas2" @@ -120,7 +121,7 @@ int main(int argv, char ** argc) ( { trans["sAtlas3"], - jGL::TextureOffset(0.0, 16.0, 16.0, 16.0), + jGL::TextureRegion(0, 16, 16, 16), atlas }, "sAtlas3" @@ -130,7 +131,7 @@ int main(int argv, char ** argc) ( { trans["sAtlas4"], - jGL::TextureOffset(16.0, 16.0, 16.0, 16.0), + jGL::TextureRegion(16, 16, 16, 16), atlas }, "sAtlas4" @@ -140,17 +141,37 @@ int main(int argv, char ** argc) ( { trans["sAtlasFull"], - jGL::TextureOffset(), + jGL::TextureRegion(), atlas }, "sAtlasFull" ); + std::vector animationFrames + { + jGL::TextureRegion(0, 0, 16, 16), + jGL::TextureRegion(16, 0, 16, 16), + jGL::TextureRegion(0, 16, 16, 16), + jGL::TextureRegion(16, 16, 16, 16) + }; + uint8_t animationFrame = 0; + + sprites->add + ( + { + trans["sAtlasAnimated"], + animationFrames[animationFrame], + atlas + }, + "sAtlasAnimated" + ); + + sprites->add ( { trans["sPi"], - jGL::TextureOffset(), + jGL::TextureRegion(), Pi }, "sPi" @@ -160,7 +181,7 @@ int main(int argv, char ** argc) ( { trans["sHeart"], - jGL::TextureOffset(), + jGL::TextureRegion(), heart }, "sHeart" @@ -170,7 +191,7 @@ int main(int argv, char ** argc) ( { trans["sRandom"], - jGL::TextureOffset(), + jGL::TextureRegion(), random }, "sRandom" @@ -180,7 +201,7 @@ int main(int argv, char ** argc) ( { trans["lowest"], - jGL::TextureOffset(), + jGL::TextureRegion(), Pi }, "lowest" @@ -190,7 +211,7 @@ int main(int argv, char ** argc) ( { trans["middle"], - jGL::TextureOffset(), + jGL::TextureRegion(), heart, 0.5f }, @@ -202,7 +223,7 @@ int main(int argv, char ** argc) ( { trans["highest"], - jGL::TextureOffset(), + jGL::TextureRegion(), jerboa }, "highest", @@ -228,6 +249,12 @@ int main(int argv, char ** argc) trans["sHeart"] = jGL::Transform(0.5f, 0.5f, theta, 0.1f); trans["sPi"] = jGL::Transform(0.2f, 0.2f, theta, scale); + sprites->getSprite("sAtlasAnimated").setTextureRegion(animationFrames[animationFrame]); + if (frameId % 15 == 0) + { + animationFrame = (animationFrame+1)%animationFrames.size(); + } + sprites->draw(); delta = 0.0; diff --git a/include/jGL/primitive.h b/include/jGL/primitive.h index 23e42afd..d809b87c 100644 --- a/include/jGL/primitive.h +++ b/include/jGL/primitive.h @@ -56,25 +56,54 @@ namespace jGL }; /** - * @brief Rectangular region of a texture. + * @brief Rectangular region of a texture in pixels. * * @param tx Lower left corner of region, along width. * @param ty Lower left corner of region, along height. * @param lx Length of region along width. * @param ly Length of region along height. * - * @remark Negative lx and ly will be handled as the maximum lengths in e.g. + * @remark 0 lx and ly will be handled as the maximum lengths in e.g. * SpriteRenderer. */ - struct TextureOffset : public Primitive + struct TextureRegion : public Primitive { - TextureOffset(float x, float y, float w, float h) + TextureRegion(uint16_t x, uint16_t y, uint16_t w, uint16_t h) : tx(x), ty(y), lx(w), ly(h) {} - TextureOffset() - : tx(0.0f), ty(0.0f), lx(-1.0), ly(-1.0) + TextureRegion() + : tx(0), ty(0), lx(0), ly(0) + {} + + uint16_t tx; + uint16_t ty; + uint16_t lx; + uint16_t ly; + }; + + /** + * @brief Rectangular region of a texture, normalised by the textures dimensions. + * + * @param tx Lower left corner of region, along width, in [0, 1]. + * @param ty Lower left corner of region, along height, in [0, 1]. + * @param lx Length of region along width, in [0, 1]. + * @param ly Length of region along height, in [0, 1]. + * + * @remark Values are normalised to a texture width and height. + * @remark 0 lx and ly will be handled as the maximum lengths in e.g. + * SpriteRenderer. + */ + struct NormalisedTextureRegion : public Primitive + { + + NormalisedTextureRegion(float x, float y, float w, float h) + : tx(x), ty(y), lx(w), ly(h) + {} + + NormalisedTextureRegion() + : tx(0), ty(0), lx(0), ly(0) {} float tx; diff --git a/include/jGL/sprite.h b/include/jGL/sprite.h index c50dc519..08848ea5 100644 --- a/include/jGL/sprite.h +++ b/include/jGL/sprite.h @@ -12,7 +12,7 @@ * * Observes a Transform (position, orientation, scale), and a Texture. * - * Rendered using the TextureOffset (pixel units) region of the Texture, at the alpha value. + * Rendered using the TextureRegion (pixel units) region of the Texture, at the alpha value. * */ @@ -23,10 +23,18 @@ namespace jGL public: + /** + * @brief Construct a Sprite + * + * @param tra x, y, theta, scale Transform + * @param to Region of texture to draw + * @param tex Texture to draw + * @param alpha Transparency modifier (alpha * texture alpha) + */ Sprite ( const Transform & tra, - TextureOffset to, + TextureRegion to, const std::shared_ptr tex, float alpha = 1.0f ) @@ -48,15 +56,22 @@ namespace jGL const float getAlpha() const { return alpha; } /** - * @brief Get the Texture Offset in pixel units. + * @brief Get the TextureRegion in pixel units. * - * @param normalised return the texture region in normalised units. - * - * @return TextureOffset (pixel by default). + * @return TextureRegion */ - TextureOffset getTextureOffset(bool normalised = false) const + TextureRegion getTextureRegion() const { - if (!normalised) {return fromNormalised(texOffset); } + return fromNormalised(texOffset); + } + + /** + * @brief Get the NormalisedTextureRegion in normalised units. + * + * @return TextureRegion + */ + NormalisedTextureRegion getNormalisedTextureRegion() const + { return texOffset; } @@ -67,39 +82,39 @@ namespace jGL * @remark Lengths lx or ly < 0 will be set to maximum. * */ - void setTextureOffset(TextureOffset to) { texOffset = toNormalised(texOffset); } + void setTextureRegion(TextureRegion to) { texOffset = toNormalised(to); } const Transform & transform; const std::shared_ptr texture; protected: - TextureOffset toNormalised(TextureOffset to) + NormalisedTextureRegion toNormalised(TextureRegion to) { glm::vec3 whc = texture->size(); - if (to.lx < 0.0){ to.lx = whc.x; } - if (to.ly < 0.0){ to.ly = whc.y; } - return TextureOffset( - std::clamp(to.tx / whc.x, 0.0f, 1.0f), - std::clamp(to.ty / whc.y, 0.0f, 1.0f), - std::clamp(to.lx / whc.x, 0.0f, 1.0f), - std::clamp(to.ly / whc.y, 0.0f, 1.0f) + if (to.lx == 0){ to.lx = whc.x; } + if (to.ly == 0){ to.ly = whc.y; } + return NormalisedTextureRegion( + std::clamp(float(to.tx) / float(whc.x), 0.0f, 1.0f), + std::clamp(float(to.ty) / float(whc.y), 0.0f, 1.0f), + std::clamp(float(to.lx) / float(whc.x), 0.0f, 1.0f), + std::clamp(float(to.ly) / float(whc.y), 0.0f, 1.0f) ); } - TextureOffset fromNormalised(TextureOffset to) const + TextureRegion fromNormalised(NormalisedTextureRegion to) const { glm::vec3 whc = texture->size(); - return TextureOffset( - to.tx * whc.x, - to.ty * whc.y, - to.lx * whc.x, - to.ly * whc.y + return TextureRegion( + uint16_t(to.tx * whc.x), + uint16_t(to.ty * whc.y), + uint16_t(to.lx * whc.x), + uint16_t(to.ly * whc.y) ); } float alpha; - TextureOffset texOffset; + NormalisedTextureRegion texOffset; }; } diff --git a/include/jGL/spriteRenderer.h b/include/jGL/spriteRenderer.h index c2312822..48ef56e8 100644 --- a/include/jGL/spriteRenderer.h +++ b/include/jGL/spriteRenderer.h @@ -53,14 +53,13 @@ namespace jGL const Transform & getTransform(SpriteId id) { return getSprite(id).transform; } /** - * @brief Get a Sprites TextureOffset - * @remark In pixel units, see TextureOffset::getTextureOffset() + * @brief Get a Sprites TextureRegion + * @remark In pixel units, see TextureRegion::getTextureRegion() * * @param id - * @param normalised Return in normalised units. - * @return const TextureOffset + * @return const TextureRegion */ - const TextureOffset getTextureOffset(SpriteId id, bool normalised = false) { return getSprite(id).getTextureOffset(normalised); } + const TextureRegion getTextureRegion(SpriteId id) { return getSprite(id).getTextureRegion(); } /** * @brief Draw with overriding render priority and shader. diff --git a/src/jGL/OpenGL/glSpriteRenderer.cpp b/src/jGL/OpenGL/glSpriteRenderer.cpp index 2e535356..21fabdd6 100644 --- a/src/jGL/OpenGL/glSpriteRenderer.cpp +++ b/src/jGL/OpenGL/glSpriteRenderer.cpp @@ -39,7 +39,7 @@ namespace jGL::GL const Sprite & sprite = sprites.at(sid.second); const Transform & trans = sprite.transform; - const TextureOffset toff = sprite.getTextureOffset(true); + const NormalisedTextureRegion toff = sprite.getNormalisedTextureRegion(); const float alpha = sprite.getAlpha(); offsets[i*offsetDim] = trans.x;