From 86bc0390d6ec3efdcd161270553bd2cca4679524 Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Mon, 20 Mar 2023 16:48:50 +0000 Subject: [PATCH] Moved all functions to header files It's all inline. Why? Well, all of them are basically called constantly, may as well inline them. Massive binary, but why not? You have some RAM. --- Spacewar.c | 204 +-------------------------------------------- spacewarGraphics.h | 43 ++++++++++ spacewarPlayer.h | 166 ++++++++++++++++++++++++++++++++++++ 3 files changed, 211 insertions(+), 202 deletions(-) create mode 100644 spacewarGraphics.h diff --git a/Spacewar.c b/Spacewar.c index 7a21f3e..581e0b5 100644 --- a/Spacewar.c +++ b/Spacewar.c @@ -12,208 +12,7 @@ #include #include "xyVector.h" #include "spacewarPlayer.h" - -void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius) -{ - const int32_t diameter = (radius * 2); - - int32_t x = (radius - 1); - int32_t y = 0; - int32_t tx = 1; - int32_t ty = 1; - int32_t error = (tx - diameter); - - while (x >= y) - { - // Each of the following renders an octant of the circle - SDL_RenderDrawPoint(renderer, centreX + x, centreY - y); - SDL_RenderDrawPoint(renderer, centreX + x, centreY + y); - SDL_RenderDrawPoint(renderer, centreX - x, centreY - y); - SDL_RenderDrawPoint(renderer, centreX - x, centreY + y); - SDL_RenderDrawPoint(renderer, centreX + y, centreY - x); - SDL_RenderDrawPoint(renderer, centreX + y, centreY + x); - SDL_RenderDrawPoint(renderer, centreX - y, centreY - x); - SDL_RenderDrawPoint(renderer, centreX - y, centreY + x); - - if (error <= 0) - { - ++y; - error += ty; - ty += 2; - } - - if (error > 0) - { - --x; - tx += 2; - error += (tx - diameter); - } - } -} - -void calculateGravity(xyVector * starPosition, ship * shipUnderGravity) -{ - // Calculate the vector between the star and ship: - xyVectorBetweenPoints(shipUnderGravity->position.xComponent, shipUnderGravity->position.yComponent, - starPosition->xComponent, starPosition->yComponent, &shipUnderGravity->gravity); - - // Make it into a unit vector: - double gravityMagnitude = normalizeXYVector(&shipUnderGravity->gravity); - double gravityAcceleration = 0; - - // Calculate the gravity between the star and ship: - if(gravityMagnitude != 0) - { - if(gravityMagnitude >= 116) - { - gravityAcceleration = pow(2, (3000 / (pow(gravityMagnitude, 2)))) / 8; - } - else - { - gravityAcceleration = 1; - } - } - else - { - gravityAcceleration = 1; - } - - if(gravityAcceleration < 0.01) - { - gravityAcceleration = 0.01; - } - - // Scale the vector: - multiplyXYVector(&shipUnderGravity->gravity, gravityAcceleration); -} - -// Create a ship with the given parameters: -ship createShip(int width, int height, double positionX, double positionY, double velocityX, double velocityY, int number) -{ - ship newShip; - - // Player number: - newShip.number = number; - - // Rectangle to show the ship in: - newShip.rectangle.w = width; - newShip.rectangle.h = height; - - // Position: - newShip.position.xComponent = positionX; - newShip.position.yComponent = positionY; - - // Velocity: - newShip.velocity.xComponent = velocityX; - newShip.velocity.yComponent = velocityY; - - // Gravity: - newShip.gravity.xComponent = 0; - newShip.gravity.yComponent = 0; - - // Engine: - newShip.engine.yComponent = 0; - newShip.engine.xComponent = 0.1; - return newShip; -} - -playerController createShipPlayerController(ship * ship) -{ - playerController newController; - newController.number = ship->number; - return newController; -} - -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); - } -} - -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); - } -} +#include "spacewarGraphics.h" int main(int argc, char ** argv) { @@ -289,6 +88,7 @@ int main(int argc, char ** argv) // Check for joysticks: if (SDL_NumJoysticks() < 1 ) { + playerOne.joystick = NULL; printf( "Warning: No joysticks connected!\n" ); } else diff --git a/spacewarGraphics.h b/spacewarGraphics.h new file mode 100644 index 0000000..9f612cd --- /dev/null +++ b/spacewarGraphics.h @@ -0,0 +1,43 @@ +#ifndef SPACEWAR_GRAPHICS_H +#define SPACEWAR_GRAPHICS_H +#include + +static inline void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius) +{ + const int32_t diameter = (radius * 2); + + int32_t x = (radius - 1); + int32_t y = 0; + int32_t tx = 1; + int32_t ty = 1; + int32_t error = (tx - diameter); + + while (x >= y) + { + // Each of the following renders an octant of the circle + SDL_RenderDrawPoint(renderer, centreX + x, centreY - y); + SDL_RenderDrawPoint(renderer, centreX + x, centreY + y); + SDL_RenderDrawPoint(renderer, centreX - x, centreY - y); + SDL_RenderDrawPoint(renderer, centreX - x, centreY + y); + SDL_RenderDrawPoint(renderer, centreX + y, centreY - x); + SDL_RenderDrawPoint(renderer, centreX + y, centreY + x); + SDL_RenderDrawPoint(renderer, centreX - y, centreY - x); + SDL_RenderDrawPoint(renderer, centreX - y, centreY + x); + + if (error <= 0) + { + ++y; + error += ty; + ty += 2; + } + + if (error > 0) + { + --x; + tx += 2; + error += (tx - diameter); + } + } +} + +#endif diff --git a/spacewarPlayer.h b/spacewarPlayer.h index 4a933b1..a6287ed 100644 --- a/spacewarPlayer.h +++ b/spacewarPlayer.h @@ -23,4 +23,170 @@ typedef struct playerController bool turningClockwise, turningAnticlockwise, accelerating; } playerController; +static inline void calculateGravity(xyVector * starPosition, ship * shipUnderGravity) +{ + // Calculate the vector between the star and ship: + xyVectorBetweenPoints(shipUnderGravity->position.xComponent, shipUnderGravity->position.yComponent, + starPosition->xComponent, starPosition->yComponent, &shipUnderGravity->gravity); + + // Make it into a unit vector: + double gravityMagnitude = normalizeXYVector(&shipUnderGravity->gravity); + double gravityAcceleration = 0; + + // Calculate the gravity between the star and ship: + if(gravityMagnitude != 0) + { + if(gravityMagnitude >= 116) + { + gravityAcceleration = pow(2, (3000 / (pow(gravityMagnitude, 2)))) / 8; + } + else + { + gravityAcceleration = 1; + } + } + else + { + gravityAcceleration = 1; + } + + if(gravityAcceleration < 0.01) + { + gravityAcceleration = 0.01; + } + + // Scale the vector: + 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; + 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) +{ + ship newShip; + + // Player number: + newShip.number = number; + + // Rectangle to show the ship in: + newShip.rectangle.w = width; + newShip.rectangle.h = height; + + // Position: + newShip.position.xComponent = positionX; + newShip.position.yComponent = positionY; + + // Velocity: + newShip.velocity.xComponent = velocityX; + newShip.velocity.yComponent = velocityY; + + // Gravity: + newShip.gravity.xComponent = 0; + newShip.gravity.yComponent = 0; + + // Engine: + newShip.engine.yComponent = 0; + newShip.engine.xComponent = 0.1; + return newShip; +} + + #endif