From 705f84cbf15b87b32f06eca6af69093b8223a5f9 Mon Sep 17 00:00:00 2001 From: suve Date: Sun, 17 Dec 2017 14:19:24 +0100 Subject: [PATCH 1/6] Reduce code redundancy in FatalError() --- src/utils.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/utils.c b/src/utils.c index ad5c11d..297ac8b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -214,36 +214,43 @@ SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { al_flip_display(); al_rest(0.6); + + const int offsetx = al_get_display_width(game->display) / 2; + const int offsety = al_get_display_height(game->display) * 0.32; + const int fonth = al_get_font_line_height(game->_priv.font_bsod); + bool done = false; while (!done) { al_set_target_backbuffer(game->display); al_clear_to_color(al_map_rgb(0, 0, 170)); const char* header = game->name; + const int headw = al_get_text_width(game->_priv.font_bsod, header); - al_draw_filled_rectangle(al_get_display_width(game->display) / 2 - al_get_text_width(game->_priv.font_bsod, header) / 2 - 4, (int)(al_get_display_height(game->display) * 0.32), 4 + al_get_display_width(game->display) / 2 + al_get_text_width(game->_priv.font_bsod, header) / 2, (int)(al_get_display_height(game->display) * 0.32) + al_get_font_line_height(game->_priv.font_bsod), al_map_rgb(170, 170, 170)); + al_draw_filled_rectangle(offsetx - headw / 2 - 4, (int)(offsety), 4 + offsetx + headw / 2, (int)(offsety) + fonth, al_map_rgb(170, 170, 170)); - al_draw_text(game->_priv.font_bsod, al_map_rgb(0, 0, 170), al_get_display_width(game->display) / 2, (int)(al_get_display_height(game->display) * 0.32), ALLEGRO_ALIGN_CENTRE, header); + al_draw_text(game->_priv.font_bsod, al_map_rgb(0, 0, 170), offsetx, (int)(offsety), ALLEGRO_ALIGN_CENTRE, header); const char* header2 = "A fatal exception 0xD3RP has occured at 0028:M00F11NZ in GST SD(01) +"; + const int head2w = al_get_text_width(game->_priv.font_bsod, header2); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2, (int)(al_get_display_height(game->display) * 0.32 + 2 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_CENTRE, header2); - al_draw_textf(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2 - al_get_text_width(game->_priv.font_bsod, header2) / 2, (int)(al_get_display_height(game->display) * 0.32 + 3 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_LEFT, "%p and system just doesn't know what went wrong.", game); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + 2 * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, header2); + al_draw_textf(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 3 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "%p and system just doesn't know what went wrong.", game); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2, (int)(al_get_display_height(game->display) * 0.32 + 5 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_CENTRE, text); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + 5 * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, text); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2 - al_get_text_width(game->_priv.font_bsod, header2) / 2, (int)(al_get_display_height(game->display) * 0.32 + 7 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_LEFT, "* Press any key to terminate this error."); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2 - al_get_text_width(game->_priv.font_bsod, header2) / 2, (int)(al_get_display_height(game->display) * 0.32 + 8 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_LEFT, "* Press any key to destroy all muffins in the world."); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2 - al_get_text_width(game->_priv.font_bsod, header2) / 2, (int)(al_get_display_height(game->display) * 0.32 + 9 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_LEFT, "* Just kidding, please press any key anyway."); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 7 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "* Press any key to terminate this error."); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 8 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "* Press any key to destroy all muffins in the world."); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 9 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "* Just kidding, please press any key anyway."); - if (exit) { - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2 - al_get_text_width(game->_priv.font_bsod, header2) / 2, (int)(al_get_display_height(game->display) * 0.32 + 11 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_LEFT, "This is fatal error. My bad."); + if (exit) { + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 11 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "This is fatal error. My bad."); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2, (int)(al_get_display_height(game->display) * 0.32 + 13 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to quit _"); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + 13 * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to quit _"); } else { - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2 - al_get_text_width(game->_priv.font_bsod, header2) / 2, (int)(al_get_display_height(game->display) * 0.32 + 11 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_LEFT, "Anything I can do to help?"); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 11 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "Anything I can do to help?"); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), al_get_display_width(game->display) / 2, (int)(al_get_display_height(game->display) * 0.32 + 13 * al_get_font_line_height(game->_priv.font_bsod) * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to continue _"); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + 13 * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to continue _"); } al_flip_display(); From af53d1378fb068b20c1b14047373e0a1db444ed8 Mon Sep 17 00:00:00 2001 From: suve Date: Sun, 17 Dec 2017 14:58:03 +0100 Subject: [PATCH 2/6] Wrap long messages in FatalError() --- src/utils.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/utils.c b/src/utils.c index 297ac8b..d7faf06 100644 --- a/src/utils.c +++ b/src/utils.c @@ -237,20 +237,39 @@ SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + 2 * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, header2); al_draw_textf(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 3 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "%p and system just doesn't know what went wrong.", game); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + 5 * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, text); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 7 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "* Press any key to terminate this error."); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 8 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "* Press any key to destroy all muffins in the world."); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 9 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "* Just kidding, please press any key anyway."); + const int error_len = strlen(text); + const int error_w = al_get_text_width(game->_priv.font_bsod, text); + const int lines = ceil(error_w / (al_get_display_width(game->display) * 0.8)); + const int letters_per_line = (error_len / lines)+1; + + int row = 5, l = 0; + for(; l < lines; ++l) { + int start = l * letters_per_line; + int end = (l+1) * letters_per_line; + if(end >= sizeof(text)) end = sizeof(text)-1; + + const char save_char = text[end]; + text[end] = '\0'; + + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + row++ * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, text + start); + text[end] = save_char; + } + ++row; + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + row++ * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "* Press any key to terminate this error."); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + row++ * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "* Press any key to destroy all muffins in the world."); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + row++ * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "* Just kidding, please press any key anyway."); + + ++row; if (exit) { al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 11 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "This is fatal error. My bad."); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + 13 * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to quit _"); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + (row+2) * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to quit _"); } else { al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 11 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "Anything I can do to help?"); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + 13 * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to continue _"); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + (row+2) * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to continue _"); } al_flip_display(); From 284875308339c1ecc846e36f310f5756fa185349 Mon Sep 17 00:00:00 2001 From: suve Date: Sun, 17 Dec 2017 15:00:02 +0100 Subject: [PATCH 3/6] Use left-align for the wrapped error messages --- src/utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.c b/src/utils.c index d7faf06..560938f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -216,7 +216,7 @@ SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { const int offsetx = al_get_display_width(game->display) / 2; - const int offsety = al_get_display_height(game->display) * 0.32; + const int offsety = al_get_display_height(game->display) * 0.30; const int fonth = al_get_font_line_height(game->_priv.font_bsod); bool done = false; @@ -252,7 +252,7 @@ SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { const char save_char = text[end]; text[end] = '\0'; - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + row++ * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, text + start); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - (error_w / lines) / 2, (int)(offsety + row++ * fonth * 1.25), ALLEGRO_ALIGN_LEFT, text + start); text[end] = save_char; } ++row; From 18efca478e8b7a49ba8f1492fc74cea0ca879322 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sat, 20 Jan 2018 02:23:17 +0100 Subject: [PATCH 4/6] FatalError: print the error message to stderr Printing to console outputs it to stdout, but only if debug mode is enabled. stderr seems better suited too. --- src/utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.c b/src/utils.c index 560938f..1b34406 100644 --- a/src/utils.c +++ b/src/utils.c @@ -197,7 +197,7 @@ SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { vsnprintf(text, 1024, format, vl); SUPPRESS_END va_end(vl); - PrintConsole(game, text); + fprintf(stderr, "%s", text); // TODO: synchronize with loading thread From e2d9d21f3f66c6e34ba80548a20d161da5a29ec6 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sat, 20 Jan 2018 02:44:33 +0100 Subject: [PATCH 5/6] BSOD: fix formatting and warnings --- src/utils.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/utils.c b/src/utils.c index 1b34406..5f9a211 100644 --- a/src/utils.c +++ b/src/utils.c @@ -214,7 +214,6 @@ SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { al_flip_display(); al_rest(0.6); - const int offsetx = al_get_display_width(game->display) / 2; const int offsety = al_get_display_height(game->display) * 0.30; const int fonth = al_get_font_line_height(game->_priv.font_bsod); @@ -227,9 +226,9 @@ SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { const char* header = game->name; const int headw = al_get_text_width(game->_priv.font_bsod, header); - al_draw_filled_rectangle(offsetx - headw / 2 - 4, (int)(offsety), 4 + offsetx + headw / 2, (int)(offsety) + fonth, al_map_rgb(170, 170, 170)); + al_draw_filled_rectangle(offsetx - headw / 2 - 4, offsety, 4 + offsetx + headw / 2, offsety + fonth, al_map_rgb(170, 170, 170)); - al_draw_text(game->_priv.font_bsod, al_map_rgb(0, 0, 170), offsetx, (int)(offsety), ALLEGRO_ALIGN_CENTRE, header); + al_draw_text(game->_priv.font_bsod, al_map_rgb(0, 0, 170), offsetx, offsety, ALLEGRO_ALIGN_CENTRE, header); const char* header2 = "A fatal exception 0xD3RP has occured at 0028:M00F11NZ in GST SD(01) +"; const int head2w = al_get_text_width(game->_priv.font_bsod, header2); @@ -237,21 +236,22 @@ SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + 2 * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, header2); al_draw_textf(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 3 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "%p and system just doesn't know what went wrong.", game); - const int error_len = strlen(text); const int error_w = al_get_text_width(game->_priv.font_bsod, text); const int lines = ceil(error_w / (al_get_display_width(game->display) * 0.8)); - const int letters_per_line = (error_len / lines)+1; - + const int letters_per_line = (error_len / lines) + 1; + int row = 5, l = 0; - for(; l < lines; ++l) { + for (; l < lines; ++l) { int start = l * letters_per_line; - int end = (l+1) * letters_per_line; - if(end >= sizeof(text)) end = sizeof(text)-1; - + unsigned int end = (l + 1) * letters_per_line; + if (end >= sizeof(text)) { + end = sizeof(text) - 1; + } + const char save_char = text[end]; text[end] = '\0'; - + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - (error_w / lines) / 2, (int)(offsety + row++ * fonth * 1.25), ALLEGRO_ALIGN_LEFT, text + start); text[end] = save_char; } @@ -262,14 +262,14 @@ SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + row++ * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "* Just kidding, please press any key anyway."); ++row; - if (exit) { + if (exit) { al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 11 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "This is fatal error. My bad."); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + (row+2) * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to quit _"); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + (row + 2) * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to quit _"); } else { al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 11 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "Anything I can do to help?"); - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + (row+2) * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to continue _"); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + (row + 2) * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to continue _"); } al_flip_display(); From 0cfef4ddca8b0c2c59a636bbd26e7304033a0a7f Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sat, 20 Jan 2018 02:45:13 +0100 Subject: [PATCH 6/6] BSOD: fix placement of the second-last line --- src/utils.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils.c b/src/utils.c index 5f9a211..4eb6efd 100644 --- a/src/utils.c +++ b/src/utils.c @@ -241,8 +241,8 @@ SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { const int lines = ceil(error_w / (al_get_display_width(game->display) * 0.8)); const int letters_per_line = (error_len / lines) + 1; - int row = 5, l = 0; - for (; l < lines; ++l) { + int row = 5; + for (int l = 0; l < lines; ++l) { int start = l * letters_per_line; unsigned int end = (l + 1) * letters_per_line; if (end >= sizeof(text)) { @@ -263,11 +263,11 @@ SYMBOL_EXPORT void FatalError(struct Game* game, bool exit, char* format, ...) { ++row; if (exit) { - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 11 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "This is fatal error. My bad."); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + row * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "This is fatal error. My bad."); al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + (row + 2) * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to quit _"); } else { - al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + 11 * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "Anything I can do to help?"); + al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx - head2w / 2, (int)(offsety + row * fonth * 1.25), ALLEGRO_ALIGN_LEFT, "Anything I can do to help?"); al_draw_text(game->_priv.font_bsod, al_map_rgb(255, 255, 255), offsetx, (int)(offsety + (row + 2) * fonth * 1.25), ALLEGRO_ALIGN_CENTRE, "Press any key to continue _"); }