2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file dedicated_v.cpp Dedicated server video 'driver'. */
|
|
|
|
|
2005-07-25 09:16:10 +02:00
|
|
|
#include "../stdafx.h"
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2007-12-23 11:56:02 +01:00
|
|
|
#include "../gfx_func.h"
|
2010-01-15 17:41:15 +01:00
|
|
|
#include "../network/network.h"
|
2008-01-13 22:51:53 +01:00
|
|
|
#include "../network/network_internal.h"
|
2008-05-24 12:15:06 +02:00
|
|
|
#include "../console_func.h"
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 12:00:30 +02:00
|
|
|
#include "../genworld.h"
|
2008-08-31 12:50:05 +02:00
|
|
|
#include "../fileio_type.h"
|
2007-12-28 04:14:55 +01:00
|
|
|
#include "../fios.h"
|
2007-06-17 22:30:28 +02:00
|
|
|
#include "../blitter/factory.hpp"
|
2008-09-30 22:51:04 +02:00
|
|
|
#include "../company_func.h"
|
2008-01-29 01:26:31 +01:00
|
|
|
#include "../core/random_func.hpp"
|
2010-09-10 22:03:45 +02:00
|
|
|
#include "../saveload/saveload.h"
|
2019-03-17 11:05:53 +01:00
|
|
|
#include "../thread.h"
|
Add: draw the screen at a steady pace, also during fast-forward
During fast-forward, the game was drawing as fast as it could. This
means that the fast-forward was limited also by how fast we could
draw, something that people in general don't expect.
To give an extreme case, if you are fully zoomed out on a busy
map, fast-forward would be mostly limited because of the time it
takes to draw the screen.
By decoupling the draw-tick and game-tick, we can keep the pace
of the draw-tick the same while speeding up the game-tick. To use
the extreme case as example again, if you are fully zoomed out
now, the screen only redraws 33.33 times per second, fast-forwarding
or not. This means fast-forward is much more likely to go at the
same speed, no matter what you are looking at.
2021-02-17 15:04:46 +01:00
|
|
|
#include "../window_func.h"
|
2005-07-25 18:33:58 +02:00
|
|
|
#include "dedicated_v.h"
|
2005-07-23 19:22:39 +02:00
|
|
|
|
|
|
|
#ifdef __OS2__
|
|
|
|
# include <sys/time.h> /* gettimeofday */
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <conio.h>
|
2005-07-23 21:00:56 +02:00
|
|
|
|
|
|
|
# define INCL_DOS
|
|
|
|
# include <os2.h>
|
|
|
|
|
2005-07-23 19:22:39 +02:00
|
|
|
# define STDIN 0 /* file descriptor for standard input */
|
|
|
|
|
2005-08-01 18:31:19 +02:00
|
|
|
/**
|
2005-07-23 20:46:17 +02:00
|
|
|
* Switches OpenTTD to a console app at run-time, instead of a PM app
|
2010-08-01 21:44:49 +02:00
|
|
|
* Necessary to see stdout, etc.
|
|
|
|
*/
|
2007-03-07 12:47:46 +01:00
|
|
|
static void OS2_SwitchToConsoleMode()
|
2005-07-23 20:46:17 +02:00
|
|
|
{
|
|
|
|
PPIB pib;
|
|
|
|
PTIB tib;
|
|
|
|
|
|
|
|
DosGetInfoBlocks(&tib, &pib);
|
|
|
|
|
2009-03-15 01:32:18 +01:00
|
|
|
/* Change flag from PM to VIO */
|
2005-07-23 20:46:17 +02:00
|
|
|
pib->pib_ultype = 3;
|
|
|
|
}
|
2005-07-23 19:22:39 +02:00
|
|
|
#endif
|
|
|
|
|
2018-04-29 14:12:23 +02:00
|
|
|
#if defined(UNIX)
|
2005-07-23 19:22:39 +02:00
|
|
|
# include <sys/time.h> /* gettimeofday */
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <signal.h>
|
|
|
|
# define STDIN 0 /* file descriptor for standard input */
|
|
|
|
|
|
|
|
/* Signal handlers */
|
|
|
|
static void DedicatedSignalHandler(int sig)
|
|
|
|
{
|
2010-09-10 22:03:45 +02:00
|
|
|
if (_game_mode == GM_NORMAL && _settings_client.gui.autosave_on_exit) DoExitSave();
|
2005-07-23 19:22:39 +02:00
|
|
|
_exit_game = true;
|
|
|
|
signal(sig, DedicatedSignalHandler);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-09 02:28:14 +01:00
|
|
|
#if defined(_WIN32)
|
2007-08-04 14:53:41 +02:00
|
|
|
# include <windows.h> /* GetTickCount */
|
2018-04-29 14:21:31 +02:00
|
|
|
# include <conio.h>
|
2007-08-04 14:53:41 +02:00
|
|
|
# include <time.h>
|
|
|
|
# include <tchar.h>
|
2016-10-30 19:22:55 +01:00
|
|
|
# include "../os/windows/win32.h"
|
2019-08-06 08:43:00 +02:00
|
|
|
|
2006-09-14 22:40:59 +02:00
|
|
|
static HANDLE _hInputReady, _hWaitForInputHandling;
|
|
|
|
static HANDLE _hThread; // Thread to close
|
2005-07-23 19:22:39 +02:00
|
|
|
static char _win_console_thread_buffer[200];
|
|
|
|
|
|
|
|
/* Windows Console thread. Just loop and signal when input has been received */
|
2007-03-07 12:47:46 +01:00
|
|
|
static void WINAPI CheckForConsoleInput()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2019-03-17 01:59:46 +01:00
|
|
|
SetCurrentThreadName("ottd:win-console");
|
2016-10-30 19:22:55 +01:00
|
|
|
|
2007-03-09 01:15:43 +01:00
|
|
|
DWORD nb;
|
|
|
|
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
2011-02-25 22:53:43 +01:00
|
|
|
for (;;) {
|
2019-04-10 23:07:06 +02:00
|
|
|
ReadFile(hStdin, _win_console_thread_buffer, lengthof(_win_console_thread_buffer), &nb, nullptr);
|
2014-04-24 19:49:31 +02:00
|
|
|
if (nb >= lengthof(_win_console_thread_buffer)) nb = lengthof(_win_console_thread_buffer) - 1;
|
|
|
|
_win_console_thread_buffer[nb] = '\0';
|
|
|
|
|
2006-09-14 22:40:59 +02:00
|
|
|
/* Signal input waiting that input is read and wait for it being handled
|
|
|
|
* SignalObjectAndWait() should be used here, but it's unsupported in Win98< */
|
|
|
|
SetEvent(_hInputReady);
|
|
|
|
WaitForSingleObject(_hWaitForInputHandling, INFINITE);
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static void CreateWindowsConsoleThread()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
|
|
|
DWORD dwThreadId;
|
|
|
|
/* Create event to signal when console input is ready */
|
2019-04-10 23:07:06 +02:00
|
|
|
_hInputReady = CreateEvent(nullptr, false, false, nullptr);
|
|
|
|
_hWaitForInputHandling = CreateEvent(nullptr, false, false, nullptr);
|
|
|
|
if (_hInputReady == nullptr || _hWaitForInputHandling == nullptr) usererror("Cannot create console event!");
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
_hThread = CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, nullptr, 0, &dwThreadId);
|
|
|
|
if (_hThread == nullptr) usererror("Cannot create console thread!");
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(driver, 2, "Windows console thread started");
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static void CloseWindowsConsoleThread()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2006-09-14 22:40:59 +02:00
|
|
|
CloseHandle(_hThread);
|
|
|
|
CloseHandle(_hInputReady);
|
|
|
|
CloseHandle(_hWaitForInputHandling);
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(driver, 2, "Windows console thread shut down");
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2005-07-23 20:46:17 +02:00
|
|
|
|
2007-06-12 22:24:12 +02:00
|
|
|
static void *_dedicated_video_mem;
|
2005-07-23 20:46:17 +02:00
|
|
|
|
2010-07-19 17:44:49 +02:00
|
|
|
/* Whether a fork has been done. */
|
|
|
|
bool _dedicated_forks;
|
|
|
|
|
2020-12-06 21:11:45 +01:00
|
|
|
extern bool SafeLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = nullptr);
|
2005-07-23 20:46:17 +02:00
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
|
2005-07-23 20:46:17 +02:00
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
|
2020-05-17 23:32:08 +02:00
|
|
|
const char *VideoDriver_Dedicated::Start(const StringList &parm)
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2021-01-14 21:53:06 +01:00
|
|
|
this->UpdateAutoResolution();
|
|
|
|
|
2014-01-02 23:41:58 +01:00
|
|
|
int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
|
2019-04-10 23:07:06 +02:00
|
|
|
_dedicated_video_mem = (bpp == 0) ? nullptr : MallocT<byte>(_cur_resolution.width * _cur_resolution.height * (bpp / 8));
|
2007-06-12 22:24:12 +02:00
|
|
|
|
2008-06-16 21:38:41 +02:00
|
|
|
_screen.width = _screen.pitch = _cur_resolution.width;
|
|
|
|
_screen.height = _cur_resolution.height;
|
2009-12-19 19:46:40 +01:00
|
|
|
_screen.dst_ptr = _dedicated_video_mem;
|
2008-04-19 23:51:05 +02:00
|
|
|
ScreenSizeChanged();
|
2014-01-02 23:41:58 +01:00
|
|
|
BlitterFactory::GetCurrentBlitter()->PostResize();
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2018-12-09 02:28:14 +01:00
|
|
|
#if defined(_WIN32)
|
2009-03-15 01:32:18 +01:00
|
|
|
/* For win32 we need to allocate a console (debug mode does the same) */
|
2005-07-23 19:22:39 +02:00
|
|
|
CreateConsole();
|
|
|
|
CreateWindowsConsoleThread();
|
2006-11-28 20:58:13 +01:00
|
|
|
SetConsoleTitle(_T("OpenTTD Dedicated Server"));
|
2005-07-23 19:22:39 +02:00
|
|
|
#endif
|
|
|
|
|
2011-11-05 00:47:00 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
/* Disable the MSVC assertion message box. */
|
|
|
|
_set_error_mode(_OUT_TO_STDERR);
|
|
|
|
#endif
|
|
|
|
|
2005-07-23 19:22:39 +02:00
|
|
|
#ifdef __OS2__
|
2009-03-15 01:32:18 +01:00
|
|
|
/* For OS/2 we also need to switch to console mode instead of PM mode */
|
2005-07-23 19:22:39 +02:00
|
|
|
OS2_SwitchToConsoleMode();
|
|
|
|
#endif
|
|
|
|
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(driver, 1, "Loading dedicated server");
|
2019-04-10 23:07:06 +02:00
|
|
|
return nullptr;
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
void VideoDriver_Dedicated::Stop()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2018-12-09 02:28:14 +01:00
|
|
|
#ifdef _WIN32
|
2005-07-23 19:22:39 +02:00
|
|
|
CloseWindowsConsoleThread();
|
|
|
|
#endif
|
|
|
|
free(_dedicated_video_mem);
|
|
|
|
}
|
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
void VideoDriver_Dedicated::MakeDirty(int left, int top, int width, int height) {}
|
|
|
|
bool VideoDriver_Dedicated::ChangeResolution(int w, int h) { return false; }
|
2008-01-01 15:20:48 +01:00
|
|
|
bool VideoDriver_Dedicated::ToggleFullscreen(bool fs) { return false; }
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2018-04-29 14:12:23 +02:00
|
|
|
#if defined(UNIX) || defined(__OS2__)
|
2007-03-07 12:47:46 +01:00
|
|
|
static bool InputWaiting()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set readfds;
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 1;
|
|
|
|
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(STDIN, &readfds);
|
|
|
|
|
|
|
|
/* don't care about writefds and exceptfds: */
|
2019-04-10 23:07:06 +02:00
|
|
|
return select(STDIN + 1, &readfds, nullptr, nullptr, &tv) > 0;
|
2005-07-23 20:46:17 +02:00
|
|
|
}
|
2005-07-23 19:22:39 +02:00
|
|
|
|
|
|
|
#else
|
2005-07-23 20:46:17 +02:00
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static bool InputWaiting()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2006-09-14 22:40:59 +02:00
|
|
|
return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
|
2005-07-23 20:46:17 +02:00
|
|
|
}
|
2005-07-23 19:22:39 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static void DedicatedHandleKeyInput()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2008-08-12 22:31:49 +02:00
|
|
|
static char input_line[1024] = "";
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2006-09-14 22:40:59 +02:00
|
|
|
if (!InputWaiting()) return;
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2006-09-14 22:40:59 +02:00
|
|
|
if (_exit_game) return;
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2018-04-29 14:12:23 +02:00
|
|
|
#if defined(UNIX) || defined(__OS2__)
|
2019-04-10 23:07:06 +02:00
|
|
|
if (fgets(input_line, lengthof(input_line), stdin) == nullptr) return;
|
2005-07-23 19:22:39 +02:00
|
|
|
#else
|
2013-01-08 23:46:42 +01:00
|
|
|
/* Handle console input, and signal console thread, it can accept input again */
|
2020-12-27 11:44:22 +01:00
|
|
|
static_assert(lengthof(_win_console_thread_buffer) <= lengthof(input_line));
|
2009-04-10 22:37:05 +02:00
|
|
|
strecpy(input_line, _win_console_thread_buffer, lastof(input_line));
|
2006-09-14 22:40:59 +02:00
|
|
|
SetEvent(_hWaitForInputHandling);
|
2005-07-23 19:22:39 +02:00
|
|
|
#endif
|
|
|
|
|
2014-04-24 19:49:31 +02:00
|
|
|
/* Remove trailing \r or \n */
|
2008-08-04 17:07:17 +02:00
|
|
|
for (char *c = input_line; *c != '\0'; c++) {
|
|
|
|
if (*c == '\n' || *c == '\r' || c == lastof(input_line)) {
|
|
|
|
*c = '\0';
|
|
|
|
break;
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
}
|
2009-03-06 02:23:25 +01:00
|
|
|
str_validate(input_line, lastof(input_line));
|
2005-07-23 19:22:39 +02:00
|
|
|
|
|
|
|
IConsoleCmdExec(input_line); // execute command
|
|
|
|
}
|
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
void VideoDriver_Dedicated::MainLoop()
|
2005-07-23 19:22:39 +02:00
|
|
|
{
|
2021-02-17 14:46:19 +01:00
|
|
|
auto cur_ticks = std::chrono::steady_clock::now();
|
2021-02-17 14:49:45 +01:00
|
|
|
auto last_realtime_tick = cur_ticks;
|
Change: allow video-drivers to miss deadlines slightly
Before, every next frame was calculated from the current time.
If for some reason the current frame was drifting a bit, the
next would too, and the next more, etc etc. This meant we rarely
hit the targets we would like, like 33.33fps.
Instead, allow video-drivers to drift slightly, and schedule the
next frame based on the time the last should have happened. Only
if the drift gets too much, that deadlines are missed for longer
period of times, schedule the next frame based on the current
time.
This makes the FPS a lot smoother, as sleeps aren't as exact as
you might think.
2021-02-17 15:14:59 +01:00
|
|
|
auto next_game_tick = cur_ticks;
|
2005-07-23 19:22:39 +02:00
|
|
|
|
|
|
|
/* Signal handlers */
|
2018-04-29 14:12:23 +02:00
|
|
|
#if defined(UNIX)
|
2005-07-23 19:22:39 +02:00
|
|
|
signal(SIGTERM, DedicatedSignalHandler);
|
|
|
|
signal(SIGINT, DedicatedSignalHandler);
|
|
|
|
signal(SIGQUIT, DedicatedSignalHandler);
|
|
|
|
#endif
|
|
|
|
|
2009-03-15 01:32:18 +01:00
|
|
|
/* Load the dedicated server stuff */
|
2005-07-23 19:22:39 +02:00
|
|
|
_is_network_server = true;
|
|
|
|
_network_dedicated = true;
|
2010-06-05 15:32:42 +02:00
|
|
|
_current_company = _local_company = COMPANY_SPECTATOR;
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2011-05-28 15:55:34 +02:00
|
|
|
/* If SwitchMode is SM_LOAD_GAME, it means that the user used the '-g' options */
|
|
|
|
if (_switch_mode != SM_LOAD_GAME) {
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 12:00:30 +02:00
|
|
|
StartNewGameWithoutGUI(GENERATE_NEW_SEED);
|
2009-02-25 01:45:52 +01:00
|
|
|
SwitchToMode(_switch_mode);
|
2005-07-23 19:22:39 +02:00
|
|
|
_switch_mode = SM_NONE;
|
|
|
|
} else {
|
|
|
|
_switch_mode = SM_NONE;
|
|
|
|
/* First we need to test if the savegame can be loaded, else we will end up playing the
|
|
|
|
* intro game... */
|
2016-09-04 14:57:43 +02:00
|
|
|
if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.file_op, _file_to_saveload.detail_ftype, GM_NORMAL, BASE_DIR)) {
|
2005-07-23 19:22:39 +02:00
|
|
|
/* Loading failed, pop out.. */
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 0, "Loading requested map failed, aborting");
|
2005-07-23 19:22:39 +02:00
|
|
|
_networking = false;
|
|
|
|
} else {
|
|
|
|
/* We can load this game, so go ahead */
|
2011-05-28 15:55:34 +02:00
|
|
|
SwitchToMode(SM_LOAD_GAME);
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-15 01:32:18 +01:00
|
|
|
/* Done loading, start game! */
|
2005-07-23 19:22:39 +02:00
|
|
|
|
|
|
|
if (!_networking) {
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(net, 0, "Dedicated server could not be started, aborting");
|
2005-07-29 18:40:29 +02:00
|
|
|
return;
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
|
2005-07-29 18:40:29 +02:00
|
|
|
while (!_exit_game) {
|
2005-07-23 19:22:39 +02:00
|
|
|
InteractiveRandom(); // randomness
|
|
|
|
|
2010-07-19 17:44:49 +02:00
|
|
|
if (!_dedicated_forks) DedicatedHandleKeyInput();
|
2005-07-23 19:22:39 +02:00
|
|
|
|
2021-02-17 14:46:19 +01:00
|
|
|
cur_ticks = std::chrono::steady_clock::now();
|
2021-02-17 14:49:45 +01:00
|
|
|
|
|
|
|
/* If more than a millisecond has passed, increase the _realtime_tick. */
|
|
|
|
if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) {
|
|
|
|
auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick);
|
|
|
|
_realtime_tick += delta.count();
|
|
|
|
last_realtime_tick += delta;
|
|
|
|
}
|
|
|
|
|
Change: allow video-drivers to miss deadlines slightly
Before, every next frame was calculated from the current time.
If for some reason the current frame was drifting a bit, the
next would too, and the next more, etc etc. This meant we rarely
hit the targets we would like, like 33.33fps.
Instead, allow video-drivers to drift slightly, and schedule the
next frame based on the time the last should have happened. Only
if the drift gets too much, that deadlines are missed for longer
period of times, schedule the next frame based on the current
time.
This makes the FPS a lot smoother, as sleeps aren't as exact as
you might think.
2021-02-17 15:14:59 +01:00
|
|
|
if (cur_ticks >= next_game_tick || _ddc_fastforward) {
|
|
|
|
if (_ddc_fastforward) {
|
2021-02-17 15:31:09 +01:00
|
|
|
next_game_tick = cur_ticks + this->GetGameInterval();
|
Change: allow video-drivers to miss deadlines slightly
Before, every next frame was calculated from the current time.
If for some reason the current frame was drifting a bit, the
next would too, and the next more, etc etc. This meant we rarely
hit the targets we would like, like 33.33fps.
Instead, allow video-drivers to drift slightly, and schedule the
next frame based on the time the last should have happened. Only
if the drift gets too much, that deadlines are missed for longer
period of times, schedule the next frame based on the current
time.
This makes the FPS a lot smoother, as sleeps aren't as exact as
you might think.
2021-02-17 15:14:59 +01:00
|
|
|
} else {
|
2021-02-17 15:31:09 +01:00
|
|
|
next_game_tick += this->GetGameInterval();
|
Change: allow video-drivers to miss deadlines slightly
Before, every next frame was calculated from the current time.
If for some reason the current frame was drifting a bit, the
next would too, and the next more, etc etc. This meant we rarely
hit the targets we would like, like 33.33fps.
Instead, allow video-drivers to drift slightly, and schedule the
next frame based on the time the last should have happened. Only
if the drift gets too much, that deadlines are missed for longer
period of times, schedule the next frame based on the current
time.
This makes the FPS a lot smoother, as sleeps aren't as exact as
you might think.
2021-02-17 15:14:59 +01:00
|
|
|
/* Avoid next_game_tick getting behind more and more if it cannot keep up. */
|
2021-02-17 15:31:09 +01:00
|
|
|
if (next_game_tick < cur_ticks - ALLOWED_DRIFT * this->GetGameInterval()) next_game_tick = cur_ticks;
|
Change: allow video-drivers to miss deadlines slightly
Before, every next frame was calculated from the current time.
If for some reason the current frame was drifting a bit, the
next would too, and the next more, etc etc. This meant we rarely
hit the targets we would like, like 33.33fps.
Instead, allow video-drivers to drift slightly, and schedule the
next frame based on the time the last should have happened. Only
if the drift gets too much, that deadlines are missed for longer
period of times, schedule the next frame based on the current
time.
This makes the FPS a lot smoother, as sleeps aren't as exact as
you might think.
2021-02-17 15:14:59 +01:00
|
|
|
}
|
2005-07-23 19:22:39 +02:00
|
|
|
|
|
|
|
GameLoop();
|
Add: draw the screen at a steady pace, also during fast-forward
During fast-forward, the game was drawing as fast as it could. This
means that the fast-forward was limited also by how fast we could
draw, something that people in general don't expect.
To give an extreme case, if you are fully zoomed out on a busy
map, fast-forward would be mostly limited because of the time it
takes to draw the screen.
By decoupling the draw-tick and game-tick, we can keep the pace
of the draw-tick the same while speeding up the game-tick. To use
the extreme case as example again, if you are fully zoomed out
now, the screen only redraws 33.33 times per second, fast-forwarding
or not. This means fast-forward is much more likely to go at the
same speed, no matter what you are looking at.
2021-02-17 15:04:46 +01:00
|
|
|
InputLoop();
|
2005-07-23 19:22:39 +02:00
|
|
|
UpdateWindows();
|
|
|
|
}
|
2010-04-09 19:08:44 +02:00
|
|
|
|
|
|
|
/* Don't sleep when fast forwarding (for desync debugging) */
|
2014-04-08 22:19:41 +02:00
|
|
|
if (!_ddc_fastforward) {
|
|
|
|
/* Sleep longer on a dedicated server, if the game is paused and no clients connected.
|
|
|
|
* That can allow the CPU to better use deep sleep states. */
|
|
|
|
if (_pause_mode != 0 && !HasClients()) {
|
2021-02-17 15:19:33 +01:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
2014-04-08 22:19:41 +02:00
|
|
|
} else {
|
2021-02-17 15:19:33 +01:00
|
|
|
/* See how much time there is till we have to process the next event, and try to hit that as close as possible. */
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
if (next_game_tick > now) {
|
|
|
|
std::this_thread::sleep_for(next_game_tick - now);
|
|
|
|
}
|
2014-04-08 22:19:41 +02:00
|
|
|
}
|
|
|
|
}
|
2005-07-23 19:22:39 +02:00
|
|
|
}
|
|
|
|
}
|