Skip to content

Commit

Permalink
Animation System Kinda Working
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Jul 27, 2023
1 parent 3dd1a79 commit 53036da
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@
[submodule "Arcane/vendor/Kinetics"]
path = Arcane/vendor/Kinetics
url = https://github.com/tomheeleynz/Kinetics.git
[submodule "Arcane/vendor/ImGuizmo"]
path = Arcane/vendor/ImGuizmo
url = https://github.com/CedricGuillemet/ImGuizmo.git
1 change: 1 addition & 0 deletions Arcane/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ target_include_directories(
"vendor/entt/single_include"
"vendor/json/single_include"
"vendor/dotnet"
"vendor/ImGuizmo"
${OPENGL_INCLUDE_DIRS}
"vendor/SPIRV-Cross"
${Python_INCLUDE_DIRS}
Expand Down
8 changes: 7 additions & 1 deletion Arcane/src/Arcane.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@
/////////////////////////////////////////////////////
////////// Scripting
/////////////////////////////////////////////////////
#include "Arcane/Scripting/ScriptingEngine.h"
#include "Arcane/Scripting/ScriptingEngine.h"

/////////////////////////////////////////////////////
////////// Animation
/////////////////////////////////////////////////////
#include "Arcane/Animation/Animation.h"
#include "Arcane/Animation/AnimationController.h"
37 changes: 37 additions & 0 deletions Arcane/src/Arcane/Animation/Animation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "Animation.h"

namespace Arcane
{
/////////////////////////////////////////////////
//// Base Animation
/////////////////////////////////////////////////
Animation::Animation()
{

}

void Animation::SetNextKeyFrame()
{
m_CurrentKeyframe += 1;

if (m_CurrentKeyframe == m_KeyFrames.size())
{
m_CurrentKeyframe = 0;
}
}

/////////////////////////////////////////////////
//// Base Keyframe
/////////////////////////////////////////////////
KeyFrame::KeyFrame(KeyFrameType type)
{
m_Type = type;
}

/////////////////////////////////////////////////
//// 2D Keyframe
/////////////////////////////////////////////////
KeyFrame2D::KeyFrame2D() : KeyFrame(KeyFrameType::TWO_DIMENSIONAL)
{
}
}
62 changes: 61 additions & 1 deletion Arcane/src/Arcane/Animation/Animation.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,73 @@
#pragma once

#include <vector>
#include <string>
#include <map>
#include "Arcane/Assets/Asset.h"

namespace Arcane
{
class Animation
enum class KeyFrameType
{
TWO_DIMENSIONAL,
THREE_DIMENSIONAL
};

/////////////////////////////////////////////////
//// Base Animation Classes
/////////////////////////////////////////////////
class KeyFrame
{
public:
KeyFrame() {}
KeyFrame(KeyFrameType type);

void SetType(KeyFrameType type) { m_Type = type; }
KeyFrameType GetType() { return m_Type; }
protected:
KeyFrameType m_Type;
};

class Animation : public Asset
{
public:
Animation();

KeyFrame* GetKeyFrame(int index) { return m_KeyFrames[index]; }
std::map<int, KeyFrame*> GetKeyFrames() { return m_KeyFrames; }

void ResetKeyFrameCount() { m_CurrentKeyframe = 0; }

void AddKeyFrame(int index, KeyFrame* keyFrame) { m_KeyFrames[index] = keyFrame; }
KeyFrame* GetCurrentKeyFrame() { return m_KeyFrames[m_CurrentKeyframe]; }
void SetNextKeyFrame();

int CurrentFrameCount = 0;
private:
std::string m_Name;
std::map<int, KeyFrame*> m_KeyFrames;
int m_CurrentKeyframe = 0;
};

/////////////////////////////////////////////////
//// 2D KeyFrame
/////////////////////////////////////////////////
class KeyFrame2D : public KeyFrame
{
public:
KeyFrame2D();

void SetKeyFrameLength(float keyFrameLength) { m_KeyFrameLength = keyFrameLength; }
float GetKeyFrameLength() { return m_KeyFrameLength; }

void SetImageIndexX(float imageIndex) { m_ImageIndexX = imageIndex; }
float GetImageIndexX() { return m_ImageIndexX; }

void SetImageIndexY(float imageIndex) { m_ImageIndexY = imageIndex; }
float GetImageIndexY() { return m_ImageIndexY; }
private:
float m_KeyFrameLength;
float m_ImageIndexX;
float m_ImageIndexY;
};
}
18 changes: 18 additions & 0 deletions Arcane/src/Arcane/Animation/AnimationController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "AnimationController.h"

namespace Arcane
{
AnimationController::AnimationController()
{
}

Animation* AnimationController::GetAnimation(std::string animationName)
{
return m_Animations[animationName];
}

Animation* AnimationController::GetCurrentAnimation()
{
return m_Animations[m_CurrentAnimation];
}
}
24 changes: 24 additions & 0 deletions Arcane/src/Arcane/Animation/AnimationController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <map>
#include <string>

#include "Animation.h"

namespace Arcane
{
class AnimationController : public Asset
{
public:
AnimationController();

Animation* GetCurrentAnimation();
Animation* GetAnimation(std::string animationName);

void SetCurrentAnimation(std::string name) { m_CurrentAnimation = name; }
void AddAnimation(std::string name, Animation* animation) { m_Animations[name] = animation; };
private:
std::map<std::string, Animation*> m_Animations;
std::string m_CurrentAnimation;
};
}
3 changes: 2 additions & 1 deletion Arcane/src/Arcane/ECS/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Arcane/Renderer/Mesh.h"
#include "Arcane/Scripting/Script.h"
#include "Arcane/Renderer/Camera.h"
#include "Arcane/Animation/AnimationController.h"

namespace Arcane
{
Expand Down Expand Up @@ -96,6 +97,6 @@ namespace Arcane
//////////////////////
struct Animator
{
float stuff;
AnimationController* controller;
};
}
25 changes: 21 additions & 4 deletions Arcane/src/Arcane/Renderer/SceneRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ namespace Arcane
s_Data.quadCount++;
}

