aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/graphics
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/graphics')
-rw-r--r--src/libjin/graphics/animations/je_animation.h3
-rw-r--r--src/libjin/graphics/animations/je_animator.cpp41
-rw-r--r--src/libjin/graphics/animations/je_animator.h18
-rw-r--r--src/libjin/graphics/fonts/je_font.h5
-rw-r--r--src/libjin/graphics/fonts/je_texture_font.h4
-rw-r--r--src/libjin/graphics/je_canvas.h3
-rw-r--r--src/libjin/graphics/je_graphic.h3
-rw-r--r--src/libjin/graphics/je_mesh.h5
-rw-r--r--src/libjin/graphics/je_renderable.h34
-rw-r--r--src/libjin/graphics/je_sprite.cpp18
-rw-r--r--src/libjin/graphics/je_sprite.h18
-rw-r--r--src/libjin/graphics/je_sprite_sheet.cpp22
-rw-r--r--src/libjin/graphics/je_sprite_sheet.h6
-rw-r--r--src/libjin/graphics/je_texture.h3
-rw-r--r--src/libjin/graphics/particles/je_particle.h2
-rw-r--r--src/libjin/graphics/particles/je_particle_system.h2
16 files changed, 133 insertions, 54 deletions
diff --git a/src/libjin/graphics/animations/je_animation.h b/src/libjin/graphics/animations/je_animation.h
index 1c8a885..4037721 100644
--- a/src/libjin/graphics/animations/je_animation.h
+++ b/src/libjin/graphics/animations/je_animation.h
@@ -41,6 +41,9 @@ namespace JinEngine
std::vector<const Sprite*> mFrames;
+ ///
+ /// Frame per second.
+ ///
float mSpeed;
float mLoop;
diff --git a/src/libjin/graphics/animations/je_animator.cpp b/src/libjin/graphics/animations/je_animator.cpp
index 4528e8c..9550116 100644
--- a/src/libjin/graphics/animations/je_animator.cpp
+++ b/src/libjin/graphics/animations/je_animator.cpp
@@ -48,11 +48,12 @@ namespace JinEngine
{
if (!mIsActive || !mAnimation)
return;
+ float interval = 1 / mSpeed;
mTick += dt;
uint fc = mAnimation->getFrameCount();
- while (mTick >= mSpeed)
+ while (mTick >= interval)
{
- mTick -= mSpeed;
+ mTick -= interval;
++mIndex;
if (mLoop)
mIndex %= fc;
@@ -65,9 +66,9 @@ namespace JinEngine
mIndex = 0;
}
- void Animator::render(float x, float y, float sx, float sy, float r)
+ void Animator::render(float x, float y, float sx, float sy, float r) const
{
- if (!mIsActive || !mAnimation)
+ if (!mAnimation)
return;
const Sprite* spr = mAnimation->getFrame(mIndex);
if (spr)
@@ -80,6 +81,38 @@ namespace JinEngine
}
+ void Animator::setSpeed(float speed)
+ {
+ mSpeed = speed;
+ }
+
+ void Animator::setDefaultSpeed()
+ {
+ if(mAnimation != nullptr)
+ mSpeed = mAnimation->getSpeed();
+ else
+ {
+ jin_log_error("Animation is null.");
+ return;
+ }
+ }
+
+ void Animator::setLoop(bool loop)
+ {
+ mLoop = loop;
+ }
+
+ void Animator::setDefaultLoop()
+ {
+ if(mAnimation != nullptr)
+ mLoop = mAnimation->isLoop();
+ else
+ {
+ jin_log_error("Animation is null.");
+ return;
+ }
+ }
+
}
}
} \ No newline at end of file
diff --git a/src/libjin/graphics/animations/je_animator.h b/src/libjin/graphics/animations/je_animator.h
index 72d9021..ad8138d 100644
--- a/src/libjin/graphics/animations/je_animator.h
+++ b/src/libjin/graphics/animations/je_animator.h
@@ -1,6 +1,10 @@
#ifndef __JE_ANIMATOR_H__
#define __JE_ANIMATOR_H__
+#include <string>
+
+#include "../../utils/je_log.h"
+
#include "je_animation.h"
namespace JinEngine
@@ -10,7 +14,7 @@ namespace JinEngine
namespace Animations
{
- class Animator
+ class Animator : public IRenderable
{
public:
Animator();
@@ -25,13 +29,21 @@ namespace JinEngine
void rewind();
- void render(float x, float y, float sx, float sy, float r);
+ void render(float x, float y, float sx, float sy, float r) const override;
void setAnimation(const Animation* anim);
void forceToFrame(uint index);
- private:
+ void setSpeed(float speed);
+
+ void setDefaultSpeed();
+
+ void setLoop(bool loop);
+
+ void setDefaultLoop();
+
+ private:
const Animation* mAnimation;
uint mIndex;
diff --git a/src/libjin/graphics/fonts/je_font.h b/src/libjin/graphics/fonts/je_font.h
index 9581b9f..e72ef6b 100644
--- a/src/libjin/graphics/fonts/je_font.h
+++ b/src/libjin/graphics/fonts/je_font.h
@@ -2,6 +2,9 @@
#define __JE_FONT_H__
#include <vector>
+
+#include "../je_renderable.h"
+
#include "je_text.h"
namespace JinEngine
@@ -22,7 +25,7 @@ namespace JinEngine
///
/// Base Font class.
///
- class Font
+ class Font : public IRenderable
{
public:
///
diff --git a/src/libjin/graphics/fonts/je_texture_font.h b/src/libjin/graphics/fonts/je_texture_font.h
index 8a50699..df8f956 100644
--- a/src/libjin/graphics/fonts/je_texture_font.h
+++ b/src/libjin/graphics/fonts/je_texture_font.h
@@ -23,9 +23,7 @@ namespace JinEngine
///
///
///
- class TextureFont
- : public Font
- , public Graphic
+ class TextureFont : public Font, public Graphic
{
public:
diff --git a/src/libjin/graphics/je_canvas.h b/src/libjin/graphics/je_canvas.h
index 3964517..a908747 100644
--- a/src/libjin/graphics/je_canvas.h
+++ b/src/libjin/graphics/je_canvas.h
@@ -14,8 +14,7 @@ namespace JinEngine
///
/// A canvas is a rendering target.
///
- class Canvas
- : public Graphic
+ class Canvas : public Graphic
{
public:
///
diff --git a/src/libjin/graphics/je_graphic.h b/src/libjin/graphics/je_graphic.h
index 636ea60..58de7ec 100644
--- a/src/libjin/graphics/je_graphic.h
+++ b/src/libjin/graphics/je_graphic.h
@@ -7,6 +7,7 @@
#include "../math/je_vector2.hpp"
#include "../math/je_transform.h"
+#include "je_renderable.h"
#include "je_gl.h"
#include "je_bitmap.h"
@@ -19,7 +20,7 @@ namespace JinEngine
/// Class inherites Graphic doesn't keep any state such as origin, scale and other properties. Very low
/// level visualized resources.
///
- class Graphic
+ class Graphic : public IRenderable
{
public:
///
diff --git a/src/libjin/graphics/je_mesh.h b/src/libjin/graphics/je_mesh.h
index e0a38f8..27c0cb7 100644
--- a/src/libjin/graphics/je_mesh.h
+++ b/src/libjin/graphics/je_mesh.h
@@ -14,12 +14,13 @@ namespace JinEngine
class Mesh
{
public:
- void setGraphic();
-
+ void setGraphic(const Graphic* graphic);
+ void pushVertex(float x, float y, float u, float v);
private:
const Graphic* mGraphic;
+
};
} // namespace Graphics
diff --git a/src/libjin/graphics/je_renderable.h b/src/libjin/graphics/je_renderable.h
new file mode 100644
index 0000000..e1d0ae8
--- /dev/null
+++ b/src/libjin/graphics/je_renderable.h
@@ -0,0 +1,34 @@
+#ifndef __JE_RENDERABLE_H__
+#define __JE_RENDERABLE_H__
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+
+ enum class Origin
+ {
+ TopLeft,
+ TopCenter,
+ TopRight,
+ MiddleLeft,
+ MiddleCenter,
+ MiddleRight,
+ BottomLeft,
+ BottomCenter,
+ BottomRight
+ };
+
+ class IRenderable
+ {
+ public:
+ virtual void render(float x, float y, float sx, float sy, float r) const {};
+ virtual void render(float x, float y, float sx, float sy, float r, float ox, float oy) const {};
+ virtual void render(float x, float y, float sx, float sy, float r, Origin origin) const {};
+
+ };
+
+ }
+}
+
+#endif \ No newline at end of file
diff --git a/src/libjin/graphics/je_sprite.cpp b/src/libjin/graphics/je_sprite.cpp
index a82be79..890dc63 100644
--- a/src/libjin/graphics/je_sprite.cpp
+++ b/src/libjin/graphics/je_sprite.cpp
@@ -61,31 +61,31 @@ namespace JinEngine
Vector2<float>* org = const_cast<Vector2<float>*>(&mOrigin);
switch (origin)
{
- case TopLeft:
+ case Origin::TopLeft:
org->set(l, t);
break;
- case TopCenter:
+ case Origin::TopCenter:
org->set(r / 2.f, t);
break;
- case TopRight:
+ case Origin::TopRight:
org->set(r, t);
break;
- case MiddleLeft:
+ case Origin::MiddleLeft:
org->set(l, b / 2.f);
break;
- case MiddleCenter:
+ case Origin::MiddleCenter:
org->set(r / 2.f, b / 2.f);
break;
- case MiddleRight:
+ case Origin::MiddleRight:
org->set(r, b / 2.f);
break;
- case BottomLeft:
+ case Origin::BottomLeft:
org->set(l, b);
break;
- case BottomCenter:
+ case Origin::BottomCenter:
org->set(r / 2.f, b);
break;
- case BottomRight:
+ case Origin::BottomRight:
org->set(r, b);
break;
}
diff --git a/src/libjin/graphics/je_sprite.h b/src/libjin/graphics/je_sprite.h
index 4050ed8..c7c5a8b 100644
--- a/src/libjin/graphics/je_sprite.h
+++ b/src/libjin/graphics/je_sprite.h
@@ -6,6 +6,7 @@
#include "je_color.h"
#include "je_graphic.h"
+#include "je_renderable.h"
namespace JinEngine
{
@@ -15,23 +16,10 @@ namespace JinEngine
///
/// A sprite is unit of rendering. Animation is based on sprite, but not texture or other graphic stuff.
///
- class Sprite
+ class Sprite : public IRenderable
{
public:
- enum Origin
- {
- TopLeft,
- TopCenter,
- TopRight,
- MiddleLeft,
- MiddleCenter,
- MiddleRight,
- BottomLeft,
- BottomCenter,
- BottomRight
- };
-
Sprite(const Graphic* graphic, const Math::Quad& quad, Origin origin);
Sprite(const Graphic* graphic, const Math::Quad& quad, float ox, float oy);
@@ -44,7 +32,7 @@ namespace JinEngine
Math::Vector2<int> getSize();
- void render(float x, float y, float sx, float sy, float r) const;
+ void render(float x, float y, float sx, float sy, float r) const override;
private:
diff --git a/src/libjin/graphics/je_sprite_sheet.cpp b/src/libjin/graphics/je_sprite_sheet.cpp
index 73d3e81..6129da7 100644
--- a/src/libjin/graphics/je_sprite_sheet.cpp
+++ b/src/libjin/graphics/je_sprite_sheet.cpp
@@ -18,7 +18,7 @@ namespace JinEngine
{
}
- Sprite* SpriteSheet::createSprite(const Math::Quad& quad, Sprite::Origin origin)
+ Sprite* SpriteSheet::createSprite(const Math::Quad& quad, Origin origin)
{
Sprite* spr = new Sprite(mGraphic, quad, origin);
return spr;
@@ -30,41 +30,49 @@ namespace JinEngine
return spr;
}
- std::vector<Sprite*> SpriteSheet::createSprites(uint row, uint colum, uint w, uint h, Sprite::Origin origin)
+ std::vector<Sprite*> SpriteSheet::createSprites(uint count, uint row, uint colum, uint w, uint h, Origin origin, uint offx, uint offy)
{
vector<Sprite*> sprites;
+ int i = 0;
for (int r = 0; r < row; ++r)
{
for (int c = 0; c < colum; ++c)
{
Quad quad;
- quad.x = (r * colum + c) * w;
- quad.y = r * h;
+ quad.x = (r * colum + c) * w + offx;
+ quad.y = r * h + offy;
quad.w = w;
quad.h = h;
Sprite* spr = new Sprite(mGraphic, quad, origin);
sprites.push_back(spr);
+ if ((++i) == count)
+ goto done;
}
}
+ done:
return sprites;
}
- vector<Sprite*> SpriteSheet::createSprites(uint row, uint colum, uint w, uint h, float ox, float oy)
+ vector<Sprite*> SpriteSheet::createSprites(uint count, uint row, uint colum, uint w, uint h, float ox, float oy, uint offx, uint offy)
{
vector<Sprite*> sprites;
+ int i = 0;
for (int r = 0; r < row; ++r)
{
for (int c = 0; c < colum; ++c)
{
Quad quad;
- quad.x = (r * colum + c) * w;
- quad.y = r * h;
+ quad.x = (r * colum + c) * w + offx;
+ quad.y = r * h + offy;
quad.w = w;
quad.h = h;
Sprite* spr = new Sprite(mGraphic, quad, ox, oy);
sprites.push_back(spr);
+ if ((++i) == count)
+ goto done;
}
}
+ done:
return sprites;
}
diff --git a/src/libjin/graphics/je_sprite_sheet.h b/src/libjin/graphics/je_sprite_sheet.h
index a3db946..57e31f7 100644
--- a/src/libjin/graphics/je_sprite_sheet.h
+++ b/src/libjin/graphics/je_sprite_sheet.h
@@ -18,7 +18,7 @@ namespace JinEngine
///
/// Create a new sprite in sheet.
///
- Sprite* createSprite(const Math::Quad& quad, Sprite::Origin origin);
+ Sprite* createSprite(const Math::Quad& quad, Origin origin);
///
/// Create a new sprite in sheet.
@@ -28,9 +28,9 @@ namespace JinEngine
///
///
///
- std::vector<Sprite*> createSprites(uint row, uint colum, uint w, uint h, Sprite::Origin origin);
+ std::vector<Sprite*> createSprites(uint count, uint row, uint colum, uint w, uint h, Origin origin = Origin::TopLeft, uint offx = 0, uint offy = 0);
- std::vector<Sprite*> createSprites(uint row, uint colum, uint w, uint h, float ox, float oy);
+ std::vector<Sprite*> createSprites(uint count, uint row, uint colum, uint w, uint h, float ox = 0, float oy = 0, uint offx = 0, uint offy = 0);
SpriteSheet(const Graphic* graphic);
diff --git a/src/libjin/graphics/je_texture.h b/src/libjin/graphics/je_texture.h
index 566ba84..8e667f1 100644
--- a/src/libjin/graphics/je_texture.h
+++ b/src/libjin/graphics/je_texture.h
@@ -17,8 +17,7 @@ namespace JinEngine
///
///
///
- class Texture
- : public Graphic
+ class Texture : public Graphic
{
public:
///
diff --git a/src/libjin/graphics/particles/je_particle.h b/src/libjin/graphics/particles/je_particle.h
index c03f4ca..8012024 100644
--- a/src/libjin/graphics/particles/je_particle.h
+++ b/src/libjin/graphics/particles/je_particle.h
@@ -90,7 +90,7 @@ namespace JinEngine
/// A single particle contains various properties of particle, such as position, accelaration, color
/// and other attributes changed over time.
///
- struct Particle
+ struct Particle : public IRenderable
{
///
/// Default constructor.
diff --git a/src/libjin/graphics/particles/je_particle_system.h b/src/libjin/graphics/particles/je_particle_system.h
index fb4b8c7..614ee7c 100644
--- a/src/libjin/graphics/particles/je_particle_system.h
+++ b/src/libjin/graphics/particles/je_particle_system.h
@@ -33,7 +33,7 @@ namespace JinEngine
///
/// Particle emitter, handle all particles it emitts.
///
- class ParticleSystem
+ class ParticleSystem : public IRenderable
{
public:
///