Moved server / client checking above window initialization
This commit is contained in:
48
main.cpp
48
main.cpp
@@ -60,15 +60,50 @@ void check_server_client(int argc, char** argv, Server* server, Client* client)
|
|||||||
throw EXCEPT_TOOFEWARGS;
|
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. */
|
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]));
|
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;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
/* 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) {
|
||||||
|
if (e == EXCEPT_TOOFEWARGS) {
|
||||||
|
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 */
|
/* Initialize window and other variables */
|
||||||
SetTraceLogLevel(LOG_NONE);
|
SetTraceLogLevel(LOG_NONE);
|
||||||
raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong");
|
raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong");
|
||||||
@@ -79,17 +114,6 @@ 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;
|
|
||||||
|
|
||||||
try {
|
|
||||||
check_server_client(argc, argv, &server, &client);
|
|
||||||
} catch(int e) {
|
|
||||||
if (e == EXCEPT_TOOFEWARGS) {
|
|
||||||
std::cout << "Started in client mode, but no address was specified." << std::endl;
|
|
||||||
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);
|
||||||
|
Reference in New Issue
Block a user