From 1b1dc4a3a26852e1b554ee350af4875465a1e259 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Wed, 14 Feb 2024 18:32:40 -0500 Subject: [PATCH] Changed include paths to relative paths, and added an agnostic struct. The agnostic struct consists of a 'Mode' enum, and a 'Sock' type. The 'Sock' can be either a Server or Client, depending on the type of game. This allows polymorphism, as I don't have to worry about whether the game is being run in Server or Client mode, and I can call the same methods regardless. --- main.cpp | 73 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/main.cpp b/main.cpp index d1eefdc..8bac0df 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,14 @@ #include #include #include -#include "raylib-cpp/raylib-cpp.hpp" -#include "paddle.hpp" -#include "ball.hpp" -#include "easysock.hpp" -#include "math-helpers.hpp" -#include "connect-helpers.hpp" -#include "client.hpp" -#include "server.hpp" - +#include "includes/raylib-cpp/raylib-cpp.hpp" +#include "includes/paddle.hpp" +#include "includes/ball.hpp" +#include "includes/easysock.hpp" +#include "includes/math-helpers.hpp" +#include "includes/connect-helpers.hpp" +#include "includes/client.hpp" +#include "includes/server.hpp" /* Global variables used to instantiate structs */ const int WIDTH = 1500; const int HEIGHT = 600; @@ -171,8 +170,12 @@ int main(int argc, char** argv) { return -6; } + #ifdef DEBUG + type.mode = M_CLIENT; + #endif + /* Initialize window and other variables */ - SetTraceLogLevel(LOG_NONE); + SetTraceLogLevel(LOG_INFO); raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong"); window.ClearBackground(BLACK); SetTargetFPS(60); @@ -202,45 +205,45 @@ int main(int argc, char** argv) { } if (game_started) { + /* Receive response from the other machine, if the game is in multiplayer mode */ + std::string response = ""; + if (type.mode != M_SINGLE) { + #ifdef DEBUG + response = ""; + #else + std::string response = type.netsock->recvAll(); + #endif + } + + std::string to_send = ""; /* Update paddle velocity */ - /* Left paddle - controlled by client */ + /* Left paddle (controlled by client) */ /* Up motion */ - if (type.mode == M_CLIENT) { - if (IsKeyPressed(KEY_S)) { - type.netsock->sendAll(std::string("D")); - pad1.velocity.y = PADDLE_SPEED; /* Set positive (downward) velocity, since (0,0) is top-left */ - } - } - if (type.mode == M_SERVER) { - if (type.netsock->recvAll() == "U") { - pad1.velocity.y = PADDLE_SPEED; + if (IsKeyPressed(KEY_S) || response == "D1") { + pad1.velocity.y = PADDLE_SPEED; /* Set positive (downward) velocity, since (0,0) is top-left */ + if (type.mode == M_CLIENT) { /* If this machine controls this paddle, Send the key press to the other machine */ + to_send = "D1"; } } /* Down motion */ - if (type.mode == M_CLIENT) { - if (IsKeyPressed(KEY_W)) { - type.netsock->sendAll(std::string("U")); - pad1.velocity.y = (-1) * PADDLE_SPEED; /* Set negative (upward) velocity */ - } - } - if (type.mode == M_SERVER) { - if (type.netsock->recvAll() == "D") { - pad1.velocity.y = (-1) * PADDLE_SPEED; + if (IsKeyPressed(KEY_W) || response == "U1") { + pad1.velocity.y = (-1) * PADDLE_SPEED; /* Set negative (upward) velocity */ + if (type.mode == M_CLIENT) { + to_send = "U1"; } } - - - if (IsKeyReleased(KEY_S) || IsKeyReleased(KEY_W)) { - if (type.mode == M_CLIENT) { - type.netsock->sendAll(std::string("S")); + /* Stop */ + if ((IsKeyReleased(KEY_S) || IsKeyReleased(KEY_W)) || (response == "S1")) { pad1.velocity.y = 0; + if (type.mode == M_CLIENT) { + to_send = "S1"; } } - /* Right paddle */ + /* Right paddle - controlled by server*/ if (IsKeyPressed(KEY_DOWN)) { if (type.mode == M_SERVER) { type.netsock->sendAll(std::string("D"));