void SceneRenderer::SubmitAnimatedQuad(TransformComponent& transformComponent, SpriteRendererComponent& spriteRendererComponent)
void SceneRenderer::SubmitAnimatedQuad(TransformComponent& transformComponent, SpriteRendererComponent& spriteRendererComponent, Animator& animatorComponent)
{
int quadSize = 4;

Expand All @@ -653,11 +653,28 @@ namespace Arcane
}

AnimData newData;
newData.currentFrameCountX = 0;
newData.currentFrameCountY = 0;

newData.totalFrameCountY = 1;
Animation* currentAnimation = animatorComponent.controller->GetCurrentAnimation();
KeyFrame* currentKeyFrame = currentAnimation->GetCurrentKeyFrame();

if (currentKeyFrame->GetType() == KeyFrameType::TWO_DIMENSIONAL)
{
KeyFrame2D* currentKeyFrame2D = static_cast<KeyFrame2D*>(currentKeyFrame);

if (currentAnimation->CurrentFrameCount >= currentKeyFrame2D->GetKeyFrameLength()) {
currentAnimation->SetNextKeyFrame();
currentAnimation->CurrentFrameCount = 0;
}
else {
currentAnimation->CurrentFrameCount += 1;
}

newData.currentFrameCountX = currentKeyFrame2D->GetImageIndexX();
newData.currentFrameCountY = currentKeyFrame2D->GetImageIndexY();
}

newData.totalFrameCountX = 6;
newData.totalFrameCountY = 1;

if (spriteRendererComponent.sprite == nullptr)
s_Data.AnimatedQuadTextures[s_Data.animatedQuadCount] = s_Data.QuadBaseTexture;
Expand Down
2 changes: 1 addition & 1 deletion Arcane/src/Arcane/Renderer/SceneRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Arcane
void SetCamera(Camera* camera);
void SubmitMesh(Mesh* mesh, TransformComponent& component, Material* material);
void SubmitQuad(TransformComponent& transformComponent, SpriteRendererComponent& spriteRendererComponent);
void SubmitAnimatedQuad(TransformComponent& transformComponent, SpriteRendererComponent& spriteRendererComponent);
void SubmitAnimatedQuad(TransformComponent& transformComponent, SpriteRendererComponent& spriteRendererComponent, Animator& animatorComponent);

