Started working on client-server communication; so far, I can send the position of client to server, and vice-versa

master
Aadhavan Srinivasan 10 months ago
parent 6155cb0463
commit 6ad56fb9ef

@ -2,6 +2,7 @@
#include <cmath> #include <cmath>
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
#include <sstream>
#include "includes/raylib-cpp/raylib-cpp.hpp" #include "includes/raylib-cpp/raylib-cpp.hpp"
#include "includes/paddle.hpp" #include "includes/paddle.hpp"
#include "includes/ball.hpp" #include "includes/ball.hpp"
@ -191,14 +192,17 @@ int main(int argc, char** argv) {
SetTraceLogLevel(LOG_NONE); SetTraceLogLevel(LOG_NONE);
raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong"); raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong");
window.ClearBackground(BLACK); window.ClearBackground(BLACK);
SetTargetFPS(60); SetTargetFPS(5);
SetExitKey(KEY_Q); SetExitKey(KEY_Q);
std::string points_str = std::string("0\t\t0"); std::string points_str = std::string("0\t\t0");
bool game_started = false; bool game_started = false;
srand(std::time(NULL)); srand(std::time(NULL));
/* Variable to store the response given by the other player */ /* Variables to store the response given by the other player */
std::string response; std::string response;
std::istringstream response_stream;
/* Vector to store peer paddle position */
raylib::Vector2 peer_pos;
/* Instantiate Paddle and Ball objects */ /* Instantiate Paddle and Ball objects */
Paddle pad1 = Paddle(10, (HEIGHT / 2) - (RECT_H / 2), RECT_W, RECT_H); Paddle pad1 = Paddle(10, (HEIGHT / 2) - (RECT_H / 2), RECT_W, RECT_H);
@ -233,16 +237,27 @@ int main(int argc, char** argv) {
} }
if (game_started) { if (game_started) {
/* Receive response from the other machine, if the game is in multiplayer mode */
response = ""; /* Make the players send their position to the other player (The server provides authoritative information on the position of the ball.
if (type.mode != M_SINGLE) { The 'P' here indicates that this line provides paddle information. */
#ifdef DEBUG if (type.mode == M_SERVER) {
response = ""; type.netsock->sendAll("P" + std::to_string(pad1.getRect().x) + " " + std::to_string(pad1.getRect().y));
#else }
std::string response = type.netsock->recvAll(); if (type.mode == M_CLIENT) {
#endif type.netsock->sendAll("P" + std::to_string(pad2.getRect().x) + " " + std::to_string(pad2.getRect().y));
} }
/* Create a stream from the response of the server, and use that to create the vector of peer position */
response = type.netsock->recvAll();
if (response[0] == 'P') {
/* Create a stream, excluding the first character */
response_stream = std::istringstream(response.substr(1));
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;
}
/* When updating the paddle positions, update the peer paddle's positions based on the vector set earlier */
std::string to_send = ""; std::string to_send = "";
/* Update paddle velocity */ /* Update paddle velocity */

Loading…
Cancel
Save