From d5edec17ebbc6bc0bbbeb9f02f4134f14642bf14 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 2 Jan 2021 03:26:28 +0100 Subject: [PATCH] Change KeyCode from an extern variable to a function --- inferno/src/inferno/application.cpp | 2 +- inferno/src/inferno/inputcodes.cpp | 13 ++++++++++++- inferno/src/inferno/inputcodes.h | 4 +--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/inferno/src/inferno/application.cpp b/inferno/src/inferno/application.cpp index 9973491..a0bd06c 100644 --- a/inferno/src/inferno/application.cpp +++ b/inferno/src/inferno/application.cpp @@ -170,7 +170,7 @@ namespace Inferno { e.getKey()); // Stop the main loop on 'Escape' keypress - if (e.getKey() == KeyCode["GLFW_KEY_ESCAPE"]) { + if (e.getKey() == KeyCode("GLFW_KEY_ESCAPE")) { m_window->setShouldClose(true); } diff --git a/inferno/src/inferno/inputcodes.cpp b/inferno/src/inferno/inputcodes.cpp index 86b235a..1999e8e 100644 --- a/inferno/src/inferno/inputcodes.cpp +++ b/inferno/src/inferno/inputcodes.cpp @@ -1,10 +1,13 @@ +#include // std::unordered_map + #include +#include "inferno/assertions.h" #include "inferno/inputcodes.h" namespace Inferno { - std::unordered_map KeyCode ({ + static std::unordered_map keys ({ { MAP_KEY(GLFW_KEY_UNKNOWN) }, { MAP_KEY(GLFW_KEY_SPACE) }, { MAP_KEY(GLFW_KEY_APOSTROPHE) }, @@ -128,4 +131,12 @@ namespace Inferno { { MAP_KEY(GLFW_KEY_MENU) }, }); +// ----------------------------------------- + + int KeyCode(const char* name) + { + ASSERT(keys.find(name) != keys.end(), "Could not find KeyCode"); + return keys.at(name); + } + } diff --git a/inferno/src/inferno/inputcodes.h b/inferno/src/inferno/inputcodes.h index c708271..2183231 100644 --- a/inferno/src/inferno/inputcodes.h +++ b/inferno/src/inferno/inputcodes.h @@ -3,11 +3,9 @@ #define MAP_KEY(key) #key, key -#include // std::unordered_map - namespace Inferno { - extern std::unordered_map KeyCode; + int KeyCode(const char* name); }