Continued implementing getPlayerInput.

This commit is contained in:
Barra Ó Catháin 2023-03-14 00:56:31 +00:00
parent fa3d3ca843
commit a50469e1f7
2 changed files with 83 additions and 86 deletions

View File

@ -133,9 +133,38 @@ static inline void takeNetworkInput(playerController * controller, int descripto
static inline void getPlayerInput(playerController * controller, int playerNumber)
{
SDL_PumpEvents();
int keyboardStateLength = 0;
uint8_t * keyboardState = SDL_GetKeyboardState(&keyboardStateLength);
const uint8_t * keyboardState = SDL_GetKeyboardState(NULL);
if(keyboardState[SDL_SCANCODE_UP] == 1)
{
controller->accelerating = true;
}
else
{
controller->accelerating = false;
}
if(keyboardState[SDL_SCANCODE_LEFT] == 1)
{
controller->turningAnticlockwise = true;
}
else
{
controller->turningAnticlockwise = false;;
}
if(keyboardState[SDL_SCANCODE_RIGHT] == 1)
{
controller->turningClockwise = true;
}
else
{
controller->turningClockwise = false;
}
if(controller->joystick != NULL)
{
controller->turningAmount = SDL_JoystickGetAxis(controller->joystick, 0);
controller->acceleratingAmount = SDL_JoystickGetAxis(controller->joystick, 5);
}
}
void doShipInput(playerController * controller, ship * ship, xyVector starPosition, double deltaTime)
{
if(controller->number == ship->number)
@ -148,15 +177,35 @@ void doShipInput(playerController * controller, ship * ship, xyVector starPositi
{
rotateXYVector(&ship->engine, 0.25 * deltaTime);
}
else if (controller->turningAmount > 2500)
{
double rotationalSpeed = (controller->turningAmount / 20000);
rotateXYVector(&ship->engine, 0.25 * deltaTime * rotationalSpeed);
}
if (controller->turningAnticlockwise)
{
rotateXYVector(&ship->engine, -0.25 * deltaTime);
}
else if (controller->turningAmount < -2500)
{
double rotationalSpeed = (controller->turningAmount / 20000);
rotateXYVector(&ship->engine, 0.25 * deltaTime * rotationalSpeed);
}
// Calculate the new current velocity:
addXYVectorDeltaScaled(&ship->velocity, &ship->gravity, deltaTime);
if (controller->accelerating)
if (controller->acceleratingAmount > 2500)
{
xyVector temporary = ship->engine;
multiplyXYVector(&ship->engine, controller->acceleratingAmount/ 32748);
SDL_HapticRumblePlay(controller->haptic, (float)controller->acceleratingAmount / 32768, 20);
addXYVectorDeltaScaled(&ship->velocity, &ship->engine, deltaTime);
ship->engine = temporary;
}
else if (controller->accelerating)
{
addXYVectorDeltaScaled(&ship->velocity, &ship->engine, deltaTime);
}
@ -229,25 +278,6 @@ int main(int argc, char ** argv)
printf("SDL Initialization Error: %s\n", SDL_GetError());
}
// Check for joysticks:
SDL_Joystick * controller = NULL;
SDL_Haptic * haptic = 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() );
}
haptic = SDL_HapticOpenFromJoystick(controller);
SDL_HapticRumbleInit(haptic);
}
// Initialize image loading:
IMG_Init(IMG_INIT_PNG);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
@ -268,11 +298,30 @@ int main(int argc, char ** argv)
anticlockwiseTexture = IMG_LoadTexture(renderer, "./Images/Ship-Anticlockwise.png");
acceleratingTexture2 = IMG_LoadTexture(renderer, "./Images/Ship-Accelerating-Frame-2.png");
currentTexture = acceleratingTexture;
// Enable resizing the window:
SDL_SetWindowResizable(window, SDL_TRUE);
playerController playerOne = createShipPlayerController(&shipA);
playerController playerTwo = createShipPlayerController(&shipB);
// Check for joysticks:
if (SDL_NumJoysticks() < 1 )
{
printf( "Warning: No joysticks connected!\n" );
}
else
{
// Load joystick
playerOne.joystick = SDL_JoystickOpen(0);
if (playerOne.joystick == NULL )
{
printf( "Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError() );
}
playerOne.haptic = SDL_HapticOpenFromJoystick(playerOne.joystick);
SDL_HapticRumbleInit(playerOne.haptic);
}
while (!quit)
{
lastFrameTime = thisFrameTime;
@ -294,63 +343,6 @@ int main(int argc, char ** argv)
quit = true;
break;
}
case SDL_KEYDOWN:
{
switch (event.key.keysym.sym)
{
case SDLK_LEFT:
{
playerOne.turningAnticlockwise = true;
break;
}
case SDLK_RIGHT:
{
playerOne.turningClockwise = true;
break;
}
case SDLK_UP:
{
playerOne.accelerating = true;
break;
}
default:
{
break;
}
}
break;
}
case SDL_KEYUP:
{
switch (event.key.keysym.sym)
{
case SDLK_LEFT:
{
playerOne.turningAnticlockwise = false;
break;
}
case SDLK_RIGHT:
{
playerOne.turningClockwise = false;
break;
}
case SDLK_UP:
{
playerOne.accelerating = false;
frameAccumulator = 0;
break;
}
default:
{
break;
}
}
break;
}
default:
{
break;
}
}
}
@ -393,14 +385,17 @@ int main(int argc, char ** argv)
shipB.velocity.yComponent *= 0.9;
}
//
doShipInput(&playerOne, &shipA, starPosition, deltaTime);
// Get the needed input:
getPlayerInput(&playerOne, 0);
takeNetworkInput(&playerTwo, receiveSocket);
// Do the needed input:
doShipInput(&playerOne, &shipA, starPosition, deltaTime);
doShipInput(&playerTwo, &shipB, starPosition, deltaTime);
shipA.rectangle.x = (width/2) - 16 - (shipA.velocity.xComponent * 15);
shipA.rectangle.y = (height/2) - 16 - (shipA.velocity.yComponent * 15);
shipB.rectangle.x = (long)((((shipB.position.xComponent - shipA.position.xComponent) - 32) + width/2) - (shipA.velocity.xComponent * 15));
shipB.rectangle.y = (long)((((shipB.position.yComponent - shipA.position.yComponent) - 32) + height/2) - (shipA.velocity.yComponent * 15));

View File

@ -16,8 +16,10 @@ typedef struct ship
// A struct to store the input state for one player:
typedef struct playerController
{
SDL_Joystick * joystick;
SDL_Haptic * haptic;
int number;
int clockwiseTurn, anticlockwiseTurn, acceleratingAmount;
double turningAmount, acceleratingAmount;
bool turningClockwise, turningAnticlockwise, accelerating;
} playerController;