Skip to content

Commit 105c5c4

Browse files
committed
added animations!
1 parent 71a04bc commit 105c5c4

10 files changed

+130
-14
lines changed

FluffyEngine/Animation.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "Animation.h"
2+
3+
Fluffy::Animation::Animation(const glm::vec2& textureSize, int textureColumns, const int textureRows, float framesPerSecond)
4+
: m_TextureSize{ textureSize }
5+
, m_TextureColumns{ textureColumns }
6+
, m_TextureRows{ textureRows }
7+
, m_FramesPerSecond{ framesPerSecond }
8+
, m_SourceRect{ 0.0f, 0.0f, m_TextureSize.x / m_TextureColumns, m_TextureSize.y / m_TextureRows }
9+
{
10+
11+
}
12+
13+
void Fluffy::Animation::Reset()
14+
{
15+
m_SourceRect.left = 0.0f;
16+
m_SourceRect.top = 0.0f;
17+
18+
m_CurrentFrame.x = 0;
19+
m_CurrentFrame.y = 0;
20+
m_AccumulatedSeconds = 0.0f;
21+
}
22+
23+
void Fluffy::Animation::Update(float deltaTime)
24+
{
25+
m_AccumulatedSeconds += deltaTime;
26+
27+
if (m_AccumulatedSeconds >= (1.0f / m_FramesPerSecond))
28+
{
29+
m_AccumulatedSeconds = 0.0f;
30+
31+
if (++m_CurrentFrame.x >= m_TextureColumns)
32+
{
33+
m_CurrentFrame.x = 0.0f;
34+
35+
if (++m_CurrentFrame.y >= m_TextureRows)
36+
{
37+
m_CurrentFrame.y = 0.0f;
38+
}
39+
40+
m_SourceRect.top = m_SourceRect.height * m_CurrentFrame.y;
41+
}
42+
43+
m_SourceRect.left = m_SourceRect.width * m_CurrentFrame.x;
44+
}
45+
}

FluffyEngine/Animation.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
#include "Rectf.h"
3+
#include "glm/glm.hpp"
4+
5+
namespace Fluffy
6+
{
7+
class Animation final
8+
{
9+
public:
10+
Animation() = default;
11+
Animation(const glm::vec2& textureSize, int textureColumns, const int textureRows, float framesPerSecond);
12+
~Animation() = default;
13+
14+
void Reset();
15+
void Update(float elapsedSec);
16+
inline const Rectf& GetSourceRect() const { return m_SourceRect; }
17+
inline float GetDuration() const { return (m_TextureColumns * m_TextureRows) / m_FramesPerSecond; }
18+
inline bool HasFrames() const { return m_TextureColumns > 1 || m_TextureRows > 1; }
19+
20+
private:
21+
glm::vec2 m_TextureSize{};
22+
int m_TextureColumns{ 1 };
23+
int m_TextureRows{ 1 };
24+
Rectf m_SourceRect{};
25+
float m_FramesPerSecond{ 8.0f };
26+
float m_AccumulatedSeconds{ 0.0f };
27+
glm::vec2 m_CurrentFrame{ 0.0f, 0.0f };
28+
};
29+
}

FluffyEngine/FluffyEngine.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
</Lib>
199199
</ItemDefinitionGroup>
200200
<ItemGroup>
201+
<ClInclude Include="Animation.h" />
201202
<ClInclude Include="BaseInputDevice.h" />
202203
<ClInclude Include="Character.h" />
203204
<ClInclude Include="CollidersHandler.h" />
@@ -231,6 +232,7 @@
231232
<ClInclude Include="Utils.h" />
232233
</ItemGroup>
233234
<ItemGroup>
235+
<ClCompile Include="Animation.cpp" />
234236
<ClCompile Include="BaseInputDevice.cpp" />
235237
<ClCompile Include="CollidersHandler.cpp" />
236238
<ClCompile Include="Command.cpp" />

