Skip to content

Commit 1b7d7fc

Browse files
fix multiple cancellable tasks, audio error refactoring
fix more compilerwarnings
1 parent 81b2a1f commit 1b7d7fc

File tree

9 files changed

+42
-39
lines changed

9 files changed

+42
-39
lines changed

game/src/async.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ struct UI_Task final {
2929

3030
UI_Task(IdPoolRef id, TaskFlags flags, const std::string &title, const std::string &desc, unsigned steps, unsigned total)
3131
: id(id), flags(flags), title(title), desc(desc), steps(steps), total(total) {}
32+
33+
constexpr bool is_cancellable() const noexcept {
34+
return !!((unsigned)flags & (unsigned)TaskFlags::cancellable);
35+
}
3236
};
3337

3438
/** Wrapper to manage ui task info. */

game/src/engine.cpp

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
#include "debug.hpp"
4444

45+
#pragma clang diagnostic ignored "-Wswitch"
46+
4547
namespace aoe {
4648

4749
Engine *eng;
@@ -346,37 +348,37 @@ void Engine::display_ui_tasks() {
346348
std::lock_guard<std::mutex> lock(m_ui);
347349

348350
ImGui::OpenPopup("tasks");
349-
350351
ImGui::SetNextWindowSize(ImVec2(400, 0));
351352

352353
if (ImGui::BeginPopupModal("tasks", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
353354
int i = 0;
354-
bool cancellable = false;
355+
unsigned cancellable = 0;
355356

356357
for (auto it = eng->ui_tasks.begin(); it != eng->ui_tasks.end(); ++i) {
357358
UI_Task &tsk = it->second;
358359

359360
float f = (float)tsk.steps / tsk.total;
360-
ImGui::TextWrapped("%s", tsk.title.c_str());
361+
DrawTextWrapped(tsk.title);
361362
ImGui::ProgressBar(f, ImVec2(-1, 0));
362-
if (!tsk.desc.empty())
363-
ImGui::TextWrapped("%s", tsk.desc.c_str());
363+
DrawTextWrapped(tsk.desc);
364364

365365
std::string str("Cancel##" + std::to_string(i));
366+
bool b = tsk.is_cancellable();
366367

367-
if ((unsigned)tsk.flags & (unsigned)TaskFlags::cancellable)
368-
cancellable = true;
368+
if (b)
369+
++cancellable;
369370

370-
if (((unsigned)tsk.flags & (unsigned)TaskFlags::cancellable) && ImGui::Button(str.c_str()))
371+
if (b && ImGui::Button(str.c_str()))
371372
it = eng->ui_tasks.erase(it);
372373
else
373374
++it;
374375
}
375376

376-
if (cancellable && ImGui::Button("Cancel all tasks")) {
377+
// only show when more than one can be cancelled
378+
if (cancellable > 1 && ImGui::Button("Cancel all tasks")) {
377379
for (auto it = eng->ui_tasks.begin(); it != eng->ui_tasks.end(); ++i) {
378380
UI_Task &tsk = it->second;
379-
if ((unsigned)tsk.flags & (unsigned)TaskFlags::cancellable)
381+
if (tsk.is_cancellable())
380382
it = eng->ui_tasks.erase(it);
381383
else
382384
++it;
@@ -548,29 +550,8 @@ void Engine::set_background(io::DrsId id) {
548550
}
549551

550552
void Engine::set_background(MenuState s) {
551-
io::DrsId id = io::DrsId::bkg_main_menu;
552-
553-
switch (s) {
554-
case MenuState::multiplayer_host:
555-
case MenuState::multiplayer_menu:
556-
id = io::DrsId::bkg_multiplayer;
557-
break;
558-
case MenuState::singleplayer_menu:
559-
case MenuState::singleplayer_host:
560-
id = io::DrsId::bkg_singleplayer;
561-
break;
562-
case MenuState::defeat:
563-
id = io::DrsId::bkg_defeat;
564-
break;
565-
case MenuState::victory:
566-
id = io::DrsId::bkg_victory;
567-
break;
568-
case MenuState::editor_menu:
569-
id = io::DrsId::bkg_editor_menu;
570-
break;
571-
}
572-
573-
set_background(id);
553+
const MenuInfo &mi = GetMenuInfo(s);
554+
set_background(mi.border_col);
574555
}
575556

576557
ImVec2 Engine::tilepos(float x, float y, float left, float top, int h) {

game/src/engine/assets.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <algorithm>
99

10+
#pragma clang diagnostic ignored "-Wmacro-redefined"
11+
1012
namespace aoe {
1113

1214
using namespace gfx;

game/src/engine/audio.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#define SAMPLING_FREQ 48000
1616
#define NUM_CHANNELS 2
1717

18+
#pragma clang diagnostic ignored "-Wswitch"
19+
1820
namespace aoe {
1921

2022
static const double exp_base = 5.0;
@@ -59,7 +61,7 @@ Audio::Audio() : freq(0), channels(0), format(0), music(nullptr, Mix_FreeMusic)
5961
if (msg)
6062
throw std::runtime_error(std::string("Mix: Could not initialize audio: ") + msg);
6163
else
62-
throw std::runtime_error(std::string("Mix: Could not initialize audio: unknown error"));
64+
throw std::runtime_error("Mix: Could not initialize audio: unknown error");
6365
}
6466

6567
if (Mix_OpenAudio(SAMPLING_FREQ, MIX_DEFAULT_FORMAT, NUM_CHANNELS, 1024) == -1)
@@ -143,7 +145,7 @@ void Audio::play_music(const char *file, int loops) {
143145
void Audio::play_music(MusicId id, int loops) {
144146
auto it = jukebox.find(id);
145147
if (it == jukebox.end()) {
146-
fprintf(stderr, "%s: cannot play music id %d: not found\n", __func__, id);
148+
fprintf(stderr, "%s: cannot play music id %u: not found\n", __func__, id);
147149
return;
148150
}
149151

@@ -221,7 +223,7 @@ void Audio::play_taunt(TauntId id) {
221223

222224
auto it = taunts.find(id);
223225
if (it == taunts.end()) {
224-
fprintf(stderr, "%s: cannot play taunt id %d: not found\n", __func__, id);
226+
fprintf(stderr, "%s: cannot play taunt id %u: not found\n", __func__, id);
225227
return;
226228
}
227229

game/src/engine/menu.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const MenuInfo menu_info[] = {
99
{ true, DrsId::bkg_main_menu }, // start
1010
{ true, DrsId::bkg_singleplayer },
1111
{ true, DrsId::bkg_singleplayer },
12-
{ true, DrsId::bkg_singleplayer },
12+
{ false, (DrsId)0 }, // sp game
1313
{ true, DrsId::bkg_multiplayer },
1414
{ true, DrsId::bkg_multiplayer },
15+
{ false, (DrsId)0 }, // mp game
1516
{ false, (DrsId)0 }, // mp settings
16-
{ true, DrsId::bkg_multiplayer },
1717
{ true, DrsId::bkg_defeat },
1818
{ true, DrsId::bkg_victory },
19-
{ false, (DrsId)0 }, // edit menu
19+
{ true, DrsId::bkg_editor_menu }, // edit menu
2020
{ false, (DrsId)0 }, // edit scn
2121
};
2222

game/src/legacy/legacy.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include <tracy/Tracy.hpp>
1414

15+
#pragma clang diagnostic ignored "-Wswitch"
16+
1517
// don't ask me why, but windows needs this and including the correct header does not seem to help
1618
#if _WIN32
1719
int ::rand(void);

game/src/legacy/scenario.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "../game.hpp"
1313
#include "../world/terrain.hpp"
1414

15+
#pragma clang diagnostic ignored "-Wunused-value"
16+
1517
// yes, including .c's is evil, but... we have no choice :/
1618
#include <miniz.c>
1719

game/src/ui.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,8 @@ void UICache::show_entities() {
917917

918918
#define MAPSIZE_STEP 4
919919

920+
#pragma clang diagnostic ignored "-Wparentheses"
921+
920922
// TODO extract to ui/widgets/scenario_settings.cpp
921923
void Engine::show_mph_cfg(ui::Frame &f) {
922924
ZoneScoped;
@@ -1356,6 +1358,12 @@ void DrawBorder(float x0, float y0, float x1, float y1, const BackgroundColors &
13561358
DrawLine(x0 + 2, y1 - 3, x1 - 2, y1 - 3, bkgcol.border[3]);
13571359
}
13581360

1361+
void DrawTextWrapped(const std::string &s)
1362+
{
1363+
if (!s.empty())
1364+
ImGui::TextWrapped("%s", s.c_str());
1365+
}
1366+
13591367
void Engine::draw_background_border() {
13601368
ZoneScoped;
13611369
assert(assets.get());

game/src/ui.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Engine;
3333
void DrawLine(float x0, float y0, float x1, float y1, SDL_Color col);
3434
void DrawBorder(float x0, float y0, float x1, float y1, const BackgroundColors &bkgcol);
3535

36+
void DrawTextWrapped(const std::string&);
37+
3638
namespace ui {
3739

3840
bool chkbox(const char *s, bool &b);

0 commit comments

Comments
 (0)