Skip to content

Commit

Permalink
input implementation - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Mar 16, 2024
1 parent aca3d84 commit e8b9d18
Show file tree
Hide file tree
Showing 18 changed files with 161 additions and 35 deletions.
1 change: 1 addition & 0 deletions Minigin/Command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "Command.h"
7 changes: 7 additions & 0 deletions Minigin/Command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
class Command
{
public:
virtual ~Command() = default;
virtual void Execute(class GameObject*) {};
};
2 changes: 2 additions & 0 deletions Minigin/Component.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once
#include <memory>
#include <string>

class Component
{
public:
virtual ~Component() = default;
virtual void Update(const float /*deltaTime*/) {}
virtual std::string GetTypename() = 0;

Component(const Component& other) = delete;
Component(Component&& other) = delete;
Expand Down
1 change: 1 addition & 0 deletions Minigin/FPSCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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;
std::string GetTypename() override { return "FPSCounter"; }
inline float GetCurrentFPS() const { return m_FPS; }

private:
Expand Down
13 changes: 0 additions & 13 deletions Minigin/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@ GameObject::GameObject(const float x, const float y)
SetWorldPosition(x, y);
}

void GameObject::AddComponent(const std::weak_ptr<Component> pComponent)
{
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 GameObject::Update(const float deltaTime)
{
for (const auto& components : m_Components)
Expand Down
25 changes: 22 additions & 3 deletions Minigin/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,31 @@ class Component;
class GameObject final
{
public:
void AddComponent(const std::weak_ptr<Component> pComponent);
template<typename T>
void AddComponent(const std::weak_ptr<T> pComponent)
{
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());
}
}

template<typename T>
T* GetComponentOfType()
{
const std::string name{ typeid(T*).name() };
return reinterpret_cast<T*>(m_Components[name][0].get());
}

template<typename T>
const std::vector<std::shared_ptr<Component>>& GetComponentsOfType()
{
const std::string name{ type_info(T).name() };
const std::string name{ typeid(T*).name() };
return m_Components[name];
}

Expand All @@ -25,7 +44,7 @@ class GameObject final
template<typename T>
void RemoveAllComponentsOfType()
{
const std::string name{ type_info(T).name() };
const std::string name{ typeid(T).name() };
m_Components.erase(name);
}
void Update(const float deltaTime);
Expand Down
35 changes: 21 additions & 14 deletions Minigin/InputManager.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#include <SDL.h>
#include "InputManager.h"
#include "backends/imgui_impl_sdl2.h"
//#include "xinput.h"
//#include "winbase.h"
//#include "winnt.h"

/*
#include "xinput.h"
#include "winbase.h"*/

dae::InputManager::~InputManager()
{
//delete command pointers
}

bool dae::InputManager::ProcessInput()
{
//ZeroMemory(&m_CurrentState, sizeof(XINPUT_STATE));
//XInputGetState(0, &m_CurrentState);
/*ZeroMemory(&m_CurrentState, sizeof(XINPUT_STATE));
XInputGetState(0, &m_CurrentState);*/

SDL_Event e;
while (SDL_PollEvent(&e))
Expand All @@ -17,18 +23,19 @@ bool dae::InputManager::ProcessInput()
{
return false;
}
if (e.type == SDL_KEYDOWN)
{

}
if (e.type == SDL_MOUSEBUTTONDOWN)
{

}
// etc...

const auto& command = m_MappedInputs.find(e.key.keysym.scancode);

if (command != m_MappedInputs.end())
command->second->Execute(m_pInputReceiver);

ImGui_ImplSDL2_ProcessEvent(&e);
}

return true;
}

void dae::InputManager::MapInput(SDL_Scancode input, Command* pCommand)
{
m_MappedInputs[input] = pCommand;
}
7 changes: 6 additions & 1 deletion Minigin/InputManager.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#pragma once
#include "Singleton.h"
#include "MoveCommand.h"

