Added basic keyboard input and game thread

This commit is contained in:
Barra Ó Catháin 2023-07-06 02:07:07 +01:00
parent ff01152aa1
commit 3a25a86f82
3 changed files with 163 additions and 105 deletions

View File

@ -20,40 +20,40 @@
void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius) void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius)
{ {
const int32_t diameter = (radius * 2); const int32_t diameter = (radius * 2);
int32_t x = (radius - 1); int32_t x = (radius - 1);
int32_t y = 0; int32_t y = 0;
int32_t tx = 1; int32_t tx = 1;
int32_t ty = 1; int32_t ty = 1;
int32_t error = (tx - diameter); int32_t error = (tx - diameter);
while (x >= y) while (x >= y)
{ {
// Each of the following renders an octant of the circle // 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 - 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);
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) if (error <= 0)
{ {
++y; ++y;
error += ty; error += ty;
ty += 2; ty += 2;
} }
if (error > 0) if (error > 0)
{ {
--x; --x;
tx += 2; tx += 2;
error += (tx - diameter); error += (tx - diameter);
} }
} }
} }
void * graphicsThreadHandler(void * parameters) void * graphicsThreadHandler(void * parameters)
@ -63,7 +63,6 @@ void * graphicsThreadHandler(void * parameters)
int udpSocket = 0; int udpSocket = 0;
struct sockaddr_in recieveAddress, serverAddress; struct sockaddr_in recieveAddress, serverAddress;
struct clientInput message; struct clientInput message;
message.right = true;
message.clientNumber = 1; message.clientNumber = 1;
serverAddress.sin_family = AF_INET; serverAddress.sin_family = AF_INET;
@ -74,7 +73,7 @@ void * graphicsThreadHandler(void * parameters)
// Create an SDL window and rendering context in that window: // Create an SDL window and rendering context in that window:
SDL_Window * window = SDL_CreateWindow("CSPT-Client", SDL_WINDOWPOS_CENTERED, SDL_Window * window = SDL_CreateWindow("CSPT-Client", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 640, 640, 0); SDL_WINDOWPOS_CENTERED, 600, 600, 0);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags); SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags);
// Enable resizing the window: // Enable resizing the window:
@ -84,13 +83,74 @@ void * graphicsThreadHandler(void * parameters)
struct timeval tv; struct timeval tv;
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 100000; tv.tv_usec = 1000;
setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)); setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv));
SDL_Event event;
while (true) while (true)
{ {
sendto(udpSocket, &message, sizeof(struct clientInput), MSG_CONFIRM, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in)); while( SDL_PollEvent( &event ) ){
recvfrom(udpSocket, state, sizeof(struct gameState), MSG_WAITALL, NULL, NULL); switch( event.type )
{
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
{
message.left = true;
break;
}
case SDLK_RIGHT:
{
message.right = true;
break;
}
case SDLK_UP:
{
message.up = true;
break;
}
case SDLK_DOWN:
{
message.down = true;
break;
}
default:
break;
}
break;
case SDL_KEYUP:
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
{
message.left = false;
break;
}
case SDLK_RIGHT:
{
message.right = false;
break;
}
case SDLK_UP:
{
message.up = false;
break;
}
case SDLK_DOWN:
{
message.down = false;
break;
}
}
break;
default:
break;
}
}
sendto(udpSocket, &message, sizeof(struct clientInput), 0, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
recvfrom(udpSocket, state, sizeof(struct gameState), 0, NULL, NULL);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// Clear the screen, filling it with black: // Clear the screen, filling it with black:
@ -102,7 +162,7 @@ void * graphicsThreadHandler(void * parameters)
{ {
if (state->clients[index].registered == true) if (state->clients[index].registered == true)
{ {
DrawCircle(renderer, (int)state->clients[index].yPosition, (int)state->clients[index].xPosition, DrawCircle(renderer, (long)(state->clients[index].xPosition), (long)(state->clients[index].yPosition),
10); 10);
} }
} }
@ -167,22 +227,22 @@ int main(int argc, char ** argv)
printf("%-7s | %u\n", messageStrings[currentMessage.type], currentMessage.content); printf("%-7s | %u\n", messageStrings[currentMessage.type], currentMessage.content);
switch (currentMessage.type) switch (currentMessage.type)
{ {
case 1: case 1:
{ {
// We've been told to disconnect: // We've been told to disconnect:
shutdown(serverSocket, SHUT_RDWR); shutdown(serverSocket, SHUT_RDWR);
serverSocket = 0; serverSocket = 0;
continueRunning = false; continueRunning = false;
break; break;
} }
case 2: case 2:
{ {
// Pinged, so we now must pong. // Pinged, so we now must pong.
currentMessage.type = 3; currentMessage.type = 3;
currentMessage.content = 0; currentMessage.content = 0;
send(serverSocket, &currentMessage, sizeof(struct CsptMessage), 0); send(serverSocket, &currentMessage, sizeof(struct CsptMessage), 0);
break; break;
} }
} }
} }
else else

View File

@ -1,3 +1,4 @@
#include <stdio.h>
#include "cspt-state.h" #include "cspt-state.h"
void updateInput(struct gameState * state, struct clientInput * message) void updateInput(struct gameState * state, struct clientInput * message)
{ {
@ -16,55 +17,34 @@ void doGameTick(struct gameState * state)
state->timestamp = time(NULL); state->timestamp = time(NULL);
for (int index = 0; index < 16; index++) for (int index = 0; index < 16; index++)
{ {
if (state->clients[index].registered == true) // Calculate acceleration:
if (state->clients[index].leftAcceleration)
{ {
// Calculate acceleration: state->clients[index].xVelocity -= 0.1;
if (state->clients[index].leftAcceleration) }
{ if (state->clients[index].rightAcceleration)
state->clients[index].xVelocity -= 0.5; {
} state->clients[index].xVelocity += 0.1;
if (state->clients[index].rightAcceleration) }
{ if (state->clients[index].upAcceleration)
state->clients[index].xVelocity += 0.5; {
} state->clients[index].yVelocity -= 0.1;
if (state->clients[index].upAcceleration) }
{ if (state->clients[index].downAcceleration)
state->clients[index].yVelocity += 0.5; {
} state->clients[index].yVelocity += 0.1;
if (state->clients[index].downAcceleration) }
{
state->clients[index].yVelocity += 0.5;
}
if (!state->clients[index].leftAcceleration && !state->clients[index].rightAcceleration)
{
state->clients[index].xVelocity *= 0.1;
}
if (!state->clients[index].upAcceleration && !state->clients[index].downAcceleration)
{
state->clients[index].yVelocity *= 0.1;
}
// Clamp speed: // Do movement:
if (state->clients[index].xVelocity > 15) state->clients[index].xPosition += state->clients[index].xVelocity;
{ state->clients[index].yPosition += state->clients[index].yVelocity;
state->clients[index].xVelocity = 15; if(state->clients[index].xPosition > 600 || state->clients[index].xPosition < 0)
} {
if (state->clients[index].xVelocity < -15) state->clients[index].xPosition = 300;
{ }
state->clients[index].xVelocity = -15; if(state->clients[index].yPosition > 600 || state->clients[index].yPosition < 0)
} {
if (state->clients[index].yVelocity > 15) state->clients[index].yPosition = 300;
{
state->clients[index].yVelocity = 15;
}
if (state->clients[index].yVelocity < -15)
{
state->clients[index].yVelocity = -15;
}
// Do movement:
state->clients[index].xPosition += state->clients[index].xVelocity;
state->clients[index].yPosition += state->clients[index].yVelocity;
} }
} }
} }

View File

@ -51,26 +51,38 @@ void * networkThreadHandler(void * arguments)
printf("Started network thread.\n"); printf("Started network thread.\n");
socklen_t test = sizeof(clientAddress); socklen_t test = sizeof(clientAddress);
int returnvalue = 0;
bzero(arguments, sizeof(struct gameState));
while (true) while (true)
{ {
recvfrom(udpSocket, &message, sizeof(struct clientInput), MSG_WAITALL, (struct sockaddr *)&clientAddress, &test); recvfrom(udpSocket, &message, sizeof(struct clientInput), 0, (struct sockaddr *)&clientAddress, &test);
sendto(udpSocket, arguments, sizeof(struct gameState), MSG_CONFIRM, returnvalue = sendto(udpSocket, arguments, sizeof(struct gameState), 0,
(struct sockaddr *)&clientAddress, (socklen_t)sizeof(struct sockaddr_in)); (struct sockaddr *)&clientAddress, (socklen_t)sizeof(struct sockaddr_in));
updateInput(arguments, &message); if(returnvalue > 0)
doGameTick(arguments); {
updateInput(arguments, &message);
}
bzero(&message, sizeof(struct clientInput)); bzero(&message, sizeof(struct clientInput));
} }
return NULL; return NULL;
} }
void * gameThreadHandler(void * arguments)
{
while(true)
{
doGameTick(arguments);
usleep(9000);
}
}
int main(int argc, char ** argv) int main(int argc, char ** argv)
{ {
int returnValue = 0; int returnValue = 0;
int masterSocket = 0; int masterSocket = 0;
int clientSockets[16]; int clientSockets[16];
fd_set connectedClients; fd_set connectedClients;
pthread_t networkThread; pthread_t networkThread, gameThread;
struct gameState currentState; struct gameState currentState;
struct CsptMessage currentMessage; struct CsptMessage currentMessage;
struct connectionStatus clientStatus[16]; struct connectionStatus clientStatus[16];
@ -83,6 +95,7 @@ int main(int argc, char ** argv)
signal(SIGINT, sigintHandler); signal(SIGINT, sigintHandler);
pthread_create(&networkThread, NULL, networkThreadHandler, &currentState); pthread_create(&networkThread, NULL, networkThreadHandler, &currentState);
pthread_create(&gameThread, NULL, gameThreadHandler, &currentState);
// Setup TCP Master Socket: // Setup TCP Master Socket:
printf("Setting up master socket... "); printf("Setting up master socket... ");
@ -206,6 +219,10 @@ int main(int argc, char ** argv)
{ {
currentMessage.type = 0; currentMessage.type = 0;
currentState.clients[index].registered = true; currentState.clients[index].registered = true;
currentState.clients[index].xPosition = 300;
currentState.clients[index].yPosition = 300;
currentState.clients[index].xVelocity = 0;
currentState.clients[index].yVelocity = 0;
currentMessage.content = (uint8_t)index; currentMessage.content = (uint8_t)index;
send(clientSockets[index], &currentMessage, sizeof(struct CsptMessage), 0); send(clientSockets[index], &currentMessage, sizeof(struct CsptMessage), 0);
break; break;
@ -280,3 +297,4 @@ int main(int argc, char ** argv)
shutdown(masterSocket, SHUT_RDWR); shutdown(masterSocket, SHUT_RDWR);
return 0; return 0;
} }