Skip to content

Commit

Permalink
added animations!
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Jun 6, 2024
1 parent 71a04bc commit 105c5c4
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 14 deletions.
45 changes: 45 additions & 0 deletions FluffyEngine/Animation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "Animation.h"

Fluffy::Animation::Animation(const glm::vec2& textureSize, int textureColumns, const int textureRows, float framesPerSecond)
: m_TextureSize{ textureSize }
, m_TextureColumns{ textureColumns }
, m_TextureRows{ textureRows }
, m_FramesPerSecond{ framesPerSecond }
, m_SourceRect{ 0.0f, 0.0f, m_TextureSize.x / m_TextureColumns, m_TextureSize.y / m_TextureRows }
{

}

void Fluffy::Animation::Reset()
{
m_SourceRect.left = 0.0f;
m_SourceRect.top = 0.0f;

m_CurrentFrame.x = 0;
m_CurrentFrame.y = 0;
m_AccumulatedSeconds = 0.0f;
}

void Fluffy::Animation::Update(float deltaTime)
{
m_AccumulatedSeconds += deltaTime;

if (m_AccumulatedSeconds >= (1.0f / m_FramesPerSecond))
{
m_AccumulatedSeconds = 0.0f;

if (++m_CurrentFrame.x >= m_TextureColumns)
{
m_CurrentFrame.x = 0.0f;

if (++m_CurrentFrame.y >= m_TextureRows)
{
m_CurrentFrame.y = 0.0f;
}

m_SourceRect.top = m_SourceRect.height * m_CurrentFrame.y;
}

m_SourceRect.left = m_SourceRect.width * m_CurrentFrame.x;
}
}
29 changes: 29 additions & 0 deletions FluffyEngine/Animation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include "Rectf.h"
#include "glm/glm.hpp"

namespace Fluffy
{
class Animation final
{
public:
Animation() = default;
Animation(const glm::vec2& textureSize, int textureColumns, const int textureRows, float framesPerSecond);
~Animation() = default;

void Reset();
void Update(float elapsedSec);
inline const Rectf& GetSourceRect() const { return m_SourceRect; }
inline float GetDuration() const { return (m_TextureColumns * m_TextureRows) / m_FramesPerSecond; }
inline bool HasFrames() const { return m_TextureColumns > 1 || m_TextureRows > 1; }

private:
glm::vec2 m_TextureSize{};
int m_TextureColumns{ 1 };
int m_TextureRows{ 1 };
Rectf m_SourceRect{};
float m_FramesPerSecond{ 8.0f };
float m_AccumulatedSeconds{ 0.0f };
glm::vec2 m_CurrentFrame{ 0.0f, 0.0f };
};
}
2 changes: 2 additions & 0 deletions FluffyEngine/FluffyEngine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
</Lib>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Animation.h" />
<ClInclude Include="BaseInputDevice.h" />
<ClInclude Include="Character.h" />
<ClInclude Include="CollidersHandler.h" />
Expand Down Expand Up @@ -231,6 +232,7 @@
<ClInclude Include="Utils.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Animation.cpp" />
<ClCompile Include="BaseInputDevice.cpp" />
<ClCompile Include="CollidersHandler.cpp" />
<ClCompile Include="Command.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions FluffyEngine/FluffyEngine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
<ClInclude Include="RectColliderComponent.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Animation.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ResourceManager.cpp">
Expand Down Expand Up @@ -182,5 +185,8 @@
<ClCompile Include="CollidersHandler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Animation.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
20 changes: 20 additions & 0 deletions FluffyEngine/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Renderer.h"
#include "SceneManager.h"
#include "Texture2D.h"
#include "Rectf.h"

namespace Fluffy
{
Expand Down Expand Up @@ -68,5 +69,24 @@ namespace Fluffy
SDL_RenderCopy(GetSDLRenderer(), texture.GetSDLTexture(), nullptr, &dst);
}

void Renderer::RenderTexture(const Texture2D& texture, const Rectf& source, const float x, const float y) const
{
const SDL_Rect src
{
static_cast<int>(source.left),
static_cast<int>(source.top),
static_cast<int>(source.width),
static_cast<int>(source.height),
};

SDL_Rect dst{};
dst.x = static_cast<int>(x);
dst.y = static_cast<int>(y);
dst.w = static_cast<int>(source.width);
dst.h = static_cast<int>(source.height);

SDL_RenderCopy(GetSDLRenderer(), texture.GetSDLTexture(), &src, &dst);
}

SDL_Renderer* Renderer::GetSDLRenderer() const { return m_pRenderer; }
}
1 change: 1 addition & 0 deletions FluffyEngine/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Fluffy

