AGS: Implementing AGSController plugin

This commit is contained in:
Paul Gilbert 2021-04-17 21:24:51 -07:00
parent 9818b47554
commit 9dd5f7fea8
6 changed files with 278 additions and 5 deletions

View File

@ -32,6 +32,8 @@ EventsManager *g_events;
EventsManager::EventsManager() {
g_events = this;
_keys.resize(AGS3::__allegro_KEY_MAX);
Common::fill(&_joystickAxis[0], &_joystickAxis[32], 0);
Common::fill(&_joystickButton[0], &_joystickButton[32], 0);
}
EventsManager::~EventsManager() {
@ -42,22 +44,43 @@ void EventsManager::pollEvents() {
Common::Event e;
while (g_system->getEventManager()->pollEvent(e)) {
if (e.type == Common::EVENT_QUIT || e.type == Common::EVENT_RETURN_TO_LAUNCHER) {
switch (e.type) {
case Common::EVENT_QUIT:
case Common::EVENT_RETURN_TO_LAUNCHER:
_G(want_exit) = true;
_G(abort_engine) = true;
_G(check_dynamic_sprites_at_exit) = false;
break;
} else if (e.type == Common::EVENT_KEYDOWN) {
case Common::EVENT_JOYAXIS_MOTION:
assert(e.joystick.axis < 32);
_joystickAxis[e.joystick.axis] = e.joystick.position;
break;
case Common::EVENT_JOYBUTTON_DOWN:
assert(e.joystick.button < 32);
_joystickButton[e.joystick.button] = true;
break;
case Common::EVENT_JOYBUTTON_UP:
assert(e.joystick.button < 32);
_joystickButton[e.joystick.button] = false;
break;
case Common::EVENT_KEYDOWN:
updateKeys(e.kbd, true);
if (!isModifierKey(e.kbd.keycode)) {
// Add keypresses to the pending key list
_pendingKeys.push(e.kbd);
}
} else if (e.type == Common::EVENT_KEYUP) {
updateKeys(e.kbd, false);
break;
} else {
case Common::EVENT_KEYUP:
updateKeys(e.kbd, false);
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
@ -66,6 +89,7 @@ void EventsManager::pollEvents() {
_pendingEvents.back() = e;
else
_pendingEvents.push(e);
break;
}
}
}

View File

@ -35,6 +35,8 @@ private:
Common::Queue<Common::Event> _pendingEvents;
Common::Queue<Common::KeyState> _pendingKeys;
Common::Array<bool> _keys;
int16 _joystickAxis[32];
bool _joystickButton[32];
bool isModifierKey(const Common::KeyCode &keycode) const;
bool isExtendedKey(const Common::KeyCode &keycode) const;
@ -79,6 +81,32 @@ public:
* Returns the bitset of currently pressed modifier keys
*/
uint getModifierFlags() const;
/**
* Gets a joystick axis position
*/
int16 getJoystickAxis(byte axis) const {
assert(axis < 32);
return _joystickAxis[axis];
}
/**
* Gets whether a given joystick button is down
*/
bool getJoystickButton(byte button) const {
assert(button < 32);
return _joystickButton[button];
}
/**
* Gets whether a given joystick button is down once
*/
bool getJoystickButtonOnce(byte button) {
assert(button < 32);
bool result = _joystickButton[button];
_joystickButton[button] = false;
return result;
}
};
extern EventsManager *g_events;

View File

@ -287,6 +287,7 @@ MODULE_OBJS = \
plugins/agsplugin.o \
plugins/plugin_base.o \
plugins/ags_blend/ags_blend.o \
plugins/ags_controller/ags_controller.o \
plugins/ags_creditz/ags_creditz.o \
plugins/ags_creditz/ags_creditz1.o \
plugins/ags_creditz/ags_creditz2.o \

View File

@ -0,0 +1,150 @@
/* 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 "ags/plugins/ags_controller/ags_controller.h"
#include "ags/events.h"
#include "common/config-manager.h"
#include "common/system.h"
namespace AGS3 {
namespace Plugins {
namespace AGSController {
IAGSEngine *AGSController::_engine;
AGSController::AGSController() : PluginBase() {
_engine = nullptr;
DLL_METHOD(AGS_GetPluginName);
DLL_METHOD(AGS_EngineStartup);
DLL_METHOD(AGS_EngineShutdown);
DLL_METHOD(AGS_EngineOnEvent);
}
const char *AGSController::AGS_GetPluginName() {
return "AGSController";
}
void AGSController::AGS_EngineStartup(IAGSEngine *engine) {
_engine = engine;
SCRIPT_METHOD_EXT(ControllerCount, ControllerCount);
SCRIPT_METHOD_EXT(Controller::Open, Controller_Open);
SCRIPT_METHOD_EXT(Controller::Close, Controller_Close);
SCRIPT_METHOD_EXT(Controller::Plugged, Controller_Plugged);
SCRIPT_METHOD_EXT(Controller::GetAxis, Controller_GetAxis);
SCRIPT_METHOD_EXT(Controller::GetPOV, Controller_GetPOV);
SCRIPT_METHOD_EXT(Controller::IsButtonDown, Controller_IsButtonDown);
SCRIPT_METHOD_EXT(Controller::GetName^0, Controller_GetName);
SCRIPT_METHOD_EXT(Controller::Rumble, Controller_Rumble);
SCRIPT_METHOD_EXT(Controller::IsButtonDownOnce, Controller_IsButtonDownOnce);
SCRIPT_METHOD_EXT(Controller::PressAnyKey, Controller_PressAnyKey);
SCRIPT_METHOD_EXT(Controller::BatteryStatus, Controller_BatteryStatus);
SCRIPT_METHOD_EXT(ClickMouse, ClickMouse);
_engine->RequestEventHook(AGSE_PREGUIDRAW);
}
void AGSController::AGS_EngineShutdown() {
}
int64 AGSController::AGS_EngineOnEvent(int event, NumberPtr data) {
if (event == AGSE_PREGUIDRAW) {
Controller_Update();
}
return 0;
}
void AGSController::Controller_Update() {
::AGS::g_events->pollEvents();
}
void AGSController::ControllerCount(ScriptMethodParams &params) {
int joystickNum = ConfMan.getInt("joystick_num");
params._result = (joystickNum == -1) ? 0 : 1;
}
void AGSController::Controller_Open(ScriptMethodParams &params) {
// No implemented needed
}
void AGSController::Controller_Close(ScriptMethodParams &params) {
// No implemented needed
}
void AGSController::Controller_Plugged(ScriptMethodParams &params) {
int joystickNum = ConfMan.getInt("joystick_num");
params._result = joystickNum != -1;
}
void AGSController::Controller_GetAxis(ScriptMethodParams &params) {
PARAMS1(int, axis);
params._result = ::AGS::g_events->getJoystickAxis(axis);
}
void AGSController::Controller_GetPOV(ScriptMethodParams &params) {
// Not supported
params._result = 0;
}
void AGSController::Controller_IsButtonDown(ScriptMethodParams &params) {
PARAMS1(int, button);
params._result = ::AGS::g_events->getJoystickButton(button);
}
void AGSController::Controller_GetName(ScriptMethodParams &params) {
int joystickNum = ConfMan.getInt("joystick_num");
params._result = (joystickNum != -1) ? "Joystick" :"";
}
void AGSController::Controller_Rumble(ScriptMethodParams &params) {
// Not supported
}
void AGSController::Controller_IsButtonDownOnce(ScriptMethodParams &params) {
PARAMS1(int, button);
params._result = ::AGS::g_events->getJoystickButtonOnce(button);
}
void AGSController::Controller_PressAnyKey(ScriptMethodParams &params) {
params._result = -1;
for (int index = 0; index < 32; ++index) {
if (::AGS::g_events->getJoystickButton(index)) {
params._result = index;
break;
}
}
}
void AGSController::Controller_BatteryStatus(ScriptMethodParams &params) {
// Not supported, so return -1 for "UNKNOWN"
params._result = -1;
}
void AGSController::ClickMouse(ScriptMethodParams &params) {
error("TODO: ClickMouse - find out what params it gets");
}
} // namespace AGSController
} // namespace Plugins
} // namespace AGS3

View File

@ -0,0 +1,66 @@
/* 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 AGS_PLUGINS_AGSCONTROLLER_AGSCONTROLLER_H
#define AGS_PLUGINS_AGSCONTROLLER_AGSCONTROLLER_H
#include "ags/plugins/plugin_base.h"
namespace AGS3 {
namespace Plugins {
namespace AGSController {
class AGSController : public PluginBase {
private:
static IAGSEngine *_engine;
private:
static const char *AGS_GetPluginName();
static void AGS_EngineStartup(IAGSEngine *engine);
static void AGS_EngineShutdown();
static int64 AGS_EngineOnEvent(int event, NumberPtr data);
private:
static void Controller_Update();
static void ControllerCount(ScriptMethodParams &params);
static void Controller_Open(ScriptMethodParams &params);
static void Controller_Plugged(ScriptMethodParams &params);
static void Controller_GetAxis(ScriptMethodParams &params);
static void Controller_GetPOV(ScriptMethodParams &params);
static void Controller_IsButtonDown(ScriptMethodParams &params);
static void Controller_Close(ScriptMethodParams &params);
static void Controller_GetName(ScriptMethodParams &params);
static void Controller_Rumble(ScriptMethodParams &params);
static void Controller_IsButtonDownOnce(ScriptMethodParams &params);
static void Controller_PressAnyKey(ScriptMethodParams &params);
static void Controller_BatteryStatus(ScriptMethodParams &params);
static void ClickMouse(ScriptMethodParams &params);
public:
AGSController();
};
} // namespace AGSController
} // namespace Plugins
} // namespace AGS3
#endif

View File

@ -23,6 +23,7 @@
#include "ags/lib/allegro.h"
#include "ags/plugins/plugin_base.h"
#include "ags/plugins/ags_blend/ags_blend.h"
#include "ags/plugins/ags_controller/ags_controller.h"
#include "ags/plugins/ags_creditz/ags_creditz1.h"
#include "ags/plugins/ags_creditz/ags_creditz2.h"
#include "ags/plugins/ags_flashlight/ags_flashlight.h"
@ -63,6 +64,9 @@ void *pluginOpen(const char *filename) {
if (fname.equalsIgnoreCase("AGSBlend"))
return new AGSBlend::AGSBlend();
if (fname.equalsIgnoreCase("AGSController"))
return new AGSController::AGSController();
if (fname.equalsIgnoreCase("agsCreditz"))
return new AGSCreditz::AGSCreditz1();