fix(fps): stop lookup of dvars each frame (#530)

This commit is contained in:
Edo 2022-11-10 10:10:48 +00:00 committed by GitHub
parent 97e098bc5c
commit bee8965463
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,6 +15,9 @@ namespace fps
{
namespace
{
const game::dvar_t* cg_drawFPS;
const game::dvar_t* cg_drawPing;
float fps_color_good[4] = {0.6f, 1.0f, 0.0f, 1.0f};
float fps_color_ok[4] = {1.0f, 0.7f, 0.3f, 1.0f};
float fps_color_bad[4] = {1.0f, 0.3f, 0.3f, 1.0f};
@ -45,7 +48,7 @@ namespace fps
{
data->history[data->index % 32] = value;
data->instant = value;
data->min = 0x7FFFFFFF;
data->min = std::numeric_limits<int>::max();
data->max = 0;
data->average = 0.0f;
data->variance = 0.0f;
@ -93,11 +96,9 @@ namespace fps
void cg_draw_fps()
{
const auto* draw_fps = game::Dvar_FindVar("cg_drawFPS");
if (draw_fps && draw_fps->current.integer > 0)
if (cg_drawFPS->current.integer > 0)
{
const auto fps = fps::get_fps();
const auto fps = get_fps();
auto* font = game::R_RegisterFont("fonts/consolefont");
if (!font) return;
@ -107,19 +108,18 @@ namespace fps
const auto scale = 1.0f;
const auto x = (game::ScrPlace_GetViewPlacement()->realViewportSize[0] - 10.0f) - game::R_TextWidth(
fps_string, 0x7FFFFFFF, font) * scale;
fps_string, std::numeric_limits<int>::max(), font) * scale;
const auto y = font->pixelHeight * 1.2f;
const auto fps_color = fps >= 60 ? fps_color_good : (fps >= 30 ? fps_color_ok : fps_color_bad);
game::R_AddCmdDrawText(fps_string, 0x7FFFFFFF, font, x, y, scale, scale, 0.0f, fps_color, 6);
game::R_AddCmdDrawText(fps_string, std::numeric_limits<int>::max(), font, x, y, scale, scale, 0.0f, fps_color, 6);
}
}
void cg_draw_ping()
{
const auto* draw_ping = game::Dvar_FindVar("cg_drawPing");
if (draw_ping && draw_ping->current.integer > 0 && game::CL_IsCgameInitialized())
if (cg_drawPing->current.integer > 0 && game::CL_IsCgameInitialized())
{
auto* font = game::R_RegisterFont("fonts/consolefont");
if (!font) return;
@ -129,26 +129,25 @@ namespace fps
const auto scale = 1.0f;
const auto x = (game::ScrPlace_GetViewPlacement()->realViewportSize[0] - 375.0f) - game::R_TextWidth(
ping_string, 0x7FFFFFFF, font) * scale;
ping_string, std::numeric_limits<int>::max(), font) * scale;
const auto y = font->pixelHeight * 1.2f;
game::R_AddCmdDrawText(ping_string, 0x7FFFFFFF, font, x, y, scale, scale, 0.0f, ping_color, 6);
game::R_AddCmdDrawText(ping_string, std::numeric_limits<int>::max(), font, x, y, scale, scale, 0.0f, ping_color, 6);
}
}
void cg_draw_fps_register_stub(const char* name, const char** _enum, const int value, unsigned int /*flags*/,
const char* desc)
const game::dvar_t* cg_draw_fps_register_stub(const char* dvar_name, const char** value_list, const int default_index, unsigned int /*flags*/, const char* description)
{
game::Dvar_RegisterEnum(name, _enum, value, game::DVAR_FLAG_SAVED, desc);
cg_drawFPS = game::Dvar_RegisterEnum(dvar_name, value_list, default_index, game::DVAR_FLAG_SAVED, description);
return cg_drawFPS;
}
}
int get_fps()
{
return static_cast<std::int32_t>(static_cast<float>(1000.0f / static_cast<float>(cg_perf.
average))
+ 9.313225746154785e-10);
return static_cast<std::int32_t>(static_cast<float>(1000.0f /
static_cast<float>(cg_perf.average)) + 9.313225746154785e-10);
}
class component final : public component_interface
@ -174,7 +173,7 @@ namespace fps
scheduler::loop(cg_draw_fps, scheduler::pipeline::renderer);
if (game::environment::is_mp())
{
game::Dvar_RegisterInt("cg_drawPing", 0, 0, 1, game::DVAR_FLAG_SAVED, "Choose to draw ping");
cg_drawPing = game::Dvar_RegisterInt("cg_drawPing", 0, 0, 1, game::DVAR_FLAG_SAVED, "Choose to draw ping");
scheduler::loop(cg_draw_ping, scheduler::pipeline::renderer);
}