Revert "Remove haptics code"

Some part of this completely breaks the frame limiter.
I do not know which part
This commit is contained in:
Struma 2020-12-25 19:41:16 -05:00 committed by Roza
parent c099d8efc1
commit 0179e6a75b
7 changed files with 97 additions and 14 deletions

View file

@ -217,6 +217,21 @@ RB_METHOD(inputJoystickInfo) {
#undef POWERCASE
#undef M_SYMBOL
RB_METHOD(inputRumble) {
RB_UNUSED_PARAM;
VALUE duration, strength, attack, fade;
rb_scan_args(argc, argv, "13", &duration, &strength, &attack, &fade);
int dur = NUM2INT(duration);
int str = (NIL_P(strength)) ? 1 : NUM2INT(strength);
int att = (NIL_P(attack)) ? 0 : NUM2INT(attack);
int fad = (NIL_P(fade)) ? 0 : NUM2INT(fade);
shState->input().rumble(dur, str, att, fad);
return Qnil;
}
RB_METHOD(inputGetMode) {
RB_UNUSED_PARAM;
@ -317,6 +332,7 @@ void inputBindingInit() {
_rb_define_module_function(module, "mouse_y", inputMouseY);
_rb_define_module_function(module, "joystick", inputJoystickInfo);
_rb_define_module_function(module, "rumble", inputRumble);
_rb_define_module_function(module, "text_input", inputGetMode);
_rb_define_module_function(module, "text_input=", inputSetMode);

View file

@ -154,17 +154,7 @@ try { exp } catch (...) {}
SET_OPT(syncToRefreshrate, boolean);
SET_OPT(solidFonts, boolean);
SET_OPT(subImageFix, boolean);
#if defined(__MACOSX__) && defined(__aarch64__)
// OpenGL on Apple Silicon Macs is implemented on top of Metal,
// and can crash unexpectedly with some of MKXP's GL code
// Should hurry up and find something open-source
// for GLES->Vulkan->Metal or GLES->Metal
enableBlitting = false;
#else
SET_OPT(enableBlitting, boolean);
#endif
SET_OPT(maxTextureSize, integer);
SET_STRINGOPT(gameFolder, gameFolder);
SET_OPT(anyAltToggleFS, boolean);

View file

@ -91,6 +91,8 @@ enum
REQUEST_TEXTMODE,
REQUEST_SETTINGS,
REQUEST_RUMBLE,
UPDATE_FPS,
UPDATE_SCREEN_RECT,
@ -112,9 +114,11 @@ bool EventThread::allocUserEvents()
EventThread::EventThread()
: js(0),
hapt(0),
fullscreen(false),
showCursor(true),
joystickConnected(false)
joystickConnected(false),
hapticEffectId(0)
{}
void EventThread::process(RGSSThreadData &rtData)
@ -168,7 +172,9 @@ void EventThread::process(RGSSThreadData &rtData)
SDL_DisplayMode dm = {0};
SDL_GetWindowSize(win, &winW, &winH);
SDL_Haptic *hap;
memset(&hapticEffect, 0, sizeof(SDL_HapticEffect));
textInputBuffer.clear();
SettingsMenu *sMenu = 0;
@ -381,12 +387,25 @@ void EventThread::process(RGSSThreadData &rtData)
js = SDL_JoystickOpen(0);
joystickConnected = true;
hap = SDL_HapticOpenFromJoystick(js);
Debug() << (hap ? "true" : "false");
if (hap && (SDL_HapticQuery(hap) & SDL_HAPTIC_SINE))
{
hapt = hap;
Debug() << "Haptic device initialized";
}
else
{
Debug() << "No haptic support found";
}
break;
case SDL_JOYDEVICEREMOVED :
resetInputStates();
joystickConnected = false;
hapt = 0;
break;
case SDL_MOUSEBUTTONDOWN :
@ -481,6 +500,25 @@ void EventThread::process(RGSSThreadData &rtData)
}
sMenu->raise();
break;
case REQUEST_RUMBLE :
if (!hapt) break;
if (!hapticEffect.type)
{
hapticEffect.type = SDL_HAPTIC_SINE;
hapticEffect.constant.attack_level = 0;
hapticEffect.constant.fade_level = 0;
hapticEffectId = SDL_HapticNewEffect(hapt, &hapticEffect);
}
if (hapticEffect.constant.length == 0 && SDL_HapticGetEffectStatus(hapt, hapticEffectId) > 0)
{
SDL_HapticStopEffect(hapt, hapticEffectId);
break;
}
SDL_HapticUpdateEffect(hapt, hapticEffectId, &hapticEffect);
SDL_HapticRunEffect(hapt, hapticEffectId, 1);
break;
case UPDATE_FPS :
@ -694,6 +732,19 @@ void EventThread::requestSettingsMenu()
SDL_PushEvent(&event);
}
void EventThread::requestRumble(int duration, short strength, int attack, int fade)
{
SDL_Event event;
event.type = usrIdStart + REQUEST_RUMBLE;
hapticEffect.constant.length = duration;
hapticEffect.constant.level = strength;
hapticEffect.constant.attack_length = attack;
hapticEffect.constant.fade_length = fade;
SDL_PushEvent(&event);
}
void EventThread::showMessageBox(const char *body, int flags)
{
msgBoxDone.clear();
@ -730,6 +781,11 @@ SDL_Joystick *EventThread::joystick() const
return (joystickConnected) ? js : 0;
}
SDL_Haptic *EventThread::haptic() const
{
return hapt;
}
void EventThread::notifyFrame()
{
if (!fps.sendUpdates)

View file

@ -29,6 +29,7 @@
#include <SDL_scancode.h>
#include <SDL_joystick.h>
#include <SDL_haptic.h>
#include <SDL_mouse.h>
#include <SDL_mutex.h>
@ -96,6 +97,8 @@ public:
void requestTextInputMode(bool mode);
void requestSettingsMenu();
void requestRumble(int duration, short strength, int attack, int fade);
void requestTerminate();
@ -104,6 +107,7 @@ public:
bool getJoystickConnected() const;
SDL_Joystick *joystick() const;
SDL_Haptic *haptic() const;
void showMessageBox(const char *body, int flags = 0);
@ -126,6 +130,9 @@ private:
bool showCursor;
SDL_Joystick *js;
SDL_Haptic *hapt;
SDL_HapticEffect hapticEffect;
int hapticEffectId;
AtomicFlag msgBoxDone;

View file

@ -1198,6 +1198,18 @@ int Input::getJoystickPowerLevel()
SDL_JOYSTICK_POWER_UNKNOWN;
}
void Input::rumble(int duration, int strength, int attack, int fade)
{
duration = clamp(duration, 0, 10000);
strength = clamp(strength, 1, 100);
attack = clamp(attack, 0, 10000);
fade = clamp(fade, 0, 10000);
shState->eThread().requestRumble(duration,
(short)((double)strength / 100 * 32767),
attack,
fade);
}
bool Input::getTextInputMode()
{
return (SDL_IsTextInputActive() == SDL_TRUE);

View file

@ -71,7 +71,9 @@ public:
bool getJoystickConnected();
const char *getJoystickName();
int getJoystickPowerLevel();
void rumble(int duration, int strength, int attack, int fade);
bool getTextInputMode();
void setTextInputMode(bool mode);
const char *getText();

View file

@ -171,7 +171,7 @@ int main(int argc, char *argv[]) {
#endif
/* initialize SDL first */
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) {
showInitError(std::string("Error initializing SDL: ") + SDL_GetError());
return 0;
}