Limit redrawing frequency (#693)

Co-authored-by: Tamás Bálint Misius <lbphacker@gmail.com>
This commit is contained in:
Vladimir
2020-10-11 15:58:52 +06:00
committed by GitHub
parent bdcf486a8d
commit f137bad7f8
12 changed files with 87 additions and 10 deletions

View File

@@ -176,6 +176,17 @@ void SDLOpen()
} }
} }
if (Client::Ref().GetPrefBool("AutoDrawLimit", false))
{
SDL_DisplayMode displayMode;
SDL_GetCurrentDisplayMode(displayIndex, &displayMode);
if(displayMode.refresh_rate >= 60)
{
ui::Engine::Ref().SetDrawingFrequencyLimit(displayMode.refresh_rate);
}
}
#ifdef WIN #ifdef WIN
SDL_SysWMinfo SysInfo; SDL_SysWMinfo SysInfo;
SDL_VERSION(&SysInfo.version); SDL_VERSION(&SysInfo.version);
@@ -497,9 +508,16 @@ void EngineProcess()
{ {
double frameTimeAvg = 0.0f, correctedFrameTimeAvg = 0.0f; double frameTimeAvg = 0.0f, correctedFrameTimeAvg = 0.0f;
SDL_Event event; SDL_Event event;
int drawingTimer = 0;
int frameStart = 0;
while(engine->Running()) while(engine->Running())
{ {
int frameStart = SDL_GetTicks(); int oldFrameStart = frameStart;
frameStart = SDL_GetTicks();
drawingTimer += frameStart - oldFrameStart;
if(engine->Broken()) { engine->UnBreak(); break; } if(engine->Broken()) { engine->UnBreak(); break; }
event.type = 0; event.type = 0;
while (SDL_PollEvent(&event)) while (SDL_PollEvent(&event))
@@ -510,21 +528,27 @@ void EngineProcess()
if(engine->Broken()) { engine->UnBreak(); break; } if(engine->Broken()) { engine->UnBreak(); break; }
engine->Tick(); engine->Tick();
engine->Draw();
if (scale != engine->Scale || fullscreen != engine->Fullscreen || int drawcap = ui::Engine::Ref().GetDrawingFrequencyLimit();
altFullscreen != engine->GetAltFullscreen() || if (!drawcap || drawingTimer > 1000.f/drawcap)
forceIntegerScaling != engine->GetForceIntegerScaling() || resizable != engine->GetResizable())
{ {
SDLSetScreen(engine->Scale, engine->GetResizable(), engine->Fullscreen, engine->GetAltFullscreen(), engine->Draw();
engine->GetForceIntegerScaling()); drawingTimer = 0;
}
if (scale != engine->Scale || fullscreen != engine->Fullscreen ||
altFullscreen != engine->GetAltFullscreen() ||
forceIntegerScaling != engine->GetForceIntegerScaling() || resizable != engine->GetResizable())
{
SDLSetScreen(engine->Scale, engine->GetResizable(), engine->Fullscreen, engine->GetAltFullscreen(),
engine->GetForceIntegerScaling());
}
#ifdef OGLI #ifdef OGLI
blit(); blit();
#else #else
blit(engine->g->vid); blit(engine->g->vid);
#endif #endif
}
int frameTime = SDL_GetTicks() - frameStart; int frameTime = SDL_GetTicks() - frameStart;
frameTimeAvg = frameTimeAvg * 0.8 + frameTime * 0.2; frameTimeAvg = frameTimeAvg * 0.8 + frameTime * 0.2;

View File

@@ -17,6 +17,7 @@ using namespace ui;
Engine::Engine(): Engine::Engine():
FpsLimit(60.0f), FpsLimit(60.0f),
drawingFrequencyLimit(0),
Scale(1), Scale(1),
Fullscreen(false), Fullscreen(false),
FrameIndex(0), FrameIndex(0),

View File

@@ -47,6 +47,8 @@ namespace ui
void Break(); void Break();
void UnBreak(); void UnBreak();
void SetDrawingFrequencyLimit(int limit) {drawingFrequencyLimit = limit;}
inline int GetDrawingFrequencyLimit() {return drawingFrequencyLimit;}
void SetFullscreen(bool fullscreen) { Fullscreen = fullscreen; } void SetFullscreen(bool fullscreen) { Fullscreen = fullscreen; }
inline bool GetFullscreen() { return Fullscreen; } inline bool GetFullscreen() { return Fullscreen; }
void SetAltFullscreen(bool altFullscreen) { this->altFullscreen = altFullscreen; } void SetAltFullscreen(bool altFullscreen) { this->altFullscreen = altFullscreen; }
@@ -82,6 +84,7 @@ namespace ui
//inline State* GetState() { return state_; } //inline State* GetState() { return state_; }
inline Window* GetWindow() { return state_; } inline Window* GetWindow() { return state_; }
float FpsLimit; float FpsLimit;
int drawingFrequencyLimit;
Graphics * g; Graphics * g;
int Scale; int Scale;
bool Fullscreen; bool Fullscreen;

View File

@@ -117,6 +117,11 @@ void OptionsController::SetMomentumScroll(bool momentumScroll)
model->SetMomentumScroll(momentumScroll); model->SetMomentumScroll(momentumScroll);
} }
void OptionsController::SetAutoDrawLimit(bool autoDrawLimit)
{
model->SetAutoDrawLimit(autoDrawLimit);
}
void OptionsController::Exit() void OptionsController::Exit()
{ {
view->CloseActiveWindow(); view->CloseActiveWindow();

View File

@@ -34,6 +34,7 @@ public:
void SetIncludePressure(bool includePressure); void SetIncludePressure(bool includePressure);
void SetPerfectCircle(bool perfectCircle); void SetPerfectCircle(bool perfectCircle);
void SetMomentumScroll(bool momentumScroll); void SetMomentumScroll(bool momentumScroll);
void SetAutoDrawLimit(bool autoDrawLimit);
void Exit(); void Exit();
OptionsView * GetView(); OptionsView * GetView();

View File

@@ -238,6 +238,17 @@ void OptionsModel::SetMomentumScroll(bool state)
notifySettingsChanged(); notifySettingsChanged();
} }
bool OptionsModel::GetAutoDrawLimit()
{
return Client::Ref().GetPrefBool("AutoDrawLimit", false);
}
void OptionsModel::SetAutoDrawLimit(bool state)
{
Client::Ref().SetPref("AutoDrawLimit", state);
notifySettingsChanged();
}
void OptionsModel::notifySettingsChanged() void OptionsModel::notifySettingsChanged()
{ {
for (size_t i = 0; i < observers.size(); i++) for (size_t i = 0; i < observers.size(); i++)

View File

@@ -53,6 +53,8 @@ public:
void SetPerfectCircle(bool perfectCircle); void SetPerfectCircle(bool perfectCircle);
bool GetMomentumScroll(); bool GetMomentumScroll();
void SetMomentumScroll(bool momentumScroll); void SetMomentumScroll(bool momentumScroll);
bool GetAutoDrawLimit();
void SetAutoDrawLimit(bool autoDrawLimit);
virtual ~OptionsModel(); virtual ~OptionsModel();
}; };

View File

@@ -250,6 +250,17 @@ OptionsView::OptionsView():
scrollPanel->AddChild(tempLabel); scrollPanel->AddChild(tempLabel);
scrollPanel->AddChild(momentumScroll); scrollPanel->AddChild(momentumScroll);
currentY += 20;
autoDrawLimit = new ui::Checkbox(ui::Point(8, currentY), ui::Point(1, 16), "Auto Draw Rate", "");
autowidth(autoDrawLimit);
autoDrawLimit->SetActionCallback({ [this] { c->SetAutoDrawLimit(autoDrawLimit->GetChecked()); } });
tempLabel = new ui::Label(ui::Point(autoDrawLimit->Position.X + Graphics::textwidth(autoDrawLimit->GetText()) + 20, currentY), ui::Point(1, 16), "\bg- Based on monitor refresh rate at startup");
autowidth(tempLabel);
tempLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
tempLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
scrollPanel->AddChild(tempLabel);
scrollPanel->AddChild(autoDrawLimit);
currentY+=20; currentY+=20;
mouseClickRequired = new ui::Checkbox(ui::Point(8, currentY), ui::Point(1, 16), "Sticky Categories", ""); mouseClickRequired = new ui::Checkbox(ui::Point(8, currentY), ui::Point(1, 16), "Sticky Categories", "");
autowidth(mouseClickRequired); autowidth(mouseClickRequired);
@@ -350,6 +361,7 @@ void OptionsView::NotifySettingsChanged(OptionsModel * sender)
includePressure->SetChecked(sender->GetIncludePressure()); includePressure->SetChecked(sender->GetIncludePressure());
perfectCirclePressure->SetChecked(sender->GetPerfectCircle()); perfectCirclePressure->SetChecked(sender->GetPerfectCircle());
momentumScroll->SetChecked(sender->GetMomentumScroll()); momentumScroll->SetChecked(sender->GetMomentumScroll());
autoDrawLimit->SetChecked(sender->GetAutoDrawLimit());
} }
void OptionsView::AttachController(OptionsController * c_) void OptionsView::AttachController(OptionsController * c_)

