Added code to parse command-line flags, to determine whether the game was started in client or server mode, and call relevant functions to encode or decode address / key
This commit is contained in:
44
main.cpp
44
main.cpp
@@ -5,7 +5,9 @@
|
|||||||
#include "paddle.hpp"
|
#include "paddle.hpp"
|
||||||
#include "ball.hpp"
|
#include "ball.hpp"
|
||||||
#include "math-helpers.hpp"
|
#include "math-helpers.hpp"
|
||||||
|
#include "connect-helpers.hpp"
|
||||||
#include "client.hpp"
|
#include "client.hpp"
|
||||||
|
#include "server.hpp"
|
||||||
|
|
||||||
/* Global variables used to instantiate structs */
|
/* Global variables used to instantiate structs */
|
||||||
const int WIDTH = 1500;
|
const int WIDTH = 1500;
|
||||||
@@ -39,6 +41,33 @@ raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
|
|||||||
return raylib::Vector2(new_x_vel, new_y_vel);
|
return raylib::Vector2(new_x_vel, new_y_vel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* This function checks the command-line arguments passed to the program.
|
||||||
|
It then decides whether the game is in Server or Client mode (or neither), and
|
||||||
|
instantiates the appropriate object. The (uninitialized) objects are passed to the
|
||||||
|
function as pointers. */
|
||||||
|
|
||||||
|
void check_server_client(int argc, char** argv, Server* server, Client* client) {
|
||||||
|
std::string connect_code;
|
||||||
|
std::vector<std::string> addr_port; /* Vector to store (IPv4) address and port */
|
||||||
|
|
||||||
|
if (argc < 2) { /* Game was not started in client or server mode */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* GAME STARTED IN CLIENT MODE */
|
||||||
|
if (strcmp(argv[1],"-C") == 0) {
|
||||||
|
if (argc < 3) { /* No address was provided */
|
||||||
|
throw EXCEPT_TOOFEWARGS;
|
||||||
|
}
|
||||||
|
connect_code = std::string(argv[2]); /* The connect code is a special string, that contains the server address and port. It is given by the server. */
|
||||||
|
addr_port = code::decode(connect_code);
|
||||||
|
client = new Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
/* Initialize window and other variables */
|
/* Initialize window and other variables */
|
||||||
SetTraceLogLevel(LOG_NONE);
|
SetTraceLogLevel(LOG_NONE);
|
||||||
@@ -50,17 +79,18 @@ int main(int argc, char** argv) {
|
|||||||
bool game_started = false;
|
bool game_started = false;
|
||||||
srand(std::time(NULL));
|
srand(std::time(NULL));
|
||||||
bool in_server_mode = false;
|
bool in_server_mode = false;
|
||||||
|
Server server;
|
||||||
Client client;
|
Client client;
|
||||||
if (argc > 1 && strcmp(argv[1],"server") == 0) {
|
|
||||||
try {
|
try {
|
||||||
client = Client(4,'T',"127.0.0.1",6500);
|
check_server_client(argc, argv, &server, &client);
|
||||||
in_server_mode = true;
|
} catch(int e) {
|
||||||
}
|
if (e == EXCEPT_TOOFEWARGS) {
|
||||||
catch (int e) {
|
std::cout << "Started in client mode, but no address was specified." << std::endl;
|
||||||
std::cout << "Unable to connect to the given address." << std::endl;
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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);
|
||||||
Paddle pad2 = Paddle(window.GetWidth() - RECT_W - 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);
|
||||||
|
Reference in New Issue
Block a user