diff --git a/main.cpp b/main.cpp index bef06a8..8eee786 100644 --- a/main.cpp +++ b/main.cpp @@ -12,6 +12,7 @@ #include "includes/client.hpp" #include "includes/server.hpp" #include "includes/exception_consts.hpp" +#include "includes/serialization.h" /* Global variables used to instantiate structs */ const int WIDTH = 1500; @@ -203,7 +204,11 @@ int main(int argc, char** argv) { std::istringstream response_stream; /* Vector to store peer paddle position */ raylib::Vector2 peer_pos; - + + /* Byte array to hold the result of serializing a struct (in order to send it through a socket) */ + Serial_Data to_send_data; + std::string to_send_string; + /* Instantiate Paddle and Ball objects */ Paddle pad1 = Paddle(10, (HEIGHT / 2) - (RECT_H / 2), RECT_W, RECT_H); Paddle pad2 = Paddle(window.GetWidth() - RECT_W - 10, (HEIGHT / 2) - (RECT_H / 2), RECT_W, RECT_H); @@ -237,15 +242,18 @@ int main(int argc, char** argv) { } if (game_started) { - - /* Make the players send their position to the other player (The server provides authoritative information on the position of the ball. - The 'P' here indicates that this line provides paddle information. */ + /* Serialize the data that we need to send, and then send it to the peer paddle */ if (type.mode == M_SERVER) { - type.netsock->sendAll("P" + std::to_string(pad1.getRect().x) + " " + std::to_string(pad1.getRect().y)); + /* Serial_create_data creates a Serial_Data struct from our values, and Serial_serialize serializes it. + The 'sendAll' function accepts an std::string, so the byte array (uint8_t *) has to be cast to (char *), then converted to an std::string */ + to_send_data = Serial_create_data(pad1.getRect().x, pad1.getRect().y, ball.pos.x, ball.pos.y); } if (type.mode == M_CLIENT) { - type.netsock->sendAll("P" + std::to_string(pad2.getRect().x) + " " + std::to_string(pad2.getRect().y)); + /* The _server_ is the authoritative peer for the ball position, so the client sends (0, 0) as the ball position instead of actually sending a position */ + to_send_data = Serial_create_data(pad2.getRect().x, pad2.getRect().y, 0, 0); } + to_send_string = std::string((char *)Serial_serialize(to_send_data)); + type.netsock->sendAll(to_send_string); /* Create a stream from the response of the server, and use that to create the vector of peer position */ response = type.netsock->recvAll(); @@ -255,6 +263,7 @@ int main(int argc, char** argv) { response_stream >> peer_pos.x >> peer_pos.y; std::cout << "X position of peer is: " << peer_pos.x << " Y position is: " << peer_pos.y << std::endl; } + /* Since I have already received the data, I need to write code to handle it, in case it isn't 'P' */ /* When updating the paddle positions, update the peer paddle's positions based on the vector set earlier */