Added haptics.

This commit is contained in:
Barra Ó Catháin 2023-03-20 14:44:37 +00:00
parent f3096e1a54
commit aaaea33697
7 changed files with 26 additions and 18 deletions

View File

Before

Width:  |  Height:  |  Size: 6.5 KiB

After

Width:  |  Height:  |  Size: 6.5 KiB

View File

Before

Width:  |  Height:  |  Size: 1023 B

After

Width:  |  Height:  |  Size: 1023 B

View File

Before

Width:  |  Height:  |  Size: 992 B

After

Width:  |  Height:  |  Size: 992 B

View File

Before

Width:  |  Height:  |  Size: 1004 B

After

Width:  |  Height:  |  Size: 1004 B

View File

Before

Width:  |  Height:  |  Size: 981 B

After

Width:  |  Height:  |  Size: 981 B

View File

@ -1,4 +1,4 @@
// SDL Experiment 16, Barra Ó Catháin. // SDL Experiment 17, Barra Ó Catháin.
// =================================== // ===================================
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
@ -138,11 +138,11 @@ int main(int argc, char ** argv)
SDL_Texture * idleTexture, * acceleratingTexture, * clockwiseTexture, * anticlockwiseTexture, * currentTexture, SDL_Texture * idleTexture, * acceleratingTexture, * clockwiseTexture, * anticlockwiseTexture, * currentTexture,
* acceleratingTexture2; * acceleratingTexture2;
idleTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Idle.png"); idleTexture = IMG_LoadTexture(renderer, "./Images/Ship-Idle.png");
clockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Clockwise.png"); clockwiseTexture = IMG_LoadTexture(renderer, "./Images/Ship-Clockwise.png");
acceleratingTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Accelerating.png"); acceleratingTexture = IMG_LoadTexture(renderer, "./Images/Ship-Accelerating.png");
anticlockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Anticlockwise.png"); anticlockwiseTexture = IMG_LoadTexture(renderer, "./Images/Ship-Anticlockwise.png");
acceleratingTexture2 = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Accelerating-Frame-2.png"); acceleratingTexture2 = IMG_LoadTexture(renderer, "./Images/Ship-Accelerating-Frame-2.png");
// Enable resizing the window: // Enable resizing the window:
SDL_SetWindowResizable(window, SDL_TRUE); SDL_SetWindowResizable(window, SDL_TRUE);
@ -224,5 +224,5 @@ int main(int argc, char ** argv)
} }
// ======================================================================================================== // ========================================================================================================
// Local Variables: // 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: // End:

View File

@ -1,4 +1,4 @@
// SDL Experiment 16, Barra Ó Catháin. // SDL Experiment 17, Barra Ó Catháin.
// =================================== // ===================================
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
@ -212,7 +212,8 @@ int main(int argc, char ** argv)
} }
// Check for joysticks: // Check for joysticks:
SDL_Joystick* controller = NULL; SDL_Joystick * controller = NULL;
SDL_Haptic * haptic = NULL;
if (SDL_NumJoysticks() < 1 ) if (SDL_NumJoysticks() < 1 )
{ {
printf( "Warning: No joysticks connected!\n" ); printf( "Warning: No joysticks connected!\n" );
@ -225,11 +226,14 @@ int main(int argc, char ** argv)
{ {
printf( "Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError() ); printf( "Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError() );
} }
haptic = SDL_HapticOpenFromJoystick(controller);
SDL_HapticRumbleInit(haptic);
} }
// Initialize image loading: // Initialize image loading:
IMG_Init(IMG_INIT_PNG); IMG_Init(IMG_INIT_PNG);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"); 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: // 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_Window * window = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 700, 700, 0);
@ -240,11 +244,11 @@ int main(int argc, char ** argv)
SDL_Texture * idleTexture, * acceleratingTexture, * clockwiseTexture, * anticlockwiseTexture, * currentTexture, SDL_Texture * idleTexture, * acceleratingTexture, * clockwiseTexture, * anticlockwiseTexture, * currentTexture,
* acceleratingTexture2; * acceleratingTexture2;
idleTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Idle.png"); idleTexture = IMG_LoadTexture(renderer, "./Images/Ship-Idle.png");
clockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Clockwise.png"); clockwiseTexture = IMG_LoadTexture(renderer, "./Images/Ship-Clockwise.png");
acceleratingTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Accelerating.png"); acceleratingTexture = IMG_LoadTexture(renderer, "./Images/Ship-Accelerating.png");
anticlockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Anticlockwise.png"); anticlockwiseTexture = IMG_LoadTexture(renderer, "./Images/Ship-Anticlockwise.png");
acceleratingTexture2 = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Accelerating-Frame-2.png"); acceleratingTexture2 = IMG_LoadTexture(renderer, "./Images/Ship-Accelerating-Frame-2.png");
// Enable resizing the window: // Enable resizing the window:
SDL_SetWindowResizable(window, SDL_TRUE); SDL_SetWindowResizable(window, SDL_TRUE);
@ -436,8 +440,12 @@ int main(int argc, char ** argv)
addXYVectorDeltaScaled(&shipA.velocity, &shipA.gravity, deltaTime); addXYVectorDeltaScaled(&shipA.velocity, &shipA.gravity, deltaTime);
addXYVectorDeltaScaled(&shipB.velocity, &shipB.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; xyVector temporary = engineVector;
multiplyXYVector(&engineVector, SDL_JoystickGetAxis(controller, 5) / 30000); multiplyXYVector(&engineVector, SDL_JoystickGetAxis(controller, 5) / 30000);
addXYVectorDeltaScaled(&shipA.velocity, &engineVector, deltaTime); addXYVectorDeltaScaled(&shipA.velocity, &engineVector, deltaTime);
@ -508,5 +516,5 @@ int main(int argc, char ** argv)
} }
// ======================================================================================================== // ========================================================================================================
// Local Variables: // 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: // End: