Compare commits

...

5 Commits

Author SHA1 Message Date
451dc1f803 Allow user to quit in menu scree; better error handling.
I set up a try-catch to catch the exception thrown by the Server/Client when
it can't create a socket. I also used display_and_exit() to
automatically close the window after the text has been displayed.
2024-03-11 13:19:27 -05:00
6f292699f8 Updated header file to reflect new function 2024-03-11 13:19:07 -05:00
6a40a596c1 Added new function to display text then exit 2024-03-11 13:18:48 -05:00
fc59a7221b Removed unnecessary #include 2024-03-11 13:18:31 -05:00
7f0898c81e Updated TODO 2024-03-11 13:17:18 -05:00
7 changed files with 37 additions and 11 deletions

View File

@@ -1,6 +1,5 @@
#include <fcntl.h>
#include "includes/client.hpp"
#include "includes/exception_consts.hpp"
#include "includes/sock.hpp"
#include "includes/easysock.h"

View File

@@ -6,4 +6,6 @@
NEEDS RAYGUI LIBRARY. */
void display_text_centered(std::string to_disp);
/* Display the given string, and exit the game after 'time' seconds. */
void display_and_exit(std::string to_disp, int time);
#endif

View File

@@ -17,6 +17,7 @@
#include <cmath>
#include <cstring>
#include <ctime>
#include <cerrno>
#include <sstream>
#include "includes/raylib-cpp/raylib-cpp.hpp"
@@ -243,6 +244,10 @@ int main(int argc, char** argv) {
bool button_pressed = false; // Indicates whether the submit button has been pressed
while (button_pressed == false) {
if (WindowShouldClose()) {
CloseWindow();
return 0;
}
BeginDrawing();
ClearBackground(BLACK);
GuiLabel(Rectangle{(WIDTH/2)-(label_size.x/2), (HEIGHT/8), label_size.x, label_size.y}, text_to_display); // Label to display text on top
@@ -285,6 +290,11 @@ int main(int argc, char** argv) {
bool editing_ip = false; // Indicates whether the IP address text box is being edited
bool editing_port = false; // Indicates whether the port text box is being edited
while (button_pressed == false || ((strlen(ip_text) == 0) || (strlen(port_text) == 0))) {
if (WindowShouldClose()) {
CloseWindow();
return 0;
}
BeginDrawing();
ClearBackground(BLACK);
/* Label and text box for IP address */
@@ -310,11 +320,11 @@ int main(int argc, char** argv) {
try {
type = check_server(ip_text, port_text);
} 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
return -1;
} catch (std::invalid_argument& inv) {
display_text_centered(std::string(inv.what()) + "\nClosing game...");
Timer timer = timer_init(2); // Wait for two seconds
while (!timer_done(timer));
CloseWindow(); // Close and exit
display_and_exit(std::string(inv.what()) + "\nClosing game...", 2);
return -1;
}
free(ip_text);
@@ -327,6 +337,11 @@ int main(int argc, char** argv) {
const char* code_label = "Enter code:";
bool editing_code = false; // Indicates whether the port text box is being edited
while (button_pressed == false || ((strlen(code_text) == 0))) {
if (WindowShouldClose()) {
CloseWindow();
return 0;
}
BeginDrawing();
ClearBackground(BLACK);
/* Label and text box for IP address */
@@ -339,8 +354,12 @@ int main(int argc, char** argv) {
button_pressed = GuiButton(Rectangle{(WIDTH/2) - (box_size.x/2), (HEIGHT/2) + (HEIGHT/6), box_size.x, box_size.y}, "Connect");
EndDrawing();
}
type = check_client(code_text);
try {
type = check_client(code_text);
} 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
return -1;
}
free(code_text);
}
}

View File

@@ -1,6 +1,6 @@
#include "includes/raygui_helpers.hpp"
#include "includes/raygui/raygui.h"
#include "includes/timer.h"
void display_text_centered(std::string to_disp) {
const char* to_disp_cstr = to_disp.c_str();
Vector2 label_size = MeasureTextEx(GetFontDefault(), to_disp_cstr, GuiGetStyle(DEFAULT, TEXT_SIZE)+1, GuiGetStyle(DEFAULT, TEXT_SPACING)+1); // The '+1' is there to account for any rounding errors
@@ -11,3 +11,10 @@ void display_text_centered(std::string to_disp) {
EndDrawing();
return;
}
void display_and_exit(std::string to_disp, int time) {
display_text_centered(to_disp);
Timer timer = timer_init(time);
while (!timer_done(timer));
return;
}

View File

@@ -7,7 +7,6 @@
#include <fcntl.h>
#include "includes/sock.hpp"
#include "includes/server.hpp"
#include "includes/exception_consts.hpp"
#include "includes/connect_code.hpp"
#include "includes/easysock.h"

View File

@@ -1,7 +1,6 @@
#include <cerrno>
#include <stdexcept>
#include "includes/sock.hpp"
#include "includes/exception_consts.hpp"
#include "includes/easysock.h"
/* Function to create socket. This function doesn't actually create a socket

View File

@@ -4,10 +4,11 @@
5. ----IN PROGRESS---- Figure out how to input game mode and (if applicable) IP address and port through the GUI, instead of the command-line.
6. Clean up / refactor the raygui code in main.cpp, that asks user for game mode. Instead of just having a giant blob of code in main.cpp, maybe split it into a function, or move it to another file. It should be easy to split it into a different function, since none of the functions take any specific parameters. The text box function, for example, only takes in the rectangle coordinates, and the text to display. I can move the code to a function, and then pass in any parameters that I need to pass in (I don't think I need to pass many parameters, though).
7. Allow the user to quit before the game actually starts i.e. while they are inputting the game mode.
8. Add better error checking in check_server and check_client functions in check_input.cpp. e.g. If client fails to connect, display a message in GUI.
8. Add better error checking in check_server and check_client functions in check_input.cpp. e.g. If client fails to connect, display a message.
9. Add 'install' target to Meson, to allow the user to install the game. This should also copy the .so files to the right locations.
10. Allow the user to specify which paddle they want to control, in multi-player mode.
11. Add IPv6 support for the server and client sockets (and everything that goes along with it, such as error handling for IP addresses).
12. Communicate the paddle reset position to the peer, after a round.
13. Test with valgrind.
14. Use the struct to establish a connection, and to start each round (instead of sending strings).
15. Use check_client() and check_server() for CLI invocation as well, and pass a flag that indicataes whether the parameters were entered through GUI or CLI (also probably create a function to handle printing vs. GUI display).