Skip to content

Commit 7f131a7

Browse files
committed
Handle dirty keyboard and mouse states
Keeping compatibility with the old event API requires a lot of workarounds
1 parent e4bcec3 commit 7f131a7

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

src/umbra/engine.cpp

+26-20
Original file line numberDiff line numberDiff line change
@@ -593,17 +593,15 @@ int UmbraEngine::run() {
593593
}
594594

595595
SDL_AppResult UmbraEngine::onFrame() {
596-
static TCOD_key_t key{};
597-
static TCOD_mouse_t mouse{};
598596
SDL_Event event{};
599597

600598
if (paused) {
601599
if (keyboardMode >= UMBRA_KEYBOARD_SDL) {
602600
// Flush all SDL events.
603601
while (SDL_PollEvent(&event)) onEvent(event);
604602
} else {
605-
TCODSystem::checkForEvent(TCOD_EVENT_KEY_RELEASE | TCOD_EVENT_MOUSE_PRESS, &key, &mouse);
606-
keyboard(key);
603+
TCODSystem::checkForEvent(TCOD_EVENT_KEY_RELEASE | TCOD_EVENT_MOUSE_PRESS, &key_, &mouse_);
604+
keyboard(key_);
607605
}
608606
TCODConsole::root->flush();
609607
return SDL_APP_CONTINUE; // don't update or render anything anew
@@ -633,27 +631,26 @@ SDL_AppResult UmbraEngine::onFrame() {
633631
// update all active modules
634632
switch (keyboardMode) {
635633
case UMBRA_KEYBOARD_WAIT:
636-
TCODSystem::waitForEvent(TCOD_EVENT_KEY_PRESS | TCOD_EVENT_MOUSE, &key, &mouse, true);
634+
TCODSystem::waitForEvent(TCOD_EVENT_KEY_PRESS | TCOD_EVENT_MOUSE, &key_, &mouse_, true);
637635
break;
638636
case UMBRA_KEYBOARD_WAIT_NOFLUSH:
639-
TCODSystem::waitForEvent(TCOD_EVENT_KEY_PRESS | TCOD_EVENT_MOUSE, &key, &mouse, false);
637+
TCODSystem::waitForEvent(TCOD_EVENT_KEY_PRESS | TCOD_EVENT_MOUSE, &key_, &mouse_, false);
640638
break;
641639
case UMBRA_KEYBOARD_PRESSED:
642-
TCODSystem::checkForEvent(TCOD_EVENT_KEY_PRESS | TCOD_EVENT_MOUSE, &key, &mouse);
640+
TCODSystem::checkForEvent(TCOD_EVENT_KEY_PRESS | TCOD_EVENT_MOUSE, &key_, &mouse_);
643641
break;
644642
case UMBRA_KEYBOARD_PRESSED_RELEASED:
645-
TCODSystem::checkForEvent(TCOD_EVENT_KEY | TCOD_EVENT_MOUSE, &key, &mouse);
643+
TCODSystem::checkForEvent(TCOD_EVENT_KEY | TCOD_EVENT_MOUSE, &key_, &mouse_);
646644
break;
647645
case UMBRA_KEYBOARD_RELEASED:
648646
default:
649-
TCODSystem::checkForEvent(TCOD_EVENT_KEY_RELEASE | TCOD_EVENT_MOUSE, &key, &mouse);
647+
TCODSystem::checkForEvent(TCOD_EVENT_KEY_RELEASE | TCOD_EVENT_MOUSE, &key_, &mouse_);
650648
break;
651649
case UMBRA_KEYBOARD_SDL:
652-
key = {};
653650
while (SDL_PollEvent(&event)) onEvent(event);
654651
break;
655652
}
656-
keyboard(key);
653+
keyboard(key_);
657654
uint64_t startTime = SDL_GetTicks();
658655
// update all active modules by priority order
659656
activeModules.erase(
@@ -665,8 +662,8 @@ SDL_AppResult UmbraEngine::onFrame() {
665662
if (!tmpMod->getPause()) {
666663
// handle input
667664
if (keyboardMode < UMBRA_KEYBOARD_SDL) { // Old-style handling.
668-
tmpMod->keyboard(key);
669-
tmpMod->mouse(mouse);
665+
tmpMod->keyboard(key_);
666+
tmpMod->mouse(mouse_);
670667
}
671668
if (tmpMod->isTimedOut(startTime) || !tmpMod->update() || !tmpMod->getActive()) {
672669
UmbraModule* module = tmpMod;
@@ -684,6 +681,17 @@ SDL_AppResult UmbraEngine::onFrame() {
684681
return remove_this;
685682
}),
686683
activeModules.end());
684+
685+
// Clear dirty key/mouse states after module update calls
686+
key_ = {};
687+
mouse_.lbutton_pressed = false;
688+
mouse_.mbutton_pressed = false;
689+
mouse_.rbutton_pressed = false;
690+
for (auto& module : activeModules) {
691+
module->keyboard(key_);
692+
module->mouse(mouse_);
693+
}
694+
687695
uint64_t updateTime = SDL_GetTicks() - startTime;
688696
TCODConsole::root->setDefaultBackground(TCODColor::black);
689697
TCODConsole::root->clear();
@@ -701,21 +709,19 @@ SDL_AppResult UmbraEngine::onFrame() {
701709
}
702710

703711
SDL_AppResult UmbraEngine::onEvent(SDL_Event& event) {
704-
TCOD_key_t key{};
705-
TCOD_mouse_t mouse{};
706-
int is_key_event = tcod::sdl2::process_event(event, key);
707-
int is_mouse_event = tcod::sdl2::process_event(event, key);
712+
tcod::sdl2::process_event(event, key_);
713+
tcod::sdl2::process_event(event, mouse_);
708714

709715
if (!paused) {
710716
for (auto& module : activeModules) {
711717
if (module->getPause()) continue;
712718
module->onEvent(event);
713719

714-
if (is_key_event) module->keyboard(key);
715-
if (is_mouse_event) module->mouse(mouse);
720+
module->keyboard(key_);
721+
module->mouse(mouse_);
716722
}
717723
}
718-
keyboard(key);
724+
keyboard(key_);
719725
switch (event.type) {
720726
case SDL_EVENT_QUIT:
721727
paused = false;

src/umbra/engine.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ class UmbraEngine {
394394
UmbraModule* internalModules[UMBRA_INTERNAL_MAX]{};
395395
UmbraKeyboardMode keyboardMode{UMBRA_KEYBOARD_RELEASED};
396396
std::vector<UmbraCallback*> callbacks{}; // the keybinding callbacks
397+
TCOD_key_t key_{}; // TCOD key dirty state
398+
TCOD_mouse_t mouse_{}; // TCOD mouse dirty state
397399
/**
398400
* Parses the keyboard input and passes it to the registered callbacks.
399401
* @param key a reference to the keyboard event object

0 commit comments

Comments
 (0)