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.
This commit is contained in:
parent
66ed3e156c
commit
86bc0390d6
204
Spacewar.c
204
Spacewar.c
|
@ -12,208 +12,7 @@
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include "xyVector.h"
|
#include "xyVector.h"
|
||||||
#include "spacewarPlayer.h"
|
#include "spacewarPlayer.h"
|
||||||
|
#include "spacewarGraphics.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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char ** argv)
|
int main(int argc, char ** argv)
|
||||||
{
|
{
|
||||||
|
@ -289,6 +88,7 @@ int main(int argc, char ** argv)
|
||||||
// Check for joysticks:
|
// Check for joysticks:
|
||||||
if (SDL_NumJoysticks() < 1 )
|
if (SDL_NumJoysticks() < 1 )
|
||||||
{
|
{
|
||||||
|
playerOne.joystick = NULL;
|
||||||
printf( "Warning: No joysticks connected!\n" );
|
printf( "Warning: No joysticks connected!\n" );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
#ifndef SPACEWAR_GRAPHICS_H
|
||||||
|
#define SPACEWAR_GRAPHICS_H
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
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
|
166
spacewarPlayer.h
166
spacewarPlayer.h
|
@ -23,4 +23,170 @@ typedef struct playerController
|
||||||
bool turningClockwise, turningAnticlockwise, accelerating;
|
bool turningClockwise, turningAnticlockwise, accelerating;
|
||||||
} playerController;
|
} 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
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue