Skip to content

Commit a753860

Browse files
authored
feat: キー入力を非同期対応
1 parent 675f846 commit a753860

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/input/input.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "input.hpp"
22

3-
#include "../error/error.hpp"
4-
5-
#include <unordered_map>
63
#include <SDL3/SDL.h>
74

85
namespace input {
@@ -67,44 +64,46 @@ const std::unordered_map<OrgeScancode, SDL_Scancode> ORGE_SDL_MAP{
6764
{ORGE_SCANCODE_UP, SDL_SCANCODE_UP},
6865
};
6966

70-
std::unordered_map<OrgeScancode, int32_t> g_states;
67+
void Input::update() {
68+
std::lock_guard lk(_mutex);
7169

72-
void update() {
7370
int numKeys = 0;
7471
const auto states = SDL_GetKeyboardState(&numKeys);
7572
for (const auto &n: ORGE_SDL_MAP) {
7673
if (numKeys <= static_cast<int>(n.second)) {
7774
continue;
7875
}
79-
const auto state = getState(n.first);
76+
const auto state = _states.contains(n.first) ? _states[n.first] : 0;
8077
// 押下
8178
if (states[n.second]) {
8279
// 押下中ならインクリメント
8380
if (state > 0) {
84-
g_states[n.first] = state + 1;
81+
_states[n.first] = state + 1;
8582
}
8683
// そうでないなら1に設定
8784
else {
88-
g_states[n.first] = 1;
85+
_states[n.first] = 1;
8986
}
9087
}
9188
// 非押下
9289
else {
9390
// 押下中なら-1に設定
9491
if (state > 0) {
95-
g_states[n.first] = -1;
92+
_states[n.first] = -1;
9693
}
9794
// そうでないなら0に設定
9895
else {
99-
g_states[n.first] = 0;
96+
_states[n.first] = 0;
10097
}
10198
}
10299
}
103100
}
104101

105-
int32_t getState(OrgeScancode scancode) {
106-
if (input::g_states.contains(scancode)) {
107-
return error::at(input::g_states, scancode, "inputs");
102+
int32_t Input::getState(OrgeScancode scancode) const {
103+
std::lock_guard lk(_mutex);
104+
105+
if (_states.contains(scancode)) {
106+
return _states.at(scancode);
108107
} else {
109108
return 0;
110109
}

src/input/input.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
#pragma once
22

3+
#include <mutex>
34
#include <orge.h>
5+
#include <unordered_map>
46

57
namespace input {
68

7-
void update();
9+
class Input {
10+
private:
11+
mutable std::mutex _mutex;
12+
std::unordered_map<OrgeScancode, int32_t> _states;
813

9-
int32_t getState(OrgeScancode scancode);
14+
public:
15+
void update();
16+
17+
int32_t getState(OrgeScancode scancode) const;
18+
};
1019

1120
} // namespace input

src/orge.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace {
4444

4545
std::optional<graphics::Graphics> g_graphics;
4646
std::optional<audio::Audio> g_audio;
47+
std::optional<input::Input> g_input;
4748

4849
void handleVkResult(const vk::Result &e) {
4950
switch (e) {
@@ -124,12 +125,14 @@ uint8_t orgeInitialize(void) {
124125
config::initialize();
125126
g_graphics.emplace();
126127
g_audio.emplace();
128+
g_input.emplace();
127129
)
128130
}
129131

130132
void orgeTerminate(void) {
131133
g_graphics.reset();
132134
g_audio.reset();
135+
g_input.reset();
133136
}
134137

135138
uint8_t orgeUpdate(void) {
@@ -149,7 +152,7 @@ uint8_t orgeUpdate(void) {
149152
g_graphics->setFullscreen(!isFullscreen);
150153
}
151154
}
152-
input::update();
155+
g_input->update();
153156
// TODO: 例外どうしよう。
154157
g_audio->update();
155158
return 1;
@@ -313,7 +316,7 @@ uint8_t orgeEndRender(void) {
313316
// ================================================================================================================== //
314317

315318
int32_t orgeGetKeyState(uint32_t scancode) {
316-
return input::getState(static_cast<OrgeScancode>(scancode));
319+
return g_input->getState(static_cast<OrgeScancode>(scancode));
317320
}
318321

319322
// ================================================================================================================== //

0 commit comments

Comments
 (0)