Compare commits

..

2 Commits

3 changed files with 38 additions and 23 deletions

3
.gitignore vendored
View File

@ -1,2 +1 @@
Spacewar! Spacewar-Client
Spacewar Client!

View File

@ -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 SpacewarNetworkConfig typedef struct SpacewarSharedState
{ {
SpacewarClientInput input; SpacewarClientInput input;
SpacewarState * state; SpacewarState * state;
} SpacewarNetworkConfig; } SpacewarSharedState;
void * runNetworkThread (void * parameters) void * runNetworkThread (void * parameters)
{ {
SpacewarNetworkConfig * configuration = (SpacewarNetworkConfig *)parameters; SpacewarSharedState * sharedState = (SpacewarSharedState *)parameters;
int udpSocket = 0; int udpSocket = 0;
udpSocket = socket(AF_INET, SOCK_DGRAM, 0); udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
@ -44,10 +44,23 @@ void * runNetworkThread (void * parameters)
while (true) while (true)
{ {
sendto(udpSocket, &configuration->input, sizeof(SpacewarClientInput), 0, sendto(udpSocket, &sharedState->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(configuration->state, updatedState, sizeof(SpacewarState)); memcpy(sharedState->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);
} }
} }
@ -165,7 +178,7 @@ int main(int argc, char ** argv)
bool playerNumberSet, secretKeySet; bool playerNumberSet, secretKeySet;
uint8_t playerNumber; uint8_t playerNumber;
SpacewarNetworkConfig networkConfiguration; SpacewarSharedState sharedState;
SpacewarMessage message; SpacewarMessage message;
while (!playerNumberSet || !secretKeySet) while (!playerNumberSet || !secretKeySet)
@ -177,30 +190,24 @@ int main(int argc, char ** argv)
{ {
playerNumberSet = true; playerNumberSet = true;
playerNumber = message.content; playerNumber = message.content;
networkConfiguration.input.playerNumber = message.content; sharedState.input.playerNumber = message.content;
break; break;
} }
case 4: case 4:
{ {
secretKeySet = true; secretKeySet = true;
networkConfiguration.input.secret = message.content; sharedState.input.secret = message.content;
} }
} }
} }
SpacewarState * state = calloc(1, sizeof(SpacewarState)); SpacewarState * state = calloc(1, sizeof(SpacewarState));
networkConfiguration.state = state; sharedState.state = state;
// Spawn network thread: // Spawn network thread:
pthread_t networkThread; pthread_t networkThread;
pthread_create(&networkThread, NULL, runNetworkThread, &networkConfiguration); pthread_create(&networkThread, NULL, runNetworkThread, &sharedState);
// 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;
@ -240,6 +247,14 @@ 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();
@ -247,9 +262,9 @@ int main(int argc, char ** argv)
//SDL_GetWindowSize(window, &width, &height); //SDL_GetWindowSize(window, &width, &height);
// Do input: // Do input:
networkConfiguration.input.input.turningClockwise = keyboardState[SDL_SCANCODE_RIGHT]; sharedState.input.input.turningClockwise = keyboardState[SDL_SCANCODE_RIGHT];
networkConfiguration.input.input.turningAnticlockwise = keyboardState[SDL_SCANCODE_LEFT]; sharedState.input.input.turningAnticlockwise = keyboardState[SDL_SCANCODE_LEFT];
networkConfiguration.input.input.accelerating = keyboardState[SDL_SCANCODE_UP]; sharedState.input.input.accelerating = keyboardState[SDL_SCANCODE_UP];
if (keyboardState[SDL_SCANCODE_PAGEUP] == 1) if (keyboardState[SDL_SCANCODE_PAGEUP] == 1)
{ {
@ -318,7 +333,7 @@ int main(int argc, char ** argv)
{ {
continue; continue;
} }
if (state->playerStates[playerNumber].inPlay == true) if (state->playerStates[index].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) -

View File

@ -36,7 +36,8 @@ void sendCurrentState(SpacewarState * state, SpacewarConnection * connections, i
if (connections[connectionIndex].active) if (connections[connectionIndex].active)
{ {
sendto(udpSocket, state, sizeof(SpacewarState), 0, sendto(udpSocket, state, sizeof(SpacewarState), 0,
(struct sockaddr *)&connections[connectionIndex].clientAddress, sizeof(struct sockaddr_in)); (struct sockaddr *)&connections[connectionIndex].clientAddress,
sizeof(struct sockaddr_in));
} }
} }
} }