Skip to content

Commit f84e233

Browse files
fixed input devices
1 parent c6f39b3 commit f84e233

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

Tetragrama/EditorWindow.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <ZEngine/Engine.h>
55
#include <ZEngine/Event/EngineClosedEvent.h>
66
#include <ZEngine/Logging/LoggerDefinition.h>
7+
#include <ZEngine/Windows/Inputs/IDevice.h>
78
#include <ZEngine/Windows/Inputs/KeyCode.h>
89

910
#ifdef _WIN32
@@ -154,6 +155,11 @@ namespace Tetragrama
154155

155156
ZENGINE_CORE_INFO("Windows layers initialized")
156157

158+
// Initialize Devices (Keyboard, Mouse)
159+
ZEngine::Windows::Inputs::IDevice::Initialize(arena);
160+
161+
ZENGINE_CORE_INFO("Device initialized")
162+
157163
glfwSetWindowUserPointer(m_native_window, &m_property);
158164

159165
glfwSetFramebufferSizeCallback(m_native_window, EditorWindow::__OnGlfwFrameBufferSizeChanged);

ZEngine/ZEngine/Windows/Inputs/IDevice.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
namespace ZEngine::Windows::Inputs
55
{
6-
std::map<std::string, IDevice> IDevice::m_devices;
7-
}
6+
std::map<const char*, ZRawPtr(IDevice)> IDevice::Devices = {};
7+
Core::Memory::ArenaAllocator* IDevice::Arena = nullptr;
8+
} // namespace ZEngine::Windows::Inputs
Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
#pragma once
2+
#include <Core/Memory/Allocator.h>
23
#include <CoreWindow.h>
34
#include <KeyCode.h>
45
#include <algorithm>
56
#include <map>
6-
#include <memory>
7-
#include <string>
87
#include <type_traits>
98

109
namespace ZEngine::Windows::Inputs
1110
{
1211

1312
struct IDevice
1413
{
14+
IDevice(const char* name = "abstract_device") : m_name(name) {}
1515
virtual ~IDevice() = default;
16+
const char* m_name;
17+
static Core::Memory::ArenaAllocator* Arena;
18+
static std::map<const char*, ZRawPtr(IDevice)> Devices;
19+
20+
static void Initialize(Core::Memory::ArenaAllocator* arena)
21+
{
22+
Arena = arena;
23+
}
1624

1725
template <typename T, typename = std::enable_if_t<std::is_base_of_v<IDevice, T>>>
1826
static const T* As() noexcept
1927
{
2028
const std::type_info& type = typeid(T);
21-
auto it = m_devices.find(std::string(type.name()));
29+
auto it = Devices.find(type.name());
2230

23-
if (it != std::end(m_devices))
31+
if (it != std::end(Devices))
2432
{
25-
return reinterpret_cast<T*>(&it->second);
33+
return reinterpret_cast<T*>(it->second);
2634
}
2735

28-
IDevice device = T();
29-
auto pair = m_devices.emplace(std::make_pair(std::string(type.name()), device));
30-
return reinterpret_cast<T*>(&(pair.first->second));
36+
IDevice* device = ZPushStructCtor(Arena, T);
37+
auto pair = Devices.emplace(std::make_pair(type.name(), device));
38+
return reinterpret_cast<T*>(pair.first->second);
3139
}
3240

3341
virtual bool IsKeyPressed(ZENGINE_KEYCODE key, Windows::CoreWindow* const window) const
@@ -40,14 +48,9 @@ namespace ZEngine::Windows::Inputs
4048
return false;
4149
}
4250

43-
virtual std::string_view GetName() const
51+
virtual const char* GetName() const
4452
{
4553
return m_name;
4654
}
47-
48-
protected:
49-
IDevice(std::string_view name = "abstract_device") : m_name(name) {}
50-
static std::map<std::string, IDevice> m_devices;
51-
std::string m_name;
5255
};
5356
} // namespace ZEngine::Windows::Inputs

0 commit comments

Comments
 (0)