void RenderTexture(const Texture2D& texture, float x, float y) const;
void RenderTexture(const Texture2D& texture, float x, float y, float width, float height) const;
void RenderTexture(const Texture2D& texture, const struct Rectf& source, const float x, const float y) const;

SDL_Renderer* GetSDLRenderer() const;

Expand Down
24 changes: 19 additions & 5 deletions FluffyEngine/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@

namespace Fluffy
{
Sprite::Sprite(GameObject* pOwner, const std::string& fileName)
Sprite::Sprite(GameObject* pOwner, const std::string& fileName, const int textureColumns, const int textureRows, const float framesPerSecond)
: Component(pOwner)
{
glm::vec2 textureSize{};

if (!fileName.empty())
{
m_pTexture = ResourceManager::GetInstance().LoadTexture(fileName);
textureSize = m_pTexture->GetSize();
}

m_Animation = Animation(textureSize, textureColumns, textureRows, framesPerSecond);
}

Sprite::Sprite(class GameObject* pOwner, const std::string& fileName, const glm::vec2& offset)
: Sprite(pOwner, fileName)
Sprite::Sprite(class GameObject* pOwner, const std::string& fileName, const glm::vec2& offset, const int textureColumns, const int textureRows, const float framesPerSecond)
: Sprite(pOwner, fileName, textureColumns, textureRows, framesPerSecond)
{
m_Offset = offset;
}
Expand All @@ -29,13 +36,20 @@ namespace Fluffy
};
}

void Sprite::Update(const float deltaTime)
{
if (m_Animation.HasFrames())
m_Animation.Update(deltaTime);
}

void Sprite::Render() const
{
if (m_pTexture == nullptr)
return;

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

void Sprite::SetTexture(const std::string& fileName)
Expand Down
7 changes: 5 additions & 2 deletions FluffyEngine/Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@
#include "IRenderable.h"
#include "Texture2D.h"
#include "Rectf.h"
#include "Animation.h"

namespace Fluffy
{
class Sprite : public Component, public IRenderable
{
public:
void Update(const float deltaTime) override;
void Render() const override;
void SetTexture(const std::string& fileName);
inline glm::vec2 GetTextureSize() const { return m_pTexture->GetSize(); }
Rectf GetRect() const;

Sprite(class GameObject* pOwner, const std::string& fileName);
Sprite(class GameObject* pOwner, const std::string& fileName, const glm::vec2& offset);
Sprite(class GameObject* pOwner, const std::string& fileName, const int textureColumns = 1, const int textureRows = 1, const float framesPerSecond = 0.0f);
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);
virtual ~Sprite() = default;
Sprite(const Sprite& other) = delete;
Sprite(Sprite&& other) = delete;
Sprite& operator=(const Sprite& other) = delete;
Sprite& operator=(Sprite&& other) = delete;

private:
Animation m_Animation{};
std::shared_ptr<class Texture2D> m_pTexture{};
glm::vec2 m_Offset{ 0.0f, 0.0f };
};
Expand Down
9 changes: 3 additions & 6 deletions Galaga/CharactersManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ void CharactersManager::StartLevel1()
Parser::GetInstance().ParseEnemyLayoutData("D:/Repos/FluffyEngine/Data/Formations/galaga_level_1.csv", data);
data;

std::string beeFormationsData{ "Formations/Formation1Bees.txt" };
std::string butterflyFormationsData{ "Formations/Formation1Butterflies.txt" };
std::string bossFormationsData{ "Formations/Formation1Boss.txt" };

std::queue<EnemyEnteringData> enemiesData{};

BezierPath firstPath{};
Expand Down Expand Up @@ -84,7 +80,7 @@ EnemyCharacter* CharactersManager::SpawnEnemy(const EnemyEnteringData& data)
switch (data.type)
{
case EnemyType::Boss:
fileName = "galaga_boss.png";
fileName = "galaga_boss_green.png";
break;

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

pEnemy->AddComponent<Fluffy::Sprite>(fileName);
pEnemy->AddComponent<Fluffy::Sprite>(fileName, 2, 1, 2.0f);

const float enemySpeed{ 200.0f };
EnemyCharacter* pEnemyCharacter{ pEnemy->AddComponent<EnemyCharacter>(data.type, enemySpeed) };
m_pOwner->GetScene()->Add(pEnemy);
Expand Down
1 change: 0 additions & 1 deletion Galaga/ShootCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ void ShootCommand::Execute()
{
const glm::vec2 position{ CharactersManager::GetInstance()->GetPlayer(m_PlayerIndex)->GetGameObject()->GetWorldPosition() };
BulletsManager::GetInstance().Shoot(m_PlayerIndex, position);
++m_ShotsFired;
}

0 comments on commit 105c5c4

Please sign in to comment.