Skip to content

Commit

Permalink
Rename TextureOffset -> TextureRegion, Add NormalisedTextureRegion
Browse files Browse the repository at this point in the history
Adds animation example from atlas
  • Loading branch information
Jerboa-app committed Jul 31, 2024
1 parent abf540e commit ccb4e6f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 48 deletions.
51 changes: 39 additions & 12 deletions examples/Sprite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand All @@ -90,7 +91,7 @@ int main(int argv, char ** argc)
(
{
trans["sJerboa"],
jGL::TextureOffset(),
jGL::TextureRegion(),
jerboa
},
"sJerboa"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -140,17 +141,37 @@ int main(int argv, char ** argc)
(
{
trans["sAtlasFull"],
jGL::TextureOffset(),
jGL::TextureRegion(),
atlas
},
"sAtlasFull"
);

std::vector<jGL::TextureRegion> 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"
Expand All @@ -160,7 +181,7 @@ int main(int argv, char ** argc)
(
{
trans["sHeart"],
jGL::TextureOffset(),
jGL::TextureRegion(),
heart
},
"sHeart"
Expand All @@ -170,7 +191,7 @@ int main(int argv, char ** argc)
(
{
trans["sRandom"],
jGL::TextureOffset(),
jGL::TextureRegion(),
random
},
"sRandom"
Expand All @@ -180,7 +201,7 @@ int main(int argv, char ** argc)
(
{
trans["lowest"],
jGL::TextureOffset(),
jGL::TextureRegion(),
Pi
},
"lowest"
Expand All @@ -190,7 +211,7 @@ int main(int argv, char ** argc)
(
{
trans["middle"],
jGL::TextureOffset(),
jGL::TextureRegion(),
heart,
0.5f
},
Expand All @@ -202,7 +223,7 @@ int main(int argv, char ** argc)
(
{
trans["highest"],
jGL::TextureOffset(),
jGL::TextureRegion(),
jerboa
},
"highest",
Expand All @@ -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;
Expand Down
41 changes: 35 additions & 6 deletions include/jGL/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
63 changes: 39 additions & 24 deletions include/jGL/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
*/

Expand All @@ -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<Texture> tex,
float alpha = 1.0f
)
Expand All @@ -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;
}

Expand All @@ -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> 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;

};
}
Expand Down
9 changes: 4 additions & 5 deletions include/jGL/spriteRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/jGL/OpenGL/glSpriteRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ccb4e6f

Please sign in to comment.