Compare commits

...

4 Commits

Author SHA1 Message Date
8848f0ff8c Removed debug print statements, cleared background before drawing to screen, and added client GUI implementation
I removed a print statement that printed out every position of the ball, because it was no longer necessary.
I also added code to clear the background before drawing to the screen at the start of the game, to remove
any lingering un-erased objects. Finally, and most substantially, I finished the initial implementation of
the client-side GUI. The client should now be able to specify a connect code through the GUI, and connect
to the appropriate server.
2024-03-08 23:35:18 -05:00
07ac3f9166 Filled out implementation of check_client function 2024-03-08 23:34:49 -05:00
9e0990156e Removed unnecessary print message, and added timer.c to source files 2024-03-08 23:33:54 -05:00
1423cc19a0 Split timer into header and implementation file 2024-03-08 23:33:24 -05:00
6 changed files with 100 additions and 32 deletions

View File

@@ -6,17 +6,17 @@
#include "includes/check_input.hpp" #include "includes/check_input.hpp"
#include "includes/raygui/raygui.h" #include "includes/raygui/raygui.h"
#include "includes/exception_consts.hpp" #include "includes/exception_consts.hpp"
#include "includes/timer.h"
/* Display the given text, centered on the screen, as a label */ /* Display the given text, centered on the screen, as a label */
void display_text_centered(std::string to_disp) { void display_text_centered(std::string to_disp) {
const char* to_disp_cstr = to_disp.c_str(); const char* to_disp_cstr = to_disp.c_str();
Vector2 label_size = MeasureTextEx(GetFontDefault(), to_disp_cstr, GuiGetStyle(DEFAULT, TEXT_SIZE), GuiGetStyle(DEFAULT, TEXT_SPACING)); Vector2 label_size = MeasureTextEx(GetFontDefault(), to_disp_cstr, GuiGetStyle(DEFAULT, TEXT_SIZE)+1, GuiGetStyle(DEFAULT, TEXT_SPACING)+1); // The '+1' is there to account for any rounding errors
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
GuiLabel(Rectangle{(GetScreenWidth()/2) - (label_size.x/2), (GetScreenHeight()/2) - (label_size.y/2), label_size.x, label_size.y}, to_disp_cstr); GuiLabel(Rectangle{(GetScreenWidth()/2) - (label_size.x/2), (GetScreenHeight()/2) - (label_size.y/2), label_size.x, label_size.y}, to_disp_cstr);
EndDrawing(); EndDrawing();
return; return;
} }
@@ -34,24 +34,56 @@ GameType check_server(char* ip_text, char* port_text) {
} }
std::string code = connect_code::encode(addr, std::to_string(port)); std::string code = connect_code::encode(addr, std::to_string(port));
display_text_centered("Your code is " + code);
/* Create server socket and wait for client to connect */ /* Create server socket and wait for client to connect */
Server* server = new Server(4, ES_UDP, addr.data(), port); Server* server = new Server(4, ES_UDP, addr.data(), port);
server->create_socket(); server->create_socket();
std::cout << "Waiting for connection..." << std::endl; display_text_centered("Your code is " + code + "\nWaiting for connection...");
std::string response = ""; std::string response = "";
char* temp_response = NULL; char* temp_response = NULL;
/* Wait for the right client to connect. Since recvAll returns a char*, we need to create a temporary variable to check for NULL. */ /* Wait for the client to connect. Since recvAll returns a char*, we need to create a temporary variable to check for NULL.
TODO - Check that the client actually sends 'GG'. */
do { do {
temp_response = server->recvAll(); temp_response = server->recvAll();
} while (temp_response == NULL); } while (temp_response == NULL);
response = std::string(temp_response); response = std::string(temp_response);
std::cout << "Connection received from " << server->get_peer_addr() << std::endl;
server->sendAll("U2"); server->sendAll("U2");
display_text_centered("Connection received from " + server->get_peer_addr());
Timer timer = timer_init(3);
while (!timer_done(timer)); // Wait for five seconds
type.mode = M_SERVER; type.mode = M_SERVER;
type.netsock = server; type.netsock = server;
return type; return type;
} }
GameType check_client(char* code_text) {
GameType type;
std::vector<std::string> addr_port;
std::string connect_code = std::string(code_text); /* The connect code is a special string, that contains the server address and port. It is given by the server. */
try {
addr_port = connect_code::decode(connect_code);
Client* client = new Client(4, ES_UDP, addr_port[0].data(), std::stoi(addr_port[1]));
client->create_socket();
/* Send a specific message to the server, and wait for the appropriate response, to know that the server is ready */
client->sendAll("GG");
// display_text_centered("Connecting...");
std::string msg_from_server = client->recvAll();
if (msg_from_server == "U2") {
display_text_centered("Connection made");
Timer timer = timer_init(3);
while (!timer_done(timer));
} else {
throw EXCEPT_WRONGRESPONSE;
}
type.mode = M_CLIENT;
type.netsock = client;
return type;
} catch (int e) {
throw;
} catch (std::exception& e) {
throw;
}
}

