Compare commits

..

No commits in common. "5fea20ff9de4c42817e85c6ff6fe3d036e7e7e3a" and "51aeaf6c24c836c3adcb9437323f9e16ddde40a2" have entirely different histories.

3 changed files with 23 additions and 38 deletions

3
.gitignore vendored
View File

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

View File

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

View File

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