namespace dae
{
class InputManager final : public Singleton<InputManager>
{
public:
~InputManager();
inline void SetInputReceiver(GameObject* pReceiver) { m_pInputReceiver = pReceiver; }
bool ProcessInput();
void MapInput(SDL_Scancode input, Command* pCommand);

private:

GameObject* m_pInputReceiver{ nullptr };
std::unordered_map<SDL_Scancode, Command*> m_MappedInputs{};
};

}
Binary file modified Minigin/Main.cpp
Binary file not shown.
14 changes: 10 additions & 4 deletions Minigin/Minigin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>opengl32.lib;xinput.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent />
</ItemDefinitionGroup>
Expand All @@ -126,7 +126,7 @@
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>opengl32.lib;xinput.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent />
</ItemDefinitionGroup>
Expand All @@ -149,7 +149,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>opengl32.lib;xinput.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent />
</ItemDefinitionGroup>
Expand All @@ -172,7 +172,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>opengl32.lib;xinput.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent />
</ItemDefinitionGroup>
Expand All @@ -185,13 +185,16 @@
<ClInclude Include="..\3rdParty\imgui\imstb_rectpack.h" />
<ClInclude Include="..\3rdParty\imgui\imstb_textedit.h" />
<ClInclude Include="..\3rdParty\imgui\imstb_truetype.h" />
<ClInclude Include="Command.h" />
<ClInclude Include="Component.h" />
<ClInclude Include="Font.h" />
<ClInclude Include="FPSCounter.h" />
<ClInclude Include="GameObject.h" />
<ClInclude Include="InputManager.h" />
<ClInclude Include="IRenderable.h" />
<ClInclude Include="Minigin.h" />
<ClInclude Include="MoveCommand.h" />
<ClInclude Include="MoveComponent.h" />
<ClInclude Include="Renderer.h" />
<ClInclude Include="ResourceManager.h" />
<ClInclude Include="Rotator.h" />
Expand All @@ -212,13 +215,16 @@
<ClCompile Include="..\3rdParty\imgui\imgui_draw.cpp" />
<ClCompile Include="..\3rdParty\imgui\imgui_tables.cpp" />
<ClCompile Include="..\3rdParty\imgui\imgui_widgets.cpp" />
<ClCompile Include="Command.cpp" />
<ClCompile Include="Component.cpp" />
<ClCompile Include="Font.cpp" />
<ClCompile Include="FPSCounter.cpp" />
<ClCompile Include="GameObject.cpp" />
<ClCompile Include="InputManager.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="Minigin.cpp" />
<ClCompile Include="MoveCommand.cpp" />
<ClCompile Include="MoveComponent.cpp" />
<ClCompile Include="Renderer.cpp" />
<ClCompile Include="ResourceManager.cpp" />
<ClCompile Include="Rotator.cpp" />
Expand Down
18 changes: 18 additions & 0 deletions Minigin/Minigin.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@
<ClInclude Include="..\3rdParty\imgui\backends\imgui_impl_sdl2.h">
<Filter>ImGui</Filter>
</ClInclude>
<ClInclude Include="Command.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MoveCommand.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MoveComponent.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ResourceManager.cpp">
Expand Down Expand Up @@ -167,5 +176,14 @@
<ClCompile Include="..\3rdParty\imgui\backends\imgui_impl_sdl2.cpp">
<Filter>ImGui</Filter>
</ClCompile>
<ClCompile Include="Command.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MoveCommand.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MoveComponent.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions Minigin/MoveCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "MoveCommand.h"
14 changes: 14 additions & 0 deletions Minigin/MoveCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include "Command.h"
#include "GameObject.h"
#include "MoveComponent.h"
#include "glm/glm.hpp"

class MoveCommand final : public Command
{
public:
void Execute(GameObject* pGameObject) override
{
pGameObject->GetComponentOfType<MoveComponent>()->Move(glm::vec2(0.0f, 1.0f));
}
};
27 changes: 27 additions & 0 deletions Minigin/MoveComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "MoveComponent.h"
#include "GameObject.h"
#include <vcruntime_typeinfo.h>

MoveComponent::MoveComponent(const std::weak_ptr<class GameObject> pOwner, float verticalSpeed, float horizontalSpeed)
: Component(pOwner)
, m_VerticalSpeed{ verticalSpeed }
, m_HorizontalSpeed{ horizontalSpeed }
{

}

void MoveComponent::Update(const float deltaTime)
{
if (m_Movement != VEC_ZERO)
{
glm::vec2 position{ GetOwner()->GetWorldPosition() };
glm::vec2 movement{ deltaTime * m_Movement };
GetOwner()->SetWorldPosition(position - movement);
m_Movement = VEC_ZERO;
}
}

void MoveComponent::Move(const glm::vec2& input)
{
m_Movement = glm::vec2((input.x * m_HorizontalSpeed), (input.y * m_VerticalSpeed));
}
28 changes: 28 additions & 0 deletions Minigin/MoveComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include "Component.h"
#include <glm/glm.hpp>
#include <memory>

constexpr glm::vec2 VEC_ZERO{ 0.0f, 0.0f };


class MoveComponent final : public Component
{
public:
MoveComponent(const std::weak_ptr<class GameObject> pOwner, float verticalSpeed, float horizontalSpeed);
void Update(const float deltaTime) override;

std::string GetTypename() override
{
return typeid(MoveComponent).name();
}

void Move(const glm::vec2& input);

private:
float m_VerticalSpeed{};
float m_HorizontalSpeed{};

glm::vec2 m_Movement{};
};

1 change: 1 addition & 0 deletions Minigin/Rotator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Rotator final : public Component
Rotator(const std::weak_ptr<GameObject> pOwner, const float angleDegrees, const float centerX, const float centerY);
~Rotator() = default;
void Update(const float deltaTime) override;
std::string GetTypename() override { return "Rotator"; }
inline void SetRotationAngle(const float angleDegrees) { m_RotationAngleDegrees = angleDegrees; }
inline void SetRotationCenter(const glm::vec2& center) { m_RotationCenter = center; }
inline void SetRotationCenter(const float centerX, const float centerY) { m_RotationCenter = glm::vec2(centerX, centerY); }
Expand Down
1 change: 1 addition & 0 deletions Minigin/Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Texture2D;
class Sprite : public Component, public IRenderable
{
public:
std::string GetTypename() override { return "Sprite"; }
void Render() const override;
void SetTexture(const std::string& fileName);

Expand Down
1 change: 1 addition & 0 deletions Minigin/Text.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Text final : public Component, public IRenderable
{
public:
void Update(const float deltaTime) override;
std::string GetTypename() override { return "Text"; }
void Render() const override;

void SetText(const std::string& text);
Expand Down

0 comments on commit e8b9d18

Please sign in to comment.