mirror of
https://github.com/luanti-org/luanti.git
synced 2025-10-05 19:31:04 +00:00
Share FpsControl code between game and menu
This commit is contained in:
parent
0c3a4cc7b9
commit
13a0e5fb4a
5 changed files with 70 additions and 123 deletions
|
@ -650,20 +650,6 @@ public:
|
|||
|
||||
const static float object_hit_delay = 0.2;
|
||||
|
||||
struct FpsControl {
|
||||
FpsControl() : last_time(0), busy_time(0), sleep_time(0) {}
|
||||
|
||||
void reset();
|
||||
|
||||
void limit(IrrlichtDevice *device, f32 *dtime);
|
||||
|
||||
u32 getBusyMs() const { return busy_time / 1000; }
|
||||
|
||||
// all values in microseconds (us)
|
||||
u64 last_time, busy_time, sleep_time;
|
||||
};
|
||||
|
||||
|
||||
/* The reason the following structs are not anonymous structs within the
|
||||
* class is that they are not used by the majority of member functions and
|
||||
* many functions that do require objects of thse types do not modify them
|
||||
|
@ -1201,7 +1187,7 @@ void Game::run()
|
|||
// Calculate dtime =
|
||||
// m_rendering_engine->run() from this iteration
|
||||
// + Sleep time until the wanted FPS are reached
|
||||
draw_times.limit(device, &dtime);
|
||||
draw_times.limit(device, &dtime, g_menumgr.pausesGame());
|
||||
|
||||
const auto current_dynamic_info = ClientDynamicInfo::getCurrent();
|
||||
if (!current_dynamic_info.equal(client_display_info)) {
|
||||
|
@ -4330,48 +4316,6 @@ void Game::drawScene(ProfilerGraph *graph, RunStats *stats)
|
|||
Misc
|
||||
****************************************************************************/
|
||||
|
||||
void FpsControl::reset()
|
||||
{
|
||||
last_time = porting::getTimeUs();
|
||||
}
|
||||
|
||||
/*
|
||||
* On some computers framerate doesn't seem to be automatically limited
|
||||
*/
|
||||
void FpsControl::limit(IrrlichtDevice *device, f32 *dtime)
|
||||
{
|
||||
const float fps_limit = (device->isWindowFocused() && !g_menumgr.pausesGame())
|
||||
? g_settings->getFloat("fps_max")
|
||||
: g_settings->getFloat("fps_max_unfocused");
|
||||
const u64 frametime_min = 1000000.0f / std::max(fps_limit, 1.0f);
|
||||
|
||||
u64 time = porting::getTimeUs();
|
||||
|
||||
if (time > last_time) // Make sure time hasn't overflowed
|
||||
busy_time = time - last_time;
|
||||
else
|
||||
busy_time = 0;
|
||||
|
||||
if (busy_time < frametime_min) {
|
||||
sleep_time = frametime_min - busy_time;
|
||||
if (sleep_time > 0)
|
||||
sleep_us(sleep_time);
|
||||
} else {
|
||||
sleep_time = 0;
|
||||
}
|
||||
|
||||
// Read the timer again to accurately determine how long we actually slept,
|
||||
// rather than calculating it by adding sleep_time to time.
|
||||
time = porting::getTimeUs();
|
||||
|
||||
if (time > last_time) // Make sure last_time hasn't overflowed
|
||||
*dtime = (time - last_time) / 1000000.0f;
|
||||
else
|
||||
*dtime = 0;
|
||||
|
||||
last_time = time;
|
||||
}
|
||||
|
||||
void Game::showOverlayMessage(const char *msg, float dtime, int percent, bool draw_sky)
|
||||
{
|
||||
m_rendering_engine->draw_load_screen(wstrgettext(msg), guienv, texture_src,
|
||||
|
|
|
@ -44,6 +44,46 @@ RenderingEngine *RenderingEngine::s_singleton = nullptr;
|
|||
const video::SColor RenderingEngine::MENU_SKY_COLOR = video::SColor(255, 140, 186, 250);
|
||||
const float RenderingEngine::BASE_BLOOM_STRENGTH = 1.0f;
|
||||
|
||||
/* Helper stuff */
|
||||
|
||||
void FpsControl::reset()
|
||||
{
|
||||
last_time = porting::getTimeUs();
|
||||
}
|
||||
|
||||
void FpsControl::limit(IrrlichtDevice *device, f32 *dtime, bool assume_paused)
|
||||
{
|
||||
const float fps_limit = (device->isWindowFocused() && !assume_paused)
|
||||
? g_settings->getFloat("fps_max")
|
||||
: g_settings->getFloat("fps_max_unfocused");
|
||||
const u64 frametime_min = 1000000.0f / std::max(fps_limit, 1.0f);
|
||||
|
||||
u64 time = porting::getTimeUs();
|
||||
|
||||
if (time > last_time) // Make sure time hasn't overflowed
|
||||
busy_time = time - last_time;
|
||||
else
|
||||
busy_time = 0;
|
||||
|
||||
if (busy_time < frametime_min) {
|
||||
sleep_time = frametime_min - busy_time;
|
||||
if (sleep_time > 0)
|
||||
sleep_us(sleep_time);
|
||||
} else {
|
||||
sleep_time = 0;
|
||||
}
|
||||
|
||||
// Read the timer again to accurately determine how long we actually slept,
|
||||
// rather than calculating it by adding sleep_time to time.
|
||||
time = porting::getTimeUs();
|
||||
|
||||
if (time > last_time) // Make sure last_time hasn't overflowed
|
||||
*dtime = (time - last_time) / 1000000.0f;
|
||||
else
|
||||
*dtime = 0;
|
||||
|
||||
last_time = time;
|
||||
}
|
||||
|
||||
static gui::GUISkin *createSkin(gui::IGUIEnvironment *environment,
|
||||
gui::EGUI_SKIN_TYPE type, video::IVideoDriver *driver)
|
||||
|
@ -66,7 +106,6 @@ static gui::GUISkin *createSkin(gui::IGUIEnvironment *environment,
|
|||
return skin;
|
||||
}
|
||||
|
||||
|
||||
static std::optional<video::E_DRIVER_TYPE> chooseVideoDriver()
|
||||
{
|
||||
auto &&configured_name = g_settings->get("video_driver");
|
||||
|
@ -106,6 +145,8 @@ static irr::IrrlichtDevice *createDevice(SIrrlichtCreationParameters params, std
|
|||
throw std::runtime_error("Could not initialize the device with any supported video driver");
|
||||
}
|
||||
|
||||
/* RenderingEngine class */
|
||||
|
||||
RenderingEngine::RenderingEngine(IEventReceiver *receiver)
|
||||
{
|
||||
sanity_check(!s_singleton);
|
||||
|
|
|
@ -46,6 +46,19 @@ class RenderingCore;
|
|||
// Instead of a mechanism to disable fog we just set it to be really far away
|
||||
#define FOG_RANGE_ALL (100000 * BS)
|
||||
|
||||
struct FpsControl {
|
||||
FpsControl() : last_time(0), busy_time(0), sleep_time(0) {}
|
||||
|
||||
void reset();
|
||||
|
||||
void limit(IrrlichtDevice *device, f32 *dtime, bool assume_paused = false);
|
||||
|
||||
u32 getBusyMs() const { return busy_time / 1000; }
|
||||
|
||||
// all values in microseconds (us)
|
||||
u64 last_time, busy_time, sleep_time;
|
||||
};
|
||||
|
||||
class RenderingEngine
|
||||
{
|
||||
public:
|
||||
|
@ -103,11 +116,6 @@ public:
|
|||
return s_singleton->m_device;
|
||||
}
|
||||
|
||||
u32 get_timer_time()
|
||||
{
|
||||
return m_device->getTimer()->getTime();
|
||||
}
|
||||
|
||||
gui::IGUIEnvironment *get_gui_env()
|
||||
{
|
||||
return m_device->getGUIEnvironment();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue