From 7c22fdeefb0c59530e4ad76d0648e3da88f84bed Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Mon, 20 Mar 2023 17:27:34 +0000 Subject: [PATCH] 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. --- Spacewar-Client.c | 2 +- Spacewar.c | 4 +- spacewarPlayer.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++ spacewarPlayer.h | 99 ++++---------------------------------------- 4 files changed, 112 insertions(+), 95 deletions(-) create mode 100644 spacewarPlayer.c diff --git a/Spacewar-Client.c b/Spacewar-Client.c index 24de34a..f9b68d5 100644 --- a/Spacewar-Client.c +++ b/Spacewar-Client.c @@ -230,5 +230,5 @@ int main(int argc, char ** argv) } // ======================================================================================================== // 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: diff --git a/Spacewar.c b/Spacewar.c index 581e0b5..4e98f0b 100644 --- a/Spacewar.c +++ b/Spacewar.c @@ -212,7 +212,7 @@ int main(int argc, char ** argv) } // Get the needed input: - getPlayerInput(&playerOne, 0); + getPlayerInput(&playerOne); takeNetworkInput(&playerTwo, receiveSocket); // Do the needed input: @@ -270,5 +270,5 @@ int main(int argc, char ** argv) } // ======================================================================================================== // 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: diff --git a/spacewarPlayer.c b/spacewarPlayer.c new file mode 100644 index 0000000..715df44 --- /dev/null +++ b/spacewarPlayer.c @@ -0,0 +1,102 @@ +// spacewarPlayer.c: Contains function definitions for player interaction. +// Barra Ó Catháin, 2023 +// ======================================================================= +#include +#include +#include +#include +#include +#include + +#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); + } +} diff --git a/spacewarPlayer.h b/spacewarPlayer.h index a6287ed..388bf36 100644 --- a/spacewarPlayer.h +++ b/spacewarPlayer.h @@ -59,105 +59,14 @@ static inline void calculateGravity(xyVector * starPosition, ship * shipUnderGra 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) { playerController newController; newController.number = ship->number; + newController.joystick = NULL; 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: 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; } +// Function prototypes: +void doShipInput(playerController * controller, ship * ship, xyVector starPosition, double deltaTime); +void takeNetworkInput(playerController * controller, int descriptor); + +void getPlayerInput(playerController * controller); + #endif