From 9ad0276d3cdb9b255320f628cf6281e815b3b04f Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Wed, 8 Sep 2021 20:19:48 -0400 Subject: [PATCH] Moved some files around Input manager is ported and just needs to be used by the core --- CMakeLists.txt | 16 +- docs/Tasks.todo | 13 +- src/core/core.cpp | 4 +- src/{graphics => core}/window.cpp | 17 ++ src/{graphics => core}/window.h | 5 +- src/{ => graphics}/dearimgui/CMakeLists.txt | 2 +- src/{ => graphics}/dearimgui/LICENSE.txt | 0 src/{ => graphics}/dearimgui/imconfig.h | 0 src/{ => graphics}/dearimgui/imgui.cpp | 0 src/{ => graphics}/dearimgui/imgui.h | 0 src/{ => graphics}/dearimgui/imgui_demo.cpp | 0 src/{ => graphics}/dearimgui/imgui_draw.cpp | 0 .../dearimgui/imgui_impl_glfw.cpp | 0 .../dearimgui/imgui_impl_glfw.h | 0 .../dearimgui/imgui_impl_opengl3.cpp | 0 .../dearimgui/imgui_impl_opengl3.h | 0 .../dearimgui/imgui_impl_opengl3_loader.h | 0 src/{ => graphics}/dearimgui/imgui_internal.h | 0 src/{ => graphics}/dearimgui/imgui_tables.cpp | 0 .../dearimgui/imgui_widgets.cpp | 0 src/{ => graphics}/dearimgui/imstb_rectpack.h | 0 src/{ => graphics}/dearimgui/imstb_textedit.h | 0 src/{ => graphics}/dearimgui/imstb_truetype.h | 0 src/graphics/{ => opengl}/defaultShaders.h | 0 src/graphics/{ => opengl}/glGraphics.cpp | 6 +- src/graphics/{ => opengl}/glGraphics.h | 2 +- src/graphics/{ => opengl}/glShader.cpp | 0 src/graphics/{ => opengl}/glShader.h | 0 src/graphics/{ => opengl}/glText.cpp | 0 src/graphics/{ => opengl}/glText.h | 0 src/input/inputManager.cpp | 132 +++++++++++ src/input/inputManager.h | 88 +++++++ src/input/keyCodes.h | 223 ++++++++++++++++++ src/input/keyboard.cpp | 188 +++++++++++++++ src/input/keyboard.h | 69 ++++++ 35 files changed, 746 insertions(+), 19 deletions(-) rename src/{graphics => core}/window.cpp (93%) rename src/{graphics => core}/window.h (89%) rename src/{ => graphics}/dearimgui/CMakeLists.txt (63%) rename src/{ => graphics}/dearimgui/LICENSE.txt (100%) rename src/{ => graphics}/dearimgui/imconfig.h (100%) rename src/{ => graphics}/dearimgui/imgui.cpp (100%) rename src/{ => graphics}/dearimgui/imgui.h (100%) rename src/{ => graphics}/dearimgui/imgui_demo.cpp (100%) rename src/{ => graphics}/dearimgui/imgui_draw.cpp (100%) rename src/{ => graphics}/dearimgui/imgui_impl_glfw.cpp (100%) rename src/{ => graphics}/dearimgui/imgui_impl_glfw.h (100%) rename src/{ => graphics}/dearimgui/imgui_impl_opengl3.cpp (100%) rename src/{ => graphics}/dearimgui/imgui_impl_opengl3.h (100%) rename src/{ => graphics}/dearimgui/imgui_impl_opengl3_loader.h (100%) rename src/{ => graphics}/dearimgui/imgui_internal.h (100%) rename src/{ => graphics}/dearimgui/imgui_tables.cpp (100%) rename src/{ => graphics}/dearimgui/imgui_widgets.cpp (100%) rename src/{ => graphics}/dearimgui/imstb_rectpack.h (100%) rename src/{ => graphics}/dearimgui/imstb_textedit.h (100%) rename src/{ => graphics}/dearimgui/imstb_truetype.h (100%) rename src/graphics/{ => opengl}/defaultShaders.h (100%) rename src/graphics/{ => opengl}/glGraphics.cpp (99%) rename src/graphics/{ => opengl}/glGraphics.h (98%) rename src/graphics/{ => opengl}/glShader.cpp (100%) rename src/graphics/{ => opengl}/glShader.h (100%) rename src/graphics/{ => opengl}/glText.cpp (100%) rename src/graphics/{ => opengl}/glText.h (100%) create mode 100644 src/input/inputManager.cpp create mode 100644 src/input/inputManager.h create mode 100644 src/input/keyCodes.h create mode 100644 src/input/keyboard.cpp create mode 100644 src/input/keyboard.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bc4c515..ac09218 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(LUNARIUM_SRC "src/core/core.cpp" "src/core/state.cpp" "src/core/version.cpp" +"src/core/window.cpp" "src/utils/types.cpp" "src/utils/logger.cpp" "src/utils/highResTimer.cpp" @@ -27,12 +28,13 @@ set(LUNARIUM_SRC "src/utils/args.cpp" "src/utils/binaryFileBuffer.cpp" "src/utils/frameCounter.cpp" -"src/graphics/window.cpp" -"src/graphics/glGraphics.cpp" -"src/graphics/glShader.cpp" +"src/graphics/opengl/glGraphics.cpp" +"src/graphics/opengl/glText.cpp" +"src/graphics/opengl/glShader.cpp" "src/graphics/image.cpp" -"src/graphics/glText.cpp" "src/graphics/internalFont.cpp" +"src/input/keyboard.cpp" +"src/input/inputManager.cpp" ) # add the executable @@ -85,7 +87,7 @@ add_subdirectory(external/glad/src) add_subdirectory(external/glm) # add dearimgui -add_subdirectory(src/dearimgui) +add_subdirectory(src/graphics/dearimgui) # add lua -- https://github.com/walterschell/Lua add_subdirectory(external/lua) @@ -102,7 +104,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC external/glfw/include PUBLIC external/glm PUBLIC external/lua/lua5.4.3/include - PUBLIC src/dearimgui + PUBLIC src/graphics/dearimgui PUBLIC external/pugixml/src PUBLIC external/glad/include PUBLIC external/freetype/include @@ -111,7 +113,7 @@ target_include_directories(${PROJECT_NAME} target_link_directories(${PROJECT_NAME} PRIVATE external/glfw/src PRIVATE external/glm - PRIVATE src/dearimgui + PRIVATE src/graphics/dearimgui PRIVATE external/glad/src PRIVATE external/freetype/src ) diff --git a/docs/Tasks.todo b/docs/Tasks.todo index 41978a8..825cb56 100644 --- a/docs/Tasks.todo +++ b/docs/Tasks.todo @@ -14,7 +14,8 @@ Core: ☐ Implement the Image creation methods Input: - ☐ Port over the Element2D input system and adjust it to use glfw + ✔ Port over the Element2D input system and adjust it to use glfw @done (9/8/2021, 8:20:07 PM) + ☐ Add the InputManager to the core Audio: @@ -26,15 +27,16 @@ Core: Interface Class: ☐ Provide Methods that give access to the C++ code - Resource Managment: - Game: ☐ Load game project data ☐ Manage list of scenes ☐ Manage global scripts + ☐ Handle Events from the core Scene: ☐ Manage scene scripts + ☐ Manage game objects in scene + Manage list of Regions: ☐ Track which regions should be loaded @@ -55,5 +57,8 @@ Game: Editor: + ☐ Reference raw asset files in a "content" folder + - \ No newline at end of file +Asset Pipeline: + ☐ Read through the contents folder and generate asset files in a custom format (useable by the engine) \ No newline at end of file diff --git a/src/core/core.cpp b/src/core/core.cpp index dfc59ae..f9ddc3f 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -10,8 +10,8 @@ #include "version.h" // Sub Systems -#include -#include +#include "window.h" +#include namespace lunarium { diff --git a/src/graphics/window.cpp b/src/core/window.cpp similarity index 93% rename from src/graphics/window.cpp rename to src/core/window.cpp index 6388a86..40c6b03 100644 --- a/src/graphics/window.cpp +++ b/src/core/window.cpp @@ -102,6 +102,23 @@ namespace lunarium return mpWindow; } + glm::vec2 Window::GetCursorPos() const + { + double x, y; + glfwGetCursorPos(mpWindow, &x, &y); + return { (float)x, (float)y }; + } + + void Window::SetCursorPos(glm::vec2 pos) + { + glfwSetCursorPos(mpWindow, pos.x, pos.y); + } + + bool Window::IsKeyDown(int key) const + { + return glfwGetKey(mpWindow, key) == GLFW_PRESS; + } + void Window::Resize(int width, int height) { glfwSetWindowSize(mpWindow, width, height); diff --git a/src/graphics/window.h b/src/core/window.h similarity index 89% rename from src/graphics/window.h rename to src/core/window.h index 7660660..00ca793 100644 --- a/src/graphics/window.h +++ b/src/core/window.h @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -27,7 +28,9 @@ namespace lunarium bool IsInit() const; GLFWwindow* GetWindow(); - + bool IsKeyDown(int key) const; + glm::vec2 GetCursorPos() const; + void SetCursorPos(glm::vec2 pos); void Resize(int width, int height); diff --git a/src/dearimgui/CMakeLists.txt b/src/graphics/dearimgui/CMakeLists.txt similarity index 63% rename from src/dearimgui/CMakeLists.txt rename to src/graphics/dearimgui/CMakeLists.txt index 68590ce..adf7ccb 100644 --- a/src/dearimgui/CMakeLists.txt +++ b/src/graphics/dearimgui/CMakeLists.txt @@ -1,7 +1,7 @@ add_library(dearimgui imgui.cpp imgui_demo.cpp imgui_widgets.cpp imgui_tables.cpp imgui_draw.cpp imgui_impl_glfw.cpp imgui_impl_opengl3.cpp) target_include_directories(dearimgui - PUBLIC ../../external/glfw/include + PUBLIC ../../../external/glfw/include ) # message( " current source dir: ${CMAKE_CURRENT_SOURCE_DIR}" ) \ No newline at end of file diff --git a/src/dearimgui/LICENSE.txt b/src/graphics/dearimgui/LICENSE.txt similarity index 100% rename from src/dearimgui/LICENSE.txt rename to src/graphics/dearimgui/LICENSE.txt diff --git a/src/dearimgui/imconfig.h b/src/graphics/dearimgui/imconfig.h similarity index 100% rename from src/dearimgui/imconfig.h rename to src/graphics/dearimgui/imconfig.h diff --git a/src/dearimgui/imgui.cpp b/src/graphics/dearimgui/imgui.cpp similarity index 100% rename from src/dearimgui/imgui.cpp rename to src/graphics/dearimgui/imgui.cpp diff --git a/src/dearimgui/imgui.h b/src/graphics/dearimgui/imgui.h similarity index 100% rename from src/dearimgui/imgui.h rename to src/graphics/dearimgui/imgui.h diff --git a/src/dearimgui/imgui_demo.cpp b/src/graphics/dearimgui/imgui_demo.cpp similarity index 100% rename from src/dearimgui/imgui_demo.cpp rename to src/graphics/dearimgui/imgui_demo.cpp diff --git a/src/dearimgui/imgui_draw.cpp b/src/graphics/dearimgui/imgui_draw.cpp similarity index 100% rename from src/dearimgui/imgui_draw.cpp rename to src/graphics/dearimgui/imgui_draw.cpp diff --git a/src/dearimgui/imgui_impl_glfw.cpp b/src/graphics/dearimgui/imgui_impl_glfw.cpp similarity index 100% rename from src/dearimgui/imgui_impl_glfw.cpp rename to src/graphics/dearimgui/imgui_impl_glfw.cpp diff --git a/src/dearimgui/imgui_impl_glfw.h b/src/graphics/dearimgui/imgui_impl_glfw.h similarity index 100% rename from src/dearimgui/imgui_impl_glfw.h rename to src/graphics/dearimgui/imgui_impl_glfw.h diff --git a/src/dearimgui/imgui_impl_opengl3.cpp b/src/graphics/dearimgui/imgui_impl_opengl3.cpp similarity index 100% rename from src/dearimgui/imgui_impl_opengl3.cpp rename to src/graphics/dearimgui/imgui_impl_opengl3.cpp diff --git a/src/dearimgui/imgui_impl_opengl3.h b/src/graphics/dearimgui/imgui_impl_opengl3.h similarity index 100% rename from src/dearimgui/imgui_impl_opengl3.h rename to src/graphics/dearimgui/imgui_impl_opengl3.h diff --git a/src/dearimgui/imgui_impl_opengl3_loader.h b/src/graphics/dearimgui/imgui_impl_opengl3_loader.h similarity index 100% rename from src/dearimgui/imgui_impl_opengl3_loader.h rename to src/graphics/dearimgui/imgui_impl_opengl3_loader.h diff --git a/src/dearimgui/imgui_internal.h b/src/graphics/dearimgui/imgui_internal.h similarity index 100% rename from src/dearimgui/imgui_internal.h rename to src/graphics/dearimgui/imgui_internal.h diff --git a/src/dearimgui/imgui_tables.cpp b/src/graphics/dearimgui/imgui_tables.cpp similarity index 100% rename from src/dearimgui/imgui_tables.cpp rename to src/graphics/dearimgui/imgui_tables.cpp diff --git a/src/dearimgui/imgui_widgets.cpp b/src/graphics/dearimgui/imgui_widgets.cpp similarity index 100% rename from src/dearimgui/imgui_widgets.cpp rename to src/graphics/dearimgui/imgui_widgets.cpp diff --git a/src/dearimgui/imstb_rectpack.h b/src/graphics/dearimgui/imstb_rectpack.h similarity index 100% rename from src/dearimgui/imstb_rectpack.h rename to src/graphics/dearimgui/imstb_rectpack.h diff --git a/src/dearimgui/imstb_textedit.h b/src/graphics/dearimgui/imstb_textedit.h similarity index 100% rename from src/dearimgui/imstb_textedit.h rename to src/graphics/dearimgui/imstb_textedit.h diff --git a/src/dearimgui/imstb_truetype.h b/src/graphics/dearimgui/imstb_truetype.h similarity index 100% rename from src/dearimgui/imstb_truetype.h rename to src/graphics/dearimgui/imstb_truetype.h diff --git a/src/graphics/defaultShaders.h b/src/graphics/opengl/defaultShaders.h similarity index 100% rename from src/graphics/defaultShaders.h rename to src/graphics/opengl/defaultShaders.h diff --git a/src/graphics/glGraphics.cpp b/src/graphics/opengl/glGraphics.cpp similarity index 99% rename from src/graphics/glGraphics.cpp rename to src/graphics/opengl/glGraphics.cpp index 852b4c8..c4ac9d9 100644 --- a/src/graphics/glGraphics.cpp +++ b/src/graphics/opengl/glGraphics.cpp @@ -8,9 +8,9 @@ #include "glGraphics.h" #include "defaultShaders.h" -#include "window.h" -#include "image.h" -#include "internalFont.h" +#include +#include "../image.h" +#include "../internalFont.h" #include #include #include diff --git a/src/graphics/glGraphics.h b/src/graphics/opengl/glGraphics.h similarity index 98% rename from src/graphics/glGraphics.h rename to src/graphics/opengl/glGraphics.h index 7168bdc..37ead13 100644 --- a/src/graphics/glGraphics.h +++ b/src/graphics/opengl/glGraphics.h @@ -9,7 +9,7 @@ #ifndef OGLGRAPHICS_H_ #define OGLGRAPHICS_H_ -#include "igraphics.h" +#include "../igraphics.h" #include #include "glShader.h" #include "glText.h" diff --git a/src/graphics/glShader.cpp b/src/graphics/opengl/glShader.cpp similarity index 100% rename from src/graphics/glShader.cpp rename to src/graphics/opengl/glShader.cpp diff --git a/src/graphics/glShader.h b/src/graphics/opengl/glShader.h similarity index 100% rename from src/graphics/glShader.h rename to src/graphics/opengl/glShader.h diff --git a/src/graphics/glText.cpp b/src/graphics/opengl/glText.cpp similarity index 100% rename from src/graphics/glText.cpp rename to src/graphics/opengl/glText.cpp diff --git a/src/graphics/glText.h b/src/graphics/opengl/glText.h similarity index 100% rename from src/graphics/glText.h rename to src/graphics/opengl/glText.h diff --git a/src/input/inputManager.cpp b/src/input/inputManager.cpp new file mode 100644 index 0000000..5cf24ee --- /dev/null +++ b/src/input/inputManager.cpp @@ -0,0 +1,132 @@ + +/****************************************************************************** +* File - InputManager.pp +* Author - Joey Pollack +* Date - 2019/11/14 (y/m/d) +* Mod Date - 2019/11/25 (y/m/d) +* Description - Handles all input for windows systems +* +******************************************************************************/ + +#include +#include "inputManager.h" +#include + +namespace lunarium +{ + InputManager::InputManager() + : mpWindow(nullptr) + { + memset(mKeyboardState, 0, NUM_KEYS); + } + + void InputManager::Initialize(Window* pWindow) + { + mpWindow = pWindow; + } + + const InputManager::_KeyEvents& InputManager::PollKeys() + { + mKeyEvents.KeysPressed.clear(); + mKeyEvents.KeysReleased.clear(); + + // Check Modifiers first so we only have to do it once + // bool shiftHeld = GetAsyncKeyState(VK_SHIFT); + // bool controlHeld = GetAsyncKeyState(VK_CONTROL); + // bool altHeld = GetAsyncKeyState(VK_MENU); + bool shiftHeld = mpWindow->IsKeyDown(KeyCode::SHIFT); + bool controlHeld = mpWindow->IsKeyDown(KeyCode::CONTROL); + bool altHeld = mpWindow->IsKeyDown(KeyCode::ALT); + + + for (int i = 0; i < NUM_KEYS; i++) + { + Keyboard::Key key = Keyboard::GetInstance()->GetKey((KeyCode)i); + bool keyIsDown = mpWindow->IsKeyDown(KeyCodeList[i]); + + // Mouse Buttons are a special case + if (IsMouseButton(i)) + { + if (keyIsDown) + { + mKeyboardState[i] = KEY_DOWN; + } + else + { + mKeyboardState[i] = KEY_UP; + } + + continue; + } + + // Key is a known keyboard key + if (keyIsDown) + { + // Ignore key presses for unknown keys + if (KeyCode::KEY_UNKNOWN == key.Code) + { + Logger::Log(LogCategory::CORE, LogLevel::INFO_VERBOSE, + "Input Manager skipping unknown key press: idx: %d, code: %d", i, KeyCodeList[i]); + continue; + } + + // Key was just pressed this frame + if (KEY_UP == mKeyboardState[i]) + { + mKeyboardState[i] = KEY_DOWN; + + KeyPress kp; + kp.Key = key; + kp.ShiftHeld = shiftHeld; + kp.ControlHeld = controlHeld; + kp.AltHeld = altHeld; + + mKeyEvents.KeysPressed.push_back(kp); + } + } + else + { + // Key was just released this frame + if (KEY_DOWN == mKeyboardState[i]) + { + mKeyboardState[i] = KEY_UP; + mKeyEvents.KeysReleased.push_back(key); + } + } + } + + return mKeyEvents; + } + + bool InputManager::IsMouseButton(int keyCode) + { + return (keyCode == KeyCode::MOUSE_LEFT_BUTTON || + keyCode == KeyCode::MOUSE_RIGHT_BUTTON || + keyCode == KeyCode::MOUSE_MIDDLE_BUTTON || + keyCode == KeyCode::MOUSE_X1_BUTTON || + keyCode == KeyCode::MOUSE_X2_BUTTON ); + } + + bool InputManager::IsKeyDown(KeyCode key) + { + return (mKeyboardState[key] == KEY_DOWN); + } + + // TODO: FIX WINDOWS SPECIFIC CODE: + glm::vec2 InputManager::GetMousePosition() + { + // POINT p; + // memset(&p, 0, sizeof(POINT)); + // GetCursorPos(&p); + + // return { (float)p.x, (float)p.y }; + return mpWindow->GetCursorPos(); + } + + void InputManager::SetMousePosition(glm::vec2 p) + { + //SetCursorPos((int)p.X, (int)p.Y); + mpWindow->SetCursorPos(p); + } + /////////////////////////////////////////////////// +} \ No newline at end of file diff --git a/src/input/inputManager.h b/src/input/inputManager.h new file mode 100644 index 0000000..fe0a369 --- /dev/null +++ b/src/input/inputManager.h @@ -0,0 +1,88 @@ + +/****************************************************************************** +* File - InputManager.h +* Author - Joey Pollack +* Date - 2019/11/14 (y/m/d) +* Mod Date - 2021/09/08 (y/m/d) +* Description - Handles all input for windows systems +* +******************************************************************************/ + +#ifndef INPUT_MANAGER_H_ +#define INPUT_MANAGER_H_ + +#include +#include +#include + +#include "keyboard.h" + +namespace lunarium +{ + class Window; + + class InputManager + { + public: + + struct KeyPress + { + Keyboard::Key Key; + + // Modifiers + bool ShiftHeld; + bool ControlHeld; + bool AltHeld; + }; + + struct MouseButton + { + KeyCode code; + std::string Name; + }; + + private: + friend class Core; + + struct _KeyEvents + { + std::vector KeysPressed; + std::vector KeysReleased; + + } mKeyEvents; + + const int KEY_UP = 0; + const int KEY_DOWN = 0xFF; + + + private: // ENGINE ONLY METHODS + InputManager(); + void Initialize(Window* pWindow); + + // Returns an array of keys that were pressed + // Keys are only pressed if they were not held on the previous frame + const _KeyEvents& PollKeys(); + + bool IsMouseButton(int keyCode); + // TODO: Remove these functions + // KeyCode TranslateKey(int k); + // char GetAsciiFromKeyPress(KeyCode k, bool shiftHeld); + + public: + + bool IsKeyDown(KeyCode key); + + // Returns the mouse position in screen coordinates + glm::vec2 GetMousePosition(); + void SetMousePosition(glm::vec2 p); + + private: + Window* mpWindow; + + unsigned char mKeyboardState[NUM_KEYS]; + //unsigned char mKeyboardPreviousState[NUM_KEYS]; + std::vector mModifiers; + }; +} + +#endif // WIN_INPUT_MANAGER_H_ \ No newline at end of file diff --git a/src/input/keyCodes.h b/src/input/keyCodes.h new file mode 100644 index 0000000..a08bb20 --- /dev/null +++ b/src/input/keyCodes.h @@ -0,0 +1,223 @@ +/****************************************************************************** +* File - KeyCodes.h +* Author - Joey Pollack +* Date - 2019/11/14 (y/m/d) +* Mod Date - 2021/09/08 (y/m/d) +* Description - Key code definitions using GLFW +* +******************************************************************************/ + +#ifndef KEY_CODES_H_ +#define KEY_CODES_H_ + +#include + +namespace lunarium +{ + enum KeyCode + { + A = 'A', + B = 'B', + C = 'C', + D = 'D', + E = 'E', + F = 'F', + G = 'G', + H = 'H', + I = 'I', + J = 'J', + K = 'K', + L = 'L', + M = 'M', + N = 'N', + O = 'O', + P = 'P', + Q = 'Q', + R = 'R', + S = 'S', + T = 'T', + U = 'U', + V = 'V', + W = 'W', + X = 'X', + Y = 'Y', + Z = 'Z', + + NUM_0 = '0', + NUM_1 = '1', + NUM_2 = '2', + NUM_3 = '3', + NUM_4 = '4', + NUM_5 = '5', + NUM_6 = '6', + NUM_7 = '7', + NUM_8 = '8', + NUM_9 = '9', + + F1 = GLFW_KEY_F1, + F2 = GLFW_KEY_F2, + F3 = GLFW_KEY_F3, + F4 = GLFW_KEY_F4, + F5 = GLFW_KEY_F5, + F6 = GLFW_KEY_F6, + F7 = GLFW_KEY_F7, + F8 = GLFW_KEY_F8, + F9 = GLFW_KEY_F9, + F10 = GLFW_KEY_F10, + F11 = GLFW_KEY_F11, + F12 = GLFW_KEY_F12, + + LSHIFT = GLFW_KEY_LEFT_SHIFT, + RSHIFT = GLFW_KEY_RIGHT_SHIFT, + LCONTROL = GLFW_KEY_LEFT_CONTROL, + RCONTROL = GLFW_KEY_RIGHT_CONTROL, + LALT = GLFW_KEY_LEFT_ALT, + RALT = GLFW_KEY_RIGHT_ALT, + + SPACE = GLFW_KEY_SPACE, + + // Left Arrow Key + LEFT = GLFW_KEY_LEFT, + + // Up Arrow Key + UP = GLFW_KEY_UP, + + // Right Arrow Key + RIGHT = GLFW_KEY_RIGHT, + + // Down Arrow Key + DOWN = GLFW_KEY_DOWN, + + // DELETE + DEL = GLFW_KEY_DELETE, + + // ENTER + RETURN = GLFW_KEY_ENTER, + + BACKSPACE = GLFW_KEY_BACKSPACE, + TAB = GLFW_KEY_TAB, + + ESCAPE = GLFW_KEY_ESCAPE, + + // Punctuation + SEMICOLON = GLFW_KEY_SEMICOLON, // ';:' for US + EQUALS = GLFW_KEY_EQUAL, // '+' any country + COMMA = GLFW_KEY_COMMA, // ',' any country + DASH = GLFW_KEY_MINUS, // '-' any country + DOT = GLFW_KEY_PERIOD, // '.' any country + FORWARD_SLASH = GLFW_KEY_SLASH, // '/?' for US + TILDE = GLFW_KEY_GRAVE_ACCENT, // '`~' for US + + OPEN_BRACKET = GLFW_KEY_LEFT_BRACKET, // '[{' for US + BACKSLASH = GLFW_KEY_BACKSLASH, // '\|' for US + CLOSE_BRACKET = GLFW_KEY_RIGHT_BRACKET, // ']}' for US + QUOTE = GLFW_KEY_APOSTROPHE, // ''"' for US + + // MOUSE BUTTONS + MOUSE_LEFT_BUTTON = GLFW_MOUSE_BUTTON_LEFT, + MOUSE_RIGHT_BUTTON = GLFW_MOUSE_BUTTON_RIGHT, + MOUSE_MIDDLE_BUTTON = GLFW_MOUSE_BUTTON_MIDDLE, + + // NOTE: Not sure about these + MOUSE_X1_BUTTON = GLFW_MOUSE_BUTTON_4, + MOUSE_X2_BUTTON = GLFW_MOUSE_BUTTON_5, + + NUM_KEYS, + + // ALTERNATE NAMES + SHIFT = GLFW_KEY_LEFT_SHIFT, + CONTROL = GLFW_KEY_LEFT_CONTROL, + ALT = GLFW_KEY_LEFT_ALT, + + KEY_UNKNOWN = 0xFF + }; + + static int KeyCodeList[] = { + KeyCode::A, + KeyCode::B, + KeyCode::C, + KeyCode::D, + KeyCode::E, + KeyCode::F, + KeyCode::G, + KeyCode::H, + KeyCode::I, + KeyCode::J, + KeyCode::K, + KeyCode::L, + KeyCode::M, + KeyCode::N, + KeyCode::O, + KeyCode::P, + KeyCode::Q, + KeyCode::R, + KeyCode::S, + KeyCode::T, + KeyCode::U, + KeyCode::V, + KeyCode::W, + KeyCode::X, + KeyCode::Y, + KeyCode::Z, + + KeyCode::NUM_0, + KeyCode::NUM_1, + KeyCode::NUM_2, + KeyCode::NUM_3, + KeyCode::NUM_4, + KeyCode::NUM_5, + KeyCode::NUM_6, + KeyCode::NUM_7, + KeyCode::NUM_8, + KeyCode::NUM_9, + + KeyCode::F1, + KeyCode::F2, + KeyCode::F3, + KeyCode::F4, + KeyCode::F5, + KeyCode::F6, + KeyCode::F7, + KeyCode::F8, + KeyCode::F9, + KeyCode::F10, + KeyCode::F11, + KeyCode::F12, + + KeyCode::LSHIFT, + KeyCode::RSHIFT, + KeyCode::LCONTROL, + KeyCode::RCONTROL, + KeyCode::LALT, + KeyCode::RALT, + KeyCode::SPACE, + KeyCode::LEFT, + KeyCode::UP, + KeyCode::RIGHT, + KeyCode::DOWN, + KeyCode::DEL, + KeyCode::RETURN, + KeyCode::BACKSPACE, + KeyCode::TAB, + KeyCode::ESCAPE, + KeyCode::SEMICOLON, + KeyCode::EQUALS, + KeyCode::COMMA, + KeyCode::DASH, + KeyCode::DOT, + KeyCode::FORWARD_SLASH, + KeyCode::TILDE, + KeyCode::OPEN_BRACKET, + KeyCode::BACKSLASH, + KeyCode::CLOSE_BRACKET, + KeyCode::QUOTE, + KeyCode::MOUSE_LEFT_BUTTON, + KeyCode::MOUSE_RIGHT_BUTTON, + KeyCode::MOUSE_MIDDLE_BUTTON, + KeyCode::MOUSE_X1_BUTTON, + KeyCode::MOUSE_X2_BUTTON + }; +} + + +#endif // KEY_CODES_H_ \ No newline at end of file diff --git a/src/input/keyboard.cpp b/src/input/keyboard.cpp new file mode 100644 index 0000000..ac6bfc6 --- /dev/null +++ b/src/input/keyboard.cpp @@ -0,0 +1,188 @@ + +/****************************************************************************** +* File - Keyboard.cpp +* Author - Joey Pollack +* Date - 2019/11/15 (y/m/d) +* Mod Date - 2019/11/15 (y/m/d) +* Description - Stores data about the keys on the keyboard +* +******************************************************************************/ + +#include "keyboard.h" + +namespace lunarium +{ + Keyboard* Keyboard::mpInstance = nullptr; + + void Keyboard::Shutdown() + { + if (mpInstance) + { + delete mpInstance; + mpInstance = nullptr; + } + } + + Keyboard * Keyboard::GetInstance() + { + if (!mpInstance) + { + mpInstance = new Keyboard; + mpInstance->Initialize(); + } + + return mpInstance; + } + + Keyboard::Keyboard() + { + + } + + void Keyboard::Initialize() + { + // Add letters + for (char letter = 'A'; letter <= 'Z'; letter++) + { + Key k; + k.Code = (KeyCode)letter; + k.Name = std::string() + letter; + k.ShiftName = std::string() + (char)(letter + 32); + k.AsciiValue = letter; + k.ShiftAsciiValue = letter + 32; + + mKeyboard.insert(std::pair(k.Code, k)); + } + + // Add Numbers + char numShift[10] = { ')', '!', '@', '#', '$', '%', '^', '&', '*', '(' }; + for (int i = 0; i <= 9; i++) + { + Key k; + k.Code = (KeyCode)(i + 48); + + char name[2]; + sprintf_s(name, 2, "%d", i); + + k.Name = std::string(name); + k.ShiftName = std::string() + numShift[i]; + k.AsciiValue = (char)(i + 48); + k.ShiftAsciiValue = numShift[i]; + + mKeyboard.insert(std::pair(k.Code, k)); + } + + // Punctuation + /* + SEMICOLON = GLFW_KEY_SEMICOLON, // ';:' for US + EQUALS = GLFW_KEY_EQUAL, // '+' any country + COMMA = GLFW_KEY_COMMA, // ',' any country + DASH = GLFW_KEY_MINUS, // '-' any country + DOT = GLFW_KEY_PERIOD, // '.' any country + FORWARD_SLASH = GLFW_KEY_SLASH, // '/?' for US + TILDE = GLFW_KEY_GRAVE_ACCENT, // '`~' for US + + OPEN_BRACKET = GLFW_KEY_LEFT_BRACKET, // '[{' for US + BACKSLASH = GLFW_KEY_BACKSLASH, // '\|' for US + CLOSE_BRACKET = GLFW_KEY_RIGHT_BRACKET, // ']}' for US + QUOTE = GLFW_KEY_APOSTROPHE, // ''"' for US + */ + mKeyboard.insert(std::pair(SEMICOLON, { SEMICOLON, "semi colon", "colon", ';', ';' })); + mKeyboard.insert(std::pair(EQUALS, { EQUALS, "equals", "plus", '=', '+' })); + mKeyboard.insert(std::pair(COMMA, { COMMA, "comma", "less than", ',', '<' })); + mKeyboard.insert(std::pair(DASH, { DASH, "dash", "underscore", '-', '_' })); + mKeyboard.insert(std::pair(DOT, { DOT, "dot", "greater than", '.', '>' })); + mKeyboard.insert(std::pair(FORWARD_SLASH, { FORWARD_SLASH, "forward slash", "question mark", '/', '?' })); + mKeyboard.insert(std::pair(TILDE, { TILDE, "tick", "tilde", '`', '~' })); + mKeyboard.insert(std::pair(OPEN_BRACKET, { OPEN_BRACKET, "open square bracket", "open curly brace", '[', '{' })); + mKeyboard.insert(std::pair(BACKSLASH, { BACKSLASH, "back slash", "pipe", '\\', '|' })); + mKeyboard.insert(std::pair(CLOSE_BRACKET, { CLOSE_BRACKET, "close square bracket", "close curly brace", ']', '}' })); + mKeyboard.insert(std::pair(QUOTE, { QUOTE, "single quote", "double quote", '\'', '\"' })); + + + // Modifiers + mKeyboard.insert(std::pair(KeyCode::LSHIFT, { KeyCode::LSHIFT, "left shift", "left shift", (char)0, (char)0 })); + mKeyboard.insert(std::pair(KeyCode::RSHIFT, { KeyCode::RSHIFT, "right shift", "right shift", (char)0, (char)0 })); + + mKeyboard.insert(std::pair(KeyCode::LCONTROL, { KeyCode::LCONTROL, "left control", "left control", (char)0, (char)0 })); + mKeyboard.insert(std::pair(KeyCode::RCONTROL, { KeyCode::RCONTROL, "right control", "right control", (char)0, (char)0 })); + + mKeyboard.insert(std::pair(KeyCode::LALT, { KeyCode::LALT, "left alt", "left alt", (char)0, (char)0 })); + mKeyboard.insert(std::pair(KeyCode::RALT, { KeyCode::RALT, "right alt", "right alt", (char)0, (char)0 })); + + + // Control characters + mKeyboard.insert(std::pair(KeyCode::DEL, { KeyCode::DEL, "delete", "delete", (char)(127), (char)(127) })); + mKeyboard.insert(std::pair(KeyCode::BACKSPACE, { KeyCode::BACKSPACE, "backspace", "backspace", (char)(8), (char)(8) })); + mKeyboard.insert(std::pair(KeyCode::TAB, { KeyCode::TAB, "tab", "tab", (char)(9), (char)(9) })); + mKeyboard.insert(std::pair(KeyCode::RETURN, { KeyCode::RETURN, "return", "return", (char)(10), (char)(10) })); + mKeyboard.insert(std::pair(KeyCode::ESCAPE, { KeyCode::ESCAPE, "escape", "escape", (char)(27), (char)(27) })); + + // Arrow Keys + mKeyboard.insert(std::pair(KeyCode::LEFT, { KeyCode::LEFT, "Left Arrow", "Left Arrow", (char)(0), (char)(0) })); + mKeyboard.insert(std::pair(KeyCode::UP, { KeyCode::UP, "Up Arrow", "Up Arrow", (char)(0), (char)(0) })); + mKeyboard.insert(std::pair(KeyCode::RIGHT, { KeyCode::RIGHT, "Right Arrow", "Right Arrow", (char)(0), (char)(0) })); + mKeyboard.insert(std::pair(KeyCode::DOWN, { KeyCode::DOWN, "Down Arrow", "Down Arrow", (char)(0), (char)(0) })); + + // Set up the cross references + InitKeysByName(); + InitKeysByAscii(); + } + + + Keyboard::Key Keyboard::GetKey(KeyCode code) + { + auto iter = mKeyboard.find(code); + if (mKeyboard.end() == iter) + return Key::Unknown(); + + return (*iter).second; + } + + Keyboard::Key Keyboard::GetKey(std::string name) + { + auto iter = mKeysByName.find(name); + if (mKeysByName.end() == iter) + return Key::Unknown(); + + return (*iter).second; + } + + Keyboard::Key Keyboard::GetKey(char ascii) + { + auto iter = mKeysByAscii.find(ascii); + if (mKeysByAscii.end() == iter) + return Key::Unknown(); + + return (*iter).second; + } + + void Keyboard::InitKeysByName() + { + for (auto iter = mKeyboard.begin(); iter != mKeyboard.end(); iter++) + { + mKeysByName.insert(std::pair((*iter).second.Name, (*iter).second)); + } + } + + void Keyboard::InitKeysByAscii() + { + for (auto iter = mKeyboard.begin(); iter != mKeyboard.end(); iter++) + { + mKeysByAscii.insert(std::pair((*iter).second.AsciiValue, (*iter).second)); + mKeysByAscii.insert(std::pair((*iter).second.ShiftAsciiValue, (*iter).second)); + } + } + + Keyboard::Key Keyboard::Key::Unknown() + { + Key k; + k.Code = KeyCode::KEY_UNKNOWN; + k.Name = "unknown"; + k.ShiftName = "unknown"; + k.AsciiValue = (char)0; + k.ShiftAsciiValue = (char)0; + + return k; + } +} \ No newline at end of file diff --git a/src/input/keyboard.h b/src/input/keyboard.h new file mode 100644 index 0000000..6bdacd2 --- /dev/null +++ b/src/input/keyboard.h @@ -0,0 +1,69 @@ + +/****************************************************************************** +* File - Keyboard.h +* Author - Joey Pollack +* Date - 2019/11/15 (y/m/d) +* Mod Date - 2019/11/15 (y/m/d) +* Description - Stores data about the keys on the keyboard +* +******************************************************************************/ + +#ifndef KEYBOARD_H_ +#define KEYBOARD_H_ + +#include +#include + +#include "keyCodes.h" + +namespace lunarium +{ + class Keyboard + { + friend class Core; + + public: // Data structures + struct Key + { + KeyCode Code; + std::string Name; + std::string ShiftName; + char AsciiValue; + char ShiftAsciiValue; + + static Key Unknown(); + }; + + private: // Internal / Engine Only + Keyboard(); + Keyboard(const Keyboard&) = delete; + Keyboard& operator=(const Keyboard&) = delete; + + void Initialize(); + static void Shutdown(); + + public: // Interface + + static Keyboard* GetInstance(); + + Key GetKey(KeyCode code); + Key GetKey(std::string name); + Key GetKey(char ascii); + + private: // The instance + static Keyboard* mpInstance; + + private: // Data + std::map mKeyboard; + + // Cross references + std::map mKeysByName; + std::map mKeysByAscii; + + private: // Helper methods + void InitKeysByName(); + void InitKeysByAscii(); + }; +} + +#endif // KEYBOARD_H_ \ No newline at end of file