Allow user to quit in menu screen; 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.
This commit is contained in:
29
main.cpp
29
main.cpp
@@ -17,6 +17,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <cerrno>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include "includes/raylib-cpp/raylib-cpp.hpp"
|
#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
|
bool button_pressed = false; // Indicates whether the submit button has been pressed
|
||||||
|
|
||||||
while (button_pressed == false) {
|
while (button_pressed == false) {
|
||||||
|
if (WindowShouldClose()) {
|
||||||
|
CloseWindow();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(BLACK);
|
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
|
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_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
|
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))) {
|
while (button_pressed == false || ((strlen(ip_text) == 0) || (strlen(port_text) == 0))) {
|
||||||
|
if (WindowShouldClose()) {
|
||||||
|
CloseWindow();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
/* Label and text box for IP address */
|
/* Label and text box for IP address */
|
||||||
@@ -310,11 +320,11 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
type = check_server(ip_text, port_text);
|
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) {
|
} catch (std::invalid_argument& inv) {
|
||||||
display_text_centered(std::string(inv.what()) + "\nClosing game...");
|
display_and_exit(std::string(inv.what()) + "\nClosing game...", 2);
|
||||||
Timer timer = timer_init(2); // Wait for two seconds
|
|
||||||
while (!timer_done(timer));
|
|
||||||
CloseWindow(); // Close and exit
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
free(ip_text);
|
free(ip_text);
|
||||||
@@ -327,6 +337,11 @@ int main(int argc, char** argv) {
|
|||||||
const char* code_label = "Enter code:";
|
const char* code_label = "Enter code:";
|
||||||
bool editing_code = false; // Indicates whether the port text box is being edited
|
bool editing_code = false; // Indicates whether the port text box is being edited
|
||||||
while (button_pressed == false || ((strlen(code_text) == 0))) {
|
while (button_pressed == false || ((strlen(code_text) == 0))) {
|
||||||
|
if (WindowShouldClose()) {
|
||||||
|
CloseWindow();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
/* Label and text box for IP address */
|
/* 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");
|
button_pressed = GuiButton(Rectangle{(WIDTH/2) - (box_size.x/2), (HEIGHT/2) + (HEIGHT/6), box_size.x, box_size.y}, "Connect");
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
type = check_client(code_text);
|
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);
|
free(code_text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user