From aaaea336977c95604d6b1d7afa51d2fade634e8c Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Mon, 20 Mar 2023 14:44:37 +0000 Subject: [PATCH] Added haptics. --- .../Ship-Accelerating-Frame-2.png | Bin .../Ship-Accelerating.png | Bin .../Ship-Anticlockwise.png | Bin .../Ship-Clockwise.png | Bin .../Ship-Idle.png | Bin Spacewar-Client.c | 14 ++++---- Spacewar.c | 30 +++++++++++------- 7 files changed, 26 insertions(+), 18 deletions(-) rename {Experiment-16-Images => Images}/Ship-Accelerating-Frame-2.png (100%) rename {Experiment-16-Images => Images}/Ship-Accelerating.png (100%) rename {Experiment-16-Images => Images}/Ship-Anticlockwise.png (100%) rename {Experiment-16-Images => Images}/Ship-Clockwise.png (100%) rename {Experiment-16-Images => Images}/Ship-Idle.png (100%) diff --git a/Experiment-16-Images/Ship-Accelerating-Frame-2.png b/Images/Ship-Accelerating-Frame-2.png similarity index 100% rename from Experiment-16-Images/Ship-Accelerating-Frame-2.png rename to Images/Ship-Accelerating-Frame-2.png diff --git a/Experiment-16-Images/Ship-Accelerating.png b/Images/Ship-Accelerating.png similarity index 100% rename from Experiment-16-Images/Ship-Accelerating.png rename to Images/Ship-Accelerating.png diff --git a/Experiment-16-Images/Ship-Anticlockwise.png b/Images/Ship-Anticlockwise.png similarity index 100% rename from Experiment-16-Images/Ship-Anticlockwise.png rename to Images/Ship-Anticlockwise.png diff --git a/Experiment-16-Images/Ship-Clockwise.png b/Images/Ship-Clockwise.png similarity index 100% rename from Experiment-16-Images/Ship-Clockwise.png rename to Images/Ship-Clockwise.png diff --git a/Experiment-16-Images/Ship-Idle.png b/Images/Ship-Idle.png similarity index 100% rename from Experiment-16-Images/Ship-Idle.png rename to Images/Ship-Idle.png diff --git a/Spacewar-Client.c b/Spacewar-Client.c index 3e78f97..03836eb 100644 --- a/Spacewar-Client.c +++ b/Spacewar-Client.c @@ -1,4 +1,4 @@ -// SDL Experiment 16, Barra Ó Catháin. +// SDL Experiment 17, Barra Ó Catháin. // =================================== #include #include @@ -138,11 +138,11 @@ int main(int argc, char ** argv) SDL_Texture * idleTexture, * acceleratingTexture, * clockwiseTexture, * anticlockwiseTexture, * currentTexture, * acceleratingTexture2; - idleTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Idle.png"); - clockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Clockwise.png"); - acceleratingTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Accelerating.png"); - anticlockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Anticlockwise.png"); - acceleratingTexture2 = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Accelerating-Frame-2.png"); + idleTexture = IMG_LoadTexture(renderer, "./Images/Ship-Idle.png"); + clockwiseTexture = IMG_LoadTexture(renderer, "./Images/Ship-Clockwise.png"); + acceleratingTexture = IMG_LoadTexture(renderer, "./Images/Ship-Accelerating.png"); + anticlockwiseTexture = IMG_LoadTexture(renderer, "./Images/Ship-Anticlockwise.png"); + acceleratingTexture2 = IMG_LoadTexture(renderer, "./Images/Ship-Accelerating-Frame-2.png"); // Enable resizing the window: SDL_SetWindowResizable(window, SDL_TRUE); @@ -224,5 +224,5 @@ int main(int argc, char ** argv) } // ======================================================================================================== // Local Variables: -// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-16-Client.c -lSDL2_image -lm -o 'Spacewar Client!'" +// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-17-Client.c -lSDL2_image -lm -o 'Spacewar Client!'" // End: diff --git a/Spacewar.c b/Spacewar.c index 911ecd4..af97df0 100644 --- a/Spacewar.c +++ b/Spacewar.c @@ -1,4 +1,4 @@ -// SDL Experiment 16, Barra Ó Catháin. +// SDL Experiment 17, Barra Ó Catháin. // =================================== #include #include @@ -212,7 +212,8 @@ int main(int argc, char ** argv) } // Check for joysticks: - SDL_Joystick* controller = NULL; + SDL_Joystick * controller = NULL; + SDL_Haptic * haptic = NULL; if (SDL_NumJoysticks() < 1 ) { printf( "Warning: No joysticks connected!\n" ); @@ -225,12 +226,15 @@ int main(int argc, char ** argv) { printf( "Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError() ); } + haptic = SDL_HapticOpenFromJoystick(controller); + SDL_HapticRumbleInit(haptic); } // Initialize image loading: IMG_Init(IMG_INIT_PNG); SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"); - + SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); + // Create an SDL window and rendering context in that window: SDL_Window * window = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 700, 700, 0); SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags); @@ -240,11 +244,11 @@ int main(int argc, char ** argv) SDL_Texture * idleTexture, * acceleratingTexture, * clockwiseTexture, * anticlockwiseTexture, * currentTexture, * acceleratingTexture2; - idleTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Idle.png"); - clockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Clockwise.png"); - acceleratingTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Accelerating.png"); - anticlockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Anticlockwise.png"); - acceleratingTexture2 = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Accelerating-Frame-2.png"); + idleTexture = IMG_LoadTexture(renderer, "./Images/Ship-Idle.png"); + clockwiseTexture = IMG_LoadTexture(renderer, "./Images/Ship-Clockwise.png"); + acceleratingTexture = IMG_LoadTexture(renderer, "./Images/Ship-Accelerating.png"); + anticlockwiseTexture = IMG_LoadTexture(renderer, "./Images/Ship-Anticlockwise.png"); + acceleratingTexture2 = IMG_LoadTexture(renderer, "./Images/Ship-Accelerating-Frame-2.png"); // Enable resizing the window: SDL_SetWindowResizable(window, SDL_TRUE); @@ -273,7 +277,7 @@ int main(int argc, char ** argv) else { rotatingClockwise = false; - } + } // Check for movement on the right trigger: if (SDL_JoystickGetAxis(controller, 5) > 2500) @@ -436,8 +440,12 @@ int main(int argc, char ** argv) addXYVectorDeltaScaled(&shipA.velocity, &shipA.gravity, deltaTime); addXYVectorDeltaScaled(&shipB.velocity, &shipB.gravity, deltaTime); - if(accelerating) + if (accelerating) { + if (controller != NULL) + { + SDL_HapticRumblePlay(haptic, (float)SDL_JoystickGetAxis(controller, 5) / 32768, 20); + } xyVector temporary = engineVector; multiplyXYVector(&engineVector, SDL_JoystickGetAxis(controller, 5) / 30000); addXYVectorDeltaScaled(&shipA.velocity, &engineVector, deltaTime); @@ -508,5 +516,5 @@ int main(int argc, char ** argv) } // ======================================================================================================== // Local Variables: -// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-16.c -lSDL2_image -lm -o 'Spacewar!'" +// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-17.c -lSDL2_image -lm -o 'Spacewar!'" // End: