Commit Graph

61 Commits

Author SHA1 Message Date
Michael Lutz 421b599541 Codechange: [SDL2] Split driver in base-part and default backend 2021-02-22 22:16:07 +01:00
Michael Lutz a77b202767 Codechange: [SDL] Move dirty_rect to class scope. 2021-02-22 22:16:07 +01:00
Patric Stout e75858ce5e Codechange: [SDL2] Allow several places to hook into the SDL driver
This allows future subdrivers to use these to manage their
own flow.
2021-02-22 22:16:07 +01:00
Patric Stout 101e394475 Codechange: [SDL2] Move functions and variables to class-scope
This allows future subdrivers to override them.
2021-02-22 22:16:07 +01:00
Patric Stout 0d58bc9384 Codechange: [SDL2] Move SDLSurface code to its own function
This increases readability, and allow future subdrivers to not
use SDLSurface to draw.
2021-02-22 22:16:07 +01:00
Patric Stout 6098811b49 Codechange: [SDL2] Split Start() in a few more functions
This makes it a bit easier to follow what is going on, and
allow future subdrivers to hook into a few of these functions.

Reworked the code slighly while at it, to return early where
possible.
2021-02-22 22:16:07 +01:00
Michael Lutz d16dc22fdf Codechange: Use C++-ism for zeroing. 2021-02-20 21:14:44 +01:00
Patric Stout 0e76d965f1 Codechange: deduplicate tick-handlers of all video drivers
They were all identical, so better put this in a single place
hoping it is less likely to break.
2021-02-20 17:08:44 +01:00
Patric Stout ec1dd0bf61 Codechange: be consistent in what CheckPaletteAnim() does and when it is called
Additionally, make sure this is a class method. Later commits
will make use of this.
2021-02-20 17:08:44 +01:00
Patric Stout 790fa7102e Codechange: be consistent in naming the paint function Paint()
Also move this function to be a class member. This to allow
further deduplicating of code in a later commit.
2021-02-20 17:08:44 +01:00
Patric Stout 761efbb457 Codechange: use (Un)LockVideoBuffer() to manage video buffer 2021-02-20 17:08:44 +01:00
Patric Stout 661eb39ecc Codechange: move all input-handling of video-drivers into InputLoop 2021-02-20 17:08:44 +01:00
Patric Stout 932abe6d5c Fix fa170b9: [SDL2] forgot to use GetGameInterval where needed 2021-02-19 11:34:00 +01:00
Patric Stout c4df0f95ae Fix: during switching of game-mode, drawing could show closed windows that shouldn't be closed yet
The higher your refresh-rate, the more likely this is. Mostly you
notice this when creating a new game or when abandoning a game.

This is a bit of a hack to keep the old behaviour, as before this
patch the game was already freezing your mouse while it was changing
game-mode, and it does this too after this patch. Just now it
freezes too a few frames earlier, to prevent not drawing windows
people still expect to see.
2021-02-19 10:43:15 +01:00
Patric Stout fa170b9ace Feature: configurable refresh-rate and change default to 60fps
Most modern games run on 60 fps, and for good reason. This gives
a much smoother experiences.

As some people have monitors that can do 144Hz or even 240Hz, allow
people to configure the refresh rate. Of course, the higher you
set the value, the more time the game spends on drawing pixels
instead of simulating the game, which has an effect on simulation
speed.

The simulation will still always run at 33.33 fps, and is not
influences by this setting.
2021-02-19 10:43:15 +01:00
Patric Stout eb9b1ad68d Change: sleep till the next tick in the main loop
Sleep for 1ms (which is always (a lot) more than 1ms) is just
randomly guessing and hoping you hit your deadline, give or take.

But given we can calculate when our next frame is happening, we
can just sleep for that exact amount. As these values are often
a bit larger, it is also more likely the OS can schedule us back
in close to our requested target. This means it is more likely we
hit our deadlines, which makes the FPS a lot more stable.
2021-02-19 10:43:15 +01:00
Patric Stout ae7a2b9f02 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-19 10:43:15 +01:00
Patric Stout c81c6e5eb7 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-19 10:43:15 +01:00
Patric Stout 5bfa014505 Codechange: track _realtime_tick more accurate
_realtime_tick was reset every time the diff was calculated. This
means if it would trigger, say, every N.9 milliseconds, it would
after two iterations already drift a millisecond. This adds up
pretty quick.
2021-02-19 10:43:15 +01:00
Patric Stout 25f6851ca1 Codechange: switch all video drivers to std::chrono for keeping time
On all OSes we tested the std::chrono::steady_clock is of a high
enough resolution to do millisecond measurements, which is all we
need.