View File

@@ -17,6 +17,7 @@ typedef struct {
/* This function checks the IP address and port passed to it, and returns a struct, /* This function checks the IP address and port passed to it, and returns a struct,
that contains information about the game mode, and contains the server socket. that contains information about the game mode, and contains the server socket.
It assumes that both ip_text and port_text are non-null
TODO - Add better error checking. */ TODO - Add better error checking. */
GameType check_server(char* ip_text, char* port_text); GameType check_server(char* ip_text, char* port_text);

View File

@@ -3,33 +3,26 @@ extern "C" {
#endif #endif
#ifndef _TIMER_H #ifndef _TIMER_H
#define _TIMER_H #define _TIMER_H
/* This file defines a simple timer struct, and methods to initialize it,
and keep track of time elapsed. #include <stdbool.h>
/* This file defines a simple timer Types, and declares functions to initialize it,
and keep track of time elapsed. The actual definition of the Timer struct is in timer.c
It was copied from https://github.com/raysan5/raylib/wiki/Frequently-Asked-Questions#how-do-i-make-a-timer */ It was copied from https://github.com/raysan5/raylib/wiki/Frequently-Asked-Questions#how-do-i-make-a-timer */
typedef struct Timer { typedef struct Timer_s {
double start_time; // Start time (seconds) double start_time; // Start time (seconds)
double lifetime; // Lifetime (seconds) double lifetime; // Lifetime (seconds)
} Timer; } Timer;
Timer timer_init(double lifetime_secs) /* Starts a timer for given number of seconds */
{ Timer timer_init(double lifetime_secs);
Timer timer;
timer.start_time = GetTime();
timer.lifetime = lifetime_secs;
return timer; /* Returns true when timer finishes, false if not */
} bool timer_done(Timer timer);
bool timer_done(Timer timer) /* Returns amount of time elapsed since start of timer */
{ double timer_get_elapsed(Timer timer);
return GetTime() - timer.start_time >= timer.lifetime;
}
double timer_get_elapsed(Timer timer)
{
return GetTime() - timer.start_time;
}
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -152,7 +152,8 @@ GameType check_server_client(int argc, char** argv) {
std::cout << "Waiting for connection..." << std::endl; std::cout << "Waiting for connection..." << std::endl;
std::string response = ""; std::string response = "";
char* temp_response = NULL; char* temp_response = NULL;
/* Wait for the right client to connect. Since recvAll returns a char*, we need to create a temporary variable to check for NULL. */ /* Wait for the right client to connect. Since recvAll returns a char*, we need to create a temporary variable to check for NULL.
TODO - Check that the client actually sends 'GG'. */
do { do {
temp_response = server->recvAll(); temp_response = server->recvAll();
} while (temp_response == NULL); } while (temp_response == NULL);
@@ -278,7 +279,7 @@ int main(int argc, char** argv) {
int port_label_x_size = MeasureTextEx(GetFontDefault(), port_label, font_size, font_spacing).x; // Custom size for port label, because it's long int port_label_x_size = MeasureTextEx(GetFontDefault(), port_label, font_size, font_spacing).x; // Custom size for port label, because it's long
bool editing_ip = false; // Indicates whether the IP address text box is being edited bool editing_ip = false; // Indicates whether the IP address text box is being edited
bool editing_port = false; // Indicates whether the port text box is being edited bool editing_port = false; // Indicates whether the port text box is being edited
while (button_pressed == false) { while (button_pressed == false || ((strlen(ip_text) == 0) || (strlen(port_text) == 0))) {
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
/* Label and text box for IP address */ /* Label and text box for IP address */
@@ -306,6 +307,29 @@ int main(int argc, char** argv) {
free(ip_text); free(ip_text);
free(port_text); free(port_text);
} }
if (selected_item == M_CLIENT) {
button_pressed = false; // Whether submit button is pressed
char* code_text = (char *)calloc(100, sizeof(char)); // Holds the connect code
const char* code_label = "Enter code:";
bool editing_code = false; // Indicates whether the port text box is being edited
while (button_pressed == false || ((strlen(code_text) == 0))) {
BeginDrawing();
ClearBackground(BLACK);
/* Label and text box for IP address */
GuiLabel(Rectangle{(WIDTH/2)-(label_size.x/2), (HEIGHT/2) - (HEIGHT/6) - label_size.y - 10, label_size.x, label_size.y}, code_label);
if (GuiTextBox(Rectangle{(WIDTH/2) - (box_size.x/2), (HEIGHT/2) - (HEIGHT/6), box_size.x, box_size.y}, code_text, 100, editing_code)) {
editing_code = !editing_code;
}
button_pressed = GuiButton(Rectangle{(WIDTH/2) - (box_size.x/2), (HEIGHT/2) + (HEIGHT/6), box_size.x, box_size.y}, "Connect");
EndDrawing();
}
type = check_client(code_text);
free(code_text);
}
} }
/* Variable to store the response given by the other player */ /* Variable to store the response given by the other player */
@@ -324,6 +348,7 @@ int main(int argc, char** argv) {
Ball ball = Ball(window.GetWidth()/2, window.GetHeight()/2, CIRC_RAD, BASE_SPEED, 0); Ball ball = Ball(window.GetWidth()/2, window.GetHeight()/2, CIRC_RAD, BASE_SPEED, 0);
window.BeginDrawing(); window.BeginDrawing();
window.ClearBackground(BLACK);
pad1.draw(); pad1.draw();
pad2.draw(); pad2.draw();
ball.draw(); ball.draw();
@@ -372,7 +397,6 @@ int main(int argc, char** argv) {
uint8_t* response_array = (uint8_t *)(type.netsock->recvAll()); uint8_t* response_array = (uint8_t *)(type.netsock->recvAll());
if (response_array != NULL) { if (response_array != NULL) {
response_data = Serial_deserialize(response_array); response_data = Serial_deserialize(response_array);
std::cout << response_data.pad_x << "\t" << response_data.pad_y << "\t" << response_data.ball_x << "\t" << response_data.ball_y << std::endl;
} else { } else {
/* If the response is NULL, that means it timed-out. In this case, there's no value to print */ /* If the response is NULL, that means it timed-out. In this case, there's no value to print */
std::cout << "NOTHING RECEIVED" << std::endl; std::cout << "NOTHING RECEIVED" << std::endl;

View File

@@ -7,7 +7,6 @@ cmake = import('cmake')
if get_option('default_library') == 'shared' if get_option('default_library') == 'shared'
raylib = dependency('raylib', required: false) # Try to find dependency with pkg-config raylib = dependency('raylib', required: false) # Try to find dependency with pkg-config
if not raylib.found() if not raylib.found()
message('===========+RAYLIB NOT FOUND===============')
raylib = compiler.find_library('raylib', has_headers: ['raylib.h', 'raymath.h'], required: false) # Try to manually search for the dependency raylib = compiler.find_library('raylib', has_headers: ['raylib.h', 'raymath.h'], required: false) # Try to manually search for the dependency
endif endif
if not raylib.found() if not raylib.found()
@@ -35,6 +34,6 @@ endif
executable('pong', executable('pong',
'main.cpp', 'easysock.cpp', 'sock.cpp','paddle.cpp', 'ball.cpp', 'numeric_base.cpp', 'connect_code.cpp', 'server.cpp', 'client.cpp', 'check_input.cpp', 'main.cpp', 'easysock.cpp', 'sock.cpp','paddle.cpp', 'ball.cpp', 'numeric_base.cpp', 'connect_code.cpp', 'server.cpp', 'client.cpp', 'check_input.cpp',
'serialization.c', 'serialization.c', 'timer.c',
dependencies: [raylib, ws2_dep, winmm] dependencies: [raylib, ws2_dep, winmm]
) )

19
timer.c Normal file
View File

@@ -0,0 +1,19 @@
#include <stdbool.h>
#include "includes/timer.h"
#include "includes/raygui/raygui.h"
Timer timer_init(double lifetime_secs) {
Timer timer;
timer.start_time = GetTime();
timer.lifetime = lifetime_secs;
return timer;
}
bool timer_done(Timer timer) {
return GetTime() - timer.start_time >= timer.lifetime;
}
double timer_get_elapsed(Timer timer) {
return GetTime() - timer.start_time;
}