generated from avadae/minigin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dae namespace is removed for now. Another namespace will be added for engine stuff later.
- Loading branch information
1 parent
c5ceba4
commit 1a690c1
Showing
34 changed files
with
600 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "Component.h" | ||
|
||
Component::Component(const std::weak_ptr<GameObject> pOwner) | ||
: m_pOwner{ pOwner } | ||
{ | ||
|
||
} | ||
|
||
std::shared_ptr<GameObject> Component::GetOwner() const | ||
{ | ||
if (!m_pOwner.expired()) | ||
return m_pOwner.lock(); | ||
|
||
return nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
#include <memory> | ||
|
||
class Component | ||
{ | ||
public: | ||
Component(const std::weak_ptr<class GameObject> pOwner); | ||
virtual ~Component() = default; | ||
virtual void Update(const float /*deltaTime*/) {} | ||
|
||
Component(const Component& other) = delete; | ||
Component(Component&& other) = delete; | ||
Component& operator=(const Component& other) = delete; | ||
Component& operator=(Component&& other) = delete; | ||
|
||
protected: | ||
// Returning as shared_ptr so that components can use it, instead of having to check for validity and lock every time everywhere | ||
std::shared_ptr<class GameObject> GetOwner() const; | ||
|
||
private: | ||
std::weak_ptr<class GameObject> m_pOwner; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "FPSCounter.h" | ||
#include "Text.h" | ||
#include <sstream> | ||
#include <format> | ||
#include <iomanip> | ||
|
||
FPSCounter::FPSCounter(const std::weak_ptr<GameObject> pOwner, const std::weak_ptr<Text> pText) | ||
: Component(pOwner) | ||
, m_pText{ pText.lock() } | ||
{ | ||
|
||
} | ||
|
||
void FPSCounter::Update(const float deltaTime) | ||
{ | ||
m_UpdateTimer += deltaTime; | ||
++m_CurrentIntervalFrameCount; | ||
|
||
if (m_UpdateTimer < m_UpdateFrequency) | ||
return; | ||
|
||
m_FPS = m_CurrentIntervalFrameCount / m_UpdateFrequency; | ||
m_UpdateTimer -= m_UpdateFrequency; | ||
m_CurrentIntervalFrameCount = 0; | ||
|
||
std::stringstream string{ }; | ||
string << std::fixed << std::setprecision(1) << m_FPS << " FPS"; | ||
m_pText->SetText(string.str()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
#include "Component.h" | ||
|
||
class FPSCounter : public Component | ||
{ | ||
public: | ||
FPSCounter(const std::weak_ptr<GameObject> pOwner, const std::weak_ptr<class Text> pText); | ||
void Update(const float deltaTime) override; | ||
inline float GetCurrentFPS() const { return m_FPS; } | ||
|
||
private: | ||
std::shared_ptr<class Text> m_pText; // Does not own the text component | ||
float m_FPS{ 0.0f }; | ||
const float m_UpdateFrequency{ 0.5f }; | ||
float m_UpdateTimer{ 0.0f }; | ||
int m_CurrentIntervalFrameCount{ 0 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
#include <string> | ||
#include <typeinfo> | ||
#include "GameObject.h" | ||
#include "ResourceManager.h" | ||
#include "Renderer.h" | ||
#include "Component.h" | ||
#include "Utils.h" | ||
|
||
dae::GameObject::~GameObject() = default; | ||
|
||
void dae::GameObject::Update(){} | ||
GameObject::GameObject(const float x, const float y) | ||
{ | ||
SetPosition(x, y); | ||
} | ||
|
||
void dae::GameObject::Render() const | ||
void GameObject::AddComponent(const std::weak_ptr<Component> pComponent) | ||
{ | ||
const auto& pos = m_transform.GetPosition(); | ||
Renderer::GetInstance().RenderTexture(*m_texture, pos.x, pos.y); | ||
const auto component{ pComponent.lock() }; | ||
|
||
if (component) | ||
{ | ||
const std::string name = typeid(component.get()).name(); | ||
|
||
m_Components.try_emplace(name); | ||
m_Components[name].push_back(pComponent.lock()); | ||
} | ||
} | ||
|
||
void dae::GameObject::SetTexture(const std::string& filename) | ||
void GameObject::Update(const float deltaTime) | ||
{ | ||
m_texture = ResourceManager::GetInstance().LoadTexture(filename); | ||
for (const auto& components : m_Components) | ||
{ | ||
for (auto& component : components.second) | ||
{ | ||
component.get()->Update(deltaTime); | ||
} | ||
} | ||
} | ||
|
||
void dae::GameObject::SetPosition(float x, float y) | ||
void GameObject::SetPosition(const float x, const float y) | ||
{ | ||
m_transform.SetPosition(x, y, 0.0f); | ||
m_Transform.SetPosition(x, y, 0.0f); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,48 @@ | ||
#pragma once | ||
#include <memory> | ||
#include <vector> | ||
#include <string> | ||
#include <unordered_map> | ||
#include "Transform.h" | ||
|
||
namespace dae | ||
class Texture2D; | ||
class Component; | ||
|
||
class GameObject final | ||
{ | ||
class Texture2D; | ||
public: | ||
void AddComponent(const std::weak_ptr<Component> pComponent); | ||
|
||
template<typename T> | ||
const std::vector<std::shared_ptr<Component>>& GetComponentsOfType() | ||
{ | ||
const std::string name{ type_info(T).name() }; | ||
return m_Components[name]; | ||
} | ||
|
||
// todo: this should become final. | ||
class GameObject | ||
inline const std::unordered_map<std::string, std::vector<std::shared_ptr<Component>>>& GetAllComponents() const { return m_Components; } | ||
|
||
template<typename T> | ||
void RemoveAllComponentsOfType() | ||
{ | ||
public: | ||
virtual void Update(); | ||
virtual void Render() const; | ||
|
||
void SetTexture(const std::string& filename); | ||
void SetPosition(float x, float y); | ||
|
||
GameObject() = default; | ||
virtual ~GameObject(); | ||
GameObject(const GameObject& other) = delete; | ||
GameObject(GameObject&& other) = delete; | ||
GameObject& operator=(const GameObject& other) = delete; | ||
GameObject& operator=(GameObject&& other) = delete; | ||
|
||
private: | ||
Transform m_transform{}; | ||
// todo: mmm, every gameobject has a texture? Is that correct? | ||
std::shared_ptr<Texture2D> m_texture{}; | ||
}; | ||
} | ||
const std::string name{ type_info(T).name() }; | ||
m_Components.erase(name); | ||
} | ||
void Update(const float deltaTime); | ||
|
||
inline Transform GetTransform() const { return m_Transform; } | ||
void SetPosition(const float x, const float y); | ||
|
||
GameObject() = default; | ||
GameObject(const float x, const float y); | ||
~GameObject() = default; | ||
GameObject(const GameObject& other) = delete; | ||
GameObject(GameObject&& other) = delete; | ||
GameObject& operator=(const GameObject& other) = delete; | ||
GameObject& operator=(GameObject&& other) = delete; | ||
|
||
private: | ||
Transform m_Transform{}; | ||
|
||
std::unordered_map<std::string, std::vector<std::shared_ptr<Component>>> m_Components; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
class IRenderable | ||
{ | ||
public: | ||
virtual void Render() const = 0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.