From 8b00661b2292ace4b34ae807f6bc6aa80b678636 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Mon, 11 Dec 2023 00:29:49 +0000 Subject: [PATCH] Change: Add sound memory usage to framerate window. --- src/framerate_gui.cpp | 22 ++++++++-------------- src/newgrf_sound.cpp | 15 +++++++++++++++ src/newgrf_sound.h | 1 + src/widgets/framerate_widget.h | 1 - 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/framerate_gui.cpp b/src/framerate_gui.cpp index 8292001594..3bb8f7e597 100644 --- a/src/framerate_gui.cpp +++ b/src/framerate_gui.cpp @@ -10,6 +10,7 @@ #include "framerate_type.h" #include #include "gfx_func.h" +#include "newgrf_sound.h" #include "window_gui.h" #include "window_func.h" #include "table/sprites.h" @@ -393,9 +394,7 @@ static constexpr NWidgetPart _framerate_window_widgets[] = { NWidget(WWT_EMPTY, COLOUR_GREY, WID_FRW_TIMES_NAMES), SetScrollbar(WID_FRW_SCROLLBAR), NWidget(WWT_EMPTY, COLOUR_GREY, WID_FRW_TIMES_CURRENT), SetScrollbar(WID_FRW_SCROLLBAR), NWidget(WWT_EMPTY, COLOUR_GREY, WID_FRW_TIMES_AVERAGE), SetScrollbar(WID_FRW_SCROLLBAR), - NWidget(NWID_SELECTION, INVALID_COLOUR, WID_FRW_SEL_MEMORY), - NWidget(WWT_EMPTY, COLOUR_GREY, WID_FRW_ALLOCSIZE), SetScrollbar(WID_FRW_SCROLLBAR), - EndContainer(), + NWidget(WWT_EMPTY, COLOUR_GREY, WID_FRW_ALLOCSIZE), SetScrollbar(WID_FRW_SCROLLBAR), EndContainer(), NWidget(WWT_TEXT, COLOUR_GREY, WID_FRW_INFO_DATA_POINTS), SetDataTip(STR_FRAMERATE_DATA_POINTS, 0x0), SetFill(1, 0), SetResize(1, 0), EndContainer(), @@ -409,7 +408,6 @@ static constexpr NWidgetPart _framerate_window_widgets[] = { struct FramerateWindow : Window { bool small; - bool showing_memory; int num_active; int num_displayed; @@ -452,7 +450,6 @@ struct FramerateWindow : Window { { this->InitNested(number); this->small = this->IsShaded(); - this->showing_memory = true; this->UpdateData(); this->num_displayed = this->num_active; @@ -480,7 +477,6 @@ struct FramerateWindow : Window { void UpdateData() { double gl_rate = _pf_data[PFE_GAMELOOP].GetRate(); - bool have_script = false; this->rate_gameloop.SetRate(gl_rate, _pf_data[PFE_GAMELOOP].expected_rate); this->speed_gameloop.SetRate(gl_rate / _pf_data[PFE_GAMELOOP].expected_rate, 1.0); if (this->small) return; // in small mode, this is everything needed @@ -493,22 +489,14 @@ struct FramerateWindow : Window { this->times_longterm[e].SetTime(_pf_data[e].GetAverageDurationMilliseconds(NUM_FRAMERATE_POINTS), MILLISECONDS_PER_TICK); if (_pf_data[e].num_valid > 0) { new_active++; - if (e == PFE_GAMESCRIPT || e >= PFE_AI0) have_script = true; } } - if (this->showing_memory != have_script) { - NWidgetStacked *plane = this->GetWidget(WID_FRW_SEL_MEMORY); - plane->SetDisplayedPlane(have_script ? 0 : SZSP_VERTICAL); - this->showing_memory = have_script; - } - if (new_active != this->num_active) { this->num_active = new_active; Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR); sb->SetCount(this->num_active); sb->SetCapacity(std::min(this->num_displayed, this->num_active)); - this->ReInit(); } } @@ -642,6 +630,12 @@ struct FramerateWindow : Window { y += GetCharacterHeight(FS_NORMAL); drawable--; if (drawable == 0) break; + } else if (e == PFE_SOUND) { + SetDParam(0, GetSoundPoolAllocatedMemory()); + DrawString(r.left, r.right, y, STR_FRAMERATE_BYTES_GOOD, TC_FROMSTRING, SA_RIGHT); + y += GetCharacterHeight(FS_NORMAL); + drawable--; + if (drawable == 0) break; } else { /* skip non-script */ y += GetCharacterHeight(FS_NORMAL); diff --git a/src/newgrf_sound.cpp b/src/newgrf_sound.cpp index 9febfdc4bd..6bff1edda2 100644 --- a/src/newgrf_sound.cpp +++ b/src/newgrf_sound.cpp @@ -59,6 +59,21 @@ uint GetNumSounds() return (uint)_sounds.size(); } +/** + * Get size of memory allocated to sound effects. + * @return Approximate memory allocated by loaded sound effects. + */ +size_t GetSoundPoolAllocatedMemory() +{ + size_t bytes = 0; + for (SoundEntry &sound : _sounds) { + if (sound.data == nullptr) continue; + + const auto &data = *sound.data; + bytes += data.capacity() * sizeof(data[0]); + } + return bytes; +} /** * Extract meta data from a NewGRF sound. diff --git a/src/newgrf_sound.h b/src/newgrf_sound.h index ae6b7d94a7..e67a3c49d8 100644 --- a/src/newgrf_sound.h +++ b/src/newgrf_sound.h @@ -34,6 +34,7 @@ bool LoadNewGRFSound(SoundEntry &sound, SoundID sound_id); SoundID GetNewGRFSoundID(const struct GRFFile *file, SoundID sound_id); SoundEntry *GetSound(SoundID sound_id); uint GetNumSounds(); +size_t GetSoundPoolAllocatedMemory(); bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event, bool force = false); void PlayTileSound(const struct GRFFile *file, SoundID sound_id, TileIndex tile); diff --git a/src/widgets/framerate_widget.h b/src/widgets/framerate_widget.h index 0192b540c6..34418f5007 100644 --- a/src/widgets/framerate_widget.h +++ b/src/widgets/framerate_widget.h @@ -21,7 +21,6 @@ enum FramerateWindowWidgets : WidgetID { WID_FRW_TIMES_CURRENT, WID_FRW_TIMES_AVERAGE, WID_FRW_ALLOCSIZE, - WID_FRW_SEL_MEMORY, WID_FRW_SCROLLBAR, };