diff --git a/src/blitter/32bpp_anim.cpp b/src/blitter/32bpp_anim.cpp index 80ec54da43..61c2758221 100644 --- a/src/blitter/32bpp_anim.cpp +++ b/src/blitter/32bpp_anim.cpp @@ -480,7 +480,7 @@ void Blitter_32bppAnim::PaletteAnimate(const Palette &palette) } /* Make sure the backend redraws the whole screen */ - _video_driver->MakeDirty(0, 0, _screen.width, _screen.height); + VideoDriver::GetInstance()->MakeDirty(0, 0, _screen.width, _screen.height); } Blitter::PaletteAnimation Blitter_32bppAnim::UsePaletteAnimation() diff --git a/src/bootstrap_gui.cpp b/src/bootstrap_gui.cpp index c2b76f0b1c..5e2af004d1 100644 --- a/src/bootstrap_gui.cpp +++ b/src/bootstrap_gui.cpp @@ -244,7 +244,7 @@ bool HandleBootstrap() new BootstrapAskForDownloadWindow(); /* Process the user events. */ - _video_driver->MainLoop(); + VideoDriver::GetInstance()->MainLoop(); /* _exit_game is used to get out of the video driver's main loop. * In case GM_BOOTSTRAP is still set we did not exit it via the diff --git a/src/console_gui.cpp b/src/console_gui.cpp index c3dcdaa122..ff3c509c95 100644 --- a/src/console_gui.cpp +++ b/src/console_gui.cpp @@ -185,7 +185,7 @@ struct IConsoleWindow : Window ~IConsoleWindow() { _iconsole_mode = ICONSOLE_CLOSED; - _video_driver->EditBoxLostFocus(); + VideoDriver::GetInstance()->EditBoxLostFocus(); } /** @@ -374,7 +374,7 @@ struct IConsoleWindow : Window virtual void OnFocusLost() { - _video_driver->EditBoxLostFocus(); + VideoDriver::GetInstance()->EditBoxLostFocus(); } }; diff --git a/src/crashlog.cpp b/src/crashlog.cpp index 986736bb3e..7bb30e583b 100644 --- a/src/crashlog.cpp +++ b/src/crashlog.cpp @@ -138,14 +138,14 @@ char *CrashLog::LogConfiguration(char *buffer, const char *last) const BaseGraphics::GetUsedSet() == NULL ? "none" : BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet() == NULL ? UINT32_MAX : BaseGraphics::GetUsedSet()->version, _current_language == NULL ? "none" : _current_language->file, - _music_driver == NULL ? "none" : _music_driver->GetName(), + MusicDriver::GetInstance() == NULL ? "none" : MusicDriver::GetInstance()->GetName(), BaseMusic::GetUsedSet() == NULL ? "none" : BaseMusic::GetUsedSet()->name, BaseMusic::GetUsedSet() == NULL ? UINT32_MAX : BaseMusic::GetUsedSet()->version, _networking ? (_network_server ? "server" : "client") : "no", - _sound_driver == NULL ? "none" : _sound_driver->GetName(), + SoundDriver::GetInstance() == NULL ? "none" : SoundDriver::GetInstance()->GetName(), BaseSounds::GetUsedSet() == NULL ? "none" : BaseSounds::GetUsedSet()->name, BaseSounds::GetUsedSet() == NULL ? UINT32_MAX : BaseSounds::GetUsedSet()->version, - _video_driver == NULL ? "none" : _video_driver->GetName() + VideoDriver::GetInstance() == NULL ? "none" : VideoDriver::GetInstance()->GetName() ); buffer += seprintf(buffer, last, @@ -482,7 +482,7 @@ bool CrashLog::MakeCrashLog() const */ /* static */ void CrashLog::AfterCrashLogCleanup() { - if (_music_driver != NULL) _music_driver->Stop(); - if (_sound_driver != NULL) _sound_driver->Stop(); - if (_video_driver != NULL) _video_driver->Stop(); + if (MusicDriver::GetInstance() != NULL) MusicDriver::GetInstance()->Stop(); + if (SoundDriver::GetInstance() != NULL) SoundDriver::GetInstance()->Stop(); + if (VideoDriver::GetInstance() != NULL) VideoDriver::GetInstance()->Stop(); } diff --git a/src/driver.cpp b/src/driver.cpp index dd03e5e7b6..6ef0563f7d 100644 --- a/src/driver.cpp +++ b/src/driver.cpp @@ -16,17 +16,14 @@ #include "video/video_driver.hpp" #include "string_func.h" -VideoDriver *_video_driver; ///< The currently active video driver. char *_ini_videodriver; ///< The video driver a stored in the configuration file. int _num_resolutions; ///< The number of resolutions. Dimension _resolutions[32]; ///< List of resolutions. Dimension _cur_resolution; ///< The current resolution. bool _rightclick_emulate; ///< Whether right clicking is emulated. -SoundDriver *_sound_driver; ///< The currently active sound driver. char *_ini_sounddriver; ///< The sound driver a stored in the configuration file. -MusicDriver *_music_driver; ///< The currently active music driver. char *_ini_musicdriver; ///< The music driver a stored in the configuration file. char *_ini_blitter; ///< The blitter as stored in the configuration file. @@ -86,9 +83,25 @@ int GetDriverParamInt(const char * const *parm, const char *name, int def) * @param type the type of driver to select * @post Sets the driver so GetCurrentDriver() returns it too. */ -Driver *DriverFactoryBase::SelectDriver(const char *name, Driver::Type type) +void DriverFactoryBase::SelectDriver(const char *name, Driver::Type type) { - if (GetDrivers().size() == 0) return NULL; + if (!DriverFactoryBase::SelectDriverImpl(name, type)) { + StrEmpty(name) ? + usererror("Failed to autoprobe %s driver", GetDriverTypeName(type)) : + usererror("Failed to select requested %s driver '%s'", GetDriverTypeName(type), name); + } +} + +/** + * Find the requested driver and return its class. + * @param name the driver to select. + * @param type the type of driver to select + * @post Sets the driver so GetCurrentDriver() returns it too. + * @return True upon success, otherwise false. + */ +bool DriverFactoryBase::SelectDriverImpl(const char *name, Driver::Type type) +{ + if (GetDrivers().size() == 0) return false; if (StrEmpty(name)) { /* Probe for this driver, but do not fall back to dedicated/null! */ @@ -101,15 +114,18 @@ Driver *DriverFactoryBase::SelectDriver(const char *name, Driver::Type type) if (d->type != type) continue; if (d->priority != priority) continue; + Driver *oldd = *GetActiveDriver(type); Driver *newd = d->CreateInstance(); + *GetActiveDriver(type) = newd; + const char *err = newd->Start(NULL); if (err == NULL) { DEBUG(driver, 1, "Successfully probed %s driver '%s'", GetDriverTypeName(type), d->name); - delete *GetActiveDriver(type); - *GetActiveDriver(type) = newd; - return newd; + delete oldd; + return true; } + *GetActiveDriver(type) = oldd; DEBUG(driver, 1, "Probing %s driver '%s' failed with error: %s", GetDriverTypeName(type), d->name, err); delete newd; } @@ -158,7 +174,7 @@ Driver *DriverFactoryBase::SelectDriver(const char *name, Driver::Type type) DEBUG(driver, 1, "Successfully loaded %s driver '%s'", GetDriverTypeName(type), d->name); delete *GetActiveDriver(type); *GetActiveDriver(type) = newd; - return newd; + return true; } usererror("No such %s driver: %s\n", GetDriverTypeName(type), buffer); } diff --git a/src/driver.h b/src/driver.h index a466d67d00..12ca4474d4 100644 --- a/src/driver.h +++ b/src/driver.h @@ -59,6 +59,10 @@ DECLARE_POSTFIX_INCREMENT(Driver::Type) /** Base for all driver factories. */ class DriverFactoryBase { private: + friend class MusicDriver; + friend class SoundDriver; + friend class VideoDriver; + Driver::Type type; ///< The type of driver. int priority; ///< The priority of this factory. const char *name; ///< The name of the drivers of this factory. @@ -97,6 +101,8 @@ private: return driver_type_name[type]; } + static bool SelectDriverImpl(const char *name, Driver::Type type); + protected: DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description); @@ -114,7 +120,7 @@ public: } } - static Driver *SelectDriver(const char *name, Driver::Type type); + static void SelectDriver(const char *name, Driver::Type type); static char *GetDriversInfo(char *p, const char *last); /** diff --git a/src/genworld.cpp b/src/genworld.cpp index 822fe141fa..c2f4c518b5 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -98,7 +98,7 @@ static void _GenerateWorld(void *) try { _generating_world = true; _modal_progress_work_mutex->BeginCritical(); - if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait..."); + if (_network_dedicated) DEBUG(net, 1, "Generating map, please wait..."); /* Set the Random() seed to generation_seed so we produce the same map with the same seed */ if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom(); _random.SetSeed(_settings_game.game_creation.generation_seed); @@ -195,7 +195,7 @@ static void _GenerateWorld(void *) ShowNewGRFError(); - if (_network_dedicated) DEBUG(net, 0, "Map generated, starting game"); + if (_network_dedicated) DEBUG(net, 1, "Map generated, starting game"); DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed); if (_debug_desync_level > 0) { @@ -328,7 +328,7 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti _gw.thread = NULL; } - if (!_video_driver->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) { + if (!VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread)) { DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode"); _gw.threaded = false; _modal_progress_work_mutex->EndCritical(); diff --git a/src/gfx.cpp b/src/gfx.cpp index adc644d205..9a034c1726 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -85,7 +85,7 @@ void GfxScroll(int left, int top, int width, int height, int xo, int yo) blitter->ScrollBuffer(_screen.dst_ptr, left, top, width, height, xo, yo); /* This part of the screen is now dirty. */ - _video_driver->MakeDirty(left, top, width, height); + VideoDriver::GetInstance()->MakeDirty(left, top, width, height); } @@ -1183,7 +1183,7 @@ void UndrawMouseCursor() Blitter *blitter = BlitterFactory::GetCurrentBlitter(); _cursor.visible = false; blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), _cursor_backup.GetBuffer(), _cursor.draw_size.x, _cursor.draw_size.y); - _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y); + VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y); } } @@ -1243,7 +1243,7 @@ void DrawMouseCursor() _cur_dpi = &_screen; DrawSprite(_cursor.sprite, _cursor.pal, _cursor.pos.x + _cursor.short_vehicle_offset, _cursor.pos.y); - _video_driver->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y); + VideoDriver::GetInstance()->MakeDirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y); _cursor.visible = true; _cursor.dirty = false; @@ -1267,7 +1267,7 @@ void RedrawScreenRect(int left, int top, int right, int bottom) DrawOverlappedWindowForAll(left, top, right, bottom); - _video_driver->MakeDirty(left, top, right - left, bottom - top); + VideoDriver::GetInstance()->MakeDirty(left, top, right - left, bottom - top); } /** @@ -1576,12 +1576,12 @@ void SetAnimatedMouseCursor(const AnimCursor *table) bool ChangeResInGame(int width, int height) { - return (_screen.width == width && _screen.height == height) || _video_driver->ChangeResolution(width, height); + return (_screen.width == width && _screen.height == height) || VideoDriver::GetInstance()->ChangeResolution(width, height); } bool ToggleFullScreen(bool fs) { - bool result = _video_driver->ToggleFullscreen(fs); + bool result = VideoDriver::GetInstance()->ToggleFullscreen(fs); if (_fullscreen != fs && _num_resolutions == 0) { DEBUG(driver, 0, "Could not find a suitable fullscreen resolution"); } diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp index 109db2e08a..3151a76715 100644 --- a/src/gfxinit.cpp +++ b/src/gfxinit.cpp @@ -282,9 +282,9 @@ static bool SwitchNewGRFBlitter() break; } - if (!_video_driver->AfterBlitterChange()) { + if (!VideoDriver::GetInstance()->AfterBlitterChange()) { /* Failed to switch blitter, let's hope we can return to the old one. */ - if (BlitterFactory::SelectBlitter(cur_blitter) == NULL || !_video_driver->AfterBlitterChange()) usererror("Failed to reinitialize video driver. Specify a fixed blitter in the config"); + if (BlitterFactory::SelectBlitter(cur_blitter) == NULL || !VideoDriver::GetInstance()->AfterBlitterChange()) usererror("Failed to reinitialize video driver. Specify a fixed blitter in the config"); } return true; diff --git a/src/music/extmidi.cpp b/src/music/extmidi.cpp index 93492aa996..367b2e4645 100644 --- a/src/music/extmidi.cpp +++ b/src/music/extmidi.cpp @@ -35,8 +35,8 @@ static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi; const char *MusicDriver_ExtMidi::Start(const char * const * parm) { - if (strcmp(_video_driver->GetName(), "allegro") == 0 || - strcmp(_sound_driver->GetName(), "allegro") == 0) { + if (strcmp(VideoDriver::GetInstance()->GetName(), "allegro") == 0 || + strcmp(SoundDriver::GetInstance()->GetName(), "allegro") == 0) { return "the extmidi driver does not work when Allegro is loaded."; } diff --git a/src/music/music_driver.hpp b/src/music/music_driver.hpp index 03c70d5691..be09d3ea2b 100644 --- a/src/music/music_driver.hpp +++ b/src/music/music_driver.hpp @@ -39,9 +39,15 @@ public: * @param vol The new volume. */ virtual void SetVolume(byte vol) = 0; + + /** + * Get the currently active instance of the music driver. + */ + static MusicDriver *GetInstance() { + return static_cast(*DriverFactoryBase::GetActiveDriver(Driver::DT_MUSIC)); + } }; -extern MusicDriver *_music_driver; extern char *_ini_musicdriver; #endif /* MUSIC_MUSIC_DRIVER_HPP */ diff --git a/src/music_gui.cpp b/src/music_gui.cpp index 896624cb7d..1e20716512 100644 --- a/src/music_gui.cpp +++ b/src/music_gui.cpp @@ -174,7 +174,7 @@ static void SkipToNextSong() static void MusicVolumeChanged(byte new_vol) { - _music_driver->SetVolume(new_vol); + MusicDriver::GetInstance()->SetVolume(new_vol); } static void DoPlaySong() @@ -183,13 +183,13 @@ static void DoPlaySong() if (FioFindFullPath(filename, lengthof(filename), BASESET_DIR, BaseMusic::GetUsedSet()->files[_music_wnd_cursong - 1].filename) == NULL) { FioFindFullPath(filename, lengthof(filename), OLD_GM_DIR, BaseMusic::GetUsedSet()->files[_music_wnd_cursong - 1].filename); } - _music_driver->PlaySong(filename); + MusicDriver::GetInstance()->PlaySong(filename); SetWindowDirty(WC_MUSIC_WINDOW, 0); } static void DoStopMusic() { - _music_driver->StopSong(); + MusicDriver::GetInstance()->StopSong(); SetWindowDirty(WC_MUSIC_WINDOW, 0); } @@ -271,7 +271,7 @@ void MusicLoop() if (!_song_is_active) return; - if (!_music_driver->IsSongPlaying()) { + if (!MusicDriver::GetInstance()->IsSongPlaying()) { if (_game_mode != GM_MENU) { StopMusic(); SkipToNextSong(); diff --git a/src/network/network_chat_gui.cpp b/src/network/network_chat_gui.cpp index 8b965feff2..d7d2d59a39 100644 --- a/src/network/network_chat_gui.cpp +++ b/src/network/network_chat_gui.cpp @@ -167,7 +167,7 @@ void NetworkUndrawChatMessage() /* Put our 'shot' back to the screen */ blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height); /* And make sure it is updated next time */ - _video_driver->MakeDirty(x, y, width, height); + VideoDriver::GetInstance()->MakeDirty(x, y, width, height); _chatmessage_dirty = true; } @@ -254,7 +254,7 @@ void NetworkDrawChatMessage() } /* Make sure the data is updated next flush */ - _video_driver->MakeDirty(x, y, width, height); + VideoDriver::GetInstance()->MakeDirty(x, y, width, height); _chatmessage_visible = true; _chatmessage_dirty = false; diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 337b3edd11..d1ace81c15 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -773,7 +773,7 @@ void ScanNewGRFFiles(NewGRFScanCallback *callback) /* Only then can we really start, especially by marking the whole screen dirty. Get those other windows hidden!. */ MarkWholeScreenDirty(); - if (!_video_driver->HasGUI() || !ThreadObject::New(&DoScanNewGRFFiles, callback, NULL)) { + if (!VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&DoScanNewGRFFiles, callback, NULL)) { _modal_progress_work_mutex->EndCritical(); _modal_progress_paint_mutex->EndCritical(); DoScanNewGRFFiles(callback); diff --git a/src/openttd.cpp b/src/openttd.cpp index c901272c57..d4b47dda79 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -95,7 +95,7 @@ void CDECL usererror(const char *s, ...) va_end(va); ShowOSErrorBox(buf, false); - if (_video_driver != NULL) _video_driver->Stop(); + if (VideoDriver::GetInstance() != NULL) VideoDriver::GetInstance()->Stop(); exit(1); } @@ -341,7 +341,7 @@ static void LoadIntroGame(bool load_newgrfs = true) CheckForMissingGlyphs(); /* Play main theme */ - if (_music_driver->IsSongPlaying()) ResetMusic(); + if (MusicDriver::GetInstance()->IsSongPlaying()) ResetMusic(); } void MakeNewgameSettingsLive() @@ -434,7 +434,7 @@ struct AfterNewGRFScan : NewGRFScanCallback { *save_config_ptr = save_config; /* restore saved music volume */ - _music_driver->SetVolume(_settings_client.music.music_vol); + MusicDriver::GetInstance()->SetVolume(_settings_client.music.music_vol); if (startyear != INVALID_YEAR) _settings_newgame.game_creation.starting_year = startyear; if (generation_seed != GENERATE_NEW_SEED) _settings_newgame.game_creation.generation_seed = generation_seed; @@ -766,12 +766,7 @@ int openttd_main(int argc, char *argv[]) free(blitter); if (videodriver == NULL && _ini_videodriver != NULL) videodriver = strdup(_ini_videodriver); - _video_driver = (VideoDriver*)DriverFactoryBase::SelectDriver(videodriver, Driver::DT_VIDEO); - if (_video_driver == NULL) { - StrEmpty(videodriver) ? - usererror("Failed to autoprobe video driver") : - usererror("Failed to select requested video driver '%s'", videodriver); - } + DriverFactoryBase::SelectDriver(videodriver, Driver::DT_VIDEO); free(videodriver); InitializeSpriteSorter(); @@ -802,7 +797,7 @@ int openttd_main(int argc, char *argv[]) goto exit_bootstrap; } - _video_driver->ClaimMousePointer(); + VideoDriver::GetInstance()->ClaimMousePointer(); /* initialize screenshot formats */ InitializeScreenshotFormats(); @@ -834,21 +829,11 @@ int openttd_main(int argc, char *argv[]) free(music_set); if (sounddriver == NULL && _ini_sounddriver != NULL) sounddriver = strdup(_ini_sounddriver); - _sound_driver = (SoundDriver*)DriverFactoryBase::SelectDriver(sounddriver, Driver::DT_SOUND); - if (_sound_driver == NULL) { - StrEmpty(sounddriver) ? - usererror("Failed to autoprobe sound driver") : - usererror("Failed to select requested sound driver '%s'", sounddriver); - } + DriverFactoryBase::SelectDriver(sounddriver, Driver::DT_SOUND); free(sounddriver); if (musicdriver == NULL && _ini_musicdriver != NULL) musicdriver = strdup(_ini_musicdriver); - _music_driver = (MusicDriver*)DriverFactoryBase::SelectDriver(musicdriver, Driver::DT_MUSIC); - if (_music_driver == NULL) { - StrEmpty(musicdriver) ? - usererror("Failed to autoprobe music driver") : - usererror("Failed to select requested music driver '%s'", musicdriver); - } + DriverFactoryBase::SelectDriver(musicdriver, Driver::DT_MUSIC); free(musicdriver); /* Take our initial lock on whatever we might want to do! */ @@ -866,7 +851,7 @@ int openttd_main(int argc, char *argv[]) ScanNewGRFFiles(scanner); scanner = NULL; - _video_driver->MainLoop(); + VideoDriver::GetInstance()->MainLoop(); WaitTillSaved(); @@ -934,7 +919,7 @@ static void MakeNewGameDone() SettingsDisableElrail(_settings_game.vehicle.disable_elrails); /* In a dedicated server, the server does not play */ - if (!_video_driver->HasGUI()) { + if (!VideoDriver::GetInstance()->HasGUI()) { SetLocalCompany(COMPANY_SPECTATOR); if (_settings_client.gui.pause_on_newgame) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE); IConsoleCmdExec("exec scripts/game_start.scr 0"); @@ -1500,6 +1485,6 @@ void GameLoop() InputLoop(); - _sound_driver->MainLoop(); + SoundDriver::GetInstance()->MainLoop(); MusicLoop(); } diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp index 09bf6c6e70..c9c98ea9f4 100644 --- a/src/os/unix/unix.cpp +++ b/src/os/unix/unix.cpp @@ -337,9 +337,18 @@ uint GetCPUCoreCount() int ncpu = 0; size_t len = sizeof(ncpu); +#ifdef OPENBSD + int name[2]; + name[0] = CTL_HW; + name[1] = HW_NCPU; + if (sysctl(name, 2, &ncpu, &len, NULL, 0) < 0) { + ncpu = 0; + } +#else if (sysctlbyname("hw.availcpu", &ncpu, &len, NULL, 0) < 0) { sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0); } +#endif /* #ifdef OPENBSD */ if (ncpu > 0) count = ncpu; #elif defined(_SC_NPROCESSORS_ONLN) diff --git a/src/os/windows/crashlog_win.cpp b/src/os/windows/crashlog_win.cpp index 41f83b60ba..2a44aeb1d7 100644 --- a/src/os/windows/crashlog_win.cpp +++ b/src/os/windows/crashlog_win.cpp @@ -521,7 +521,7 @@ static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep) /* Close any possible log files */ CloseConsoleLogIfActive(); - if ((_video_driver == NULL || _video_driver->HasGUI()) && _safe_esp != NULL) { + if ((VideoDriver::GetInstance() == NULL || VideoDriver::GetInstance()->HasGUI()) && _safe_esp != NULL) { #ifdef _M_AMD64 ep->ContextRecord->Rip = (DWORD64)ShowCrashlogWindow; ep->ContextRecord->Rsp = (DWORD64)_safe_esp; diff --git a/src/osk_gui.cpp b/src/osk_gui.cpp index 2516c4dbf6..7ed5554634 100644 --- a/src/osk_gui.cpp +++ b/src/osk_gui.cpp @@ -206,7 +206,7 @@ struct OskWindow : public Window { virtual void OnFocusLost() { - _video_driver->EditBoxLostFocus(); + VideoDriver::GetInstance()->EditBoxLostFocus(); delete this; } }; diff --git a/src/sound/sound_driver.hpp b/src/sound/sound_driver.hpp index 2b9cf145d0..0df69b8b45 100644 --- a/src/sound/sound_driver.hpp +++ b/src/sound/sound_driver.hpp @@ -19,9 +19,15 @@ class SoundDriver : public Driver { public: /** Called once every tick */ virtual void MainLoop() {} + + /** + * Get the currently active instance of the sound driver. + */ + static SoundDriver *GetInstance() { + return static_cast(*DriverFactoryBase::GetActiveDriver(Driver::DT_SOUND)); + } }; -extern SoundDriver *_sound_driver; extern char *_ini_sounddriver; #endif /* SOUND_SOUND_DRIVER_HPP */ diff --git a/src/strgen/strgen.cpp b/src/strgen/strgen.cpp index c407f23454..01692d47e9 100644 --- a/src/strgen/strgen.cpp +++ b/src/strgen/strgen.cpp @@ -122,7 +122,7 @@ struct FileStringReader : StringReader { /* virtual */ char *ReadLine(char *buffer, size_t size) { - return fgets(buffer, size, this->fh); + return fgets(buffer, ClampToU16(size), this->fh); } /* virtual */ void HandlePragma(char *str); diff --git a/src/video/cocoa/cocoa_v.mm b/src/video/cocoa/cocoa_v.mm index 4a95d1901b..ed9d3a91fa 100644 --- a/src/video/cocoa/cocoa_v.mm +++ b/src/video/cocoa/cocoa_v.mm @@ -597,16 +597,16 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel _cocoa_video_dialog = true; bool wasstarted = _cocoa_video_started; - if (_video_driver == NULL) { + if (VideoDriver::GetInstance() == NULL) { setupApplication(); // Setup application before showing dialog - } else if (!_cocoa_video_started && _video_driver->Start(NULL) != NULL) { + } else if (!_cocoa_video_started && VideoDriver::GetInstance()->Start(NULL) != NULL) { fprintf(stderr, "%s: %s\n", title, message); return; } NSRunAlertPanel([ NSString stringWithUTF8String:title ], [ NSString stringWithUTF8String:message ], [ NSString stringWithUTF8String:buttonLabel ], nil, nil); - if (!wasstarted && _video_driver != NULL) _video_driver->Stop(); + if (!wasstarted && VideoDriver::GetInstance() != NULL) VideoDriver::GetInstance()->Stop(); _cocoa_video_dialog = false; } diff --git a/src/video/cocoa/event.mm b/src/video/cocoa/event.mm index b4f171a98a..78785f8d0c 100644 --- a/src/video/cocoa/event.mm +++ b/src/video/cocoa/event.mm @@ -287,7 +287,7 @@ static bool QZ_KeyEvent(unsigned short keycode, unsigned short unicode, BOOL dow case QZ_RETURN: case QZ_f: if (down && (_current_mods & NSCommandKeyMask)) { - _video_driver->ToggleFullscreen(!_fullscreen); + VideoDriver::GetInstance()->ToggleFullscreen(!_fullscreen); } break; } diff --git a/src/video/sdl_v.cpp b/src/video/sdl_v.cpp index cad21955d0..c9935e8a36 100644 --- a/src/video/sdl_v.cpp +++ b/src/video/sdl_v.cpp @@ -409,7 +409,7 @@ bool VideoDriver_SDL::CreateMainSurface(uint w, uint h) break; case Blitter::PALETTE_ANIMATION_BLITTER: - if (_video_driver != NULL) blitter->PaletteAnimate(_local_palette); + if (VideoDriver::GetInstance() != NULL) blitter->PaletteAnimate(_local_palette); break; default: diff --git a/src/video/video_driver.hpp b/src/video/video_driver.hpp index 190bfcc10f..916044d358 100644 --- a/src/video/video_driver.hpp +++ b/src/video/video_driver.hpp @@ -78,9 +78,15 @@ public: * An edit box lost the input focus. Abort character compositing if necessary. */ virtual void EditBoxLostFocus() {} + + /** + * Get the currently active instance of the video driver. + */ + static VideoDriver *GetInstance() { + return static_cast(*DriverFactoryBase::GetActiveDriver(Driver::DT_VIDEO)); + } }; -extern VideoDriver *_video_driver; extern char *_ini_videodriver; extern int _num_resolutions; extern Dimension _resolutions[32]; diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp index c0da6db54e..fc1a3a064a 100644 --- a/src/video/win32_v.cpp +++ b/src/video/win32_v.cpp @@ -1003,7 +1003,7 @@ static LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lP if (active && minimized) { /* Restore the game window */ ShowWindow(hwnd, SW_RESTORE); - static_cast(_video_driver)->MakeWindow(true); + static_cast(VideoDriver::GetInstance())->MakeWindow(true); } else if (!active && !minimized) { /* Minimise the window and restore desktop */ ShowWindow(hwnd, SW_MINIMIZE); diff --git a/src/window.cpp b/src/window.cpp index 8f97693f60..d39bc494e9 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -446,7 +446,7 @@ bool EditBoxInGlobalFocus() void Window::UnfocusFocusedWidget() { if (this->nested_focus != NULL) { - if (this->nested_focus->type == WWT_EDITBOX) _video_driver->EditBoxLostFocus(); + if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus(); /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */ this->nested_focus->SetDirty(this); @@ -470,7 +470,7 @@ bool Window::SetFocusedWidget(int widget_index) /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */ this->nested_focus->SetDirty(this); - if (this->nested_focus->type == WWT_EDITBOX) _video_driver->EditBoxLostFocus(); + if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus(); } this->nested_focus = this->GetWidget(widget_index); return true; @@ -481,7 +481,7 @@ bool Window::SetFocusedWidget(int widget_index) */ void Window::OnFocusLost() { - if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) _video_driver->EditBoxLostFocus(); + if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus(); } /**