Skip to content

Commit 37c204d

Browse files
improve error reporting for game data verification, start new menu buttons
diff --git a/game/src/engine.cpp b/game/src/engine.cpp index 769ae44..6525f55 100644 --- a/game/src/engine.cpp +++ b/game/src/engine.cpp
1 parent de1820d commit 37c204d

File tree

8 files changed

+119
-40
lines changed

8 files changed

+119
-40
lines changed

game/src/engine.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ unsigned sp_player_count = 5, sp_player_ui_count = sp_player_count - 1;
5353
std::array<PlayerSetting, max_legacy_players> sp_players;
5454
ScenarioSettings sp_scn;
5555

56+
MenuState next_menu_state = MenuState::init;
57+
5658
Engine::Engine()
5759
: net(), show_demo(false), show_debug(false), font_scaling(true)
5860
, connection_mode(0), connection_port(32768), connection_host("")
59-
, menu_state(MenuState::init), next_menu_state(MenuState::init)
61+
, menu_state(MenuState::init)
6062
, multiplayer_ready(false), m_show_menubar(false)
6163
, chat_line(), chat(), server()
6264
, tp(2), ui_tasks(), ui_mod_id(), popups(), popups_async()

game/src/engine.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ enum class EngineAsyncTask {
6060

6161
class EngineView;
6262

63+
extern MenuState next_menu_state;
64+
6365
// TODO make engine view that wraps eng and m_eng magic
6466
// TODO split async?
6567
// TODO split ui
@@ -70,7 +72,7 @@ class Engine final {
7072
int connection_mode;
7173
unsigned short connection_port;
7274
char connection_host[256]; // 253 according to https://web.archive.org/web/20190518124533/https://devblogs.microsoft.com/oldnewthing/?p=7873
73-
MenuState menu_state, next_menu_state;
75+
MenuState menu_state;
7476

7577
// multiplayer_host
7678
bool multiplayer_ready;
@@ -220,9 +222,8 @@ class Engine final {
220222
void trigger_async_flags(unsigned f);
221223
void trigger_async_flags(EngineAsyncTask t) { trigger_async_flags((unsigned)t); }
222224

223-
private:
224225
void open_help();
225-
226+
private:
226227
void goto_multiplayer_menu();
227228
void start_multiplayer_game();
228229

game/src/engine/views/engine_view.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void EngineView::trigger_async_flags(EngineAsyncTask t) {
1414
}
1515

1616
void EngineView::goto_menu(MenuState ms) {
17-
eng->next_menu_state = ms;
17+
next_menu_state = ms;
1818
}
1919

2020
void EngineView::play_sfx(SfxId id, int loops) {

game/src/legacy/drs.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ struct DrsHdr final {
2626

2727
DRS::DRS(const std::string &path) : in(path, std::ios_base::binary), items() {
2828
ZoneScoped;
29-
in.exceptions(std::ofstream::failbit | std::ofstream::badbit);
29+
try {
30+
in.exceptions(std::ifstream::failbit | std::ifstream::badbit);
31+
} catch (const std::ifstream::failure &ex) {
32+
throw std::runtime_error(std::string("cannot find \"") + path + "\"");
33+
}
3034

3135
DrsHdr hdr{ 0 };
3236
in.read((char*)&hdr, sizeof(hdr));

game/src/net/netrw.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ unsigned NetPkg::read(const std::string &fmt, netargs &dst, unsigned offset) {
107107
if (!has_mult)
108108
throw std::runtime_error("string has no length");
109109

110-
// FIXME this looks like a bug... shouldn't this be: sz = mult
110+
// NOTE the size of data should be correct, but just in case limit it to mult
111111
unsigned sz = std::min<unsigned>(data.at(offset) << 8 | data.at(offset + 1), mult);
112112

113113
std::string s;

game/src/ui.cpp

Lines changed: 103 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ void Engine::show_multiplayer_diplomacy() {
461461
return;
462462

463463
Assets &a = *assets.get();
464-
ImDrawList *lst = ImGui::GetWindowDrawList();
464+
ImDrawList *lst = ImGui::GetWindowDrawList(); // TODO refactor to bkg
465465

466466
const gfx::ImageRef &rbkg = a.at(io::DrsId::img_dialog0);
467467
ImVec2 pos(ImGui::GetWindowPos());
@@ -543,6 +543,7 @@ void Engine::show_multiplayer_host() {
543543
f.str("Multiplayer game -");
544544
f.sl();
545545

546+
// FIXME make this less ugly
546547
--player_count;
547548
f.scalar(player_count == 1 ? "player" : "players", player_count, 1, 1, MAX_PLAYERS - 1);
548549
++player_count;
@@ -555,6 +556,7 @@ void Engine::show_multiplayer_host() {
555556

556557
player_tbl_y = ImGui::GetCursorPosY();
557558
} else {
559+
// FIXME make this less ugly
558560
--player_count;
559561
f.fmt("Multiplayer game - %u %s", player_count, player_count == 1 ? "player" : "players");
560562
++player_count;
@@ -631,7 +633,7 @@ void UICache::show_editor_menu() {
631633
ImGui::SetCursorPosY(272.0f / 768.0f * vp->WorkSize.y);
632634

633635
if (btn(f, "Create Scenario", TextHalign::center, e->sfx))
634-
e->next_menu_state = MenuState::editor_scenario;
636+
next_menu_state = MenuState::editor_scenario;
635637

636638
ImGui::SetCursorPosY(352.0f / 768.0f * vp->WorkSize.y);
637639

@@ -644,7 +646,7 @@ void UICache::show_editor_menu() {
644646
ImGui::SetCursorPosY(512.0f / 768.0f * vp->WorkSize.y);
645647

646648
if (btn(f, "Cancel", TextHalign::center, e->sfx))
647-
e->next_menu_state = MenuState::start;
649+
next_menu_state = MenuState::start;
648650

649651
ImGui::SetCursorPosX(old_x);
650652
}
@@ -1007,51 +1009,107 @@ void Engine::open_help() {
10071009
open_url("https://github.com/FolkertVanVerseveld/aoe");
10081010
}
10091011

1010-
void Engine::show_start() {
1011-
ZoneScoped;
1012-
ImGuiViewport *vp = ImGui::GetMainViewport();
1013-
ImGui::SetNextWindowPos(vp->WorkPos);
1012+
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) // NOTE a is compile constant and evaluated twice
10141013

1015-
Frame f;
1014+
class MenuButton final {
1015+
public:
1016+
float relY;
1017+
float x0, y0, x1, y1;
1018+
const char *name;
10161019

1017-
if (!f.begin("start", ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBackground))
1018-
return;
1020+
MenuButton(float y, const char *name)
1021+
: relY(y), name(name)
1022+
, x0(0), y0(0), x1(0), y1(0) {}
10191023

1020-
ImGui::SetWindowSize(vp->WorkSize);
1024+
void reshape(ImGuiViewport *vp) {
1025+
float w = vp->WorkSize.x, h = vp->WorkSize.y;
1026+
x0 = 212 / 800.0f * w;
1027+
x1 = 586 / 800.0f * w;
1028+
y0 = relY / 768.0f * h;
1029+
y1 = (relY + 64.0f) / 768.0f * h;
1030+
}
10211031

1022-
float old_x = ImGui::GetCursorPosX();
1032+
bool showDisabled(Frame &f, const char *tooltip, const BackgroundColors &col) const {
1033+
DrawBorder(x0, y0, x1, y1, col);
1034+
ImGui::SetCursorPosY(y0);
1035+
return f.xbtn(name, tooltip, TextHalign::center);
1036+
}
10231037

1024-
{
1025-
FontGuard fg(fnt.copper);
1038+
bool show(Frame &f, Audio &sfx, const BackgroundColors &col) const {
1039+
ImGui::SetCursorPosY(y0);
1040+
bool b = btn(f, name, TextHalign::center, sfx);
1041+
if (b)
1042+
DrawBorderInv(x0, y0, x1, y1, col);
1043+
else
1044+
DrawBorder(x0, y0, x1, y1, col);
1045+
return b;
1046+
}
1047+
};
1048+
1049+
MenuButton mainMenuButtons[] = {
1050+
{284, "Single Player"},
1051+
{364, "Multiplayer"},
1052+
{444, "Help"},
1053+
{524, "Scenario Builder"},
1054+
{604, "Exit"},
1055+
};
1056+
1057+
class MainMenu final {
1058+
public:
1059+
const MenuButton &sp, &mp, &help, &scn, &quit;
1060+
1061+
MainMenu() : sp(mainMenuButtons[0]), mp(mainMenuButtons[1])
1062+
, help(mainMenuButtons[2]), scn(mainMenuButtons[3]), quit(mainMenuButtons[4]) {}
1063+
1064+
void reshape(ImGuiViewport *vp) {
1065+
for (unsigned i = 0; i < ARRAY_SIZE(mainMenuButtons); ++i)
1066+
mainMenuButtons[i].reshape(vp);
1067+
}
1068+
} mainMenu;
1069+
1070+
static void DrawMainMenu(Frame &f, Audio &sfx, Assets &ass)
1071+
{
1072+
MainMenu &mm = mainMenu;
1073+
ImGuiViewport *vp = ImGui::GetMainViewport();
10261074

1027-
ImGui::SetCursorPosY(284.0f / 768.0f * vp->WorkSize.y);
1075+
FontGuard fg(fnt.copper);
1076+
1077+
mm.reshape(vp);
1078+
1079+
float y0 = 284 / 768.0f * vp->WorkSize.y, y1 = 348 / 768.0f * vp->WorkSize.y;
1080+
BackgroundColors col = ass.bkg_cols.at(io::DrsId::bkg_main_menu);
1081+
1082+
#define DRAW_BTN(btn) btn.show(f, sfx, col)
1083+
#define DRAW_DISABLED(btn, tt) btn.showDisabled(f, tt, col)
10281084

10291085
#if 0
1030-
if (btn(f, "Single Player", TextHalign::center, sfx))
1031-
next_menu_state = MenuState::singleplayer_menu;
1086+
if (DRAW_BTN(mm.sp))
1087+
next_menu_state = MenuState::singleplayer_menu;
10321088
#else
1033-
f.xbtn("Single Player", "Single player mode is not supported yet. Use multiplayer mode with 1 player instead.", TextHalign::center);
1089+
DRAW_DISABLED(mm.sp, "Single player mode is not supported yet. Use multiplayer mode with 1 player instead.");
10341090
#endif
10351091

1036-
ImGui::SetCursorPosY(364.0f / 768.0f * vp->WorkSize.y);
1092+
if (DRAW_BTN(mm.mp )) next_menu_state = MenuState::multiplayer_menu;
1093+
if (DRAW_BTN(mm.help)) eng->open_help();
1094+
if (DRAW_BTN(mm.scn )) next_menu_state = MenuState::editor_menu;
1095+
if (DRAW_BTN(mm.quit)) throw 0;
1096+
}
10371097

1038-
if (btn(f, "Multiplayer", TextHalign::center, sfx))
1039-
next_menu_state = MenuState::multiplayer_menu;
1098+
void Engine::show_start() {
1099+
ZoneScoped;
1100+
ImGuiViewport *vp = ImGui::GetMainViewport();
1101+
ImGui::SetNextWindowPos(vp->WorkPos);
10401102

1041-
ImGui::SetCursorPosY(444.0f / 768.0f * vp->WorkSize.y);
1042-
if (btn(f, "Help", TextHalign::center, sfx))
1043-
open_help();
1103+
Frame f;
10441104

1045-
ImGui::SetCursorPosY(524.0f / 768.0f * vp->WorkSize.y);
1105+
if (!f.begin("start", ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBackground))
1106+
return;
10461107

1047-
if (btn(f, "Scenario Builder", TextHalign::center, sfx))
1048-
next_menu_state = MenuState::editor_menu;
1108+
ImGui::SetWindowSize(vp->WorkSize);
10491109

1050-
ImGui::SetCursorPosY(604.0f / 768.0f * vp->WorkSize.y);
1110+
float old_x = ImGui::GetCursorPosX();
10511111

1052-
if (btn(f, "Exit", TextHalign::center, sfx))
1053-
throw 0;
1054-
}
1112+
DrawMainMenu(f, sfx, *assets.get());
10551113

10561114
ImGui::SetCursorPosX(old_x);
10571115
ImGui::SetCursorPosY((710.0f - 40.0f) / 768.0f * vp->WorkSize.y);
@@ -1076,7 +1134,7 @@ void Engine::show_init() {
10761134
f.str("Age of Empires game setup");
10771135

10781136
ImGui::TextWrapped("%s", "In this menu, you can change general settings how the game behaves and where the game assets will be loaded from.");
1079-
ImGui::TextWrapped("%s", "This game is free software. If you have paid for this free software remake, you have been scammed! If you like Age of Empires, please support Microsoft by buying the original game on Steam");
1137+
ImGui::TextWrapped("%s", "This game is free software. If you have paid for this free software remake, you have been fooled!");
10801138

10811139

10821140
if (!fnt.loaded()) {
@@ -1329,6 +1387,19 @@ void DrawLine(float x0, float y0, float x1, float y1, SDL_Color col)
13291387
bkg->AddLine(ImVec2(x0, y0), ImVec2(x1, y1), IM_COL32(col.r, col.g, col.b, SDL_ALPHA_OPAQUE), 1);
13301388
}
13311389

1390+
void DrawBorderInv(float x0, float y0, float x1, float y1, const BackgroundColors &bkgcol)
1391+
{
1392+
BackgroundColors bkgcol2;
1393+
bkgcol2.border[0] = bkgcol.border[3];
1394+
bkgcol2.border[1] = bkgcol.border[4];
1395+
bkgcol2.border[2] = bkgcol.border[5];
1396+
bkgcol2.border[3] = bkgcol.border[0];
1397+
bkgcol2.border[4] = bkgcol.border[1];
1398+
bkgcol2.border[5] = bkgcol.border[2];
1399+
1400+
DrawBorder(x0, y0, x1, y1, bkgcol2);
1401+
}
1402+
13321403
void DrawBorder(float x0, float y0, float x1, float y1, const BackgroundColors &bkgcol)
13331404
{
13341405
DrawLine(x0, y0, x1, y0, bkgcol.border[0]);

game/src/ui.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Assets;
3131
class Engine;
3232

3333
void DrawLine(float x0, float y0, float x1, float y1, SDL_Color col);
34+
void DrawBorderInv(float x0, float y0, float x1, float y1, const BackgroundColors &bkgcol);
3435
void DrawBorder(float x0, float y0, float x1, float y1, const BackgroundColors &bkgcol);
3536

3637
void DrawTextWrapped(const std::string&);

game/src/ui/scenario_editor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void UICache::show_editor_scenario() {
100100

101101
if (ImGui::BeginPopup("EditMenuPopup")) {
102102
if (ImGui::MenuItem("Quit")) {
103-
e->next_menu_state = MenuState::start;
103+
next_menu_state = MenuState::start;
104104
ImGui::CloseCurrentPopup();
105105
}
106106

0 commit comments

Comments
 (0)