Skip to content

Commit

Permalink
Implemented base functionality
Browse files Browse the repository at this point in the history
dae namespace is removed for now. Another namespace will be added for engine stuff later.
  • Loading branch information
RiverHillbug committed Feb 26, 2024
1 parent c5ceba4 commit 1a690c1
Show file tree
Hide file tree
Showing 34 changed files with 600 additions and 330 deletions.
15 changes: 15 additions & 0 deletions Minigin/Component.cpp
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;
}
22 changes: 22 additions & 0 deletions Minigin/Component.h
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;
};
29 changes: 29 additions & 0 deletions Minigin/FPSCounter.cpp
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());
}
17 changes: 17 additions & 0 deletions Minigin/FPSCounter.h
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 };
};
8 changes: 2 additions & 6 deletions Minigin/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
#include <SDL_ttf.h>
#include "Font.h"

TTF_Font* dae::Font::GetFont() const {
return m_font;
}

dae::Font::Font(const std::string& fullPath, unsigned int size) : m_font(nullptr)
Font::Font(const std::string& fullPath, unsigned int size) : m_font(nullptr)
{
m_font = TTF_OpenFont(fullPath.c_str(), size);
if (m_font == nullptr)
Expand All @@ -15,7 +11,7 @@ dae::Font::Font(const std::string& fullPath, unsigned int size) : m_font(nullptr
}
}

dae::Font::~Font()
Font::~Font()
{
TTF_CloseFont(m_font);
}
35 changes: 17 additions & 18 deletions Minigin/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
#include <string>

struct _TTF_Font;
namespace dae

/**
* Simple RAII wrapper for a _TTF_Font
*/
class Font final
{
/**
* Simple RAII wrapper for a _TTF_Font
*/
class Font final
{
public:
_TTF_Font* GetFont() const;
explicit Font(const std::string& fullPath, unsigned int size);
~Font();
public:
explicit Font(const std::string& fullPath, unsigned int size);
~Font();

inline _TTF_Font* GetFont() const { return m_font; }

Font(const Font &) = delete;
Font(Font &&) = delete;
Font & operator= (const Font &) = delete;
Font & operator= (const Font &&) = delete;
private:
_TTF_Font* m_font;
};
}
Font(const Font&) = delete;
Font(Font&&) = delete;
Font& operator= (const Font&) = delete;
Font& operator= (const Font&&) = delete;
private:
_TTF_Font* m_font;
};
37 changes: 27 additions & 10 deletions Minigin/GameObject.cpp
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);
}
65 changes: 41 additions & 24 deletions Minigin/GameObject.h
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;
};
7 changes: 7 additions & 0 deletions Minigin/IRenderable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

class IRenderable
{
public:
virtual void Render() const = 0;
};
12 changes: 8 additions & 4 deletions Minigin/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
bool dae::InputManager::ProcessInput()
{
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
return false;
}
if (e.type == SDL_KEYDOWN) {
if (e.type == SDL_KEYDOWN)
{

}
if (e.type == SDL_MOUSEBUTTONDOWN) {
if (e.type == SDL_MOUSEBUTTONDOWN)
{

}
// etc...
Expand Down
Binary file modified Minigin/Main.cpp
Binary file not shown.
41 changes: 32 additions & 9 deletions Minigin/Minigin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <chrono>
#include <thread>
#include "Minigin.h"
#include "InputManager.h"
#include "SceneManager.h"
#include "Renderer.h"
#include "ResourceManager.h"

SDL_Window* g_window{};
SDL_Window* g_Window{};

void PrintSDLVersion()
{
Expand Down Expand Up @@ -49,46 +51,67 @@ dae::Minigin::Minigin(const std::string &dataPath)
throw std::runtime_error(std::string("SDL_Init Error: ") + SDL_GetError());
}

g_window = SDL_CreateWindow(
g_Window = SDL_CreateWindow(
"Programming 4 assignment",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640,
480,
SDL_WINDOW_OPENGL
);
if (g_window == nullptr)

if (g_Window == nullptr)
{
throw std::runtime_error(std::string("SDL_CreateWindow Error: ") + SDL_GetError());
}

Renderer::GetInstance().Init(g_window);
Renderer::GetInstance().Init(g_Window);

ResourceManager::GetInstance().Init(dataPath);
}

dae::Minigin::~Minigin()
{
Renderer::GetInstance().Destroy();
SDL_DestroyWindow(g_window);
g_window = nullptr;
SDL_DestroyWindow(g_Window);
g_Window = nullptr;
SDL_Quit();
}

void dae::Minigin::Run(const std::function<void()>& load)
{
load();

auto& renderer = Renderer::GetInstance();
const auto& renderer = Renderer::GetInstance();
auto& sceneManager = SceneManager::GetInstance();
auto& input = InputManager::GetInstance();

// todo: this update loop could use some work.
bool doContinue = true;

const float fixedTimeStep{ 0.02f };
auto previousTime{ std::chrono::high_resolution_clock::now() };
float lag{ 0.0f };
const float millisecondsPerFrame{ 1.0f / 60.0f }; // 60 FPS

while (doContinue)
{
const auto currentTime{ std::chrono::high_resolution_clock::now() };
const float deltaTime{ std::chrono::duration<float>(currentTime - previousTime).count() };
previousTime = currentTime;
lag += deltaTime;

doContinue = input.ProcessInput();
sceneManager.Update();
sceneManager.Update(deltaTime);

while (lag >= fixedTimeStep)
{
sceneManager.FixedUpdate(fixedTimeStep);
lag -= fixedTimeStep;
}

renderer.Render();

const auto sleepTime{ currentTime + std::chrono::duration<float>(millisecondsPerFrame) - std::chrono::high_resolution_clock::now() };
std::this_thread::sleep_for(sleepTime);
}
}
Loading

0 comments on commit 1a690c1

Please sign in to comment.