Implement Rumble
This commit is contained in:
@@ -23,6 +23,36 @@ extern "C"
|
||||
|
||||
#include "audio.h"
|
||||
|
||||
static SDL_Haptic *gHaptic = nullptr;
|
||||
|
||||
static void InitHaptics(SDL_GameController *controller)
|
||||
{
|
||||
if (controller == nullptr || gHaptic != nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_Joystick *joy = SDL_GameControllerGetJoystick(controller);
|
||||
|
||||
if (joy == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
gHaptic = SDL_HapticOpenFromJoystick(joy);
|
||||
|
||||
if (gHaptic == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (SDL_HapticRumbleInit(gHaptic) != 0)
|
||||
{
|
||||
SDL_HapticClose(gHaptic);
|
||||
gHaptic = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t *utils_read_file_alloc(const char *path, size_t *fileLength)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
@@ -68,6 +98,20 @@ static void DebugPrint(const char *message)
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
static void Rumble(int32_t marioId, int16_t level, int16_t time)
|
||||
{
|
||||
if (gHaptic == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
float strength = (float)level;
|
||||
|
||||
Uint32 duration = (Uint32)time * 2;
|
||||
|
||||
SDL_HapticRumblePlay(gHaptic, strength, duration);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
size_t romSize;
|
||||
@@ -87,6 +131,7 @@ int main(void)
|
||||
sm64_audio_init(rom);
|
||||
|
||||
// sm64_register_debug_print_function(DebugPrint);
|
||||
sm64_register_rumble_callback_function(Rumble);
|
||||
|
||||
sm64_static_surfaces_load(surfaces, surfaces_count);
|
||||
int32_t marioId = sm64_mario_create(0, 1000, 0);
|
||||
@@ -142,6 +187,7 @@ int main(void)
|
||||
tick += dt;
|
||||
|
||||
SDL_GameController *controller = context_get_controller();
|
||||
InitHaptics(controller);
|
||||
float x_axis, y_axis, x0_axis;
|
||||
|
||||
if (!controller) // keyboard
|
||||
@@ -243,5 +289,11 @@ int main(void)
|
||||
sm64_global_terminate();
|
||||
context_terminate();
|
||||
|
||||
if (gHaptic != nullptr)
|
||||
{
|
||||
SDL_HapticClose(gHaptic);
|
||||
gHaptic = nullptr;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user