Added basic gamepad support.
Before Width: | Height: | Size: 6.5 KiB After Width: | Height: | Size: 6.5 KiB |
Before Width: | Height: | Size: 1023 B After Width: | Height: | Size: 1023 B |
Before Width: | Height: | Size: 992 B After Width: | Height: | Size: 992 B |
Before Width: | Height: | Size: 1004 B After Width: | Height: | Size: 1004 B |
Before Width: | Height: | Size: 981 B After Width: | Height: | Size: 981 B |
|
@ -1,4 +1,4 @@
|
||||||
// SDL Experiment 15, Barra Ó Catháin.
|
// SDL Experiment 16, 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-15-Images/Ship-Idle.png");
|
idleTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Idle.png");
|
||||||
clockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-15-Images/Ship-Clockwise.png");
|
clockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Clockwise.png");
|
||||||
acceleratingTexture = IMG_LoadTexture(renderer, "./Experiment-15-Images/Ship-Accelerating.png");
|
acceleratingTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Accelerating.png");
|
||||||
anticlockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-15-Images/Ship-Anticlockwise.png");
|
anticlockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Anticlockwise.png");
|
||||||
acceleratingTexture2 = IMG_LoadTexture(renderer, "./Experiment-15-Images/Ship-Accelerating-Frame-2.png");
|
acceleratingTexture2 = IMG_LoadTexture(renderer, "./Experiment-16-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-15-Client.c -lSDL2_image -lm -o 'Spacewar Client!'"
|
// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-16-Client.c -lSDL2_image -lm -o 'Spacewar Client!'"
|
||||||
// End:
|
// End:
|
||||||
|
|
88
Spacewar.c
|
@ -1,4 +1,4 @@
|
||||||
// SDL Experiment 15, Barra Ó Catháin.
|
// SDL Experiment 16, Barra Ó Catháin.
|
||||||
// ===================================
|
// ===================================
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_image.h>
|
#include <SDL2/SDL_image.h>
|
||||||
|
@ -142,7 +142,7 @@ void calculateGravity(xyVector * starPosition, ship * shipUnderGravity)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
gravityAcceleration = pow(2, (2500 / (pow(gravityMagnitude, 2)))) / 5;
|
gravityAcceleration = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -202,7 +202,6 @@ int main(int argc, char ** argv)
|
||||||
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
|
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||||
serverAddress.sin_port = htons(12000);
|
serverAddress.sin_port = htons(12000);
|
||||||
|
|
||||||
// Get the initial
|
|
||||||
ship shipA = createShip(32, 32, 512, 512, 1, 0, 0);
|
ship shipA = createShip(32, 32, 512, 512, 1, 0, 0);
|
||||||
ship shipB = createShip(32, 32, -512, -512, 0, 1, 1);
|
ship shipB = createShip(32, 32, -512, -512, 0, 1, 1);
|
||||||
|
|
||||||
|
@ -212,6 +211,22 @@ int main(int argc, char ** argv)
|
||||||
printf("SDL Initialization Error: %s\n", SDL_GetError());
|
printf("SDL Initialization Error: %s\n", SDL_GetError());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for joysticks:
|
||||||
|
SDL_Joystick* controller = NULL;
|
||||||
|
if (SDL_NumJoysticks() < 1 )
|
||||||
|
{
|
||||||
|
printf( "Warning: No joysticks connected!\n" );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Load joystick
|
||||||
|
controller = SDL_JoystickOpen(0);
|
||||||
|
if (controller == NULL )
|
||||||
|
{
|
||||||
|
printf( "Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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");
|
||||||
|
@ -225,11 +240,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-15-Images/Ship-Idle.png");
|
idleTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Idle.png");
|
||||||
clockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-15-Images/Ship-Clockwise.png");
|
clockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Clockwise.png");
|
||||||
acceleratingTexture = IMG_LoadTexture(renderer, "./Experiment-15-Images/Ship-Accelerating.png");
|
acceleratingTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Accelerating.png");
|
||||||
anticlockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-15-Images/Ship-Anticlockwise.png");
|
anticlockwiseTexture = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Anticlockwise.png");
|
||||||
acceleratingTexture2 = IMG_LoadTexture(renderer, "./Experiment-15-Images/Ship-Accelerating-Frame-2.png");
|
acceleratingTexture2 = IMG_LoadTexture(renderer, "./Experiment-16-Images/Ship-Accelerating-Frame-2.png");
|
||||||
|
|
||||||
// Enable resizing the window:
|
// Enable resizing the window:
|
||||||
SDL_SetWindowResizable(window, SDL_TRUE);
|
SDL_SetWindowResizable(window, SDL_TRUE);
|
||||||
|
@ -239,6 +254,36 @@ int main(int argc, char ** argv)
|
||||||
lastFrameTime = thisFrameTime;
|
lastFrameTime = thisFrameTime;
|
||||||
thisFrameTime = SDL_GetPerformanceCounter();
|
thisFrameTime = SDL_GetPerformanceCounter();
|
||||||
deltaTime = (double)(((thisFrameTime - lastFrameTime) * 1000) / (double)SDL_GetPerformanceFrequency());
|
deltaTime = (double)(((thisFrameTime - lastFrameTime) * 1000) / (double)SDL_GetPerformanceFrequency());
|
||||||
|
|
||||||
|
// Check for left movement on the stick:
|
||||||
|
if (SDL_JoystickGetAxis(controller, 0) < -2500)
|
||||||
|
{
|
||||||
|
rotatingAnticlockwise = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rotatingAnticlockwise = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for right movement on the stick:
|
||||||
|
if (SDL_JoystickGetAxis(controller, 0) > 2500)
|
||||||
|
{
|
||||||
|
rotatingClockwise = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rotatingClockwise = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for movement on the right trigger:
|
||||||
|
if (SDL_JoystickGetAxis(controller, 5) > 2500)
|
||||||
|
{
|
||||||
|
accelerating = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
accelerating = false;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if the user wants to quit:
|
// Check if the user wants to quit:
|
||||||
while (SDL_PollEvent(&event))
|
while (SDL_PollEvent(&event))
|
||||||
|
@ -309,7 +354,7 @@ int main(int argc, char ** argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrap the positions if the ship goes interstellar:
|
// Wrap the positions if the ship goes interstellar:
|
||||||
if(shipA.position.xComponent > 4096)
|
if(shipA.position.xComponent > 4096)
|
||||||
{
|
{
|
||||||
|
@ -362,12 +407,28 @@ int main(int argc, char ** argv)
|
||||||
// Rotate the engine vector if needed:
|
// Rotate the engine vector if needed:
|
||||||
if(rotatingClockwise)
|
if(rotatingClockwise)
|
||||||
{
|
{
|
||||||
rotateXYVector(&engineVector, 0.25 * deltaTime);
|
if (SDL_JoystickGetAxis(controller, 0) > 2500)
|
||||||
|
{
|
||||||
|
double rotationalSpeed = ((double)SDL_JoystickGetAxis(controller, 0) / 20000) * -1;
|
||||||
|
rotateXYVector(&engineVector, -0.25 * deltaTime * rotationalSpeed);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rotateXYVector(&engineVector, 0.25 * deltaTime);
|
||||||
|
}
|
||||||
currentTexture = clockwiseTexture;
|
currentTexture = clockwiseTexture;
|
||||||
}
|
}
|
||||||
if(rotatingAnticlockwise)
|
if(rotatingAnticlockwise)
|
||||||
{
|
{
|
||||||
rotateXYVector(&engineVector, -0.25 * deltaTime);
|
if (SDL_JoystickGetAxis(controller, 0) < -2500)
|
||||||
|
{
|
||||||
|
double rotationalSpeed = ((double)SDL_JoystickGetAxis(controller, 0) / 20000) * -1;
|
||||||
|
rotateXYVector(&engineVector, -0.25 * deltaTime * rotationalSpeed);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rotateXYVector(&engineVector, -0.25 * deltaTime);
|
||||||
|
}
|
||||||
currentTexture = anticlockwiseTexture;
|
currentTexture = anticlockwiseTexture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -377,7 +438,10 @@ int main(int argc, char ** argv)
|
||||||
|
|
||||||
if(accelerating)
|
if(accelerating)
|
||||||
{
|
{
|
||||||
|
xyVector temporary = engineVector;
|
||||||
|
multiplyXYVector(&engineVector, SDL_JoystickGetAxis(controller, 5) / 30000);
|
||||||
addXYVectorDeltaScaled(&shipA.velocity, &engineVector, deltaTime);
|
addXYVectorDeltaScaled(&shipA.velocity, &engineVector, deltaTime);
|
||||||
|
engineVector = temporary;
|
||||||
frameAccumulator += deltaTime;
|
frameAccumulator += deltaTime;
|
||||||
currentTexture = acceleratingTexture;
|
currentTexture = acceleratingTexture;
|
||||||
if((long)frameAccumulator % 4)
|
if((long)frameAccumulator % 4)
|
||||||
|
@ -444,5 +508,5 @@ int main(int argc, char ** argv)
|
||||||
}
|
}
|
||||||
// ========================================================================================================
|
// ========================================================================================================
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-15.c -lSDL2_image -lm -o 'Spacewar!'"
|
// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-Experiment-16.c -lSDL2_image -lm -o 'Spacewar!'"
|
||||||
// End:
|
// End:
|
||||||
|
|