Uninlined I/O bound functions.
Yeah, I was meming when I inlined everything. It's kind of pointless for I/O bound functions, but there are actually a lot of candidates for inlining that make sense.
This commit is contained in:
parent
958e7b65ff
commit
7c22fdeefb
|
@ -230,5 +230,5 @@ int main(int argc, char ** argv)
|
||||||
}
|
}
|
||||||
// ========================================================================================================
|
// ========================================================================================================
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
// compile-command: "gcc `sdl2-config --libs --cflags` Spacewar-Client.c -lSDL2_image -lm -o 'Spacewar Client!'"
|
// compile-command: "gcc `sdl2-config --libs --cflags` Spacewar-Client.c spacewarPlayer.c -lSDL2_image -lm -o 'Spacewar Client!'"
|
||||||
// End:
|
// End:
|
||||||
|
|
|
@ -212,7 +212,7 @@ int main(int argc, char ** argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the needed input:
|
// Get the needed input:
|
||||||
getPlayerInput(&playerOne, 0);
|
getPlayerInput(&playerOne);
|
||||||
takeNetworkInput(&playerTwo, receiveSocket);
|
takeNetworkInput(&playerTwo, receiveSocket);
|
||||||
|
|
||||||
// Do the needed input:
|
// Do the needed input:
|
||||||
|
@ -270,5 +270,5 @@ int main(int argc, char ** argv)
|
||||||
}
|
}
|
||||||
// ========================================================================================================
|
// ========================================================================================================
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
// compile-command: "gcc `sdl2-config --libs --cflags` Spacewar.c -lSDL2_image -lm -o 'Spacewar!'"
|
// compile-command: "gcc `sdl2-config --libs --cflags` Spacewar.c spacewarPlayer.c -lSDL2_image -lm -o 'Spacewar!'"
|
||||||
// End:
|
// End:
|
||||||
|
|
|
@ -0,0 +1,102 @@
|
||||||
|
// spacewarPlayer.c: Contains function definitions for player interaction.
|
||||||
|
// Barra Ó Catháin, 2023
|
||||||
|
// =======================================================================
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
#include "xyVector.h"
|
||||||
|
#include "spacewarPlayer.h"
|
||||||
|
|
||||||
|
void takeNetworkInput(playerController * controller, int descriptor)
|
||||||
|
{
|
||||||
|
recvfrom(descriptor, controller, sizeof(playerController), 0, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void getPlayerInput(playerController * controller)
|
||||||
|
{
|
||||||
|
SDL_PumpEvents();
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
// Calculate the gravity for the ships:
|
||||||
|
calculateGravity(&starPosition, ship);
|
||||||
|
|
||||||
|
// Rotate the engine vector if needed:
|
||||||
|
if (controller->turningClockwise)
|
||||||
|
{
|
||||||
|
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->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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the new position:
|
||||||
|
addXYVectorDeltaScaled(&ship->position, &ship->velocity, deltaTime);
|
||||||
|
}
|
||||||
|
}
|
|
@ -59,105 +59,14 @@ static inline void calculateGravity(xyVector * starPosition, ship * shipUnderGra
|
||||||
multiplyXYVector(&shipUnderGravity->gravity, gravityAcceleration);
|
multiplyXYVector(&shipUnderGravity->gravity, gravityAcceleration);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void takeNetworkInput(playerController * controller, int descriptor)
|
|
||||||
{
|
|
||||||
recvfrom(descriptor, controller, sizeof(playerController), 0, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static inline void getPlayerInput(playerController * controller, int playerNumber)
|
|
||||||
{
|
|
||||||
SDL_PumpEvents();
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline playerController createShipPlayerController(ship * ship)
|
static inline playerController createShipPlayerController(ship * ship)
|
||||||
{
|
{
|
||||||
playerController newController;
|
playerController newController;
|
||||||
newController.number = ship->number;
|
newController.number = ship->number;
|
||||||
|
newController.joystick = NULL;
|
||||||
return newController;
|
return newController;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static inline void doShipInput(playerController * controller, ship * ship, xyVector starPosition, double deltaTime)
|
|
||||||
{
|
|
||||||
if(controller->number == ship->number)
|
|
||||||
{
|
|
||||||
// Calculate the gravity for the ships:
|
|
||||||
calculateGravity(&starPosition, ship);
|
|
||||||
|
|
||||||
// Rotate the engine vector if needed:
|
|
||||||
if (controller->turningClockwise)
|
|
||||||
{
|
|
||||||
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->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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate the new position:
|
|
||||||
addXYVectorDeltaScaled(&ship->position, &ship->velocity, deltaTime);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a ship with the given parameters:
|
// Create a ship with the given parameters:
|
||||||
static inline ship createShip(int width, int height, double positionX, double positionY, double velocityX, double velocityY, int number)
|
static inline ship createShip(int width, int height, double positionX, double positionY, double velocityX, double velocityY, int number)
|
||||||
{
|
{
|
||||||
|
@ -188,5 +97,11 @@ static inline ship createShip(int width, int height, double positionX, double po
|
||||||
return newShip;
|
return newShip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function prototypes:
|
||||||
|
void doShipInput(playerController * controller, ship * ship, xyVector starPosition, double deltaTime);
|
||||||
|
|
||||||
|
void takeNetworkInput(playerController * controller, int descriptor);
|
||||||
|
|
||||||
|
void getPlayerInput(playerController * controller);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue