Moved server / client checking above window initialization

master
Aadhavan Srinivasan 8 months ago
parent fbea42df25
commit 2ca17a6225

@ -60,28 +60,37 @@ void check_server_client(int argc, char** argv, Server* server, Client* client)
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);
addr_port = connect_code::decode(connect_code);
client = new Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1]));
}
/* GAME STARTED IN SERVER MODE */
else if (strcmp(argv[1],"-S") == 0) {
std::string addr = "127.0.0.1";
uint16_t port;
if (argc < 3) {
std::cout << "No port specified, using 6500..." << std::endl;
port = 6500;
} else {
port = std::stoi(std::string(argv[2]));
}
std::string code = connect_code::encode(addr, std::to_string(port));
std::cout << "Your code is " << code << std::endl;
}
else {
throw EXCEPT_INVALIDARGS;
}
return;
}
int main(int argc, char** argv) {
/* Initialize window and other variables */
SetTraceLogLevel(LOG_NONE);
raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong");
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));
bool in_server_mode = false;
/* Check if game was started in server or client mode, and set appropriate variables */
Server server;
Client client;
try {
check_server_client(argc, argv, &server, &client);
} catch(int e) {
@ -89,8 +98,23 @@ int main(int argc, char** argv) {
std::cout << "Started in client mode, but no address was specified." << std::endl;
return -1;
}
if (e == EXCEPT_INVALIDARGS) {
std::cout << "Invalid argument. Optional arguments are -S for server mode, or -C for client mode." << std::endl;
return -2;
}
}
/* Initialize window and other variables */
SetTraceLogLevel(LOG_NONE);
raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong");
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));
bool in_server_mode = false;
/* 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);

Loading…
Cancel
Save