Skip to content

Commit

Permalink
Change: Add sound memory usage to framerate window.
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterN committed Dec 6, 2024
1 parent ce5279a commit 8b00661
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
22 changes: 8 additions & 14 deletions src/framerate_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "framerate_type.h"
#include <chrono>
#include "gfx_func.h"
#include "newgrf_sound.h"
#include "window_gui.h"
#include "window_func.h"
#include "table/sprites.h"
Expand Down Expand Up @@ -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(),
Expand All @@ -409,7 +408,6 @@ static constexpr NWidgetPart _framerate_window_widgets[] = {

struct FramerateWindow : Window {
bool small;
bool showing_memory;
int num_active;
int num_displayed;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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<NWidgetStacked>(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();
}
}

Expand Down Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions src/newgrf_sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/newgrf_sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion src/widgets/framerate_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down

0 comments on commit 8b00661

Please sign in to comment.