FluffyEngine/FluffyEngine.vcxproj.filters

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@
108108
<ClInclude Include="RectColliderComponent.h">
109109
<Filter>Header Files</Filter>
110110
</ClInclude>
111+
<ClInclude Include="Animation.h">
112+
<Filter>Header Files</Filter>
113+
</ClInclude>
111114
</ItemGroup>
112115
<ItemGroup>
113116
<ClCompile Include="ResourceManager.cpp">
@@ -182,5 +185,8 @@
182185
<ClCompile Include="CollidersHandler.cpp">
183186
<Filter>Source Files</Filter>
184187
</ClCompile>
188+
<ClCompile Include="Animation.cpp">
189+
<Filter>Source Files</Filter>
190+
</ClCompile>
185191
</ItemGroup>
186192
</Project>

FluffyEngine/Renderer.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "Renderer.h"
33
#include "SceneManager.h"
44
#include "Texture2D.h"
5+
#include "Rectf.h"
56

67
namespace Fluffy
78
{
@@ -68,5 +69,24 @@ namespace Fluffy
6869
SDL_RenderCopy(GetSDLRenderer(), texture.GetSDLTexture(), nullptr, &dst);
6970
}
7071

72+
void Renderer::RenderTexture(const Texture2D& texture, const Rectf& source, const float x, const float y) const
73+
{
74+
const SDL_Rect src
75+
{
76+
static_cast<int>(source.left),
77+
static_cast<int>(source.top),
78+
static_cast<int>(source.width),
79+
static_cast<int>(source.height),
80+
};
81+
82+
SDL_Rect dst{};
83+
dst.x = static_cast<int>(x);
84+
dst.y = static_cast<int>(y);
85+
dst.w = static_cast<int>(source.width);
86+
dst.h = static_cast<int>(source.height);
87+
88+
SDL_RenderCopy(GetSDLRenderer(), texture.GetSDLTexture(), &src, &dst);
89+
}
90+
7191
SDL_Renderer* Renderer::GetSDLRenderer() const { return m_pRenderer; }
7292
}

FluffyEngine/Renderer.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Fluffy
1818

1919
void RenderTexture(const Texture2D& texture, float x, float y) const;
2020
void RenderTexture(const Texture2D& texture, float x, float y, float width, float height) const;
21+
void RenderTexture(const Texture2D& texture, const struct Rectf& source, const float x, const float y) const;
2122

2223
SDL_Renderer* GetSDLRenderer() const;
2324

FluffyEngine/Sprite.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@
77

88
namespace Fluffy
99
{
10-
Sprite::Sprite(GameObject* pOwner, const std::string& fileName)
10+
Sprite::Sprite(GameObject* pOwner, const std::string& fileName, const int textureColumns, const int textureRows, const float framesPerSecond)
1111
: Component(pOwner)
1212
{
13+
glm::vec2 textureSize{};
14+
1315
if (!fileName.empty())
16+
{
1417
m_pTexture = ResourceManager::GetInstance().LoadTexture(fileName);
18+
textureSize = m_pTexture->GetSize();
19+
}
20+
21+
m_Animation = Animation(textureSize, textureColumns, textureRows, framesPerSecond);
1522
}
1623

17-
Sprite::Sprite(class GameObject* pOwner, const std::string& fileName, const glm::vec2& offset)
18-
: Sprite(pOwner, fileName)
24+
Sprite::Sprite(class GameObject* pOwner, const std::string& fileName, const glm::vec2& offset, const int textureColumns, const int textureRows, const float framesPerSecond)
25+
: Sprite(pOwner, fileName, textureColumns, textureRows, framesPerSecond)
1926
{
2027
m_Offset = offset;
2128
}
@@ -29,13 +36,20 @@ namespace Fluffy
2936
};
3037
}
3138

39+
void Sprite::Update(const float deltaTime)
40+
{
41+
if (m_Animation.HasFrames())
42+
m_Animation.Update(deltaTime);
43+
}
44+
3245
void Sprite::Render() const
3346
{
3447
if (m_pTexture == nullptr)
3548
return;
3649

37-
const auto& pos = m_pOwner->GetWorldPosition() - (m_pTexture->GetSize() * 0.5f) + m_Offset;
38-
Renderer::GetInstance().RenderTexture(*m_pTexture, pos.x, pos.y);
50+
const glm::vec2 size{ m_Animation.GetSourceRect().width, m_Animation.GetSourceRect().height };
51+
const auto& pos = m_pOwner->GetWorldPosition() - (size * 0.5f) + m_Offset;
52+
Renderer::GetInstance().RenderTexture(*m_pTexture, m_Animation.GetSourceRect(), pos.x, pos.y);
3953
}
4054

