From fba13ed0f706a6fa36ca8b0bd9727d64ef52d1ee Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Sat, 18 Sep 2021 21:24:36 -0700 Subject: [PATCH] CHEWY: Added events manager class --- engines/chewy/chewy.cpp | 10 ++--- engines/chewy/chewy.h | 2 + engines/chewy/events.cpp | 74 +++++++++++++++++++++++++++++++++++++ engines/chewy/events.h | 80 ++++++++++++++++++++++++++++++++++++++++ engines/chewy/module.mk | 1 + 5 files changed, 161 insertions(+), 6 deletions(-) create mode 100644 engines/chewy/events.cpp create mode 100644 engines/chewy/events.h diff --git a/engines/chewy/chewy.cpp b/engines/chewy/chewy.cpp index 02a60da42dd..87a931d4cdc 100644 --- a/engines/chewy/chewy.cpp +++ b/engines/chewy/chewy.cpp @@ -24,6 +24,7 @@ #include "common/system.h" #include "engines/util.h" #include "chewy/chewy.h" +#include "chewy/events.h" #include "chewy/main.h" #include "chewy/resource.h" #include "chewy/sound.h" @@ -36,6 +37,7 @@ ChewyEngine::ChewyEngine(OSystem *syst, const ChewyGameDescription *gameDesc) : Engine(syst), _gameDescription(gameDesc), _rnd("chewy"), + _events(nullptr), _screen(nullptr), _sound(nullptr) { @@ -52,12 +54,14 @@ ChewyEngine::ChewyEngine(OSystem *syst, const ChewyGameDescription *gameDesc) } ChewyEngine::~ChewyEngine() { + delete _events; delete _screen; delete _sound; g_engine = nullptr; } void ChewyEngine::initialize() { + _events = new EventsManager(); _screen = new Graphics::Screen(); _sound = new Sound(_mixer); } @@ -71,12 +75,6 @@ Common::Error ChewyEngine::run() { game_main(); - // Run a dummy loop - while (!shouldQuit()) { - g_system->updateScreen(); - g_system->delayMillis(10); - } - return Common::kNoError; } diff --git a/engines/chewy/chewy.h b/engines/chewy/chewy.h index 8c029b4cd5f..501d61708eb 100644 --- a/engines/chewy/chewy.h +++ b/engines/chewy/chewy.h @@ -38,6 +38,7 @@ namespace Chewy { #define SCREEN_HEIGHT 200 struct ChewyGameDescription; +class EventsManager; class Sound; class ChewyEngine : public Engine { @@ -50,6 +51,7 @@ protected: void shutdown(); public: + EventsManager *_events; Sound *_sound; Graphics::Screen *_screen; public: diff --git a/engines/chewy/events.cpp b/engines/chewy/events.cpp new file mode 100644 index 00000000000..b52fe18ea99 --- /dev/null +++ b/engines/chewy/events.cpp @@ -0,0 +1,74 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "common/system.h" +#include "chewy/events.h" + +namespace Chewy { + +EventsManager *g_events; + +EventsManager::EventsManager() { + g_events = this; +} + +EventsManager::~EventsManager() { + g_events = nullptr; +} + +void EventsManager::pollEvents() { + Common::Event e; + + while (g_system->getEventManager()->pollEvent(e)) { + switch (e.type) { + case Common::EVENT_QUIT: + case Common::EVENT_RETURN_TO_LAUNCHER: + return; + + case Common::EVENT_KEYDOWN: + _keyEvents.push(e); + break; + + default: + // Add other event types to the pending events queue. If the event is a + // mouse move and the prior one was also, then discard the prior one. + // This'll help prevent too many mouse move events accumulating + if (e.type == Common::EVENT_MOUSEMOVE && !_pendingEvents.empty() && + _pendingEvents.back().type == Common::EVENT_MOUSEMOVE) + _pendingEvents.back() = e; + else + _pendingEvents.push(e); + break; + } + } +} + +Common::Event EventsManager::readEvent() { + pollEvents(); + return _pendingEvents.empty() ? Common::Event() : _pendingEvents.pop(); +} + +void EventsManager::warpMouse(const Common::Point &newPos) { + g_system->warpMouse(newPos.x, newPos.y); +} + +} // namespace Chewy diff --git a/engines/chewy/events.h b/engines/chewy/events.h new file mode 100644 index 00000000000..e514ae6df54 --- /dev/null +++ b/engines/chewy/events.h @@ -0,0 +1,80 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef CHEWY_EVENTS_H +#define CHEWY_EVENTS_H + +#include "common/array.h" +#include "common/queue.h" +#include "common/events.h" + +namespace Chewy { + +class EventsManager { +private: + Common::Queue _pendingEvents; + Common::Queue _keyEvents; + +public: + EventsManager(); + ~EventsManager(); + + /** + * Poll any pending events + */ + void pollEvents(); + + /** + * Returns true if any unprocessed keyboard events are pending + */ + bool keyEventPending() const { + return !_keyEvents.empty(); + } + + /** + * Returns the next pending unprocessed keyboard event + */ + Common::Event getPendingKeyEvent() { + return _keyEvents.pop(); + } + + /** + * Returns the next event, if any + */ + Common::Event readEvent(); + + /** + * Sets the mouse position + */ + void warpMouse(const Common::Point &newPos); + + void clearEvents() { + _pendingEvents.clear(); + _keyEvents.clear(); + } +}; + +extern EventsManager *g_events; + +} // namespace Chewy + +#endif diff --git a/engines/chewy/module.mk b/engines/chewy/module.mk index bf5e156c9e5..65fc60313ff 100644 --- a/engines/chewy/module.mk +++ b/engines/chewy/module.mk @@ -15,6 +15,7 @@ MODULE_OBJS = \ episode2.o \ episode3.o \ episode4.o \ + events.o \ fehler.o \ file.o \ flic.o \