summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2023-10-11 13:03:35 -0400
committerJonathan Bradley <jcb@pikum.xyz>2023-10-11 13:03:35 -0400
commitfb5c06777557fc28c0d8e919d9a82bdf51adeea7 (patch)
tree2b80595cf3d286a3d72619102d0df06582de41dd /src
parent40ab6886e72c660d424fec9140feb8685db7d363 (diff)
checkpoint for handling player input
Diffstat (limited to 'src')
-rw-r--r--src/game-settings.hpp2
-rw-r--r--src/game-type-defs.hpp11
-rw-r--r--src/game.cpp1
-rw-r--r--src/game.hpp7
-rw-r--r--src/main.cpp14
-rw-r--r--src/player-input.cpp119
-rw-r--r--src/player-input.hpp57
7 files changed, 197 insertions, 14 deletions
diff --git a/src/game-settings.hpp b/src/game-settings.hpp
index 2f7fb26..a624bd6 100644
--- a/src/game-settings.hpp
+++ b/src/game-settings.hpp
@@ -1,6 +1,7 @@
#ifndef PKE_GAME_SETTINGS_HPP
#define PKE_GAME_SETTINGS_HPP
+#include <chrono>
#include <cstdint>
struct GameSettings {
@@ -9,6 +10,7 @@ struct GameSettings {
bool isFramerateUnlocked = true;
bool isShowingEditor = true;
bool isRenderingDebug = false;
+ std::chrono::steady_clock steadyClock;
int64_t targetFPS = 144;
int64_t minFPS = 20;
double deltaPerFrame = 1 / double(targetFPS);
diff --git a/src/game-type-defs.hpp b/src/game-type-defs.hpp
new file mode 100644
index 0000000..62d8c6a
--- /dev/null
+++ b/src/game-type-defs.hpp
@@ -0,0 +1,11 @@
+#ifndef PKE_GAME_TYPE_DEFS_HPP
+#define PKE_GAME_TYPE_DEFS_HPP
+
+#include <chrono>
+
+using GameTimeDuration = std::chrono::duration<int64_t, std::nano>;
+using GameTimePoint = std::chrono::steady_clock::time_point;
+#define NANO_DENOM std::chrono::nanoseconds::period::den
+#define NANO_DENOM_DOUBLE double(std::chrono::nanoseconds::period::den)
+
+#endif /* PKE_GAME_TYPE_DEFS_HPP */
diff --git a/src/game.cpp b/src/game.cpp
index 4de3f04..366eee8 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -453,7 +453,6 @@ void RecordImGuiEntityList() {
ImGui::PushID(row);
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
- char label[32];
if (ImGui::Button("Select"))
selectedEntity = entity->handle;
ImGui::TableSetColumnIndex(1);
diff --git a/src/game.hpp b/src/game.hpp
index 05f22e5..a449ed4 100644
--- a/src/game.hpp
+++ b/src/game.hpp
@@ -1,13 +1,6 @@
#ifndef PKE_GAME_HPP
#define PKE_GAME_HPP
-#include <chrono>
-
-using GameTimeDuration = std::chrono::duration<int64_t, std::nano>;
-using GameTimePoint = std::chrono::steady_clock::time_point;
-#define NANO_DENOM std::chrono::nanoseconds::period::den
-#define NANO_DENOM_DOUBLE double(std::chrono::nanoseconds::period::den)
-
void Game_Init();
void Game_Tick(double delta);
void Game_Teardown();
diff --git a/src/main.cpp b/src/main.cpp
index a587b88..ec0bf71 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6,12 +6,14 @@
#include <thread>
#include <csignal>
+#include "player-input.hpp"
#include "asset-manager.hpp"
#include "ecs.hpp"
-#include "game.hpp"
-#include "window.hpp"
#include "entities.hpp"
#include "game-settings.hpp"
+#include "game-type-defs.hpp"
+#include "game.hpp"
+#include "window.hpp"
void signal_handler(int signal_num) {
printf("Received signal: %d - shutting down\n", signal_num);
@@ -32,13 +34,13 @@ int main() {
AM_Init();
Game_Init();
ECS_Init();
+ PkeInput_Init();
CreateWindow(&windowProps);
EntityType_Init();
- auto steadyClock = std::chrono::steady_clock();
- GameTimePoint lastTimePoint = steadyClock.now();
+ GameTimePoint lastTimePoint = pkeSettings.steadyClock.now();
double deltaTillNextRender = pkeSettings.deltaPerFrame;
- GameTimePoint lastLogTimePoint = steadyClock.now();
+ GameTimePoint lastLogTimePoint = pkeSettings.steadyClock.now();
int64_t tickCount = 0;
int64_t nsAhead = 0.0;
@@ -52,7 +54,7 @@ int main() {
nsAhead = 0;
}
- GameTimePoint currentTimePoint = steadyClock.now();
+ GameTimePoint currentTimePoint = pkeSettings.steadyClock.now();
double deltaThisTick = (currentTimePoint - lastTimePoint).count() / NANO_DENOM_DOUBLE;
deltaThisTick = std::min(deltaThisTick, pkeSettings.minimumDeltaPerFrame);
deltaTillNextRender -= deltaThisTick;
diff --git a/src/player-input.cpp b/src/player-input.cpp
new file mode 100644
index 0000000..148eb22
--- /dev/null
+++ b/src/player-input.cpp
@@ -0,0 +1,119 @@
+
+#include "player-input.hpp"
+#include "window.hpp"
+#include "game-settings.hpp"
+
+TypeSafeInt_B(InputEventHash);
+TypeSafeInt_B(InputEventType);
+TypeSafeInt_B(InputCursorEntered);
+TypeSafeInt_B(InputKeyboardKeyAction);
+TypeSafeInt_B(InputMouseButtonAction);
+TypeSafeInt_B(InputWindowFocused);
+
+DynArray<PkeInputEvent> UnhandledPkeInputEvents{0};
+
+void PkeInput_Tick() { }
+
+void CursorEnterCallback(GLFWwindow *window, int entered) {
+ auto &ev = UnhandledPkeInputEvents.Push();
+ ev.type = PKE_INPUT_EVENT_TYPE_CURSOR_ENTER;
+ ev.timePoint = pkeSettings.steadyClock.now();
+ ev.cursorEntered = InputCursorEntered{static_cast<InputCursorEntered_T>(entered)};
+ ev.keyboardKeyAction = PKE_INPUT_KEYBOARD_KEY_ACTION_UNSET;
+ ev.mouseButtonAction = PKE_INPUT_MOUSE_BUTTON_ACTION_UNSET;
+ ev.windowFocused = PKE_INPUT_WINDOW_FOCUSED_UNSET;
+ ev.x = 0;
+ ev.y = 0;
+ ev.button = 0;
+ ev.scanCode = 0;
+ ev.mods = 0;
+}
+
+void CursorPosCallback(GLFWwindow *window, double xPos, double yPos) {
+ auto &ev = UnhandledPkeInputEvents.Push();
+ ev.type = PKE_INPUT_EVENT_TYPE_CURSOR_POS;
+ ev.timePoint = pkeSettings.steadyClock.now();
+ ev.cursorEntered = PKE_INPUT_CURSOR_ENTERED_UNSET;
+ ev.keyboardKeyAction = PKE_INPUT_KEYBOARD_KEY_ACTION_UNSET;
+ ev.mouseButtonAction = PKE_INPUT_MOUSE_BUTTON_ACTION_UNSET;
+ ev.windowFocused = PKE_INPUT_WINDOW_FOCUSED_UNSET;
+ ev.x = xPos;
+ ev.y = yPos;
+ ev.button = 0;
+ ev.scanCode = 0;
+ ev.mods = 0;
+}
+
+void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
+ auto &ev = UnhandledPkeInputEvents.Push();
+ ev.type = PKE_INPUT_EVENT_TYPE_KEY;
+ ev.timePoint = pkeSettings.steadyClock.now();
+ ev.cursorEntered = PKE_INPUT_CURSOR_ENTERED_UNSET;
+ ev.keyboardKeyAction = InputKeyboardKeyAction{static_cast<InputKeyboardKeyAction_T>(action)};
+ ev.mouseButtonAction = PKE_INPUT_MOUSE_BUTTON_ACTION_UNSET;
+ ev.windowFocused = PKE_INPUT_WINDOW_FOCUSED_UNSET;
+ ev.x = 0;
+ ev.y = 0;
+ ev.button = key;
+ ev.scanCode = scancode;
+ ev.mods = mods;
+}
+
+void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods) {
+ auto &ev = UnhandledPkeInputEvents.Push();
+ ev.type = PKE_INPUT_EVENT_TYPE_MOUSE_BUTTON;
+ ev.timePoint = pkeSettings.steadyClock.now();
+ ev.cursorEntered = PKE_INPUT_CURSOR_ENTERED_UNSET;
+ ev.keyboardKeyAction = PKE_INPUT_KEYBOARD_KEY_ACTION_UNSET;
+ ev.mouseButtonAction = InputMouseButtonAction{static_cast<InputMouseButtonAction_T>(action)};
+ ev.windowFocused = PKE_INPUT_WINDOW_FOCUSED_UNSET;
+ ev.x = 0;
+ ev.y = 0;
+ ev.button = button;
+ ev.scanCode = 0;
+ ev.mods = mods;
+}
+
+void ScrollCallback(GLFWwindow *window, double xOffset, double yOffset) {
+ auto &ev = UnhandledPkeInputEvents.Push();
+ ev.type = PKE_INPUT_EVENT_TYPE_SCROLL;
+ ev.timePoint = pkeSettings.steadyClock.now();
+ ev.cursorEntered = PKE_INPUT_CURSOR_ENTERED_UNSET;
+ ev.keyboardKeyAction = PKE_INPUT_KEYBOARD_KEY_ACTION_UNSET;
+ ev.mouseButtonAction = PKE_INPUT_MOUSE_BUTTON_ACTION_UNSET;
+ ev.windowFocused = PKE_INPUT_WINDOW_FOCUSED_UNSET;
+ ev.x = xOffset;
+ ev.y = yOffset;
+ ev.button = 0;
+ ev.scanCode = 0;
+ ev.mods = 0;
+}
+
+void WindowFocusCallback(GLFWwindow *window, int focused) {
+ auto &ev = UnhandledPkeInputEvents.Push();
+ ev.type = PKE_INPUT_EVENT_TYPE_WINDOW_FOCUS;
+ ev.timePoint = pkeSettings.steadyClock.now();
+ ev.cursorEntered = PKE_INPUT_CURSOR_ENTERED_UNSET;
+ ev.keyboardKeyAction = PKE_INPUT_KEYBOARD_KEY_ACTION_UNSET;
+ ev.mouseButtonAction = PKE_INPUT_MOUSE_BUTTON_ACTION_UNSET;
+ ev.windowFocused = InputWindowFocused{static_cast<InputWindowFocused_T>(focused)};
+ ev.x = 0;
+ ev.y = 0;
+ ev.button = 0;
+ ev.scanCode = 0;
+ ev.mods = 0;
+}
+
+void PkeInput_Init() {
+ glfwSetCursorEnterCallback(window, CursorEnterCallback);
+ glfwSetCursorPosCallback(window, CursorPosCallback);
+ glfwSetKeyCallback(window, KeyCallback);
+ glfwSetMouseButtonCallback(window, MouseButtonCallback);
+ glfwSetScrollCallback(window, ScrollCallback);
+ glfwSetWindowFocusCallback(window, WindowFocusCallback);
+}
+
+void PkeInput_Teardown() {
+ UnhandledPkeInputEvents.~DynArray();
+}
+
diff --git a/src/player-input.hpp b/src/player-input.hpp
new file mode 100644
index 0000000..c6429eb
--- /dev/null
+++ b/src/player-input.hpp
@@ -0,0 +1,57 @@
+#ifndef PKE_PLAYER_INPUT_HPP
+#define PKE_PLAYER_INPUT_HPP
+
+#include "dynamic-array.hpp"
+#include "game-type-defs.hpp"
+#include "macros.hpp"
+
+TypeSafeInt_H(InputEventHash, int16_t, 255);
+TypeSafeInt_H(InputEventType, int16_t, 255);
+TypeSafeInt_H(InputCursorEntered, int16_t, 255);
+TypeSafeInt_H(InputKeyboardKeyAction, int16_t, 255);
+TypeSafeInt_H(InputMouseButtonAction, int16_t, 255);
+TypeSafeInt_H(InputWindowFocused, int16_t, 255);
+
+const InputEventType PKE_INPUT_EVENT_TYPE_CURSOR_ENTER = InputEventType{0};
+const InputEventType PKE_INPUT_EVENT_TYPE_CURSOR_POS = InputEventType{1};
+const InputEventType PKE_INPUT_EVENT_TYPE_KEY = InputEventType{2};
+const InputEventType PKE_INPUT_EVENT_TYPE_MOUSE_BUTTON = InputEventType{3};
+const InputEventType PKE_INPUT_EVENT_TYPE_SCROLL = InputEventType{4};
+const InputEventType PKE_INPUT_EVENT_TYPE_WINDOW_FOCUS = InputEventType{5};
+
+const InputCursorEntered PKE_INPUT_CURSOR_ENTERED_UNSET = InputCursorEntered{-1};
+const InputCursorEntered PKE_INPUT_CURSOR_ENTERED_FALSE = InputCursorEntered{0};
+const InputCursorEntered PKE_INPUT_CURSOR_ENTERED_TRUE = InputCursorEntered{1};
+
+const InputKeyboardKeyAction PKE_INPUT_KEYBOARD_KEY_ACTION_UNSET = InputKeyboardKeyAction{-1};
+const InputKeyboardKeyAction PKE_INPUT_KEYBOARD_KEY_ACTION_RELEASE = InputKeyboardKeyAction{0};
+const InputKeyboardKeyAction PKE_INPUT_KEYBOARD_KEY_ACTION_PRESS = InputKeyboardKeyAction{1};
+const InputKeyboardKeyAction PKE_INPUT_KEYBOARD_KEY_ACTION_REPEAT = InputKeyboardKeyAction{2};
+
+const InputMouseButtonAction PKE_INPUT_MOUSE_BUTTON_ACTION_UNSET = InputMouseButtonAction{-1};
+const InputMouseButtonAction PKE_INPUT_MOUSE_BUTTON_ACTION_RELEASE = InputMouseButtonAction{0};
+const InputMouseButtonAction PKE_INPUT_MOUSE_BUTTON_ACTION_PRESS = InputMouseButtonAction{1};
+
+const InputWindowFocused PKE_INPUT_WINDOW_FOCUSED_UNSET = InputWindowFocused{-1};
+const InputWindowFocused PKE_INPUT_WINDOW_FOCUSED_FALSE = InputWindowFocused{0};
+const InputWindowFocused PKE_INPUT_WINDOW_FOCUSED_TRUE = InputWindowFocused{1};
+
+struct PkeInputEvent {
+ InputEventType type;
+ GameTimePoint timePoint;
+ InputCursorEntered cursorEntered;
+ InputKeyboardKeyAction keyboardKeyAction;
+ InputMouseButtonAction mouseButtonAction;
+ InputWindowFocused windowFocused;
+ double x;
+ double y;
+ int32_t button;
+ int32_t scanCode;
+ int32_t mods;
+};
+
+void PkeInput_Tick();
+void PkeInput_Init();
+void PkeInput_Teardown();
+
+#endif /* PKE_PLAYER_INPUT_HPP */