11#include "../openttd.h"
12#include "../gfx_func.h"
13#include "../blitter/factory.hpp"
15#include "../progress.h"
16#include "../core/math_func.hpp"
17#include "../core/geometry_func.hpp"
18#include "../core/utf8.hpp"
19#include "../fileio_func.h"
20#include "../framerate_type.h"
21#include "../window_func.h"
25# include <emscripten.h>
26# include <emscripten/html5.h>
29#include "../safeguards.h"
33 Rect r = {left, top, left + width, top + height};
40 this->
MakeDirty(0, 0, _screen.width, _screen.height);
43static const Dimension default_resolutions[] = {
57static void FindResolutions()
61 for (
int display = 0; display < SDL_GetNumVideoDisplays(); display++) {
62 for (
int i = 0; i < SDL_GetNumDisplayModes(display); i++) {
64 SDL_GetDisplayMode(display, i, &mode);
66 if (mode.w < 640 || mode.h < 480)
continue;
74 _resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
80static void GetAvailableVideoMode(uint *w, uint *h)
93 if (newdelta < delta) {
102static uint FindStartupDisplay(uint startup_display)
104 int num_displays = SDL_GetNumVideoDisplays();
107 if (
IsInsideBS(startup_display, 0, num_displays))
return startup_display;
111 SDL_GetGlobalMouseState(&mx, &my);
112 for (
int display = 0; display < num_displays; ++display) {
114 if (SDL_GetDisplayBounds(display, &r) == 0 &&
IsInsideBS(mx, r.x, r.w) &&
IsInsideBS(my, r.y, r.h)) {
115 Debug(driver, 1,
"SDL2: Mouse is at ({}, {}), use display {} ({}, {}, {}, {})", mx, my, display, r.x, r.y, r.w, r.h);
139 flags |= SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
142 flags |= SDL_WINDOW_FULLSCREEN;
145 int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
147 if (SDL_GetDisplayBounds(this->startup_display, &r) == 0) {
148 x = r.x + std::max(0, r.w -
static_cast<int>(w)) / 2;
149 y = r.y + std::max(0, r.h -
static_cast<int>(h)) / 4;
160 Debug(driver, 0,
"SDL2: Couldn't allocate a window to draw on: {}", SDL_GetError());
165 if (!icon_path.empty()) {
167 SDL_Surface *icon = SDL_LoadBMP(icon_path.c_str());
168 if (icon !=
nullptr) {
170 uint32_t rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
172 SDL_SetColorKey(icon, SDL_TRUE, rgbmap);
174 SDL_FreeSurface(icon);
181bool VideoDriver_SDL_Base::CreateMainSurface(uint w, uint h,
bool resize)
183 GetAvailableVideoMode(&w, &h);
184 Debug(driver, 1,
"SDL2: using mode {}x{}", w, h);
187 if (resize) SDL_SetWindowSize(this->
sdl_window, w, h);
193 if (_fullscreen) _cursor.
in_window =
true;
198bool VideoDriver_SDL_Base::ClaimMousePointer()
201#ifndef __EMSCRIPTEN__
213 SDL_StartTextInput();
231 std::vector<int> rates = {};
232 for (
int i = 0; i < SDL_GetNumVideoDisplays(); i++) {
233 SDL_DisplayMode mode = {};
234 if (SDL_GetDisplayMode(i, 0, &mode) != 0)
continue;
235 if (mode.refresh_rate != 0) rates.push_back(mode.refresh_rate);
242 const SDL_Keycode vk_from;
243 const uint8_t vk_count;
244 const uint8_t map_to;
245 const bool unprintable;
247 constexpr SDLVkMapping(SDL_Keycode vk_first, SDL_Keycode vk_last, uint8_t map_first, [[maybe_unused]] uint8_t map_last,
bool unprintable)
248 : vk_from(vk_first), vk_count(vk_last - vk_first + 1), map_to(map_first), unprintable(unprintable)
250 assert((vk_last - vk_first) == (map_last - map_first));
254#define AS(x, z) {x, x, z, z, false}
255#define AM(x, y, z, w) {x, y, z, w, false}
256#define AS_UP(x, z) {x, x, z, z, true}
257#define AM_UP(x, y, z, w) {x, y, z, w, true}
261 AS_UP(SDLK_PAGEUP, WKC_PAGEUP),
262 AS_UP(SDLK_PAGEDOWN, WKC_PAGEDOWN),
263 AS_UP(SDLK_UP, WKC_UP),
264 AS_UP(SDLK_DOWN, WKC_DOWN),
265 AS_UP(SDLK_LEFT, WKC_LEFT),
266 AS_UP(SDLK_RIGHT, WKC_RIGHT),
268 AS_UP(SDLK_HOME, WKC_HOME),
269 AS_UP(SDLK_END, WKC_END),
271 AS_UP(SDLK_INSERT, WKC_INSERT),
272 AS_UP(SDLK_DELETE, WKC_DELETE),
275 AM(SDLK_a, SDLK_z,
'A',
'Z'),
276 AM(SDLK_0, SDLK_9,
'0',
'9'),
278 AS_UP(SDLK_ESCAPE, WKC_ESC),
279 AS_UP(SDLK_PAUSE, WKC_PAUSE),
280 AS_UP(SDLK_BACKSPACE, WKC_BACKSPACE),
282 AS(SDLK_SPACE, WKC_SPACE),
283 AS(SDLK_RETURN, WKC_RETURN),
284 AS(SDLK_TAB, WKC_TAB),
287 AM_UP(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
300 AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
301 AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
302 AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
303 AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
304 AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
305 AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
321static uint ConvertSdlKeyIntoMy(SDL_Keysym *sym,
char32_t *character)
324 bool unprintable =
false;
326 for (
const auto &map : _vk_mapping) {
327 if (
IsInsideBS(sym->sym, map.vk_from, map.vk_count)) {
328 key = sym->sym - map.vk_from + map.map_to;
329 unprintable = map.unprintable;
335 if (sym->scancode == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
338 if (sym->mod & KMOD_GUI) key |= WKC_META;
339 if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
340 if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
341 if (sym->mod & KMOD_ALT) key |= WKC_ALT;
344 if (sym->mod & KMOD_GUI ||
345 sym->mod & KMOD_CTRL ||
346 sym->mod & KMOD_ALT ||
348 *character = WKC_NONE;
350 *character = sym->sym;
364 for (
const auto &map : _vk_mapping) {
365 if (
IsInsideBS(kc, map.vk_from, map.vk_count)) {
366 key = kc - map.vk_from + map.map_to;
373 SDL_Scancode sc = SDL_GetScancodeFromKey(kc);
374 if (sc == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
383 if (!SDL_PollEvent(&ev))
return false;
386 case SDL_MOUSEMOTION: {
387 int32_t x = ev.motion.x;
388 int32_t y = ev.motion.y;
393 while (SDL_PeepEvents(&ev, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION)) {
406 case SDL_MOUSEWHEEL: {
407 if (ev.wheel.y > 0) {
409 }
else if (ev.wheel.y < 0) {
414 const float SCROLL_BUILTIN_MULTIPLIER = 14.0f;
415#if SDL_VERSION_ATLEAST(2, 18, 0)
422 _cursor.wheel_moved =
true;
426 case SDL_MOUSEBUTTONDOWN:
428 ev.button.button = SDL_BUTTON_RIGHT;
431 switch (ev.button.button) {
432 case SDL_BUTTON_LEFT:
436 case SDL_BUTTON_RIGHT:
446 case SDL_MOUSEBUTTONUP:
451 }
else if (ev.button.button == SDL_BUTTON_LEFT) {
454 }
else if (ev.button.button == SDL_BUTTON_RIGHT) {
461 HandleExitGameRequest();
465 if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_GUI)) &&
466 (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
467 if (ev.key.repeat == 0) ToggleFullScreen(!_fullscreen);
471 uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
475 keycode == WKC_DELETE ||
476 keycode == WKC_NUM_ENTER ||
477 keycode == WKC_LEFT ||
478 keycode == WKC_RIGHT ||
480 keycode == WKC_DOWN ||
481 keycode == WKC_HOME ||
482 keycode == WKC_END ||
483 keycode & WKC_META ||
484 keycode & WKC_CTRL ||
486 (keycode >= WKC_F1 && keycode <= WKC_F12) ||
493 case SDL_TEXTINPUT: {
495 SDL_Keycode kc = SDL_GetKeyFromName(ev.text.text);
506 case SDL_WINDOWEVENT: {
507 if (ev.window.event == SDL_WINDOWEVENT_EXPOSED) {
509 this->
MakeDirty(0, 0, _screen.width, _screen.height);
510 }
else if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
511 int w = std::max(ev.window.data1, 64);
512 int h = std::max(ev.window.data2, 64);
513 CreateMainSurface(w, h, w != ev.window.data1 || h != ev.window.data2);
514 }
else if (ev.window.event == SDL_WINDOWEVENT_ENTER) {
518 SDL_SetRelativeMouseMode(SDL_FALSE);
519 }
else if (ev.window.event == SDL_WINDOWEVENT_LEAVE) {
531static std::optional<std::string_view> InitializeSDL()
534 if (SDL_WasInit(SDL_INIT_VIDEO) != 0)
return std::nullopt;
536#ifdef SDL_HINT_APP_NAME
537 SDL_SetHint(SDL_HINT_APP_NAME,
"OpenTTD");
540 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
return SDL_GetError();
544std::optional<std::string_view> VideoDriver_SDL_Base::Initialize()
548 auto error = InitializeSDL();
549 if (error)
return error;
561 auto error = this->Initialize();
562 if (error)
return error;
564#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
569 if (!SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE,
"0"))
return SDL_GetError();
573 this->startup_display = FindStartupDisplay(
GetDriverParamInt(param,
"display", -1));
576 return SDL_GetError();
579 const char *dname = SDL_GetCurrentVideoDriver();
580 Debug(driver, 1,
"SDL2: using driver '{}'", dname);
593 this->is_game_threaded =
false;
603 SDL_QuitSubSystem(SDL_INIT_VIDEO);
604 if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
611 uint32_t mod = SDL_GetModState();
612 const Uint8 *keys = SDL_GetKeyboardState(
nullptr);
625 (keys[SDL_SCANCODE_LEFT] ? 1 : 0) |
626 (keys[SDL_SCANCODE_UP] ? 2 : 0) |
627 (keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
628 (keys[SDL_SCANCODE_DOWN] ? 8 : 0);
633void VideoDriver_SDL_Base::LoopOnce()
643 extern void PostMainLoop();
646 emscripten_cancel_main_loop();
647 emscripten_exit_pointerlock();
651 if (_game_mode == GM_BOOTSTRAP) {
652 EM_ASM(
if (window[
"openttd_bootstrap_reload"]) openttd_bootstrap_reload());
654 EM_ASM(
if (window[
"openttd_exit"]) openttd_exit());
664#ifndef __EMSCRIPTEN__
673 emscripten_set_main_loop_arg(&this->EmscriptenLoop,
this, 0, 1);
677 while (!_exit_game) {
687 return CreateMainSurface(w, h,
true);
699 if (SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(this->
sdl_window), &dm) < 0) {
700 Debug(driver, 0,
"SDL_GetCurrentDisplayMode() failed: {}", SDL_GetError());
702 SDL_SetWindowSize(this->
sdl_window, dm.w, dm.h);
706 Debug(driver, 1,
"SDL2: Setting {}", fullscreen ?
"fullscreen" :
"windowed");
707 int ret = SDL_SetWindowFullscreen(this->
sdl_window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
710 _fullscreen = fullscreen;
711 if (!fullscreen) SDL_SetWindowSize(this->
sdl_window, w, h);
713 Debug(driver, 0,
"SDL_SetWindowFullscreen() failed: {}", SDL_GetError());
725 return CreateMainSurface(w, h,
false);
730 SDL_DisplayMode mode;
733 return {
static_cast<uint
>(mode.w),
static_cast<uint
>(mode.h) };
742 assert(_screen.dst_ptr !=
nullptr);
749 if (_screen.dst_ptr !=
nullptr) {
752 _screen.dst_ptr =
nullptr;
#define AS(ap_name, size_x, size_y, min_year, max_year, catchment, noise, maint_cost, ttdpatch_type, class_id, name, preview)
AirportSpec definition for airports with at least one depot.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
virtual void PostResize()
Post resize event.
virtual std::string_view GetName() const =0
Get the name of this driver.
std::optional< std::string_view > Start(const StringList ¶m) override
Start this driver.
bool buffer_locked
Video buffer was locked by the main thread.
Dimension GetScreenSize() const override
Get the resolution of the main screen.
bool PollEvent() override
Process a single system event.
void EditBoxLostFocus() override
This is called to indicate that an edit box has lost focus, text input mode should be disabled.
virtual void ReleaseVideoPointer()=0
Hand video buffer back to the painting backend.
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
virtual bool AllocateBackingStore(int w, int h, bool force=false)=0
(Re-)create the backing store.
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Palette local_palette
Current palette to use for drawing.
void MainLoop() override
Perform the actual drawing.
std::string driver_info
Information string about selected driver.
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
void ClientSizeChanged(int w, int h, bool force)
Indicate to the driver the client-side might have changed.
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
virtual bool CreateMainWindow(uint w, uint h, uint flags=0)
Create the main window.
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
bool edit_box_focused
This is true to indicate that keyboard input is in text input mode, and SDL_TEXTINPUT events are enab...
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
void Stop() override
Stop this driver.
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
void EditBoxGainedFocus() override
This is called to indicate that an edit box has gained focus, text input mode should be enabled.
Rect dirty_rect
Rectangle encompassing the dirty area of the video buffer.
void CheckPaletteAnim() override
Process any pending palette animation.
struct SDL_Window * sdl_window
Main SDL window.
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
virtual Dimension GetScreenSize() const
Get the resolution of the main screen.
bool fast_forward_key_pressed
The fast-forward key is being pressed.
void Tick()
Give the video-driver a tick.
void SleepTillNextTick()
Sleep till the next tick is about to happen.
void StartGameThread()
Start the loop for game-tick.
static std::string GetCaption()
Get the caption to use for the game's title bar.
void StopGameThread()
Stop the loop for the game-tick.
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
std::pair< size_t, char32_t > DecodeUtf8(std::string_view buf)
Decode a character from UTF-8.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
int GetDriverParamInt(const StringList &parm, std::string_view name, int def)
Get an integer parameter the list of parameters.
bool GetDriverParamBool(const StringList &parm, std::string_view name)
Get a boolean parameter the list of parameters.
std::vector< Dimension > _resolutions
List of resolutions.
Dimension _cur_resolution
The current resolution.
bool _rightclick_emulate
Whether right clicking is emulated.
std::string FioFindFullPath(Subdirectory subdir, std::string_view filename)
Find a path to the filename in one of the search directories.
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Rect BoundingRect(const Rect &r1, const Rect &r2)
Compute the bounding rectangle around two rectangles.
bool _shift_pressed
Is Shift pressed?
bool _left_button_down
Is left mouse button pressed?
bool _ctrl_pressed
Is Ctrl pressed?
uint8_t _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
bool _left_button_clicked
Is left mouse button clicked?
bool _right_button_clicked
Is right mouse button clicked?
bool _right_button_down
Is right mouse button pressed?
void HandleCtrlChanged()
State of CONTROL key has changed.
void GameSizeChanged()
Size of the application screen changed.
void HandleMouseEvents()
Handle a mouse event from the video driver.
void HandleKeypress(uint keycode, char32_t key)
Handle keyboard input.
void HandleTextInput(std::string_view str, bool marked=false, std::optional< size_t > caret=std::nullopt, std::optional< size_t > insert_location=std::nullopt, std::optional< size_t > replacement_end=std::nullopt)
Handle text input.
@ WKC_BACKSLASH
\ Backslash
@ WKC_SLASH
/ Forward slash
@ WKC_SINGLEQUOTE
' Single quote
@ WKC_R_BRACKET
] Right square bracket
@ WKC_L_BRACKET
[ Left square bracket
@ WKC_SEMICOLON
; Semicolon
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
constexpr T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
static uint ConvertSdlKeycodeIntoMy(SDL_Keycode kc)
Like ConvertSdlKeyIntoMy(), but takes an SDL_Keycode as input instead of an SDL_Keysym.
Base of the SDL2 video driver.
ClientSettings _settings_client
The current settings for this game.
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
std::vector< std::string > StringList
Type for a list of strings.
GUISettings gui
settings related to the GUI
bool UpdateCursorPosition(int x, int y)
Update cursor position on mouse movement.
bool fix_at
mouse is moving, but cursor is not (used for scrolling)
Point pos
logical mouse position
bool in_window
mouse inside this window, determines drawing logic
int wheel
mouse wheel movement
Dimensions (a width and height) of a rectangle in 2D.
uint8_t scrollwheel_multiplier
how much 'wheel' per incoming event from the OS?
Specification of a rectangle with absolute coordinates of all edges.
bool FocusedWindowIsConsole()
Check if a console is focused.
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
@ WC_GAME_OPTIONS
Game options window; Window numbers: