Changeset - r23731:be6c5f9d307a
[Not reviewed]
master
0 5 0
Niels Martin Hansen - 6 years ago 2019-04-15 18:29:37
nielsm@indvikleren.dk
Add: Show memory allocations by GS and AI in framerate window
5 files changed with 65 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/framerate_gui.cpp
Show inline comments
 
@@ -19,12 +19,15 @@
 
#include "strings_func.h"
 
#include "console_func.h"
 
#include "console_type.h"
 
#include "guitimer_func.h"
 
#include "company_base.h"
 
#include "ai/ai_info.hpp"
 
#include "ai/ai_instance.hpp"
 
#include "game/game.hpp"
 
#include "game/game_instance.hpp"
 

	
 
#include "widgets/framerate_widget.h"
 
#include "safeguards.h"
 

	
 

	
 
/**
 
@@ -362,12 +365,15 @@ static const NWidgetPart _framerate_wind
 
		NWidget(WWT_PANEL, COLOUR_GREY),
 
			NWidget(NWID_VERTICAL), SetPadding(6), SetPIP(0, 3, 0),
 
				NWidget(NWID_HORIZONTAL), SetPIP(0, 6, 0),
 
					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(),
 
				EndContainer(),
 
				NWidget(WWT_TEXT, COLOUR_GREY, WID_FRW_INFO_DATA_POINTS), SetDataTip(STR_FRAMERATE_DATA_POINTS, 0x0),
 
			EndContainer(),
 
		EndContainer(),
 
		NWidget(NWID_VERTICAL),
 
			NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_FRW_SCROLLBAR),
 
@@ -375,12 +381,13 @@ static const NWidgetPart _framerate_wind
 
		EndContainer(),
 
	EndContainer(),
 
};
 

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

	
 
	struct CachedDecimal {
 
		StringID strid;
 
@@ -421,12 +428,13 @@ struct FramerateWindow : Window {
 
	static const int MIN_ELEMENTS = 5;      ///< smallest number of elements to display
 

	
 
	FramerateWindow(WindowDesc *desc, WindowNumber number) : Window(desc)
 
	{
 
		this->InitNested(number);
 
		this->small = this->IsShaded();
 
		this->showing_memory = true;
 
		this->UpdateData();
 
		this->num_displayed = this->num_active;
 
		this->next_update.SetInterval(100);
 

	
 
		/* Window is always initialised to MIN_ELEMENTS height, resize to contain num_displayed */
 
		ResizeWindow(this, 0, (max(MIN_ELEMENTS, this->num_displayed) - MIN_ELEMENTS) * FONT_HEIGHT_NORMAL);
 
