Compare commits
2 Commits
26999a1145
...
c2bedb0601
Author | SHA1 | Date | |
---|---|---|---|
c2bedb0601 | |||
77a147e08f |
51
main.cpp
51
main.cpp
@@ -49,6 +49,24 @@ const float BASE_BOUNCE_RAD = (BASE_BOUNCE_DEG / 180.0) * M_PI;
|
|||||||
const float BASE_SPEED_COMPONENTS = 15;
|
const float BASE_SPEED_COMPONENTS = 15;
|
||||||
const float BASE_SPEED = sqrt(powf(BASE_SPEED_COMPONENTS, 2) * 2);
|
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"
|
||||||
|
"\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"
|
||||||
|
"\n"
|
||||||
|
"-C: Client mode. Connects to a server, using the provided connection code.\n"
|
||||||
|
"\n"
|
||||||
|
"If no mode is specified, single player mode is used as default.\n"
|
||||||
|
"\n"
|
||||||
|
"CONTROLS:"
|
||||||
|
"\'W\' and \'S\' control left paddle (AKA client paddle)\n"
|
||||||
|
"Up and Down arrow keys control right paddle (AKA server paddle)\n";
|
||||||
|
|
||||||
|
|
||||||
/* Simple function to return 1 if a value is positive, and -1 if it is negative */
|
/* Simple function to return 1 if a value is positive, and -1 if it is negative */
|
||||||
int signum(int num) {
|
int signum(int num) {
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
@@ -77,11 +95,23 @@ raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Checks the number and type of the command-line arguments. Throws an exception
|
/* Checks the number and type of the command-line arguments. Throws an exception
|
||||||
if the args are invalid. */
|
if the args are invalid. DOES NOT PROCESS VALID ARGUMENTS. */
|
||||||
void check_num_args(int argc, char** argv) {
|
void check_num_args(int argc, char** argv) {
|
||||||
|
if (argc > 3) {
|
||||||
|
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]) == "-C" && 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.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This function checks the command-line arguments passed to the program.
|
/* This function checks the command-line arguments passed to the program.
|
||||||
@@ -188,6 +218,19 @@ GameType check_server_client(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
/* Check the number of command-line arguments */
|
||||||
|
try {
|
||||||
|
check_num_args(argc, argv);
|
||||||
|
} catch (std::invalid_argument& inv) {
|
||||||
|
std::cout << inv.what() << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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. */
|
||||||
|
|
||||||
/* Check if game was started in server or client mode, and set appropriate variables */
|
/* 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
|
/* GameType struct, to define whether the game is in single or multi-player mode, and
|
||||||
|
1
todo.txt
1
todo.txt
@@ -9,3 +9,4 @@
|
|||||||
9. Allow the user to specify which paddle they want to control, in multi-player mode.
|
9. Allow the user to specify which paddle they want to control, in multi-player mode.
|
||||||
10. Try to make the ball go between screens.
|
10. Try to make the ball go between screens.
|
||||||
11. Change the networking code, so that a single server can connect two clients with each other. The server should provide player 1 with a code, and player 2 can connect with player 1 using that code (essentially like a room).
|
11. Change the networking code, so that a single server can connect two clients with each other. The server should provide player 1 with a code, and player 2 can connect with player 1 using that code (essentially like a room).
|
||||||
|
12. Add a --help option, that displays information about the game and how to run it.
|
||||||
|
Reference in New Issue
Block a user