|
|
@ -15,15 +15,15 @@
|
|
|
|
#include "Spacewar-Server.h"
|
|
|
|
#include "Spacewar-Server.h"
|
|
|
|
|
|
|
|
|
|
|
|
const char * messageStrings[] = {"HELLO", "GOODBYE", "PING", "PONG", "SECRET"};
|
|
|
|
const char * messageStrings[] = {"HELLO", "GOODBYE", "PING", "PONG", "SECRET"};
|
|
|
|
typedef struct SpacewarSharedState
|
|
|
|
typedef struct SpacewarNetworkConfig
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SpacewarClientInput input;
|
|
|
|
SpacewarClientInput input;
|
|
|
|
SpacewarState * state;
|
|
|
|
SpacewarState * state;
|
|
|
|
} SpacewarSharedState;
|
|
|
|
} SpacewarNetworkConfig;
|
|
|
|
|
|
|
|
|
|
|
|
void * runNetworkThread (void * parameters)
|
|
|
|
void * runNetworkThread (void * parameters)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SpacewarSharedState * sharedState = (SpacewarSharedState *)parameters;
|
|
|
|
SpacewarNetworkConfig * configuration = (SpacewarNetworkConfig *)parameters;
|
|
|
|
int udpSocket = 0;
|
|
|
|
int udpSocket = 0;
|
|
|
|
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
|
|
|
@ -44,23 +44,10 @@ void * runNetworkThread (void * parameters)
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sendto(udpSocket, &sharedState->input, sizeof(SpacewarClientInput), 0,
|
|
|
|
sendto(udpSocket, &configuration->input, sizeof(SpacewarClientInput), 0,
|
|
|
|
(struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
|
|
|
|
(struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
|
|
|
|
recvfrom(udpSocket, updatedState, sizeof(SpacewarState), 0, NULL, NULL);
|
|
|
|
recvfrom(udpSocket, updatedState, sizeof(SpacewarState), 0, NULL, NULL);
|
|
|
|
memcpy(sharedState->state, updatedState, sizeof(SpacewarState));
|
|
|
|
memcpy(configuration->state, updatedState, sizeof(SpacewarState));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void * runPredictionThread(void * parameters)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
SpacewarSharedState * sharedState = (SpacewarSharedState *)parameters;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
memcpy(&sharedState->state->playerInputs[sharedState->input.playerNumber],
|
|
|
|
|
|
|
|
&sharedState->input.input, sizeof(SpacewarShipInput));
|
|
|
|
|
|
|
|
doPhysicsTick(sharedState->state);
|
|
|
|
|
|
|
|
usleep(15625);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -178,7 +165,7 @@ int main(int argc, char ** argv)
|
|
|
|
|
|
|
|
|
|
|
|
bool playerNumberSet, secretKeySet;
|
|
|
|
bool playerNumberSet, secretKeySet;
|
|
|
|
uint8_t playerNumber;
|
|
|
|
uint8_t playerNumber;
|
|
|
|
SpacewarSharedState sharedState;
|
|
|
|
SpacewarNetworkConfig networkConfiguration;
|
|
|
|
SpacewarMessage message;
|
|
|
|
SpacewarMessage message;
|
|
|
|
|
|
|
|
|
|
|
|
while (!playerNumberSet || !secretKeySet)
|
|
|
|
while (!playerNumberSet || !secretKeySet)
|
|
|
@ -190,24 +177,30 @@ int main(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
playerNumberSet = true;
|
|
|
|
playerNumberSet = true;
|
|
|
|
playerNumber = message.content;
|
|
|
|
playerNumber = message.content;
|
|
|
|
sharedState.input.playerNumber = message.content;
|
|
|
|
networkConfiguration.input.playerNumber = message.content;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
{
|
|
|
|
secretKeySet = true;
|
|
|
|
secretKeySet = true;
|
|
|
|
sharedState.input.secret = message.content;
|
|
|
|
networkConfiguration.input.secret = message.content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SpacewarState * state = calloc(1, sizeof(SpacewarState));
|
|
|
|
SpacewarState * state = calloc(1, sizeof(SpacewarState));
|
|
|
|
sharedState.state = state;
|
|
|
|
networkConfiguration.state = state;
|
|
|
|
|
|
|
|
|
|
|
|
// Spawn network thread:
|
|
|
|
// Spawn network thread:
|
|
|
|
pthread_t networkThread;
|
|
|
|
pthread_t networkThread;
|
|
|
|
pthread_create(&networkThread, NULL, runNetworkThread, &sharedState);
|
|
|
|
pthread_create(&networkThread, NULL, runNetworkThread, &networkConfiguration);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Spawn client-side-prediction thread:
|
|
|
|
|
|
|
|
if (!runServer)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
pthread_t clientSidePredictionThread;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Load in all of our textures:
|
|
|
|
// Load in all of our textures:
|
|
|
|
SDL_Texture * blackHoleTexture, * idleTexture, * acceleratingTexture, * clockwiseTexture,
|
|
|
|
SDL_Texture * blackHoleTexture, * idleTexture, * acceleratingTexture, * clockwiseTexture,
|
|
|
|
* anticlockwiseTexture, * currentTexture, * acceleratingTexture2, *blackHolePointerTexture;
|
|
|
|
* anticlockwiseTexture, * currentTexture, * acceleratingTexture2, *blackHolePointerTexture;
|
|
|
@ -247,14 +240,6 @@ int main(int argc, char ** argv)
|
|
|
|
|
|
|
|
|
|
|
|
SDL_Rect rendererSize;
|
|
|
|
SDL_Rect rendererSize;
|
|
|
|
int width, height;
|
|
|
|
int width, height;
|
|
|
|
|
|
|
|
|
|
|
|
// Spawn client-side-prediction thread:
|
|
|
|
|
|
|
|
pthread_t predictionThread;
|
|
|
|
|
|
|
|
if (!runServer)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
pthread_create(&predictionThread, NULL, runPredictionThread, &sharedState);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SDL_PumpEvents();
|
|
|
|
SDL_PumpEvents();
|
|
|
@ -262,9 +247,9 @@ int main(int argc, char ** argv)
|
|
|
|
//SDL_GetWindowSize(window, &width, &height);
|
|
|
|
//SDL_GetWindowSize(window, &width, &height);
|
|
|
|
|
|
|
|
|
|
|
|
// Do input:
|
|
|
|
// Do input:
|
|
|
|
sharedState.input.input.turningClockwise = keyboardState[SDL_SCANCODE_RIGHT];
|
|
|
|
networkConfiguration.input.input.turningClockwise = keyboardState[SDL_SCANCODE_RIGHT];
|
|
|
|
sharedState.input.input.turningAnticlockwise = keyboardState[SDL_SCANCODE_LEFT];
|
|
|
|
networkConfiguration.input.input.turningAnticlockwise = keyboardState[SDL_SCANCODE_LEFT];
|
|
|
|
sharedState.input.input.accelerating = keyboardState[SDL_SCANCODE_UP];
|
|
|
|
networkConfiguration.input.input.accelerating = keyboardState[SDL_SCANCODE_UP];
|
|
|
|
|
|
|
|
|
|
|
|
if (keyboardState[SDL_SCANCODE_PAGEUP] == 1)
|
|
|
|
if (keyboardState[SDL_SCANCODE_PAGEUP] == 1)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -333,7 +318,7 @@ int main(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (state->playerStates[index].inPlay == true)
|
|
|
|
if (state->playerStates[playerNumber].inPlay == true)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
shipRectangles[index].x = ((long)(state->playerStates[index].position.xComponent -
|
|
|
|
shipRectangles[index].x = ((long)(state->playerStates[index].position.xComponent -
|
|
|
|
state->playerStates[playerNumber].position.xComponent) -
|
|
|
|
state->playerStates[playerNumber].position.xComponent) -
|
|
|
|