@@ -450,23 +458,33 @@ 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
 

	
 
		this->rate_drawing.SetRate(_pf_data[PFE_DRAWING].GetRate(), _pf_data[PFE_DRAWING].expected_rate);
 

	
 
		int new_active = 0;
 
		for (PerformanceElement e = PFE_FIRST; e < PFE_MAX; e++) {
 
			this->times_shortterm[e].SetTime(_pf_data[e].GetAverageDurationMilliseconds(8), MILLISECONDS_PER_TICK);
 
			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 (_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);
 
@@ -542,13 +560,14 @@ struct FramerateWindow : Window {
 
					size->width = max(size->width, line_size.width);
 
				}
 
				break;
 
			}
 

	
 
			case WID_FRW_TIMES_CURRENT:
 
			case WID_FRW_TIMES_AVERAGE: {
 
			case WID_FRW_TIMES_AVERAGE:
 
			case WID_FRW_ALLOCSIZE: {
 
				*size = GetStringBoundingBox(STR_FRAMERATE_CURRENT + (widget - WID_FRW_TIMES_CURRENT));
 
				SetDParam(0, 999999);
 
				SetDParam(1, 2);
 
				Dimension item_size = GetStringBoundingBox(STR_FRAMERATE_MS_GOOD);
 
				size->width = max(size->width, item_size.width);
 
				size->height += FONT_HEIGHT_NORMAL * MIN_ELEMENTS + VSPACING;
 
@@ -579,12 +598,43 @@ struct FramerateWindow : Window {
 
				drawable--;
 
				if (drawable == 0) break;
 
			}
 
		}
 
	}
 

	
 
	void DrawElementAllocationsColumn(const Rect &r) const
 
	{
 
		const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
 
		uint16 skip = sb->GetPosition();
 
		int drawable = this->num_displayed;
 
		int y = r.top;
 
		DrawString(r.left, r.right, y, STR_FRAMERATE_MEMORYUSE, TC_FROMSTRING, SA_CENTER, true);
 
		y += FONT_HEIGHT_NORMAL + VSPACING;
 
		for (PerformanceElement e : DISPLAY_ORDER_PFE) {
 
			if (_pf_data[e].num_valid == 0) continue;
 
			if (skip > 0) {
 
				skip--;
 
			} else if (e == PFE_GAMESCRIPT || e >= PFE_AI0) {
 
				if (e == PFE_GAMESCRIPT) {
 
					SetDParam(0, Game::GetInstance()->GetAllocatedMemory());
 
				} else {
 
					SetDParam(0, Company::Get(e - PFE_AI0)->ai_instance->GetAllocatedMemory());
 
				}
 
				DrawString(r.left, r.right, y, STR_FRAMERATE_BYTES_GOOD, TC_FROMSTRING, SA_RIGHT);
 
				y += FONT_HEIGHT_NORMAL;
 
				drawable--;
 
				if (drawable == 0) break;
 
			} else {
 
				/* skip non-script */
 
				y += FONT_HEIGHT_NORMAL;
 
				drawable--;
 
				if (drawable == 0) break;
 
			}
 
		}
 
	}
 

	
 
	void DrawWidget(const Rect &r, int widget) const override
 
	{
 
		switch (widget) {
 
			case WID_FRW_TIMES_NAMES: {
 
				/* Render a column of titles for performance element names */
 
				const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
 
@@ -615,12 +665,15 @@ struct FramerateWindow : Window {
 
				DrawElementTimesColumn(r, STR_FRAMERATE_CURRENT, this->times_shortterm);
 
				break;
 
			case WID_FRW_TIMES_AVERAGE:
 
				/* Render averages of all recorded values */
 
				DrawElementTimesColumn(r, STR_FRAMERATE_AVERAGE, this->times_longterm);
 
				break;
 
			case WID_FRW_ALLOCSIZE:
 
				DrawElementAllocationsColumn(r);
 
				break;
 
		}
 
	}
 

	
 
	void OnClick(Point pt, int widget, int click_count) override
 
	{
 
		switch (widget) {
src/lang/english.txt
Show inline comments
 
@@ -2739,19 +2739,23 @@ STR_FRAMERATE_RATE_GAMELOOP_TOOLTIP     
 
STR_FRAMERATE_RATE_BLITTER                                      :{BLACK}Graphics frame rate: {STRING2}
 
STR_FRAMERATE_RATE_BLITTER_TOOLTIP                              :{BLACK}Number of video frames rendered per second.
 
STR_FRAMERATE_SPEED_FACTOR                                      :{BLACK}Current game speed factor: {DECIMAL}x
 
STR_FRAMERATE_SPEED_FACTOR_TOOLTIP                              :{BLACK}How fast the game is currently running, compared to the expected speed at normal simulation rate.
 
STR_FRAMERATE_CURRENT                                           :{WHITE}Current
 
STR_FRAMERATE_AVERAGE                                           :{WHITE}Average
 
STR_FRAMERATE_MEMORYUSE                                         :{WHITE}Memory
 
STR_FRAMERATE_DATA_POINTS                                       :{BLACK}Data based on {COMMA} measurements
 
STR_FRAMERATE_MS_GOOD                                           :{LTBLUE}{DECIMAL} ms
 
STR_FRAMERATE_MS_WARN                                           :{YELLOW}{DECIMAL} ms
 
STR_FRAMERATE_MS_BAD                                            :{RED}{DECIMAL} ms
 
STR_FRAMERATE_FPS_GOOD                                          :{LTBLUE}{DECIMAL} frames/s
 
STR_FRAMERATE_FPS_WARN                                          :{YELLOW}{DECIMAL} frames/s
 
STR_FRAMERATE_FPS_BAD                                           :{RED}{DECIMAL} frames/s
 
STR_FRAMERATE_BYTES_GOOD                                        :{LTBLUE}{BYTES}
 
STR_FRAMERATE_BYTES_WARN                                        :{YELLOW}{BYTES}
 
STR_FRAMERATE_BYTES_BAD                                         :{RED}{BYTES}
 
STR_FRAMERATE_GRAPH_MILLISECONDS                                :{TINY_FONT}{COMMA} ms
 
STR_FRAMERATE_GRAPH_SECONDS                                     :{TINY_FONT}{COMMA} s
 
############ Leave those lines in this order!!
 
STR_FRAMERATE_GAMELOOP                                          :{BLACK}Game loop total:
 
STR_FRAMERATE_GL_ECONOMY                                        :{BLACK}  Cargo handling:
 
STR_FRAMERATE_GL_TRAINS                                         :{BLACK}  Train ticks:
src/script/api/game/game_window.hpp.sq
Show inline comments
 
@@ -465,12 +465,14 @@ void SQGSWindow_Register(Squirrel *engin
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_RATE_DRAWING,                      "WID_FRW_RATE_DRAWING");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_RATE_FACTOR,                       "WID_FRW_RATE_FACTOR");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_INFO_DATA_POINTS,                  "WID_FRW_INFO_DATA_POINTS");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_TIMES_NAMES,                       "WID_FRW_TIMES_NAMES");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_TIMES_CURRENT,                     "WID_FRW_TIMES_CURRENT");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_TIMES_AVERAGE,                     "WID_FRW_TIMES_AVERAGE");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_ALLOCSIZE,                         "WID_FRW_ALLOCSIZE");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_SEL_MEMORY,                        "WID_FRW_SEL_MEMORY");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FRW_SCROLLBAR,                         "WID_FRW_SCROLLBAR");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FGW_CAPTION,                           "WID_FGW_CAPTION");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_FGW_GRAPH,                             "WID_FGW_GRAPH");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_GL_TEMPERATE,                          "WID_GL_TEMPERATE");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_GL_ARCTIC,                             "WID_GL_ARCTIC");
 
	SQGSWindow.DefSQConst(engine, ScriptWindow::WID_GL_TROPICAL,                           "WID_GL_TROPICAL");
