From c83b3476205d9354642d899c567d468961b67848 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Wed, 31 Jan 2024 21:01:11 -0500 Subject: [PATCH] Changed include paths, added a cmdline argument ('server') to indicate if the game is networkded or not --- main.cpp | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index 754a81b..2449e92 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,11 @@ #include #include #include -#include -#include "includes/paddle.hpp" -#include "includes/ball.hpp" -#include "includes/math-helpers.hpp" -#include "includes/client.hpp" +#include "raylib-cpp/raylib-cpp.hpp" +#include "paddle.hpp" +#include "ball.hpp" +#include "math-helpers.hpp" +#include "client.hpp" /* Global variables used to instantiate structs */ const int WIDTH = 1500; @@ -39,18 +39,28 @@ raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) { return raylib::Vector2(new_x_vel, new_y_vel); } -int main() { +int main(int argc, char** argv) { /* Initialize window and other variables */ + SetTraceLogLevel(LOG_NONE); raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong"); - SetTraceLogLevel(LOG_INFO); window.ClearBackground(BLACK); SetTargetFPS(60); SetExitKey(KEY_Q); std::string points_str = std::string("0\t\t0"); bool game_started = false; srand(std::time(NULL)); - Client client = Client(4,'T',"127.0.0.1",6500); - + bool in_server_mode = false; + Client client; + if (argc > 1 && strcmp(argv[1],"server") == 0) { + try { + client = Client(4,'T',"127.0.0.1",6500); + in_server_mode = true; + } + catch (int e) { + std::cout << "Unable to connect to the given address." << std::endl; + return -1; + } + } /* 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); @@ -85,15 +95,21 @@ int main() { } if (IsKeyPressed(KEY_UP)) { - client.sendAll(std::string("U")); + if(in_server_mode) { + client.sendAll(std::string("U")); + } pad2.velocity.y = (-1) * PADDLE_SPEED; } if (IsKeyPressed(KEY_DOWN)) { - client.sendAll(std::string("D")); + if (in_server_mode) { + client.sendAll(std::string("D")); + } pad2.velocity.y = PADDLE_SPEED; } if (IsKeyReleased(KEY_UP) || IsKeyReleased(KEY_DOWN)) { - client.sendAll(std::string("S")); + if (in_server_mode) { + client.sendAll(std::string("S")); + } pad2.velocity.y = 0; }