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 sdl_v.cpp Implementation of the SDL video driver. */
|
2005-07-25 09:16:10 +02:00
|
|
|
|
|
|
|
#ifdef WITH_SDL
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
#include "../stdafx.h"
|
2005-07-25 09:16:10 +02:00
|
|
|
#include "../openttd.h"
|
2007-12-23 11:56:02 +01:00
|
|
|
#include "../gfx_func.h"
|
2008-05-04 23:53:36 +02:00
|
|
|
#include "../rev.h"
|
2007-06-17 22:30:28 +02:00
|
|
|
#include "../blitter/factory.hpp"
|
2007-12-23 00:30:28 +01:00
|
|
|
#include "../network/network.h"
|
2019-03-17 01:59:46 +01:00
|
|
|
#include "../thread.h"
|
2011-08-21 14:48:46 +02:00
|
|
|
#include "../progress.h"
|
2010-01-15 17:41:15 +01:00
|
|
|
#include "../core/random_func.hpp"
|
2011-02-07 23:49:38 +01:00
|
|
|
#include "../core/math_func.hpp"
|
2011-09-09 11:13:18 +02:00
|
|
|
#include "../fileio_func.h"
|
2018-07-19 21:17:07 +02:00
|
|
|
#include "../framerate_type.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 09:16:10 +02:00
|
|
|
#include "sdl_v.h"
|
2005-07-23 17:16:57 +02:00
|
|
|
#include <SDL.h>
|
2019-03-11 00:45:39 +01:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
static FVideoDriver_SDL iFVideoDriver_SDL;
|
|
|
|
|
2020-06-04 08:23:57 +02:00
|
|
|
static SDL_Surface *_sdl_surface;
|
2013-02-14 12:06:12 +01:00
|
|
|
static SDL_Surface *_sdl_realscreen;
|
2005-07-23 17:16:57 +02:00
|
|
|
static bool _all_modes;
|
|
|
|
|
2009-10-15 19:41:06 +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. */
|
2019-04-10 23:07:06 +02:00
|
|
|
static std::recursive_mutex *_draw_mutex = nullptr;
|
2019-03-11 00:45:39 +01:00
|
|
|
/** Signal to draw the next frame. */
|
2019-04-10 23:07:06 +02:00
|
|
|
static std::condition_variable_any *_draw_signal = nullptr;
|
2009-10-15 19:41:06 +02:00
|
|
|
/** Should we keep continue drawing? */
|
|
|
|
static volatile bool _draw_continue;
|
2011-12-08 21:01:31 +01:00
|
|
|
static Palette _local_palette;
|
2009-10-15 19:41:06 +02:00
|
|
|
|
2005-07-23 17:16:57 +02:00
|
|
|
#define MAX_DIRTY_RECTS 100
|
|
|
|
static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
|
|
|
|
static int _num_dirty_rects;
|
2013-02-14 12:06:12 +01:00
|
|
|
static int _use_hwpalette;
|
2013-02-15 12:01:45 +01:00
|
|
|
static int _requested_hwpalette; /* Did we request a HWPALETTE for the current video mode? */
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
void VideoDriver_SDL::MakeDirty(int left, int top, int width, int height)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
|
|
|
if (_num_dirty_rects < MAX_DIRTY_RECTS) {
|
|
|
|
_dirty_rects[_num_dirty_rects].x = left;
|
|
|
|
_dirty_rects[_num_dirty_rects].y = top;
|
|
|
|
_dirty_rects[_num_dirty_rects].w = width;
|
|
|
|
_dirty_rects[_num_dirty_rects].h = height;
|
|
|
|
}
|
|
|
|
_num_dirty_rects++;
|
|
|
|
}
|
|
|
|
|
2013-02-14 12:06:12 +01:00
|
|
|
static void UpdatePalette(bool init = false)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2005-07-29 12:16:49 +02:00
|
|
|
SDL_Color pal[256];
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2011-12-08 21:01:31 +01:00
|
|
|
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;
|
2005-07-23 17:16:57 +02:00
|
|
|
pal[i].unused = 0;
|
|
|
|
}
|
|
|
|
|
2020-06-04 08:23:57 +02:00
|
|
|
SDL_SetColors(_sdl_surface, pal, _local_palette.first_dirty, _local_palette.count_dirty);
|
2013-02-14 12:06:12 +01:00
|
|
|
|
2020-06-04 08:23:57 +02:00
|
|
|
if (_sdl_surface != _sdl_realscreen && init) {
|
2013-02-14 12:06:12 +01:00
|
|
|
/* When using a shadow surface, also set our palette on the real screen. This lets SDL
|
2020-06-04 08:23:57 +02:00
|
|
|
* allocate as many colors (or approximations) as
|
2013-02-14 12:06:12 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_SetColors(_sdl_realscreen, pal, _local_palette.first_dirty, _local_palette.count_dirty);
|
2013-02-14 12:06:12 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 08:23:57 +02:00
|
|
|
if (_sdl_surface != _sdl_realscreen && !init) {
|
2013-02-14 12:06:12 +01:00
|
|
|
/* We're not using real hardware palette, but are letting SDL
|
|
|
|
* approximate the palette during shadow -> screen copy. To
|
|
|
|
* change the palette, we need to recopy the entire screen.
|
|
|
|
*
|
|
|
|
* Note that this operation can slow down the rendering
|
|
|
|
* considerably, especially since changing the shadow
|
|
|
|
* palette will need the next blit to re-detect the
|
|
|
|
* best mapping of shadow palette colors to real palette
|
|
|
|
* colors from scratch.
|
|
|
|
*/
|
2020-06-04 08:23:57 +02:00
|
|
|
SDL_BlitSurface(_sdl_surface, nullptr, _sdl_realscreen, nullptr);
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
|
2013-02-14 12:06:12 +01:00
|
|
|
}
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static void InitPalette()
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2011-12-08 21:01:31 +01:00
|
|
|
_local_palette = _cur_palette;
|
|
|
|
_local_palette.first_dirty = 0;
|
|
|
|
_local_palette.count_dirty = 256;
|
2013-02-14 12:06:12 +01:00
|
|
|
UpdatePalette(true);
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static void CheckPaletteAnim()
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2011-12-08 19:13:29 +01:00
|
|
|
if (_cur_palette.count_dirty != 0) {
|
2014-01-02 23:41:58 +01:00
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
2007-06-19 17:18:26 +02:00
|
|
|
|
2007-06-19 17:04:08 +02:00
|
|
|
switch (blitter->UsePaletteAnimation()) {
|
|
|
|
case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
|
2011-12-08 21:01:31 +01:00
|
|
|
UpdatePalette();
|
2007-06-19 17:04:08 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Blitter::PALETTE_ANIMATION_BLITTER:
|
2011-12-08 21:01:31 +01:00
|
|
|
blitter->PaletteAnimate(_local_palette);
|
2007-06-19 17:04:08 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Blitter::PALETTE_ANIMATION_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
2011-12-08 19:13:29 +01:00
|
|
|
_cur_palette.count_dirty = 0;
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static void DrawSurfaceToScreen()
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2018-07-19 21:17:07 +02:00
|
|
|
PerformanceMeasurer framerate(PFE_VIDEO);
|
|
|
|
|
2005-07-23 17:16:57 +02:00
|
|
|
int n = _num_dirty_rects;
|
2009-01-23 17:05:58 +01:00
|
|
|
if (n == 0) return;
|
|
|
|
|
|
|
|
_num_dirty_rects = 0;
|
2020-06-04 08:23:57 +02:00
|
|
|
|
2009-01-23 17:05:58 +01:00
|
|
|
if (n > MAX_DIRTY_RECTS) {
|
2020-06-04 08:23:57 +02:00
|
|
|
if (_sdl_surface != _sdl_realscreen) {
|
|
|
|
SDL_BlitSurface(_sdl_surface, nullptr, _sdl_realscreen, nullptr);
|
2013-02-14 12:06:12 +01:00
|
|
|
}
|
2020-06-04 08:23:57 +02:00
|
|
|
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
|
2009-01-23 17:05:58 +01:00
|
|
|
} else {
|
2020-06-04 08:23:57 +02:00
|
|
|
if (_sdl_surface != _sdl_realscreen) {
|
2013-02-14 12:06:12 +01:00
|
|
|
for (int i = 0; i < n; i++) {
|
2020-06-04 08:23:57 +02:00
|
|
|
SDL_BlitSurface(_sdl_surface, &_dirty_rects[i], _sdl_realscreen, &_dirty_rects[i]);
|
2013-02-14 12:06:12 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-04 08:23:57 +02:00
|
|
|
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_UpdateRects(_sdl_realscreen, n, _dirty_rects);
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 01:59:46 +01:00
|
|
|
static void DrawSurfaceToScreenThread()
|
2009-10-15 19:41:06 +02:00
|
|
|
{
|
2010-01-13 22:34:48 +01:00
|
|
|
/* First tell the main thread we're started */
|
2019-03-11 00:45:39 +01:00
|
|
|
std::unique_lock<std::recursive_mutex> lock(*_draw_mutex);
|
|
|
|
_draw_signal->notify_one();
|
2010-01-13 22:34:48 +01:00
|
|
|
|
|
|
|
/* Now wait for the first thing to draw! */
|
2019-03-11 00:45:39 +01:00
|
|
|
_draw_signal->wait(*_draw_mutex);
|
2009-10-15 19:41:06 +02:00
|
|
|
|
|
|
|
while (_draw_continue) {
|
2011-12-08 21:01:31 +01:00
|
|
|
CheckPaletteAnim();
|
2009-10-15 19:41:06 +02:00
|
|
|
/* Then just draw and wait till we stop */
|
|
|
|
DrawSurfaceToScreen();
|
2019-03-11 00:45:39 +01:00
|
|
|
_draw_signal->wait(lock);
|
2009-10-15 19:41:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:58:34 +01:00
|
|
|
static const Dimension _default_resolutions[] = {
|
2005-07-23 17:16:57 +02:00
|
|
|
{ 640, 480},
|
|
|
|
{ 800, 600},
|
|
|
|
{1024, 768},
|
|
|
|
{1152, 864},
|
|
|
|
{1280, 800},
|
|
|
|
{1280, 960},
|
|
|
|
{1280, 1024},
|
|
|
|
{1400, 1050},
|
|
|
|
{1600, 1200},
|
|
|
|
{1680, 1050},
|
|
|
|
{1920, 1200}
|
|
|
|
};
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
static void GetVideoModes()
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2019-04-10 23:07:06 +02:00
|
|
|
SDL_Rect **modes = SDL_ListModes(nullptr, SDL_SWSURFACE | SDL_FULLSCREEN);
|
|
|
|
if (modes == nullptr) usererror("sdl: no modes available");
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2019-04-12 18:46:49 +02:00
|
|
|
_resolutions.clear();
|
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
_all_modes = (SDL_ListModes(nullptr, SDL_SWSURFACE | (_fullscreen ? SDL_FULLSCREEN : 0)) == (void*)-1);
|
2009-01-23 16:58:34 +01:00
|
|
|
if (modes == (void*)-1) {
|
|
|
|
for (uint i = 0; i < lengthof(_default_resolutions); i++) {
|
2019-03-02 01:05:36 +01:00
|
|
|
if (SDL_VideoModeOK(_default_resolutions[i].width, _default_resolutions[i].height, 8, SDL_FULLSCREEN) != 0) {
|
2019-04-12 18:46:49 +02:00
|
|
|
_resolutions.push_back(_default_resolutions[i]);
|
2009-01-23 16:58:34 +01:00
|
|
|
}
|
|
|
|
}
|
2005-07-23 17:16:57 +02:00
|
|
|
} else {
|
2009-01-23 16:58:34 +01:00
|
|
|
for (int i = 0; modes[i]; i++) {
|
2009-06-27 22:53:45 +02:00
|
|
|
uint w = modes[i]->w;
|
|
|
|
uint h = modes[i]->h;
|
2019-03-30 08:46:21 +01:00
|
|
|
if (w < 640 || h < 480) continue; // reject too small resolutions
|
2019-04-12 18:46:49 +02:00
|
|
|
if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(w, h)) != _resolutions.end()) continue;
|
|
|
|
_resolutions.emplace_back(w, h);
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
2019-04-12 18:46:49 +02:00
|
|
|
if (_resolutions.empty()) usererror("No usable screen resolutions found!\n");
|
|
|
|
SortResolutions();
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-27 22:53:45 +02:00
|
|
|
static void GetAvailableVideoMode(uint *w, uint *h)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2009-01-23 17:05:58 +01:00
|
|
|
/* All modes available? */
|
2019-04-12 18:46:49 +02:00
|
|
|
if (_all_modes || _resolutions.empty()) return;
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2009-01-23 17:05:58 +01:00
|
|
|
/* Is the wanted mode among the available modes? */
|
2019-04-12 18:46:49 +02:00
|
|
|
if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2009-01-23 17:05:58 +01:00
|
|
|
/* Use the closest possible resolution */
|
2019-04-12 18:46:49 +02:00
|
|
|
uint best = 0;
|
2009-06-27 22:53:45 +02:00
|
|
|
uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
|
2019-04-12 18:46:49 +02:00
|
|
|
for (uint i = 1; i != _resolutions.size(); ++i) {
|
2009-06-27 22:53:45 +02:00
|
|
|
uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
|
2005-07-23 17:16:57 +02:00
|
|
|
if (newdelta < delta) {
|
|
|
|
best = i;
|
|
|
|
delta = newdelta;
|
|
|
|
}
|
|
|
|
}
|
2008-06-16 21:38:41 +02:00
|
|
|
*w = _resolutions[best].width;
|
|
|
|
*h = _resolutions[best].height;
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2013-02-14 12:06:06 +01:00
|
|
|
bool VideoDriver_SDL::CreateMainSurface(uint w, uint h)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2006-08-13 10:18:11 +02:00
|
|
|
SDL_Surface *newscreen, *icon;
|
2005-07-23 17:16:57 +02:00
|
|
|
char caption[50];
|
2014-01-02 23:41:58 +01:00
|
|
|
int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
|
2013-02-14 12:06:12 +01:00
|
|
|
bool want_hwpalette;
|
2005-07-23 17:16:57 +02:00
|
|
|
|
|
|
|
GetAvailableVideoMode(&w, &h);
|
|
|
|
|
2009-06-27 22:53:45 +02:00
|
|
|
DEBUG(driver, 1, "SDL: using mode %ux%ux%d", w, h, bpp);
|
2007-06-12 22:24:12 +02:00
|
|
|
|
2008-06-05 22:54:52 +02:00
|
|
|
if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2020-12-06 21:11:44 +01:00
|
|
|
std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
|
|
|
|
if (!icon_path.empty()) {
|
2011-09-09 11:13:18 +02:00
|
|
|
/* Give the application an icon */
|
2020-12-06 21:11:44 +01:00
|
|
|
icon = SDL_LoadBMP(icon_path.c_str());
|
2019-04-10 23:07:06 +02:00
|
|
|
if (icon != nullptr) {
|
2011-09-09 11:13:18 +02:00
|
|
|
/* Get the colourkey, which will be magenta */
|
2019-03-02 01:05:36 +01:00
|
|
|
uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
|
2011-09-09 11:13:18 +02:00
|
|
|
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_SetColorKey(icon, SDL_SRCCOLORKEY, rgbmap);
|
2019-04-10 23:07:06 +02:00
|
|
|
SDL_WM_SetIcon(icon, nullptr);
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_FreeSurface(icon);
|
2011-09-09 11:13:18 +02:00
|
|
|
}
|
2006-08-13 10:18:11 +02:00
|
|
|
}
|
|
|
|
|
2013-02-14 12:06:12 +01:00
|
|
|
if (_use_hwpalette == 2) {
|
|
|
|
/* Default is to autodetect when to use SDL_HWPALETTE.
|
|
|
|
* In this case, SDL_HWPALETTE is only used for 8bpp
|
|
|
|
* blitters in fullscreen.
|
|
|
|
*
|
|
|
|
* When using an 8bpp blitter on a 8bpp system in
|
|
|
|
* windowed mode with SDL_HWPALETTE, OpenTTD will claim
|
|
|
|
* the system palette, making all other applications
|
|
|
|
* get the wrong colours. In this case, we're better of
|
|
|
|
* trying to approximate the colors we need using system
|
|
|
|
* colors, using a shadow surface (see below).
|
|
|
|
*
|
|
|
|
* On a 32bpp system, SDL_HWPALETTE is ignored, so it
|
|
|
|
* doesn't matter what we do.
|
|
|
|
*
|
|
|
|
* When using a 32bpp blitter on a 8bpp system, setting
|
|
|
|
* SDL_HWPALETTE messes up rendering (at least on X11),
|
|
|
|
* so we don't do that. In this case, SDL takes care of
|
|
|
|
* color approximation using its own shadow surface
|
|
|
|
* (which we can't force in 8bpp on 8bpp mode,
|
|
|
|
* unfortunately).
|
|
|
|
*/
|
2014-04-27 14:15:14 +02:00
|
|
|
want_hwpalette = bpp == 8 && _fullscreen && _support8bpp == S8BPP_HARDWARE;
|
2013-02-14 12:06:12 +01:00
|
|
|
} else {
|
|
|
|
/* User specified a value manually */
|
|
|
|
want_hwpalette = _use_hwpalette;
|
|
|
|
}
|
|
|
|
|
2018-04-30 12:54:00 +02:00
|
|
|
if (want_hwpalette) DEBUG(driver, 1, "SDL: requesting hardware palette");
|
2013-02-14 12:06:12 +01:00
|
|
|
|
|
|
|
/* Free any previously allocated shadow surface */
|
2020-06-04 08:23:57 +02:00
|
|
|
if (_sdl_surface != nullptr && _sdl_surface != _sdl_realscreen) SDL_FreeSurface(_sdl_surface);
|
2013-02-14 12:06:12 +01:00
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
if (_sdl_realscreen != nullptr) {
|
2013-02-15 12:01:45 +01:00
|
|
|
if (_requested_hwpalette != want_hwpalette) {
|
2013-02-14 12:06:12 +01:00
|
|
|
/* SDL (at least the X11 driver), reuses the
|
|
|
|
* same window and palette settings when the bpp
|
|
|
|
* (and a few flags) are the same. Since we need
|
|
|
|
* to hwpalette value to change (in particular
|
2013-02-15 12:01:43 +01:00
|
|
|
* when switching between fullscreen and
|
2013-02-14 12:06:12 +01:00
|
|
|
* windowed), we restart the entire video
|
|
|
|
* subsystem to force creating a new window.
|
|
|
|
*/
|
|
|
|
DEBUG(driver, 0, "SDL: Restarting SDL video subsystem, to force hwpalette change");
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
|
|
SDL_InitSubSystem(SDL_INIT_VIDEO);
|
2013-02-14 12:06:12 +01:00
|
|
|
ClaimMousePointer();
|
2013-06-04 08:36:59 +02:00
|
|
|
SetupKeyboard();
|
2013-02-14 12:06:12 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-15 12:01:45 +01:00
|
|
|
/* Remember if we wanted a hwpalette. We can't reliably query
|
|
|
|
* SDL for the SDL_HWPALETTE flag, since it might get set even
|
|
|
|
* though we didn't ask for it (when SDL creates a shadow
|
|
|
|
* surface, for example). */
|
|
|
|
_requested_hwpalette = want_hwpalette;
|
2013-02-14 12:06:12 +01:00
|
|
|
|
2009-01-23 17:05:58 +01:00
|
|
|
/* DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK */
|
2019-03-02 01:05:36 +01:00
|
|
|
newscreen = SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | (want_hwpalette ? SDL_HWPALETTE : 0) | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
|
2019-04-10 23:07:06 +02:00
|
|
|
if (newscreen == nullptr) {
|
2008-11-29 02:28:13 +01:00
|
|
|
DEBUG(driver, 0, "SDL: Couldn't allocate a window to draw on");
|
2005-07-23 17:16:57 +02:00
|
|
|
return false;
|
2008-11-29 02:28:13 +01:00
|
|
|
}
|
2013-02-14 12:06:12 +01:00
|
|
|
_sdl_realscreen = newscreen;
|
|
|
|
|
|
|
|
if (bpp == 8 && (_sdl_realscreen->flags & SDL_HWPALETTE) != SDL_HWPALETTE) {
|
|
|
|
/* Using an 8bpp blitter, if we didn't get a hardware
|
|
|
|
* palette (most likely because we didn't request one,
|
|
|
|
* see above), we'll have to set up a shadow surface to
|
|
|
|
* render on.
|
|
|
|
*
|
|
|
|
* Our palette will be applied to this shadow surface,
|
|
|
|
* while the real screen surface will use the shared
|
|
|
|
* system palette (which will partly contain our colors,
|
|
|
|
* but most likely will not have enough free color cells
|
|
|
|
* for all of our colors). SDL can use these two
|
|
|
|
* palettes at blit time to approximate colors used in
|
|
|
|
* the shadow surface using system colors automatically.
|
|
|
|
*
|
|
|
|
* Note that when using an 8bpp blitter on a 32bpp
|
|
|
|
* system, SDL will create an internal shadow surface.
|
|
|
|
* This shadow surface will have SDL_HWPALLETE set, so
|
|
|
|
* we won't create a second shadow surface in this case.
|
|
|
|
*/
|
|
|
|
DEBUG(driver, 1, "SDL: using shadow surface");
|
2019-03-02 01:05:36 +01:00
|
|
|
newscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, bpp, 0, 0, 0, 0);
|
2019-04-10 23:07:06 +02:00
|
|
|
if (newscreen == nullptr) {
|
2013-02-14 12:06:12 +01:00
|
|
|
DEBUG(driver, 0, "SDL: Couldn't allocate a shadow surface to draw on");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2009-10-15 19:41:06 +02:00
|
|
|
/* Delay drawing for this cycle; the next cycle will redraw the whole screen */
|
|
|
|
_num_dirty_rects = 0;
|
|
|
|
|
2005-07-23 17:16:57 +02:00
|
|
|
_screen.width = newscreen->w;
|
|
|
|
_screen.height = newscreen->h;
|
2007-06-12 22:24:12 +02:00
|
|
|
_screen.pitch = newscreen->pitch / (bpp / 8);
|
2009-12-19 19:46:40 +01:00
|
|
|
_screen.dst_ptr = newscreen->pixels;
|
2020-06-04 08:23:57 +02:00
|
|
|
_sdl_surface = newscreen;
|
2010-01-04 03:32:36 +01:00
|
|
|
|
2013-06-29 20:21:19 +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;
|
|
|
|
|
2014-01-02 23:41:58 +01:00
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
2012-02-23 17:23:09 +01:00
|
|
|
blitter->PostResize();
|
|
|
|
|
2012-02-23 22:10:08 +01:00
|
|
|
InitPalette();
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2014-04-23 23:12:09 +02:00
|
|
|
seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision);
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_WM_SetCaption(caption, caption);
|
2005-07-23 17:16:57 +02:00
|
|
|
|
|
|
|
GameSizeChanged();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-17 22:09:08 +01:00
|
|
|
bool VideoDriver_SDL::ClaimMousePointer()
|
|
|
|
{
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_ShowCursor(0);
|
2011-11-17 22:09:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-14 14:06:19 +01:00
|
|
|
struct SDLVkMapping {
|
2012-02-04 17:24:25 +01:00
|
|
|
#if SDL_VERSION_ATLEAST(1, 3, 0)
|
|
|
|
SDL_Keycode vk_from;
|
|
|
|
#else
|
2005-07-23 17:16:57 +02:00
|
|
|
uint16 vk_from;
|
2012-02-04 17:24:25 +01:00
|
|
|
#endif
|
2005-07-23 17:16:57 +02:00
|
|
|
byte vk_count;
|
|
|
|
byte map_to;
|
2007-03-07 13:11:48 +01:00
|
|
|
};
|
2005-07-23 17:16:57 +02:00
|
|
|
|
|
|
|
#define AS(x, z) {x, 0, z}
|
2012-02-04 17:24:25 +01:00
|
|
|
#define AM(x, y, z, w) {x, (byte)(y - x), z}
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2021-02-14 14:06:19 +01:00
|
|
|
static const SDLVkMapping _vk_mapping[] = {
|
2007-07-23 18:48:19 +02:00
|
|
|
/* Pageup stuff + up/down */
|
2005-07-23 17:16:57 +02:00
|
|
|
AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
|
2006-08-28 20:53:03 +02:00
|
|
|
AS(SDLK_UP, WKC_UP),
|
|
|
|
AS(SDLK_DOWN, WKC_DOWN),
|
|
|
|
AS(SDLK_LEFT, WKC_LEFT),
|
|
|
|
AS(SDLK_RIGHT, WKC_RIGHT),
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2006-08-28 20:53:03 +02:00
|
|
|
AS(SDLK_HOME, WKC_HOME),
|
|
|
|
AS(SDLK_END, WKC_END),
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2006-08-28 20:53:03 +02:00
|
|
|
AS(SDLK_INSERT, WKC_INSERT),
|
|
|
|
AS(SDLK_DELETE, WKC_DELETE),
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2007-07-23 18:48:19 +02:00
|
|
|
/* Map letters & digits */
|
2005-07-23 17:16:57 +02:00
|
|
|
AM(SDLK_a, SDLK_z, 'A', 'Z'),
|
|
|
|
AM(SDLK_0, SDLK_9, '0', '9'),
|
|
|
|
|
2006-08-28 20:53:03 +02:00
|
|
|
AS(SDLK_ESCAPE, WKC_ESC),
|
|
|
|
AS(SDLK_PAUSE, WKC_PAUSE),
|
|
|
|
AS(SDLK_BACKSPACE, WKC_BACKSPACE),
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2006-08-28 20:53:03 +02:00
|
|
|
AS(SDLK_SPACE, WKC_SPACE),
|
|
|
|
AS(SDLK_RETURN, WKC_RETURN),
|
|
|
|
AS(SDLK_TAB, WKC_TAB),
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2007-07-23 18:48:19 +02:00
|
|
|
/* Function keys */
|
2005-07-23 17:16:57 +02:00
|
|
|
AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
|
|
|
|
|
2007-07-23 18:48:19 +02:00
|
|
|
/* Numeric part. */
|
2008-09-07 13:55:28 +02:00
|
|
|
AM(SDLK_KP0, SDLK_KP9, '0', '9'),
|
2006-08-28 20:53:03 +02:00
|
|
|
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),
|
2007-07-23 18:39:27 +02:00
|
|
|
AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
|
|
|
|
|
2007-07-23 18:48:19 +02:00
|
|
|
/* Other non-letter keys */
|
2007-07-23 18:39:27 +02:00
|
|
|
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)
|
2005-07-23 17:16:57 +02:00
|
|
|
};
|
|
|
|
|
2013-08-05 22:36:36 +02:00
|
|
|
static uint ConvertSdlKeyIntoMy(SDL_keysym *sym, WChar *character)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2021-02-14 14:06:19 +01:00
|
|
|
const SDLVkMapping *map;
|
2005-07-23 17:16:57 +02:00
|
|
|
uint key = 0;
|
2006-09-01 12:29:40 +02:00
|
|
|
|
2005-07-23 17:16:57 +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;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-23 17:05:58 +01:00
|
|
|
/* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
|
2018-12-09 02:28:14 +01:00
|
|
|
#if defined(_WIN32) || defined(__OS2__)
|
2006-11-16 17:50:54 +01:00
|
|
|
if (sym->scancode == 41) key = WKC_BACKQUOTE;
|
2005-07-23 17:16:57 +02:00
|
|
|
#elif defined(__APPLE__)
|
2006-11-16 17:50:54 +01:00
|
|
|
if (sym->scancode == 10) key = WKC_BACKQUOTE;
|
2005-07-23 17:16:57 +02:00
|
|
|
#elif defined(__SVR4) && defined(__sun)
|
2006-11-16 17:50:54 +01:00
|
|
|
if (sym->scancode == 60) key = WKC_BACKQUOTE;
|
|
|
|
if (sym->scancode == 49) key = WKC_BACKSPACE;
|
2005-07-23 17:16:57 +02:00
|
|
|
#elif defined(__sgi__)
|
2006-11-16 17:50:54 +01:00
|
|
|
if (sym->scancode == 22) key = WKC_BACKQUOTE;
|
2005-07-23 17:16:57 +02:00
|
|
|
#else
|
2006-11-16 17:50:54 +01:00
|
|
|
if (sym->scancode == 49) key = WKC_BACKQUOTE;
|
2005-07-23 17:16:57 +02:00
|
|
|
#endif
|
|
|
|
|
2009-01-23 17:05:58 +01:00
|
|
|
/* META are the command keys on mac */
|
2006-09-01 12:29:40 +02:00
|
|
|
if (sym->mod & KMOD_META) key |= WKC_META;
|
2005-07-23 17:16:57 +02:00
|
|
|
if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
|
2006-09-01 12:29:40 +02:00
|
|
|
if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
|
|
|
|
if (sym->mod & KMOD_ALT) key |= WKC_ALT;
|
2009-01-23 17:05:58 +01:00
|
|
|
|
2013-08-05 22:36:36 +02:00
|
|
|
*character = sym->unicode;
|
|
|
|
return key;
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2013-02-14 12:06:06 +01:00
|
|
|
int VideoDriver_SDL::PollEvent()
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
|
|
|
SDL_Event ev;
|
|
|
|
|
2019-03-02 01:05:36 +01:00
|
|
|
if (!SDL_PollEvent(&ev)) return -2;
|
2005-07-23 17:16:57 +02:00
|
|
|
|
|
|
|
switch (ev.type) {
|
2006-09-01 12:29:40 +02:00
|
|
|
case SDL_MOUSEMOTION:
|
2015-02-23 00:06:45 +01:00
|
|
|
if (_cursor.UpdateCursorPosition(ev.motion.x, ev.motion.y, true)) {
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
2006-11-15 22:01:19 +01:00
|
|
|
HandleMouseEvents();
|
2006-09-01 12:29:40 +02:00
|
|
|
break;
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2006-09-01 12:29:40 +02:00
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
2019-03-02 01:05:36 +01:00
|
|
|
if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
|
2006-09-01 12:29:40 +02:00
|
|
|
ev.button.button = SDL_BUTTON_RIGHT;
|
|
|
|
}
|
2006-03-24 18:00:55 +01:00
|
|
|
|
2006-09-01 12:29:40 +02:00
|
|
|
switch (ev.button.button) {
|
|
|
|
case SDL_BUTTON_LEFT:
|
|
|
|
_left_button_down = true;
|
|
|
|
break;
|
2006-03-24 01:42:35 +01:00
|
|
|
|
2006-09-01 12:29:40 +02:00
|
|
|
case SDL_BUTTON_RIGHT:
|
|
|
|
_right_button_down = true;
|
|
|
|
_right_button_clicked = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_BUTTON_WHEELUP: _cursor.wheel--; break;
|
|
|
|
case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
2006-11-15 22:01:19 +01:00
|
|
|
HandleMouseEvents();
|
2006-09-01 12:29:40 +02:00
|
|
|
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;
|
|
|
|
}
|
2006-11-15 22:01:19 +01:00
|
|
|
HandleMouseEvents();
|
2006-09-01 12:29:40 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_ACTIVEEVENT:
|
|
|
|
if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;
|
|
|
|
|
|
|
|
if (ev.active.gain) { // mouse entered the window, enable cursor
|
|
|
|
_cursor.in_window = true;
|
2006-06-27 23:25:53 +02:00
|
|
|
} else {
|
2006-09-01 12:29:40 +02:00
|
|
|
UndrawMouseCursor(); // mouse left the window, undraw cursor
|
|
|
|
_cursor.in_window = false;
|
2006-06-27 23:25:53 +02:00
|
|
|
}
|
2006-09-01 12:29:40 +02:00
|
|
|
break;
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2009-01-23 17:05:58 +01:00
|
|
|
case SDL_QUIT:
|
|
|
|
HandleExitGameRequest();
|
|
|
|
break;
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2009-01-23 17:05:58 +01:00
|
|
|
case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
|
2006-09-01 12:29:40 +02:00
|
|
|
if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
|
|
|
|
(ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
|
|
|
|
ToggleFullScreen(!_fullscreen);
|
|
|
|
} else {
|
2013-08-05 22:36:36 +02:00
|
|
|
WChar character;
|
|
|
|
uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
|
|
|
|
HandleKeypress(keycode, character);
|
2006-09-01 12:29:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_VIDEORESIZE: {
|
2021-01-08 11:16:18 +01:00
|
|
|
int w = std::max(ev.resize.w, 64);
|
|
|
|
int h = std::max(ev.resize.h, 64);
|
2009-12-19 20:29:01 +01:00
|
|
|
CreateMainSurface(w, h);
|
2006-09-01 12:29:40 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-02-07 11:27:48 +01:00
|
|
|
case SDL_VIDEOEXPOSE: {
|
|
|
|
/* Force a redraw of the entire screen. Note
|
|
|
|
* that SDL 1.2 seems to do this automatically
|
|
|
|
* in most cases, but 1.3 / 2.0 does not. */
|
|
|
|
_num_dirty_rects = MAX_DIRTY_RECTS + 1;
|
|
|
|
break;
|
|
|
|
}
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-17 23:32:08 +02:00
|
|
|
const char *VideoDriver_SDL::Start(const StringList &parm)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
|
|
|
char buf[30];
|
2013-02-14 12:06:12 +01:00
|
|
|
_use_hwpalette = GetDriverParamInt(parm, "hw_palette", 2);
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2019-03-02 01:05:36 +01: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_EVERYTHING) == 0) {
|
|
|
|
ret_code = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
|
|
|
|
} else if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
|
|
|
|
ret_code = SDL_InitSubSystem(SDL_INIT_VIDEO);
|
|
|
|
}
|
2020-06-04 08:23:57 +02:00
|
|
|
if (ret_code < 0) return SDL_GetError();
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2021-01-14 21:53:06 +01:00
|
|
|
this->UpdateAutoResolution();
|
|
|
|
|
2009-06-30 14:36:24 +02:00
|
|
|
GetVideoModes();
|
|
|
|
if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
|
2019-03-02 01:05:36 +01:00
|
|
|
return SDL_GetError();
|
2009-06-30 14:36:24 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_VideoDriverName(buf, sizeof buf);
|
2006-12-26 18:36:18 +01:00
|
|
|
DEBUG(driver, 1, "SDL: using driver '%s'", buf);
|
2005-07-23 17:16:57 +02:00
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
2013-06-04 08:36:59 +02:00
|
|
|
SetupKeyboard();
|
2009-10-15 19:41:06 +02:00
|
|
|
|
2020-05-17 23:32:08 +02:00
|
|
|
_draw_threaded = !GetDriverParamBool(parm, "no_threads") && !GetDriverParamBool(parm, "no_thread");
|
2009-10-15 19:41:06 +02:00
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
return nullptr;
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2013-06-04 08:36:59 +02:00
|
|
|
void VideoDriver_SDL::SetupKeyboard()
|
|
|
|
{
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
|
|
|
|
SDL_EnableUNICODE(1);
|
2013-06-04 08:36:59 +02:00
|
|
|
}
|
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
void VideoDriver_SDL::Stop()
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
|
|
if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
|
|
|
|
SDL_Quit(); // If there's nothing left, quit SDL
|
|
|
|
}
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2021-02-19 11:01:49 +01:00
|
|
|
void VideoDriver_SDL::InputLoop()
|
|
|
|
{
|
|
|
|
uint32 mod = SDL_GetModState();
|
|
|
|
#if SDL_VERSION_ATLEAST(1, 3, 0)
|
|
|
|
Uint8 *keys = SDL_GetKeyboardState(&numkeys);
|
|
|
|
#else
|
|
|
|
int numkeys;
|
|
|
|
Uint8 *keys = SDL_GetKeyState(&numkeys);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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 SDL_VERSION_ATLEAST(1, 3, 0)
|
|
|
|
if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
|
|
|
|
#else
|
|
|
|
if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
|
|
|
|
#endif /* SDL_VERSION_ATLEAST(1, 3, 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 =
|
|
|
|
#if SDL_VERSION_ATLEAST(1, 3, 0)
|
|
|
|
(keys[SDL_SCANCODE_LEFT] ? 1 : 0) |
|
|
|
|
(keys[SDL_SCANCODE_UP] ? 2 : 0) |
|
|
|
|
(keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
|
|
|
|
(keys[SDL_SCANCODE_DOWN] ? 8 : 0);
|
|
|
|
#else
|
|
|
|
(keys[SDLK_LEFT] ? 1 : 0) |
|
|
|
|
(keys[SDLK_UP] ? 2 : 0) |
|
|
|
|
(keys[SDLK_RIGHT] ? 4 : 0) |
|
|
|
|
(keys[SDLK_DOWN] ? 8 : 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
|
|
|
|
}
|
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
void VideoDriver_SDL::MainLoop()
|
2005-07-23 17:16:57 +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;
|
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
|
|
|
auto next_game_tick = cur_ticks;
|
|
|
|
auto next_draw_tick = cur_ticks;
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2011-12-08 20:37:33 +01:00
|
|
|
CheckPaletteAnim();
|
|
|
|
|
2019-03-17 01:59:46 +01:00
|
|
|
std::thread draw_thread;
|
2009-10-15 19:41:06 +02:00
|
|
|
if (_draw_threaded) {
|
|
|
|
/* Initialise the mutex first, because that's the thing we *need*
|
|
|
|
* directly in the newly created thread. */
|
2019-03-11 00:45:39 +01:00
|
|
|
_draw_mutex = new std::recursive_mutex();
|
2019-04-10 23:07:06 +02:00
|
|
|
if (_draw_mutex == nullptr) {
|
2009-10-15 19:41:06 +02:00
|
|
|
_draw_threaded = false;
|
|
|
|
} else {
|
2021-02-20 10:49:27 +01:00
|
|
|
this->draw_lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
|
2019-03-11 00:45:39 +01:00
|
|
|
_draw_signal = new std::condition_variable_any();
|
2009-10-15 19:41:06 +02:00
|
|
|
_draw_continue = true;
|
|
|
|
|
2019-03-17 01:59:46 +01:00
|
|
|
_draw_threaded = StartNewThread(&draw_thread, "ottd:draw-sdl", &DrawSurfaceToScreenThread);
|
2009-10-15 19:41:06 +02:00
|
|
|
|
2009-11-02 13:12:13 +01:00
|
|
|
/* Free the mutex if we won't be able to use it. */
|
|
|
|
if (!_draw_threaded) {
|
2021-02-20 10:49:27 +01:00
|
|
|
this->draw_lock.unlock();
|
|
|
|
this->draw_lock.release();
|
2009-11-02 13:12:13 +01:00
|
|
|
delete _draw_mutex;
|
2019-03-11 00:45:39 +01:00
|
|
|
delete _draw_signal;
|
2019-04-10 23:07:06 +02:00
|
|
|
_draw_mutex = nullptr;
|
|
|
|
_draw_signal = nullptr;
|
2010-01-13 22:34:48 +01:00
|
|
|
} else {
|
|
|
|
/* Wait till the draw mutex has started itself. */
|
2019-03-11 00:45:39 +01:00
|
|
|
_draw_signal->wait(*_draw_mutex);
|
2009-11-02 13:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
2009-10-15 19:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no ");
|
|
|
|
|
2005-07-23 17:16:57 +02:00
|
|
|
for (;;) {
|
|
|
|
InteractiveRandom(); // randomness
|
|
|
|
|
2006-11-15 20:49:16 +01:00
|
|
|
while (PollEvent() == -1) {}
|
2009-11-02 14:36:17 +01:00
|
|
|
if (_exit_game) break;
|
2005-07-23 17:16:57 +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;
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
}
|
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;
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2021-02-19 11:01:49 +01:00
|
|
|
this->InputLoop();
|
|
|
|
::InputLoop();
|
2005-07-23 17:16:57 +02:00
|
|
|
UpdateWindows();
|
2011-12-08 21:01:31 +01:00
|
|
|
_local_palette = _cur_palette;
|
2009-12-03 09:24:39 +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
|
|
|
if (_draw_mutex != nullptr && !HasModalProgress()) {
|
|
|
|
_draw_signal->notify_one();
|
|
|
|
} else {
|
|
|
|
CheckPaletteAnim();
|
|
|
|
DrawSurfaceToScreen();
|
|
|
|
}
|
2009-12-03 09:24:39 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
}
|
2009-10-15 19:41:06 +02:00
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
if (_draw_mutex != nullptr) {
|
2009-10-15 19:41:06 +02:00
|
|
|
_draw_continue = false;
|
2009-11-02 14:36:17 +01:00
|
|
|
/* Sending signal if there is no thread blocked
|
|
|
|
* is very valid and results in noop */
|
2019-03-11 00:45:39 +01:00
|
|
|
_draw_signal->notify_one();
|
2021-02-20 10:49:27 +01:00
|
|
|
if (this->draw_lock.owns_lock()) this->draw_lock.unlock();
|
|
|
|
this->draw_lock.release();
|
2019-03-17 01:59:46 +01:00
|
|
|
draw_thread.join();
|
2009-10-15 19:41:06 +02:00
|
|
|
|
|
|
|
delete _draw_mutex;
|
2019-03-11 00:45:39 +01:00
|
|
|
delete _draw_signal;
|
2013-02-17 21:37:18 +01:00
|
|
|
|
2019-04-10 23:07:06 +02:00
|
|
|
_draw_mutex = nullptr;
|
|
|
|
_draw_signal = nullptr;
|
2009-10-15 19:41:06 +02:00
|
|
|
}
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
bool VideoDriver_SDL::ChangeResolution(int w, int h)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2019-03-11 00:45:39 +01:00
|
|
|
std::unique_lock<std::recursive_mutex> lock;
|
2019-04-10 23:07:06 +02:00
|
|
|
if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
|
2019-03-11 00:45:39 +01:00
|
|
|
|
|
|
|
return CreateMainSurface(w, h);
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2008-01-01 15:20:48 +01:00
|
|
|
bool VideoDriver_SDL::ToggleFullscreen(bool fullscreen)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2019-03-11 00:45:39 +01:00
|
|
|
std::unique_lock<std::recursive_mutex> lock;
|
2019-04-10 23:07:06 +02:00
|
|
|
if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
|
2019-03-11 00:45:39 +01:00
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
_fullscreen = fullscreen;
|
2005-07-23 17:16:57 +02:00
|
|
|
GetVideoModes(); // get the list of available video modes
|
2019-04-12 18:46:49 +02:00
|
|
|
bool ret = !_resolutions.empty() && CreateMainSurface(_cur_resolution.width, _cur_resolution.height);
|
2014-02-16 22:57:22 +01:00
|
|
|
|
|
|
|
if (!ret) {
|
2009-01-23 17:05:58 +01:00
|
|
|
/* switching resolution failed, put back full_screen to original status */
|
2006-09-01 12:29:40 +02:00
|
|
|
_fullscreen ^= true;
|
|
|
|
}
|
2014-02-16 22:57:22 +01:00
|
|
|
|
|
|
|
return ret;
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2011-10-04 23:35:40 +02:00
|
|
|
bool VideoDriver_SDL::AfterBlitterChange()
|
2017-03-11 14:05:54 +01:00
|
|
|
{
|
|
|
|
return CreateMainSurface(_screen.width, _screen.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDriver_SDL::AcquireBlitterLock()
|
2011-10-04 23:35:40 +02:00
|
|
|
{
|
2019-04-10 23:07:06 +02:00
|
|
|
if (_draw_mutex != nullptr) _draw_mutex->lock();
|
2017-03-11 14:05:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDriver_SDL::ReleaseBlitterLock()
|
|
|
|
{
|
2019-04-10 23:07:06 +02:00
|
|
|
if (_draw_mutex != nullptr) _draw_mutex->unlock();
|
2011-10-04 23:35:40 +02:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2007-02-12 22:55:10 +01:00
|
|
|
#endif /* WITH_SDL */
|