View File

@@ -32,6 +32,7 @@ class OptionsView: public ui::Window
ui::DropDown * decoSpace; ui::DropDown * decoSpace;
ui::Checkbox * showAvatars; ui::Checkbox * showAvatars;
ui::Checkbox * momentumScroll; ui::Checkbox * momentumScroll;
ui::Checkbox * autoDrawLimit;
ui::Checkbox * mouseClickRequired; ui::Checkbox * mouseClickRequired;
ui::Checkbox * includePressure; ui::Checkbox * includePressure;
ui::Checkbox * perfectCirclePressure; ui::Checkbox * perfectCirclePressure;

View File

@@ -1378,6 +1378,21 @@ int luatpt_setfpscap(lua_State* l)
return 0; return 0;
} }
int luatpt_setdrawcap(lua_State* l)
{
int acount = lua_gettop(l);
if (acount == 0)
{
lua_pushinteger(l, ui::Engine::Ref().GetDrawingFrequencyLimit());
return 1;
}
int drawcap = luaL_checkint(l, 1);
if(drawcap < 0)
return luaL_error(l, "draw cap too small");
ui::Engine::Ref().SetDrawingFrequencyLimit(drawcap);
return 0;
}
int luatpt_getscript(lua_State* l) int luatpt_getscript(lua_State* l)
{ {
int scriptID = luaL_checkinteger(l, 1); int scriptID = luaL_checkinteger(l, 1);

View File

@@ -127,6 +127,7 @@ int luatpt_setfire(lua_State* l);
int luatpt_setdebug(lua_State* l); int luatpt_setdebug(lua_State* l);
int luatpt_setfpscap(lua_State* l); int luatpt_setfpscap(lua_State* l);
int luatpt_setdrawcap(lua_State* l);
int luatpt_getscript(lua_State* l); int luatpt_getscript(lua_State* l);

View File

@@ -213,6 +213,7 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m):
{"graphics_func",&luatpt_graphics_func}, {"graphics_func",&luatpt_graphics_func},
{"get_clipboard", &platform_clipboardCopy}, {"get_clipboard", &platform_clipboardCopy},
{"set_clipboard", &platform_clipboardPaste}, {"set_clipboard", &platform_clipboardPaste},
{"setdrawcap", &luatpt_setdrawcap},
{NULL,NULL} {NULL,NULL}
}; };