// Lighting code
void SetDirectionalLight(LightComponent& light, TransformComponent& transform);
Expand Down
2 changes: 1 addition & 1 deletion Arcane/src/Arcane/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ namespace Arcane
Entity entityHandle = Entity(entity, this);

if (entityHandle.HasComponent<Animator>())
m_SceneRenderer->SubmitAnimatedQuad(transformComponent, spriteRendererComponent);
m_SceneRenderer->SubmitAnimatedQuad(transformComponent, spriteRendererComponent, entityHandle.GetComponent<Animator>());
else
m_SceneRenderer->SubmitQuad(transformComponent, spriteRendererComponent);
}
Expand Down
1 change: 1 addition & 0 deletions Arcane/vendor/ImGuizmo
Submodule ImGuizmo added at 822be7
1 change: 1 addition & 0 deletions EnchantingTable/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <imgui.h>

#include <backends/imgui_impl_vulkan.h>
#include <Arcane/Platform/Vulkan/VulkanFramebuffer.h>

Expand Down
1 change: 1 addition & 0 deletions EnchantingTable/src/EnchantingTable.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <filesystem>
#include <Arcane.h>
#include <ImGuizmo.h>

#include "EditorLayer.h"
#include "OpenGLTestLayer.h"
Expand Down
13 changes: 13 additions & 0 deletions EnchantingTable/src/Panels/AnimationPanel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "AnimationPanel.h"

AnimationPanel::AnimationPanel()
{
}

void AnimationPanel::OnImGuiRender()
{
ImGui::Begin("Animation");
{
}
ImGui::End();
}
13 changes: 13 additions & 0 deletions EnchantingTable/src/Panels/AnimationPanel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <imgui.h>
#include <ImSequencer.h>

class AnimationPanel
{
public:
AnimationPanel();

void OnImGuiRender();
private:
};
38 changes: 37 additions & 1 deletion EnchantingTable/src/Panels/EntityPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,5 +532,41 @@ void EntityPanel::InitComponent<Arcane::BoxColliderComponent>()
template <>
void EntityPanel::InitComponent<Arcane::Animator>()
{
m_Context.AddComponent<Arcane::Animator>();
Arcane::Animator newAnimator;

// Create Animation Controller
Arcane::AnimationController* newController;

// Idle Animation
Arcane::Animation* idleAnimation = new Arcane::Animation();

// Create Key Frames for animation
Arcane::KeyFrame2D* firstKeyFrame = new Arcane::KeyFrame2D();
firstKeyFrame->SetImageIndexX(1);
firstKeyFrame->SetImageIndexY(1);
firstKeyFrame->SetKeyFrameLength(10);

Arcane::KeyFrame2D* secondKeyFrame = new Arcane::KeyFrame2D();
secondKeyFrame->SetImageIndexX(2);
secondKeyFrame->SetImageIndexY(1);
secondKeyFrame->SetKeyFrameLength(10);


Arcane::KeyFrame2D* thirdKeyFrame = new Arcane::KeyFrame2D();
secondKeyFrame->SetImageIndexX(3);
secondKeyFrame->SetImageIndexY(1);
secondKeyFrame->SetKeyFrameLength(10);

// Add to animation
idleAnimation->AddKeyFrame(0, firstKeyFrame);
idleAnimation->AddKeyFrame(1, secondKeyFrame);
idleAnimation->AddKeyFrame(2, thirdKeyFrame);

// Add Animation to animation controller
newController = new Arcane::AnimationController();
newController->AddAnimation("Idle", idleAnimation);
newController->SetCurrentAnimation("Idle");
newAnimator.controller = newController;

m_Context.AddComponent<Arcane::Animator>(newAnimator);
}

0 comments on commit 53036da

Please sign in to comment.