2019-09-19 17:18:50 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file sdl2_v.cpp Implementation of the SDL2 video driver. */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../openttd.h"
|
|
|
|
#include "../gfx_func.h"
|
|
|
|
#include "../rev.h"
|
|
|
|
#include "../blitter/factory.hpp"
|
|
|
|
#include "../network/network.h"
|
|
|
|
#include "../thread.h"
|
|
|
|
#include "../progress.h"
|
|
|
|
#include "../core/random_func.hpp"
|
|
|
|
#include "../core/math_func.hpp"
|
2021-02-17 21:19:32 +01:00
|
|
|
#include "../core/mem_func.hpp"
|
|
|
|
#include "../core/geometry_func.hpp"
|
2019-09-19 17:18:50 +02:00
|
|
|
#include "../fileio_func.h"
|
|
|
|
#include "../framerate_type.h"
|
2019-11-04 19:16:34 +01:00
|
|
|
#include "../window_func.h"
|
2019-09-19 17:18:50 +02:00
|
|
|
#include "sdl2_v.h"
|
|
|
|
#include <SDL.h>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2020-12-05 21:57:47 +01:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
# include <emscripten.h>
|
|
|
|
# include <emscripten/html5.h>
|
|
|
|
#endif
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
#include "../safeguards.h"
|
|
|
|
|
|
|
|
static FVideoDriver_SDL iFVideoDriver_SDL;
|
|
|
|
|
|
|
|
static SDL_Window *_sdl_window;
|
|
|
|
static SDL_Surface *_sdl_surface;
|
2021-01-24 11:43:46 +01:00
|
|
|
static SDL_Surface *_sdl_rgb_surface;
|
|
|
|
static SDL_Surface *_sdl_real_surface;
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
/** Whether the drawing is/may be done in a separate thread. */
|
|
|
|
static bool _draw_threaded;
|
|
|
|
/** Mutex to keep the access to the shared memory controlled. */
|
|
|
|
static std::recursive_mutex *_draw_mutex = nullptr;
|
|
|
|
/** Signal to draw the next frame. */
|
|
|
|
static std::condition_variable_any *_draw_signal = nullptr;
|
|
|
|
/** Should we keep continue drawing? */
|
|
|
|
static volatile bool _draw_continue;
|
|
|
|
static Palette _local_palette;
|
|
|
|
static SDL_Palette *_sdl_palette;
|
|
|
|
|
2020-12-06 20:18:19 +01:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
/** Whether we just had a window-enter event. */
|
|
|
|
static bool _cursor_new_in_window = false;
|
|
|
|
#endif
|
|
|
|
|
2021-02-17 21:19:32 +01:00
|
|
|
static Rect _dirty_rect;
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
void VideoDriver_SDL::MakeDirty(int left, int top, int width, int height)
|
|
|
|
{
|
2021-02-17 21:19:32 +01:00
|
|
|
Rect r = {left, top, left + width, top + height};
|
|
|
|
_dirty_rect = BoundingRect(_dirty_rect, r);
|
2019-09-19 17:18:50 +02:00
|
|
|
}
|
|
|
|
|
2021-01-24 12:08:37 +01:00
|
|
|
static void UpdatePalette()
|
2019-09-19 17:18:50 +02:00
|
|
|
{
|
|
|
|
SDL_Color pal[256];
|
|
|
|
|
|
|
|
for (int i = 0; i != _local_palette.count_dirty; i++) {
|
|
|
|
pal[i].r = _local_palette.palette[_local_palette.first_dirty + i].r;
|
|
|
|
pal[i].g = _local_palette.palette[_local_palette.first_dirty + i].g;
|
|
|
|
pal[i].b = _local_palette.palette[_local_palette.first_dirty + i].b;
|
|
|
|
pal[i].a = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_SetPaletteColors(_sdl_palette, pal, _local_palette.first_dirty, _local_palette.count_dirty);
|
|
|
|
SDL_SetSurfacePalette(_sdl_surface, _sdl_palette);
|
2021-01-24 12:08:37 +01:00
|
|
|
}
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-01-24 12:08:37 +01:00
|
|
|
static void MakePalette()
|
|
|
|
{
|
|
|
|
if (_sdl_palette == nullptr) {
|
|
|
|
_sdl_palette = SDL_AllocPalette(256);
|
|
|
|
if (_sdl_palette == nullptr) usererror("SDL2: Couldn't allocate palette: %s", SDL_GetError());
|
|
|
|
}
|
|
|
|
|
2021-02-11 09:35:42 +01:00
|
|
|
_cur_palette.first_dirty = 0;
|
|
|
|
_cur_palette.count_dirty = 256;
|
2021-01-24 12:08:37 +01:00
|
|
|
_local_palette = _cur_palette;
|
|
|
|
UpdatePalette();
|
|
|
|
|
|
|
|
if (_sdl_surface != _sdl_real_surface) {
|
2019-09-19 17:18:50 +02:00
|
|
|
/* When using a shadow surface, also set our palette on the real screen. This lets SDL
|
|
|
|
* allocate as many colors (or approximations) as
|
|
|
|
* possible, instead of using only the default SDL
|
|
|
|
* palette. This allows us to get more colors exactly
|
|
|
|
* right and might allow using better approximations for
|
|
|
|
* other colors.
|
|
|
|
*
|
|
|
|
* Note that colors allocations are tried in-order, so
|
|
|
|
* this favors colors further up into the palette. Also
|
|
|
|
* note that if two colors from the same animation
|
|
|
|
* sequence are approximated using the same color, that
|
|
|
|
* animation will stop working.
|
|
|
|
*
|
|
|
|
* Since changing the system palette causes the colours
|
|
|
|
* to change right away, and allocations might
|
|
|
|
* drastically change, we can't use this for animation,
|
|
|
|
* since that could cause weird coloring between the
|
|
|
|
* palette change and the blitting below, so we only set
|
|
|
|
* the real palette during initialisation.
|
|
|
|
*/
|
2021-01-24 11:43:46 +01:00
|
|
|
SDL_SetSurfacePalette(_sdl_real_surface, _sdl_palette);
|
2019-09-19 17:18:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 12:08:37 +01:00
|
|
|
void VideoDriver_SDL::CheckPaletteAnim()
|
2019-09-19 17:18:50 +02:00
|
|
|
{
|
2021-01-24 12:08:37 +01:00
|
|
|
if (_cur_palette.count_dirty == 0) return;
|
|
|
|
|
2019-09-19 17:18:50 +02:00
|
|
|
_local_palette = _cur_palette;
|
2021-01-24 12:08:37 +01:00
|
|
|
this->MakeDirty(0, 0, _screen.width, _screen.height);
|
2019-09-19 17:18:50 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 11:08:20 +01:00
|
|
|
void VideoDriver_SDL::Paint()
|
2019-09-19 17:18:50 +02:00
|
|
|
{
|
2021-01-24 12:08:37 +01:00
|
|
|
PerformanceMeasurer framerate(PFE_VIDEO);
|
|
|
|
|
2021-02-17 21:19:32 +01:00
|
|
|
if (IsEmptyRect(_dirty_rect) && _cur_palette.count_dirty == 0) return;
|
2021-01-24 12:08:37 +01:00
|
|
|
|
2019-09-19 17:18:50 +02:00
|
|
|
if (_cur_palette.count_dirty != 0) {
|
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
|
|
|
|
|
|
|
switch (blitter->UsePaletteAnimation()) {
|
|
|
|
case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
|
|
|
|
UpdatePalette();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Blitter::PALETTE_ANIMATION_BLITTER:
|
|
|
|
blitter->PaletteAnimate(_local_palette);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Blitter::PALETTE_ANIMATION_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
_cur_palette.count_dirty = 0;
|
|
|
|
}
|
|
|
|
|
2021-02-17 21:19:32 +01:00
|
|
|
SDL_Rect r = { _dirty_rect.left, _dirty_rect.top, _dirty_rect.right - _dirty_rect.left, _dirty_rect.bottom - _dirty_rect.top };
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-02-17 21:19:32 +01:00
|
|
|
if (_sdl_surface != _sdl_real_surface) {
|
|
|
|
SDL_BlitSurface(_sdl_surface, &r, _sdl_real_surface, &r);
|
2019-09-19 17:18:50 +02:00
|
|
|
}
|
2021-02-17 21:19:32 +01:00
|
|
|
SDL_UpdateWindowSurfaceRects(_sdl_window, &r, 1);
|
2021-01-24 12:08:37 +01:00
|
|
|
|
2021-02-17 21:19:32 +01:00
|
|
|
MemSetT(&_dirty_rect, 0);
|
2019-09-19 17:18:50 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 11:08:20 +01:00
|
|
|
void VideoDriver_SDL::PaintThread()
|
2019-09-19 17:18:50 +02:00
|
|
|
{
|
|
|
|
/* First tell the main thread we're started */
|
|
|
|
std::unique_lock<std::recursive_mutex> lock(*_draw_mutex);
|
|
|
|
_draw_signal->notify_one();
|
|
|
|
|
|
|
|
/* Now wait for the first thing to draw! */
|
|
|
|
_draw_signal->wait(*_draw_mutex);
|
|
|
|
|
|
|
|
while (_draw_continue) {
|
|
|
|
/* Then just draw and wait till we stop */
|
2021-02-20 11:08:20 +01:00
|
|
|
this->Paint();
|
2019-09-19 17:18:50 +02:00
|
|
|
_draw_signal->wait(lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 11:08:20 +01:00
|
|
|
/* static */ void VideoDriver_SDL::PaintThreadThunk(VideoDriver_SDL *drv)
|
|
|
|
{
|
|
|
|
drv->PaintThread();
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:09:04 +01:00
|
|
|
static const Dimension default_resolutions[] = {
|
|
|
|
{ 640, 480 },
|
|
|
|
{ 800, 600 },
|
|
|
|
{ 1024, 768 },
|
|
|
|
{ 1152, 864 },
|
|
|
|
{ 1280, 800 },
|
|
|
|
{ 1280, 960 },
|
|
|
|
{ 1280, 1024 },
|
|
|
|
{ 1400, 1050 },
|
|
|
|
{ 1600, 1200 },
|
|
|
|
{ 1680, 1050 },
|
|
|
|
{ 1920, 1200 }
|
|
|
|
};
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-01-16 15:09:04 +01:00
|
|
|
static void FindResolutions()
|
|
|
|
{
|
2019-09-19 17:18:50 +02:00
|
|
|
_resolutions.clear();
|
|
|
|
|
2021-01-16 15:09:04 +01:00
|
|
|
for (int i = 0; i < SDL_GetNumDisplayModes(0); i++) {
|
|
|
|
SDL_DisplayMode mode;
|
2019-09-19 17:18:50 +02:00
|
|
|
SDL_GetDisplayMode(0, i, &mode);
|
|
|
|
|
2021-01-16 15:09:04 +01:00
|
|
|
if (mode.w < 640 || mode.h < 480) continue;
|
|
|
|
if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(mode.w, mode.h)) != _resolutions.end()) continue;
|
|
|
|
_resolutions.emplace_back(mode.w, mode.h);
|
|
|
|
}
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-01-16 15:09:04 +01:00
|
|
|
/* We have found no resolutions, show the default list */
|
|
|
|
if (_resolutions.empty()) {
|
|
|
|
_resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
|
2019-09-19 17:18:50 +02:00
|
|
|
}
|
2021-01-16 15:09:04 +01:00
|
|
|
|
2019-09-19 17:18:50 +02:00
|
|
|
SortResolutions();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GetAvailableVideoMode(uint *w, uint *h)
|
|
|
|
{
|
|
|
|
/* All modes available? */
|
|
|
|
if (!_fullscreen || _resolutions.empty()) return;
|
|
|
|
|
|
|
|
/* Is the wanted mode among the available modes? */
|
|
|
|
if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
|
|
|
|
|
|
|
|
/* Use the closest possible resolution */
|
|
|
|
uint best = 0;
|
|
|
|
uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
|
|
|
|
for (uint i = 1; i != _resolutions.size(); ++i) {
|
|
|
|
uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
|
|
|
|
if (newdelta < delta) {
|
|
|
|
best = i;
|
|
|
|
delta = newdelta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*w = _resolutions[best].width;
|
|
|
|
*h = _resolutions[best].height;
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:25:01 +01:00
|
|
|
static uint FindStartupDisplay(uint startup_display)
|
|
|
|
{
|
|
|
|
int num_displays = SDL_GetNumVideoDisplays();
|
|
|
|
|
|
|
|
/* If the user indicated a valid monitor, use that. */
|
|
|
|
if (IsInsideBS(startup_display, 0, num_displays)) return startup_display;
|
|
|
|
|
|
|
|
/* Mouse position decides which display to use. */
|
|
|
|
int mx, my;
|
|
|
|
SDL_GetGlobalMouseState(&mx, &my);
|
|
|
|
for (int display = 0; display < num_displays; ++display) {
|
|
|
|
SDL_Rect r;
|
|
|
|
if (SDL_GetDisplayBounds(display, &r) == 0 && IsInsideBS(mx, r.x, r.w) && IsInsideBS(my, r.y, r.h)) {
|
|
|
|
DEBUG(driver, 1, "SDL2: Mouse is at (%d, %d), use display %d (%d, %d, %d, %d)", mx, my, display, r.x, r.y, r.w, r.h);
|
|
|
|
return display;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-24 11:54:36 +01:00
|
|
|
bool VideoDriver_SDL::CreateMainWindow(uint w, uint h)
|
2019-09-19 17:18:50 +02:00
|
|
|
{
|
2021-01-24 11:54:36 +01:00
|
|
|
if (_sdl_window != nullptr) return true;
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-01-24 11:54:36 +01:00
|
|
|
Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-01-24 11:54:36 +01:00
|
|
|
if (_fullscreen) {
|
|
|
|
flags |= SDL_WINDOW_FULLSCREEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
|
|
|
|
SDL_Rect r;
|
|
|
|
if (SDL_GetDisplayBounds(this->startup_display, &r) == 0) {
|
|
|
|
x = r.x + std::max(0, r.w - static_cast<int>(w)) / 2;
|
|
|
|
y = r.y + std::max(0, r.h - static_cast<int>(h)) / 4; // decent desktops have taskbars at the bottom
|
|
|
|
}
|
|
|
|
|
|
|
|
char caption[50];
|
|
|
|
seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision);
|
|
|
|
_sdl_window = SDL_CreateWindow(
|
|
|
|
caption,
|
|
|
|
x, y,
|
|
|
|
w, h,
|
|
|
|
flags);
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
if (_sdl_window == nullptr) {
|
2021-01-24 11:56:26 +01:00
|
|
|
DEBUG(driver, 0, "SDL2: Couldn't allocate a window to draw on: %s", SDL_GetError());
|
2021-01-24 11:54:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-01-24 11:54:36 +01:00
|
|
|
std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
|
|
|
|
if (!icon_path.empty()) {
|
|
|
|
/* Give the application an icon */
|
|
|
|
SDL_Surface *icon = SDL_LoadBMP(icon_path.c_str());
|
|
|
|
if (icon != nullptr) {
|
|
|
|
/* Get the colourkey, which will be magenta */
|
|
|
|
uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
|
|
|
|
|
|
|
|
SDL_SetColorKey(icon, SDL_TRUE, rgbmap);
|
|
|
|
SDL_SetWindowIcon(_sdl_window, icon);
|
|
|
|
SDL_FreeSurface(icon);
|
2019-09-19 17:18:50 +02:00
|
|
|
}
|
2021-01-24 11:54:36 +01:00
|
|
|
}
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-01-24 11:54:36 +01:00
|
|
|
return true;
|
|
|
|
}
|
2021-01-16 17:38:43 +01:00
|
|
|
|
2021-01-24 11:54:36 +01:00
|
|
|
bool VideoDriver_SDL::CreateMainSurface(uint w, uint h, bool resize)
|
|
|
|
{
|
|
|
|
int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-01-24 11:54:36 +01:00
|
|
|
GetAvailableVideoMode(&w, &h);
|
|
|
|
DEBUG(driver, 1, "SDL2: using mode %ux%ux%d", w, h, bpp);
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-01-24 11:54:36 +01:00
|
|
|
if (!this->CreateMainWindow(w, h)) return false;
|
2019-09-19 17:18:50 +02:00
|
|
|
if (resize) SDL_SetWindowSize(_sdl_window, w, h);
|
|
|
|
|
2021-01-24 11:43:46 +01:00
|
|
|
_sdl_real_surface = SDL_GetWindowSurface(_sdl_window);
|
|
|
|
if (_sdl_real_surface == nullptr) {
|
2019-09-19 17:18:50 +02:00
|
|
|
DEBUG(driver, 0, "SDL2: Couldn't get window surface: %s", SDL_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-24 11:43:46 +01:00
|
|
|
/* Free any previously allocated rgb surface. */
|
|
|
|
if (_sdl_rgb_surface != nullptr) {
|
|
|
|
SDL_FreeSurface(_sdl_rgb_surface);
|
|
|
|
_sdl_rgb_surface = nullptr;
|
|
|
|
}
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
if (bpp == 8) {
|
2021-01-24 11:43:46 +01:00
|
|
|
_sdl_rgb_surface = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-01-24 11:43:46 +01:00
|
|
|
if (_sdl_rgb_surface == nullptr) {
|
2019-09-19 17:18:50 +02:00
|
|
|
DEBUG(driver, 0, "SDL2: Couldn't allocate shadow surface: %s", SDL_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-24 11:43:46 +01:00
|
|
|
_sdl_surface = _sdl_rgb_surface;
|
|
|
|
} else {
|
|
|
|
_sdl_surface = _sdl_real_surface;
|
2019-09-19 17:18:50 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 14:06:12 +01:00
|
|
|
/* X11 doesn't appreciate it if we invalidate areas outside the window
|
|
|
|
* if shared memory is enabled (read: it crashes). So, as we might have
|
|
|
|
* gotten smaller, reset our dirty rects. GameSizeChanged() a bit lower
|
|
|
|
* will mark the whole screen dirty again anyway, but this time with the
|
|
|
|
* new dimensions. */
|
2021-02-17 21:19:32 +01:00
|
|
|
MemSetT(&_dirty_rect, 0);
|
2021-02-17 14:06:12 +01:00
|
|
|
|
2021-01-24 11:43:46 +01:00
|
|
|
_screen.width = _sdl_surface->w;
|
|
|
|
_screen.height = _sdl_surface->h;
|
|
|
|
_screen.pitch = _sdl_surface->pitch / (bpp / 8);
|
|
|
|
_screen.dst_ptr = _sdl_surface->pixels;
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-01-24 12:08:37 +01:00
|
|
|
MakePalette();
|
|
|
|
|
2019-09-19 17:18:50 +02:00
|
|
|
/* When in full screen, we will always have the mouse cursor
|
|
|
|
* within the window, even though SDL does not give us the
|
|
|
|
* appropriate event to know this. */
|
|
|
|
if (_fullscreen) _cursor.in_window = true;
|
|
|
|
|
2021-01-24 11:28:13 +01:00
|
|
|
BlitterFactory::GetCurrentBlitter()->PostResize();
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
GameSizeChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoDriver_SDL::ClaimMousePointer()
|
|
|
|
{
|
|
|
|
SDL_ShowCursor(0);
|
2020-12-06 20:18:19 +01:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
SDL_SetRelativeMouseMode(SDL_TRUE);
|
|
|
|
#endif
|
2019-09-19 17:18:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:16:34 +01:00
|
|
|
/**
|
|
|
|
* This is called to indicate that an edit box has gained focus, text input mode should be enabled.
|
|
|
|
*/
|
|
|
|
void VideoDriver_SDL::EditBoxGainedFocus()
|
|
|
|
{
|
|
|
|
if (!this->edit_box_focused) {
|
|
|
|
SDL_StartTextInput();
|
|
|
|
this->edit_box_focused = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is called to indicate that an edit box has lost focus, text input mode should be disabled.
|
|
|
|
*/
|
|
|
|
void VideoDriver_SDL::EditBoxLostFocus()
|
|
|
|
{
|
|
|
|
if (this->edit_box_focused) {
|
|
|
|
SDL_StopTextInput();
|
|
|
|
this->edit_box_focused = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-14 14:06:19 +01:00
|
|
|
struct SDLVkMapping {
|
2019-09-19 17:18:50 +02:00
|
|
|
SDL_Keycode vk_from;
|
|
|
|
byte vk_count;
|
|
|
|
byte map_to;
|
2019-11-04 20:39:49 +01:00
|
|
|
bool unprintable;
|
2019-09-19 17:18:50 +02:00
|
|
|
};
|
|
|
|
|
2019-11-04 20:39:49 +01:00
|
|
|
#define AS(x, z) {x, 0, z, false}
|
|
|
|
#define AM(x, y, z, w) {x, (byte)(y - x), z, false}
|
|
|
|
#define AS_UP(x, z) {x, 0, z, true}
|
|
|
|
#define AM_UP(x, y, z, w) {x, (byte)(y - x), z, true}
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2021-02-14 14:06:19 +01:00
|
|
|
static const SDLVkMapping _vk_mapping[] = {
|
2019-09-19 17:18:50 +02:00
|
|
|
/* Pageup stuff + up/down */
|
2019-11-04 20:39:49 +01:00
|
|
|
AS_UP(SDLK_PAGEUP, WKC_PAGEUP),
|
|
|
|
AS_UP(SDLK_PAGEDOWN, WKC_PAGEDOWN),
|
|
|
|
AS_UP(SDLK_UP, WKC_UP),
|
|
|
|
AS_UP(SDLK_DOWN, WKC_DOWN),
|
|
|
|
AS_UP(SDLK_LEFT, WKC_LEFT),
|
|
|
|
AS_UP(SDLK_RIGHT, WKC_RIGHT),
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2019-11-04 20:39:49 +01:00
|
|
|
AS_UP(SDLK_HOME, WKC_HOME),
|
|
|
|
AS_UP(SDLK_END, WKC_END),
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2019-11-04 20:39:49 +01:00
|
|
|
AS_UP(SDLK_INSERT, WKC_INSERT),
|
|
|
|
AS_UP(SDLK_DELETE, WKC_DELETE),
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
/* Map letters & digits */
|
|
|
|
AM(SDLK_a, SDLK_z, 'A', 'Z'),
|
|
|
|
AM(SDLK_0, SDLK_9, '0', '9'),
|
|
|
|
|
2019-11-04 20:39:49 +01:00
|
|
|
AS_UP(SDLK_ESCAPE, WKC_ESC),
|
|
|
|
AS_UP(SDLK_PAUSE, WKC_PAUSE),
|
|
|
|
AS_UP(SDLK_BACKSPACE, WKC_BACKSPACE),
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
AS(SDLK_SPACE, WKC_SPACE),
|
|
|
|
AS(SDLK_RETURN, WKC_RETURN),
|
|
|
|
AS(SDLK_TAB, WKC_TAB),
|
|
|
|
|
|
|
|
/* Function keys */
|
2019-11-04 20:39:49 +01:00
|
|
|
AM_UP(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
/* Numeric part. */
|
|
|
|
AM(SDLK_KP_0, SDLK_KP_9, '0', '9'),
|
|
|
|
AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
|
|
|
|
AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
|
|
|
|
AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
|
|
|
|
AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
|
|
|
|
AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
|
|
|
|
AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
|
|
|
|
|
|
|
|
/* Other non-letter keys */
|
|
|
|
AS(SDLK_SLASH, WKC_SLASH),
|
|
|
|
AS(SDLK_SEMICOLON, WKC_SEMICOLON),
|
|
|
|
AS(SDLK_EQUALS, WKC_EQUALS),
|
|
|
|
AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
|
|
|
|
AS(SDLK_BACKSLASH, WKC_BACKSLASH),
|
|
|
|
AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
|
|
|
|
|
|
|
|
AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
|
|
|
|
AS(SDLK_COMMA, WKC_COMMA),
|
|
|
|
AS(SDLK_MINUS, WKC_MINUS),
|
|
|
|
AS(SDLK_PERIOD, WKC_PERIOD)
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint ConvertSdlKeyIntoMy(SDL_Keysym *sym, WChar *character)
|
|
|
|
{
|
2021-02-14 14:06:19 +01:00
|
|
|
const SDLVkMapping *map;
|
2019-09-19 17:18:50 +02:00
|
|
|
uint key = 0;
|
2019-11-04 20:39:49 +01:00
|
|
|
bool unprintable = false;
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
|
|
|
|
if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
|
|
|
|
key = sym->sym - map->vk_from + map->map_to;
|
2019-11-04 20:39:49 +01:00
|
|
|
unprintable = map->unprintable;
|
2019-09-19 17:18:50 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
|
2019-11-04 18:59:04 +01:00
|
|
|
if (sym->scancode == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
/* META are the command keys on mac */
|
|
|
|
if (sym->mod & KMOD_GUI) key |= WKC_META;
|
|
|
|
if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
|
|
|
|
if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
|
|
|
|
if (sym->mod & KMOD_ALT) key |= WKC_ALT;
|
|
|
|
|
|
|
|
/* The mod keys have no character. Prevent '?' */
|
|
|
|
if (sym->mod & KMOD_GUI ||
|
|
|
|
sym->mod & KMOD_CTRL ||
|
2019-11-04 20:39:49 +01:00
|
|
|
sym->mod & KMOD_ALT ||
|
|
|
|
unprintable) {
|
2019-09-19 17:18:50 +02:00
|
|
|
*character = WKC_NONE;
|
|
|
|
} else {
|
|
|
|
*character = sym->sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like ConvertSdlKeyIntoMy(), but takes an SDL_Keycode as input
|
|
|
|
* instead of an SDL_Keysym.
|
|
|
|
*/
|
|
|
|
static uint ConvertSdlKeycodeIntoMy(SDL_Keycode kc)
|
|
|
|
{
|
2021-02-14 14:06:19 +01:00
|
|
|
const SDLVkMapping *map;
|
2019-09-19 17:18:50 +02:00
|
|
|
uint key = 0;
|
|
|
|
|
|
|
|
for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
|
|
|
|
if ((uint)(kc - map->vk_from) <= map->vk_count) {
|
|
|
|
key = kc - map->vk_from + map->map_to;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check scancode for BACKQUOTE key, because we want the key left
|
2021-02-05 11:00:36 +01:00
|
|
|
* of "1", not anything else (on non-US keyboards) */
|
2019-09-19 17:18:50 +02:00
|
|
|
SDL_Scancode sc = SDL_GetScancodeFromKey(kc);
|
|
|
|
if (sc == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VideoDriver_SDL::PollEvent()
|
|
|
|
{
|
|
|
|
SDL_Event ev;
|
|
|
|
|
|
|
|
if (!SDL_PollEvent(&ev)) return -2;
|
|
|
|
|
|
|
|
switch (ev.type) {
|
|
|
|
case SDL_MOUSEMOTION:
|
2020-12-06 20:18:19 +01:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
if (_cursor_new_in_window) {
|
|
|
|
/* The cursor just moved into the window; this means we don't
|
2021-02-05 11:00:36 +01:00
|
|
|
* know the absolutely position yet to move relative from.
|
|
|
|
* Before this time, SDL didn't know it either, and this is
|
|
|
|
* why we postpone it till now. Update the absolute position
|
|
|
|
* for this once, and work relative after. */
|
2020-12-06 20:18:19 +01:00
|
|
|
_cursor.pos.x = ev.motion.x;
|
|
|
|
_cursor.pos.y = ev.motion.y;
|
|
|
|
_cursor.dirty = true;
|
|
|
|
|
|
|
|
_cursor_new_in_window = false;
|
|
|
|
SDL_SetRelativeMouseMode(SDL_TRUE);
|
|
|
|
} else {
|
|
|
|
_cursor.UpdateCursorPositionRelative(ev.motion.xrel, ev.motion.yrel);
|
|
|
|
}
|
|
|
|
#else
|
2019-09-19 17:18:50 +02:00
|
|
|
if (_cursor.UpdateCursorPosition(ev.motion.x, ev.motion.y, true)) {
|
|
|
|
SDL_WarpMouseInWindow(_sdl_window, _cursor.pos.x, _cursor.pos.y);
|
|
|
|
}
|
2020-12-06 20:18:19 +01:00
|
|
|
#endif
|
2019-09-19 17:18:50 +02:00
|
|
|
HandleMouseEvents();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_MOUSEWHEEL:
|
|
|
|
if (ev.wheel.y > 0) {
|
|
|
|
_cursor.wheel--;
|
|
|
|
} else if (ev.wheel.y < 0) {
|
|
|
|
_cursor.wheel++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
|
|
|
|
ev.button.button = SDL_BUTTON_RIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ev.button.button) {
|
|
|
|
case SDL_BUTTON_LEFT:
|
|
|
|
_left_button_down = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_BUTTON_RIGHT:
|
|
|
|
_right_button_down = true;
|
|
|
|
_right_button_clicked = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
HandleMouseEvents();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
|
|
|
if (_rightclick_emulate) {
|
|
|
|
_right_button_down = false;
|
|
|
|
_left_button_down = false;
|
|
|
|
_left_button_clicked = false;
|
|
|
|
} else if (ev.button.button == SDL_BUTTON_LEFT) {
|
|
|
|
_left_button_down = false;
|
|
|
|
_left_button_clicked = false;
|
|
|
|
} else if (ev.button.button == SDL_BUTTON_RIGHT) {
|
|
|
|
_right_button_down = false;
|
|
|
|
}
|
|
|
|
HandleMouseEvents();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_QUIT:
|
|
|
|
HandleExitGameRequest();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
|
|
|
|
if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_GUI)) &&
|
|
|
|
(ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
|
|
|
|
if (ev.key.repeat == 0) ToggleFullScreen(!_fullscreen);
|
|
|
|
} else {
|
|
|
|
WChar character;
|
|
|
|
|
|
|
|
uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
|
|
|
|
// Only handle non-text keys here. Text is handled in
|
|
|
|
// SDL_TEXTINPUT below.
|
2019-11-04 19:16:34 +01:00
|
|
|
if (!this->edit_box_focused ||
|
|
|
|
keycode == WKC_DELETE ||
|
2019-09-19 17:18:50 +02:00
|
|
|
keycode == WKC_NUM_ENTER ||
|
|
|
|
keycode == WKC_LEFT ||
|
|
|
|
keycode == WKC_RIGHT ||
|
2019-11-01 15:38:43 +01:00
|
|
|
keycode == WKC_UP ||
|
|
|
|
keycode == WKC_DOWN ||
|
|
|
|
keycode == WKC_HOME ||
|
|
|
|
keycode == WKC_END ||
|
2019-09-19 17:18:50 +02:00
|
|
|
keycode & WKC_META ||
|
|
|
|
keycode & WKC_CTRL ||
|
|
|
|
keycode & WKC_ALT ||
|
|
|
|
(keycode >= WKC_F1 && keycode <= WKC_F12) ||
|
|
|
|
!IsValidChar(character, CS_ALPHANUMERAL)) {
|
|
|
|
HandleKeypress(keycode, character);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_TEXTINPUT: {
|
2019-11-04 19:16:34 +01:00
|
|
|
if (!this->edit_box_focused) break;
|
2019-09-19 17:18:50 +02:00
|
|
|
SDL_Keycode kc = SDL_GetKeyFromName(ev.text.text);
|
|
|
|
uint keycode = ConvertSdlKeycodeIntoMy(kc);
|
|
|
|
|
2019-11-04 19:16:34 +01:00
|
|
|
if (keycode == WKC_BACKQUOTE && FocusedWindowIsConsole()) {
|
|
|
|
WChar character;
|
|
|
|
Utf8Decode(&character, ev.text.text);
|
|
|
|
HandleKeypress(keycode, character);
|
|
|
|
} else {
|
|
|
|
HandleTextInput(ev.text.text);
|
|
|
|
}
|
2019-09-19 17:18:50 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_WINDOWEVENT: {
|
|
|
|
if (ev.window.event == SDL_WINDOWEVENT_EXPOSED) {
|
|
|
|
// Force a redraw of the entire screen.
|
2021-01-24 12:23:09 +01:00
|
|
|
this->MakeDirty(0, 0, _screen.width, _screen.height);
|
2019-09-19 17:18:50 +02:00
|
|
|
} else if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
|
2021-01-08 11:16:18 +01:00
|
|
|
int w = std::max(ev.window.data1, 64);
|
|
|
|
int h = std::max(ev.window.data2, 64);
|
2019-09-19 17:18:50 +02:00
|
|
|
CreateMainSurface(w, h, w != ev.window.data1 || h != ev.window.data2);
|
|
|
|
} else if (ev.window.event == SDL_WINDOWEVENT_ENTER) {
|
|
|
|
// mouse entered the window, enable cursor
|
|
|
|
_cursor.in_window = true;
|
2020-12-06 20:18:19 +01:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
/* Disable relative mouse mode for the first mouse motion,
|
|
|
|
* so we can pick up the absolutely position again. */
|
|
|
|
_cursor_new_in_window = true;
|
|
|
|
SDL_SetRelativeMouseMode(SDL_FALSE);
|
|
|
|
#endif
|
2019-09-19 17:18:50 +02:00
|
|
|
} else if (ev.window.event == SDL_WINDOWEVENT_LEAVE) {
|
|
|
|
// mouse left the window, undraw cursor
|
|
|
|
UndrawMouseCursor();
|
|
|
|
_cursor.in_window = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-17 23:32:08 +02:00
|
|
|
const char *VideoDriver_SDL::Start(const StringList &parm)
|
2019-09-19 17:18:50 +02:00
|
|
|
{
|
2021-01-16 16:27:50 +01:00
|
|
|
if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
|
|
|
|
|
2019-09-19 17:18:50 +02:00
|
|
|
/* Explicitly disable hardware acceleration. Enabling this causes
|
|
|
|
* UpdateWindowSurface() to update the window's texture instead of
|
|
|
|
* its surface. */
|
2020-12-06 20:18:19 +01:00
|
|
|
SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "0");
|
|
|
|
SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, "1");
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
/* Just on the offchance the audio subsystem started before the video system,
|
|
|
|
* check whether any part of SDL has been initialised before getting here.
|
|
|
|
* Slightly duplicated with sound/sdl_s.cpp */
|
|
|
|
int ret_code = 0;
|
|
|
|
if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
|
|
|
|
ret_code = SDL_InitSubSystem(SDL_INIT_VIDEO);
|
|
|
|
}
|
|
|
|
if (ret_code < 0) return SDL_GetError();
|
|
|
|
|
2021-01-14 21:53:06 +01:00
|
|
|
this->UpdateAutoResolution();
|
|
|
|
|
2021-01-16 15:09:04 +01:00
|
|
|
FindResolutions();
|
|
|
|
|
2021-01-16 15:25:01 +01:00
|
|
|
this->startup_display = FindStartupDisplay(GetDriverParamInt(parm, "display", -1));
|
|
|
|
|
2019-09-19 17:18:50 +02:00
|
|
|
if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height, false)) {
|
|
|
|
return SDL_GetError();
|
|
|
|
}
|
|
|
|
|
2020-01-15 21:16:53 +01:00
|
|
|
const char *dname = SDL_GetCurrentVideoDriver();
|
2019-09-19 17:18:50 +02:00
|
|
|
DEBUG(driver, 1, "SDL2: using driver '%s'", dname);
|
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
|
2020-05-17 23:32:08 +02:00
|
|
|
_draw_threaded = !GetDriverParamBool(parm, "no_threads") && !GetDriverParamBool(parm, "no_thread");
|
2021-02-06 14:09:45 +01:00
|
|
|
/* Wayland SDL video driver uses EGL to render the game. SDL created the
|
|
|
|
* EGL context from the main-thread, and with EGL you are not allowed to
|
|
|
|
* draw in another thread than the context was created. The function of
|
|
|
|
* _draw_threaded is to do exactly this: draw in another thread than the
|
|
|
|
* window was created, and as such, this fails on Wayland SDL video
|
|
|
|
* driver. So, we disable threading by default if Wayland SDL video
|
|
|
|
* driver is detected.
|
|
|
|
*/
|
|
|
|
if (strcmp(dname, "wayland") == 0) {
|
|
|
|
_draw_threaded = false;
|
|
|
|
}
|
2019-09-19 17:18:50 +02:00
|
|
|
|
2019-11-04 19:16:34 +01:00
|
|
|
SDL_StopTextInput();
|
|
|
|
this->edit_box_focused = false;
|
|
|
|
|
2019-09-19 17:18:50 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDriver_SDL::Stop()
|
|
|
|
{
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
|
|
if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
|
|
|
|
SDL_Quit(); // If there's nothing left, quit SDL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 11:01:49 +01:00
|
|
|
void VideoDriver_SDL::InputLoop()
|
|
|
|
{
|
|
|
|
uint32 mod = SDL_GetModState();
|
|
|
|
const Uint8 *keys = SDL_GetKeyboardState(NULL);
|
|
|
|
|
|
|
|
bool old_ctrl_pressed = _ctrl_pressed;
|
|
|
|
|
|
|
|
_ctrl_pressed = !!(mod & KMOD_CTRL);
|
|
|
|
_shift_pressed = !!(mod & KMOD_SHIFT);
|
|
|
|
|
|
|
|
#if defined(_DEBUG)
|
|
|
|
if (_shift_pressed)
|
|
|
|
#else
|
|
|
|
/* Speedup when pressing tab, except when using ALT+TAB
|
|
|
|
* to switch to another application. */
|
|
|
|
if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
|
|
|
|
#endif /* defined(_DEBUG) */
|
|
|
|
{
|
|
|
|
if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
|
|
|
|
} else if (_fast_forward & 2) {
|
|
|
|
_fast_forward = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine which directional keys are down. */
|
|
|
|
_dirkeys =
|
|
|
|
(keys[SDL_SCANCODE_LEFT] ? 1 : 0) |
|
|
|
|
(keys[SDL_SCANCODE_UP] ? 2 : 0) |
|
|
|
|
(keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
|
|
|
|
(keys[SDL_SCANCODE_DOWN] ? 8 : 0);
|
|
|
|
|
|
|
|
if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
|
|
|
|
}
|
|
|
|
|
2020-12-06 20:38:34 +01:00
|
|
|
void VideoDriver_SDL::LoopOnce()
|
2019-09-19 17:18:50 +02:00
|
|
|
{
|
2020-12-06 20:38:34 +01:00
|
|
|
InteractiveRandom(); // randomness
|
|
|
|
|
|
|
|
while (PollEvent() == -1) {}
|
2020-12-05 21:57:47 +01:00
|
|
|
if (_exit_game) {
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
/* Emscripten is event-driven, and as such the main loop is inside
|
|
|
|
* the browser. So if _exit_game goes true, the main loop ends (the
|
|
|
|
* cancel call), but we still have to call the cleanup that is
|
|
|
|
* normally done at the end of the main loop for non-Emscripten.
|
|
|
|
* After that, Emscripten just halts, and the HTML shows a nice
|
|
|
|
* "bye, see you next time" message. */
|
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
MainLoopCleanup();
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2020-12-06 20:38:34 +01: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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if (cur_ticks >= next_game_tick || (_fast_forward && !_pause_mode)) {
|
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 (_fast_forward && !_pause_mode) {
|
2021-02-19 11:03:15 +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-19 11:03:15 +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-19 11:03:15 +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
|
|
|
}
|
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
|
|
|
|
|
|
|
/* The gameloop is the part that can run asynchronously. The rest
|
|
|
|
* except sleeping can't. */
|
2021-02-20 10:49:27 +01:00
|
|
|
this->UnlockVideoBuffer();
|
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
|
|
|
GameLoop();
|
2021-02-20 10:49:27 +01:00
|
|
|
this->LockVideoBuffer();
|
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
|
|
|
}
|
|
|
|
|
2021-02-18 13:33:27 +01:00
|
|
|
/* Prevent drawing when switching mode, as windows can be removed when they should still appear. */
|
|
|
|
if (cur_ticks >= next_draw_tick && (_switch_mode == SM_NONE || HasModalProgress())) {
|
2021-02-17 15:31:09 +01:00
|
|
|
next_draw_tick += this->GetDrawInterval();
|
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_draw_tick getting behind more and more if it cannot keep up. */
|
2021-02-17 15:31:09 +01:00
|
|
|
if (next_draw_tick < cur_ticks - ALLOWED_DRIFT * this->GetDrawInterval()) next_draw_tick = cur_ticks;
|
2020-12-06 20:38:34 +01:00
|
|
|
|
2021-02-19 11:01:49 +01:00
|
|
|
this->InputLoop();
|
|
|
|
::InputLoop();
|
2020-12-06 20:38:34 +01:00
|
|
|
UpdateWindows();
|
2021-01-24 12:08:37 +01:00
|
|
|
this->CheckPaletteAnim();
|
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
|
|
|
|
|
|
|
if (_draw_mutex != nullptr && !HasModalProgress()) {
|
|
|
|
_draw_signal->notify_one();
|
2020-12-05 21:57:47 +01:00
|
|
|
} else {
|
2021-02-20 11:08:20 +01:00
|
|
|
this->Paint();
|
2020-12-05 21:57:47 +01:00
|
|
|
}
|
2020-12-06 20:38:34 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/* Emscripten is running an event-based mainloop; there is already some
|
|
|
|
* downtime between each iteration, so no need to sleep. */
|
|
|
|
#ifndef __EMSCRIPTEN__
|
2021-02-17 15:19:33 +01:00
|
|
|
/* If we are not in fast-forward, create some time between calls to ease up CPU usage. */
|
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
|
|
|
if (!_fast_forward || _pause_mode) {
|
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 next_tick = std::min(next_draw_tick, next_game_tick);
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
if (next_tick > now) {
|
2021-02-20 10:49:27 +01:00
|
|
|
this->UnlockVideoBuffer();
|
2021-02-17 15:19:33 +01:00
|
|
|
std::this_thread::sleep_for(next_tick - now);
|
2021-02-20 10:49:27 +01:00
|
|
|
this->LockVideoBuffer();
|
2021-02-17 15:19:33 +01:00
|
|
|
}
|
2020-12-06 20:38:34 +01:00
|
|
|
}
|
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
|
|
|
#endif
|
2020-12-06 20:38:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDriver_SDL::MainLoop()
|
|
|
|
{
|
2021-02-17 14:46:19 +01:00
|
|
|
cur_ticks = std::chrono::steady_clock::now();
|
2021-02-17 14:49:45 +01:00
|
|
|
last_realtime_tick = cur_ticks;
|
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
|
|
|
next_game_tick = cur_ticks;
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
if (_draw_threaded) {
|
|
|
|
/* Initialise the mutex first, because that's the thing we *need*
|
|
|
|
* directly in the newly created thread. */
|
|
|
|
_draw_mutex = new std::recursive_mutex();
|
|
|
|
if (_draw_mutex == nullptr) {
|
|
|
|
_draw_threaded = false;
|
|
|
|
} else {
|
|
|
|
draw_lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
|
|
|
|
_draw_signal = new std::condition_variable_any();
|
|
|
|
_draw_continue = true;
|
|
|
|
|
2021-02-20 11:08:20 +01:00
|
|
|
_draw_threaded = StartNewThread(&draw_thread, "ottd:draw-sdl", &VideoDriver_SDL::PaintThreadThunk, this);
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
/* Free the mutex if we won't be able to use it. */
|
|
|
|
if (!_draw_threaded) {
|
|
|
|
draw_lock.unlock();
|
|
|
|
draw_lock.release();
|
|
|
|
delete _draw_mutex;
|
|
|
|
delete _draw_signal;
|
|
|
|
_draw_mutex = nullptr;
|
|
|
|
_draw_signal = nullptr;
|
|
|
|
} else {
|
|
|
|
/* Wait till the draw mutex has started itself. */
|
|
|
|
_draw_signal->wait(*_draw_mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(driver, 1, "SDL2: using %sthreads", _draw_threaded ? "" : "no ");
|
|
|
|
|
2020-12-05 21:57:47 +01:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
/* Run the main loop event-driven, based on RequestAnimationFrame. */
|
|
|
|
emscripten_set_main_loop_arg(&this->EmscriptenLoop, this, 0, 1);
|
|
|
|
#else
|
2020-12-06 20:38:34 +01:00
|
|
|
while (!_exit_game) {
|
|
|
|
LoopOnce();
|
2019-09-19 17:18:50 +02:00
|
|
|
}
|
|
|
|
|
2020-12-06 20:38:34 +01:00
|
|
|
MainLoopCleanup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDriver_SDL::MainLoopCleanup()
|
|
|
|
{
|
2019-09-19 17:18:50 +02:00
|
|
|
if (_draw_mutex != nullptr) {
|
|
|
|
_draw_continue = false;
|
|
|
|
/* Sending signal if there is no thread blocked
|
|
|
|
* is very valid and results in noop */
|
|
|
|
_draw_signal->notify_one();
|
|
|
|
if (draw_lock.owns_lock()) draw_lock.unlock();
|
|
|
|
draw_lock.release();
|
|
|
|
draw_thread.join();
|
|
|
|
|
|
|
|
delete _draw_mutex;
|
|
|
|
delete _draw_signal;
|
|
|
|
|
|
|
|
_draw_mutex = nullptr;
|
|
|
|
_draw_signal = nullptr;
|
|
|
|
}
|
2020-12-05 21:57:47 +01:00
|
|
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
emscripten_exit_pointerlock();
|
|
|
|
/* In effect, the game ends here. As emscripten_set_main_loop() caused
|
|
|
|
* the stack to be unwound, the code after MainLoop() in
|
|
|
|
* openttd_main() is never executed. */
|
|
|
|
EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
|
|
|
|
EM_ASM(if (window["openttd_exit"]) openttd_exit());
|
|
|
|
#endif
|
2019-09-19 17:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoDriver_SDL::ChangeResolution(int w, int h)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::recursive_mutex> lock;
|
|
|
|
if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
|
|
|
|
|
|
|
|
return CreateMainSurface(w, h, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoDriver_SDL::ToggleFullscreen(bool fullscreen)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::recursive_mutex> lock;
|
|
|
|
if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
|
|
|
|
|
2021-02-11 11:19:05 +01:00
|
|
|
int w, h;
|
|
|
|
|
2019-09-19 17:18:50 +02:00
|
|
|
/* Remember current window size */
|
|
|
|
if (fullscreen) {
|
2021-02-11 11:19:05 +01:00
|
|
|
SDL_GetWindowSize(_sdl_window, &w, &h);
|
2019-09-19 17:18:50 +02:00
|
|
|
|
|
|
|
/* Find fullscreen window size */
|
|
|
|
SDL_DisplayMode dm;
|
|
|
|
if (SDL_GetCurrentDisplayMode(0, &dm) < 0) {
|
|
|
|
DEBUG(driver, 0, "SDL_GetCurrentDisplayMode() failed: %s", SDL_GetError());
|
|
|
|
} else {
|
|
|
|
SDL_SetWindowSize(_sdl_window, dm.w, dm.h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(driver, 1, "SDL2: Setting %s", fullscreen ? "fullscreen" : "windowed");
|
|
|
|
int ret = SDL_SetWindowFullscreen(_sdl_window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
|
|
|
|
if (ret == 0) {
|
|
|
|
/* Switching resolution succeeded, set fullscreen value of window. */
|
|
|
|
_fullscreen = fullscreen;
|
2021-02-11 11:19:05 +01:00
|
|
|
if (!fullscreen) SDL_SetWindowSize(_sdl_window, w, h);
|
2019-09-19 17:18:50 +02:00
|
|
|
} else {
|
|
|
|
DEBUG(driver, 0, "SDL_SetWindowFullscreen() failed: %s", SDL_GetError());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoDriver_SDL::AfterBlitterChange()
|
|
|
|
{
|
2021-01-16 16:27:50 +01:00
|
|
|
assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
|
2019-09-19 17:18:50 +02:00
|
|
|
int w, h;
|
|
|
|
SDL_GetWindowSize(_sdl_window, &w, &h);
|
|
|
|
return CreateMainSurface(w, h, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDriver_SDL::AcquireBlitterLock()
|
|
|
|
{
|
|
|
|
if (_draw_mutex != nullptr) _draw_mutex->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDriver_SDL::ReleaseBlitterLock()
|
|
|
|
{
|
|
|
|
if (_draw_mutex != nullptr) _draw_mutex->unlock();
|
|
|
|
}
|
|
|
|
|
2021-01-14 21:53:06 +01:00
|
|
|
Dimension VideoDriver_SDL::GetScreenSize() const
|
|
|
|
{
|
|
|
|
SDL_DisplayMode mode;
|
2021-01-14 23:29:29 +01:00
|
|
|
if (SDL_GetCurrentDisplayMode(this->startup_display, &mode) != 0) return VideoDriver::GetScreenSize();
|
2021-01-14 21:53:06 +01:00
|
|
|
|
|
|
|
return { static_cast<uint>(mode.w), static_cast<uint>(mode.h) };
|
|
|
|
}
|
2021-02-20 10:49:27 +01:00
|
|
|
|
|
|
|
bool VideoDriver_SDL::LockVideoBuffer()
|
|
|
|
{
|
|
|
|
if (_draw_threaded) this->draw_lock.lock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDriver_SDL::UnlockVideoBuffer()
|
|
|
|
{
|
|
|
|
if (_draw_threaded) this->draw_lock.unlock();
|
|
|
|
}
|