Skip to content

Commit

Permalink
Fix bug in which Enigma would not grab mouse after losing window focu…
Browse files Browse the repository at this point in the history
…s. This fixes GitHub issue #56.
  • Loading branch information
alochmann committed Aug 10, 2022
1 parent bc12542 commit 30425eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void game::StartGame() {
lev::Index *ind = lev::Index::getCurrentIndex();

video_engine->HideMouse();
ScopedInputGrab grab(enigma::Nograb ? SDL_FALSE : SDL_TRUE);
ScopedInputGrab grab(not enigma::Nograb);

// Uint32 start_tick_time = SDL_GetTicks();

Expand Down
12 changes: 10 additions & 2 deletions src/video.cc
Original file line number Diff line number Diff line change
Expand Up @@ -521,14 +521,17 @@ class VideoEngineImpl : public VideoEngine {
std::string window_caption;
VideoTileset *video_tileset;
VideoTilesetId video_tileset_id;

bool nominal_inputgrab_status;
};

VideoEngineImpl::VideoEngineImpl() :
screen(NULL),
window(NULL),
renderer(NULL),
video_tileset(nullptr),
video_tileset_id(VTS_NONE)
video_tileset_id(VTS_NONE),
nominal_inputgrab_status(false)
{}

VideoEngineImpl::~VideoEngineImpl() {
Expand Down Expand Up @@ -1013,9 +1016,14 @@ bool VideoEngineImpl::SetInputGrab(bool enabled) {
// uses only one window, we use both SDL_Set... commands in parallel,
// but only SDL_GetWindowGrab to retrieve the current state. When
// Enigma starts using several windows, this needs to be adapted.
bool old_state = GetInputGrab();
bool old_state = nominal_inputgrab_status;
SDL_SetWindowGrab(window, enabled ? SDL_TRUE : SDL_FALSE);
SDL_SetRelativeMouseMode(enabled ? SDL_TRUE : SDL_FALSE);
nominal_inputgrab_status = enabled;
// We are not returning the actual state (this would be GetInputGrab()),
// but what we expected should have been set: A loss of window focus
// would change GetInputGrab to false instead of true, but we might
// want to reset to the state before losing window focus.
return old_state;
}

Expand Down

0 comments on commit 30425eb

Please sign in to comment.