4155
void Sprite::SetTexture(const std::string& fileName)

FluffyEngine/Sprite.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,29 @@
55
#include "IRenderable.h"
66
#include "Texture2D.h"
77
#include "Rectf.h"
8+
#include "Animation.h"
89

910
namespace Fluffy
1011
{
1112
class Sprite : public Component, public IRenderable
1213
{
1314
public:
15+
void Update(const float deltaTime) override;
1416
void Render() const override;
1517
void SetTexture(const std::string& fileName);
1618
inline glm::vec2 GetTextureSize() const { return m_pTexture->GetSize(); }
1719
Rectf GetRect() const;
1820

19-
Sprite(class GameObject* pOwner, const std::string& fileName);
20-
Sprite(class GameObject* pOwner, const std::string& fileName, const glm::vec2& offset);
21+
Sprite(class GameObject* pOwner, const std::string& fileName, const int textureColumns = 1, const int textureRows = 1, const float framesPerSecond = 0.0f);
22+
Sprite(class GameObject* pOwner, const std::string& fileName, const glm::vec2& offset, const int textureColumns = 1, const int textureRows = 1, const float framesPerSecond = 0.0f);
2123
virtual ~Sprite() = default;
2224
Sprite(const Sprite& other) = delete;
2325
Sprite(Sprite&& other) = delete;
2426
Sprite& operator=(const Sprite& other) = delete;
2527
Sprite& operator=(Sprite&& other) = delete;
2628

2729
private:
30+
Animation m_Animation{};
2831
std::shared_ptr<class Texture2D> m_pTexture{};
2932
glm::vec2 m_Offset{ 0.0f, 0.0f };
3033
};

Galaga/CharactersManager.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ void CharactersManager::StartLevel1()
2929
Parser::GetInstance().ParseEnemyLayoutData("D:/Repos/FluffyEngine/Data/Formations/galaga_level_1.csv", data);
3030
data;
3131

32-
std::string beeFormationsData{ "Formations/Formation1Bees.txt" };
33-
std::string butterflyFormationsData{ "Formations/Formation1Butterflies.txt" };
34-
std::string bossFormationsData{ "Formations/Formation1Boss.txt" };
35-
3632
std::queue<EnemyEnteringData> enemiesData{};
3733

3834
BezierPath firstPath{};
@@ -84,7 +80,7 @@ EnemyCharacter* CharactersManager::SpawnEnemy(const EnemyEnteringData& data)
8480
switch (data.type)
8581
{
8682
case EnemyType::Boss:
87-
fileName = "galaga_boss.png";
83+
fileName = "galaga_boss_green.png";
8884
break;
8985

9086
case EnemyType::Butterfly:
@@ -96,7 +92,8 @@ EnemyCharacter* CharactersManager::SpawnEnemy(const EnemyEnteringData& data)
9692
break;
9793
}
9894

99-
pEnemy->AddComponent<Fluffy::Sprite>(fileName);
95+
pEnemy->AddComponent<Fluffy::Sprite>(fileName, 2, 1, 2.0f);
96+
10097
const float enemySpeed{ 200.0f };
10198
EnemyCharacter* pEnemyCharacter{ pEnemy->AddComponent<EnemyCharacter>(data.type, enemySpeed) };
10299
m_pOwner->GetScene()->Add(pEnemy);

Galaga/ShootCommand.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ void ShootCommand::Execute()
1515
{
1616
const glm::vec2 position{ CharactersManager::GetInstance()->GetPlayer(m_PlayerIndex)->GetGameObject()->GetWorldPosition() };
1717
BulletsManager::GetInstance().Shoot(m_PlayerIndex, position);
18-
++m_ShotsFired;
1918
}

0 commit comments

Comments
 (0)