src/script/api/script_window.hpp
Show inline comments
 
@@ -1306,12 +1306,14 @@ public:
 
		WID_FRW_RATE_DRAWING                         = ::WID_FRW_RATE_DRAWING,
 
		WID_FRW_RATE_FACTOR                          = ::WID_FRW_RATE_FACTOR,
 
		WID_FRW_INFO_DATA_POINTS                     = ::WID_FRW_INFO_DATA_POINTS,
 
		WID_FRW_TIMES_NAMES                          = ::WID_FRW_TIMES_NAMES,
 
		WID_FRW_TIMES_CURRENT                        = ::WID_FRW_TIMES_CURRENT,
 
		WID_FRW_TIMES_AVERAGE                        = ::WID_FRW_TIMES_AVERAGE,
 
		WID_FRW_ALLOCSIZE                            = ::WID_FRW_ALLOCSIZE,
 
		WID_FRW_SEL_MEMORY                           = ::WID_FRW_SEL_MEMORY,
 
		WID_FRW_SCROLLBAR                            = ::WID_FRW_SCROLLBAR,
 
	};
 

	
 
	/** Widgets of the #FrametimeGraphWindow class. */
 
	enum FrametimeGraphWindowWidgets {
 
		WID_FGW_CAPTION                              = ::WID_FGW_CAPTION,
src/widgets/framerate_widget.h
Show inline comments
 
@@ -19,12 +19,14 @@ enum FramerateWindowWidgets {
 
	WID_FRW_RATE_DRAWING,
 
	WID_FRW_RATE_FACTOR,
 
	WID_FRW_INFO_DATA_POINTS,
 
	WID_FRW_TIMES_NAMES,
 
	WID_FRW_TIMES_CURRENT,
 
	WID_FRW_TIMES_AVERAGE,
 
	WID_FRW_ALLOCSIZE,
 
	WID_FRW_SEL_MEMORY,
 
	WID_FRW_SCROLLBAR,
 
};
 

	
 
/** Widgets of the #FrametimeGraphWindow class. */
 
enum FrametimeGraphWindowWidgets {
 
	WID_FGW_CAPTION,
0 comments (0 inline, 0 general)