By accident, this fixes a Win32 driver bug, where we would never
hit our targets, as the resolution of the clock was too low to
do accurate millisecond measurements with (it was ~16ms resolution
instead).
2021-02-19 10:43:15 +01:00
Patric Stout 8e0d48a0f6
Fix: [SDL2] simplify what to redraw to prevent tearing (#8685)
When there are a lot of rects to redraw, of which one of the last
ones is almost the full screen, visual tearing happens over the
vertical axis. This is most visible when scrolling the map.

This can be prevented by using less rects. To simplify the situation,
and as solutions like OpenGL need this anyway, keep a single rect
that shows the biggest size that updates everything correctly.

Although this means it needs a bit more time redrawing where it
is strictly seen not needed, it also means less commands have
to be executed in the backend. In the end, this is a trade-off,
and from experiments it seems the approach of this commit gives
a better result.
2021-02-17 21:19:32 +01:00
Patric Stout 6de188d025
Fix 52317bb7: [SDL2] ensure we don't try to blit out of bounds (#8684)
During resizing, there can still be dirty-rects ready to blit based
on the old dimensions. X11 with shared memory enabled crashes if
you try to do this. So, instead, if we resize, reset the dirty-rects.

This is fine, as moments later we mark the whole (new) screen as
dirty anyway.
2021-02-17 14:06:12 +01:00
milek7 751f595bb6 Fix: VkMapping declarations violated C++ ODR rule. 2021-02-14 23:19:50 +01:00
Patric Stout 52317bb7df Change: [SDL2] Remove unneeded delay of redrawing the screen
In testing, I could find no reason why this statement is here.

The comment is rather unclear (it states what it does, but not
why it would be needed).

This line of code was introduced with f4f40448, which gives no
further insight on why it would be needed to have it here.

As such, let's remove it and see if anyone else reports any
problems with it. If so, this commit can be reverted and a more
clear comment should be added what this line of code is dealing
with (the WHY, not the WHAT).
2021-02-11 20:23:53 +01:00
Patric Stout 2e1535389a Codechange: [SDL2] Don't use globals if we can do with locals 2021-02-11 20:23:53 +01:00
Patric Stout 2bbef6b5cf Codechange: [SDL2] Name paint function Paint(), like other drivers do
This reduces confusion when reading different drivers.
2021-02-11 20:23:53 +01:00
Patric Stout 569ce6c7b4 Fix 30e69c51: palette was not marked dirty when creating a new
This means the code depended that the caller did this for us
before MakePalette() is executed, which is neither a
requirement nor a promise the code makes.
2021-02-11 20:23:53 +01:00
Patric Stout 2c9084d48c
Fix #8029: [SDL2] disable draw-thread on wayland SDL video driver (#8648)
When the wayland SDL video driver is used, an EGL context is
created in the main thread. It is not allowed to update this
context from another thread, which is exactly what our draw-thread
is trying.

The other solution would be to move all of SDL into the
draw-thread, but that would introduce a whole scala of different
problems.

The wayland SDL backend is significantly faster than the
X11 SDL backend, but there is a performance hit nevertheless.
2021-02-06 14:09:45 +01:00
SamuXarick a4035af337
Codechange: Apply coding style (#8640)
* Fix: Missing or needed spaces

* Codechange: Remove space

* Codechange: Remove space

* Codechange: More missing spaces

* Codechange: Missing spaces

* Codechange: Remove space

* Codechange: Remove space
2021-02-05 11:00:36 +01:00
Patric Stout 0e54c32452 Codechange: [SDL2] Use MakeDirty() to force a redraw
The original code is "strictly correct", but just reads really
weird, and we use MakeDirty() in several other places instead too.
2021-01-30 21:43:59 +01:00
Patric Stout 30e69c518b Codechange: [SDL2] Rework how palette is updated
It now follows more what the Win32 driver does, and has far less
exceptions and special casing.

MakePalette creates the Palette and prepares surface.
UpdatePalette updates the Palette.
CheckPaletteAnim checks if UpdatePalette needs to be called and
  marks the whole screen dirty so DrawSurfaceToScreen will do a
  full redraw.
2021-01-30 21:43:59 +01:00
Patric Stout eb80fefd1d Fix: [SDL2] Display why SDL_CreateWindow() failed in case it does
All SDL_NNN errors print SDL_GetError, except for this one place.
2021-01-30 21:43:59 +01:00
Patric Stout 19345908cb Codechange: [SDL2] Split away CreateMainWindow from CreateMainSurface
This makes the code a bit more readable, as both intentions are
more clear, and there is less nesting in the main function.
2021-01-30 21:43:59 +01:00
Patric Stout 8c37e5c526 Codechange: [SDL2] reworked the different surfaces to make it more readable 2021-01-30 21:43:59 +01:00
Patric Stout 8de325f256 Codechange: [SDL2] Only prepare "caption" if you are going to us it 2021-01-30 21:43:59 +01:00
Patric Stout e41ec5b42e Codechange: [SDL2] Minor code cleanup to remove silly variable 2021-01-30 21:43:59 +01:00
Patric Stout 678031f9b3 Codechange: [SDL2] Only set _cur_palette, never _local_palette 2021-01-30 21:43:59 +01:00
Patric Stout f31b65825f Codechange: [SDL2] Move FindStartupDisplay to its own function 2021-01-30 21:43:59 +01:00
Patric Stout 6916fc76bd Codechange: [SDL2] reworked FindResolutions to be more like the rest
There was no default resolution fallback, and the code was different
from the win32 driver. It is now named the same and much more
similar.
2021-01-30 21:43:59 +01:00
Patric Stout a52d716c88 Codechange: [SDL2] remove include-protection
This is already done by CMake: if SDL2 is not detected, this file
is not included.
2021-01-30 21:43:59 +01:00
Patric Stout 1eceee915e Codechange: [SDL2/Win32] Be consistent how 0bpp blitters are not allowed
Sometimes it returned an usererror(), sometimes Start() failed.
Now it always fails on Start(), so nothing else has to check again
what blitter is used.

AfterBlitterChange() can never change to a 0bpp, so it is sufficient
to guard this with an assert().
2021-01-17 19:57:36 +01:00
frosch 540fdfbf5a
Fix 0e62a398c7f: Only center the window, when it is smaller than the screen. (#8581) 2021-01-16 23:36:15 +01:00
frosch 0e62a398c7
Add: [SDL2] video driver parameter to put OpenTTD on a particular display on start. By default use the display where the mouse cursor is. (#8572) 2021-01-14 23:29:29 +01:00
Michael Lutz fa60c1f8b9
Feature: Choose a sensible window size on a fresh OTTD config file. (#8536) 2021-01-14 21:53:06 +01:00
Charles Pigott 9b800a96ed
Codechange: Remove min/max functions in favour of STL variants (#8502) 2021-01-08 11:16:18 +01:00
Michael Lutz f3326d34e7 Codechange: Use std::string in FIO search path handling. 2020-12-27 13:19:25 +01:00
Patric Stout d5b9f7ac37 Add: [Emscripten] use "relative mouse mode" with SDL2
This mode doesn't wrap the mouse constantly, but requests SDL
to lock the mouse pointer. This is needed, as with Emscripten
you are not allowed to change the mouse poisition (only to lock
it into place).
2020-12-15 15:46:39 +01:00
Patric Stout d15dc9f40f Add: support for emscripten (play-OpenTTD-in-the-browser)
Emscripten compiles to WASM, which can be loaded via
HTML / JavaScript. This allows you to play OpenTTD inside a
browser.

Co-authored-by: milek7 <me@milek7.pl>
2020-12-15 15:46:39 +01:00
Patric Stout 2da07f7615 Codechange: unroll the SDL2 main loop
This commit prepares for the next commit, as Emscripten needs to
have a way to trigger a single iteration of the main loop. To
keep the real changes more clear, this commit only unrolls the
loop, and makes no changes to the logic itself.
2020-12-15 15:46:39 +01:00
nikolas c9aff698d0
Fix #8104: Always add WINDOW_RESIZABLE flag to SDL2 (#8211)
This fixes a bug that can reproduced with these steps:
* Start openttd in fullscreen mode
* Turn off fullscreen mode
* Try to resize the window. The window can't be resized.
2020-06-18 08:53:06 +01:00