diff --git a/main.cpp b/main.cpp index 024bfe4..761f2b9 100644 --- a/main.cpp +++ b/main.cpp @@ -32,7 +32,7 @@ #include "includes/server.hpp" #include "includes/exception_consts.hpp" #include "includes/check_input.hpp" -#include "includes/raygui_helpers.hpp" +#include "includes/display_text.hpp" #include "includes/easysock.h" #include "includes/serialization.h" #include "includes/timer.h" @@ -52,11 +52,11 @@ const float BASE_SPEED = sqrt(powf(BASE_SPEED_COMPONENTS, 2) * 2); std::string HELP_TEXT = "\nnetpong - A networked pong game for the internet era.\n" "\n" "Usage: \n" - "netpong [MODE] [ADDRESS|CODE]\n" + "netpong [MODE] [ADDRESS PORT]|[CODE]\n" "\n" "MODE: \n" "-S : Server mode. Starts a server to allow the other player to connect.\n" - "IP address must be specified. Port is optional, 6500 is used as default.\n" + "IP address and port must be specified.\n" "\n" "-C: Client mode. Connects to a server, using the provided connection code.\n" "\n" @@ -97,18 +97,24 @@ raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) { /* Checks the number and type of the command-line arguments. Throws an exception if the args are invalid. DOES NOT PROCESS VALID ARGUMENTS. */ void check_num_args(int argc, char** argv) { - if (argc > 3) { + if (argc > 4) { throw std::invalid_argument("ARGUMENT ERROR: Too many arguments. To view syntax, use -h or --help."); } if (argc > 1) { // Either server or client mode - if (std::string(argv[1]) == "-S" && argc < 3) { // Server mode but no address - throw std::invalid_argument("ARGUMENT ERROR: Server mode specified without any address."); + if (std::string(argv[1]) == "-S") { + if (argc < 4) { // Server mode but no address and/or port + throw std::invalid_argument("ARGUMENT ERROR: Server mode specified without any address or port."); + } } - if (std::string(argv[1]) == "-C" && argc < 3) { // Client mode but no code - throw std::invalid_argument("ARGUMENT ERRROR: Client mode specified without any code."); + else if (std::string(argv[1]) == "-C") { + if (argc < 3) { // Client mode but no code + throw std::invalid_argument("ARGUMENT ERRROR: Client mode specified without any code."); + } } - if (std::string(argv[1]) == "-h" || std::string(argv[1]) == "--help") { - throw new std::invalid_argument(HELP_TEXT); // I am abusing the exception mechanism here, so that I can ensure that the caller quits the program. + else if (std::string(argv[1]) == "-h" || std::string(argv[1]) == "--help") { + throw std::invalid_argument(HELP_TEXT); // I am abusing the exception mechanism here, so that I can ensure that the caller quits the program after printing the help message. + } else { + throw std::invalid_argument("Unrecognized argument."); } } return; @@ -218,7 +224,8 @@ GameType check_server_client(int argc, char** argv) { } int main(int argc, char** argv) { - /* Check the number of command-line arguments */ + /* Check the number and validity of command-line arguments. Invalid arguments + will throw an exception. */ try { check_num_args(argc, argv); } catch (std::invalid_argument& inv) { @@ -227,43 +234,43 @@ int main(int argc, char** argv) { } /* From here on, we assume that: - a. The program was started in single player mode, OR - b. The program was started in server mode, and an address was given, OR - c. The program was started in client mode, and a code was given. */ + a. The program was started with no arguments (User is prompted in GUI), OR + b. The program was started in server mode, and an additional was given, OR + c. The program was started in client mode, and an additional argument was given. */ - /* Check if game was started in server or client mode, and set appropriate variables */ - /* GameType struct, to define whether the game is in single or multi-player mode, and to hold the appropriate socket */ GameType type; - try { - type = check_server_client(argc, argv); - } 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." << std::endl; - return -2; - } - if (e == EXCEPT_INVALIDIP) { - std::cout << "Invalid IP address provided." << std::endl; - return -5; - } - if (e == EXCEPT_WRONGRESPONSE) { - std::cout << "The server didn't respond with the correct message. Are you sure you have used the right server?" << std::endl; - return -6; - } - else { - std::cout << strerror(e) << std::endl; - return -7; - } - } catch(std::invalid_argument& inv) { + /* Check if game was started in server or client mode, and call the appropriate function to process the arguments. + If game was started in single-player mode (i.e. with no arguments), then the user is prompted in the GUI. */ + try { // I put this try-catch block outside the if-statement because the exception handling is the same for both client and server. + if (argc > 1) { // Server or client mode + if (std::string(argv[1]) == "-S") { // Server mode + type = check_server(argv[2], argv[3], IF_CLI); + } + if (std::string(argv[1]) == "-C") { // Client mode + type = check_client(argv[2], IF_CLI); + } + } + } catch (std::invalid_argument& inv) { std::cout << inv.what() << std::endl; - return -8; - } + } catch (int err) { + std::cout << strerror(err) << std::endl; + } + + //try { + //type = check_server_client(argc, argv); + //} catch(int e) { + + //else { + //std::cout << strerror(e) << std::endl; + //return -7; + //} + //} catch(std::invalid_argument& inv) { + //std::cout << inv.what() << std::endl; + //return -8; + //} /* Initialize window and other variables */ SetTraceLogLevel(LOG_NONE); @@ -371,14 +378,14 @@ int main(int argc, char** argv) { } try { - type = check_server(ip_text, port_text); + type = check_server(ip_text, port_text, IF_GUI); } catch (int e) { - display_and_exit(std::string(std::strerror(e)) + "\nClosing game...", 2); // The server constructor throws the errno if it cannot create a socket + display_and_exit_raygui(std::string(std::strerror(e)) + "\nClosing game...", 2); // The server constructor throws the errno if it cannot create a socket free(ip_text); free(port_text); return -1; } catch (std::invalid_argument& inv) { - display_and_exit(std::string(inv.what()) + "\nClosing game...", 2); + display_and_exit_raygui(std::string(inv.what()) + "\nClosing game...", 2); free(ip_text); free(port_text); return -1; @@ -411,12 +418,12 @@ int main(int argc, char** argv) { EndDrawing(); } try { - type = check_client(code_text); + type = check_client(code_text, IF_GUI); } catch (int e) { - display_and_exit(std::string(std::strerror(e)) + "\nClosing game...", 2); // The client constructor throws the errno if it cannot create a socket + display_and_exit_raygui(std::string(std::strerror(e)) + "\nClosing game...", 2); // The client constructor throws the errno if it cannot create a socket return -1; } catch (std::invalid_argument& inv) { - display_and_exit(std::string(inv.what()) + "\nClosing game...", 2); + display_and_exit_raygui(std::string(inv.what()) + "\nClosing game...", 2); return -1; } free(code_text);