From 68c6add8ccd9aa32eb799a433aa8a3b05ec84a57 Mon Sep 17 00:00:00 2001 From: peter1138 Date: Thu, 5 Jul 2007 12:23:54 +0000 Subject: (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model. --- src/video/cocoa_v.h | 27 +++++++++++++++++++++++---- src/video/cocoa_v.mm | 46 ++++++++++++++++++++-------------------------- src/video/dedicated_v.cpp | 23 ++++++++--------------- src/video/dedicated_v.h | 26 ++++++++++++++++++++++++-- src/video/null_v.cpp | 24 +++++++++--------------- src/video/null_v.h | 26 ++++++++++++++++++++++++-- src/video/sdl_v.cpp | 27 ++++++++++----------------- src/video/sdl_v.h | 26 ++++++++++++++++++++++++-- src/video/video_driver.hpp | 35 +++++++++++++++++++++++++++++++++++ src/video/win32_v.cpp | 22 +++++++--------------- src/video/win32_v.h | 26 ++++++++++++++++++++++++-- 11 files changed, 208 insertions(+), 100 deletions(-) create mode 100644 src/video/video_driver.hpp (limited to 'src/video') diff --git a/src/video/cocoa_v.h b/src/video/cocoa_v.h index 3e39ec966..a6fd41871 100644 --- a/src/video/cocoa_v.h +++ b/src/video/cocoa_v.h @@ -3,11 +3,30 @@ #ifndef VIDEO_COCOA_H #define VIDEO_COCOA_H -#include "../hal.h" +#include "video_driver.hpp" -#include "../openttd.h" -#include "../gfx.h" +class VideoDriver_Cocoa: public VideoDriver { +public: + /* virtual */ bool CanProbe() { return true; } -extern const HalVideoDriver _cocoa_video_driver; + /* virtual */ const char *Start(const char * const *param); + + /* virtual */ void Stop(); + + /* virtual */ void MakeDirty(int left, int top, int width, int height); + + /* virtual */ void MainLoop(); + + /* virtual */ bool ChangeResolution(int w, int h); + + /* virtual */ void ToggleFullscreen(bool fullscreen); +}; + +class FVideoDriver_Cocoa: public VideoDriverFactory { +public: + /* virtual */ const char *GetName() { return "cocoa"; } + /* virtual */ const char *GetDescription() { return "Cocoa Video Driver"; } + /* virtual */ Driver *CreateInstance() { return new VideoDriver_Cocoa(); } +}; #endif /* VIDEO_COCOA_H */ diff --git a/src/video/cocoa_v.mm b/src/video/cocoa_v.mm index debbc2f53..4848ce991 100644 --- a/src/video/cocoa_v.mm +++ b/src/video/cocoa_v.mm @@ -65,10 +65,12 @@ extern "C" void HideMenuBar(); #include "../stdafx.h" +#include "../openttd.h" #include "../debug.h" #include "../macros.h" #include "../os/macosx/splash.h" #include "../variables.h" +#include "../gfx.h" #include "cocoa_v.h" #include "cocoa_keys.h" #include "../blitter/factory.hpp" @@ -131,14 +133,13 @@ static void QZ_UpdatePalette(uint start, uint count); static void QZ_WarpCursor(int x, int y); static void QZ_ShowMouse(); static void QZ_HideMouse(); -static void CocoaVideoFullScreen(bool full_screen); static NSAutoreleasePool *_ottd_autorelease_pool; static OTTDMain *_ottd_main; -static struct CocoaVideoData { +static struct VideoDriver_Cocoa::Data { bool isset; bool issetting; @@ -366,7 +367,7 @@ static void QZ_KeyEvent(unsigned short keycode, unsigned short unicode, BOOL dow case QZ_RETURN: case QZ_f: if (down && (_cocoa_video_data.current_mods & NSCommandKeyMask)) { - CocoaVideoFullScreen(!_fullscreen); + _video_driver->ToggleFullscreen(!_fullscreen); } break; } @@ -1319,9 +1320,9 @@ static uint32 QZ_FadeGammaIn(const OTTD_QuartzGammaTable* table) return 0; } -static const char* QZ_SetVideoFullScreen(int width, int height) +static const char* QZ_SetVideoToggleFullscreen(int width, int height) { - const char* errstr = "QZ_SetVideoFullScreen error"; + const char* errstr = "QZ_SetVideoToggleFullscreen error"; int exact_match; CFNumberRef number; int bpp; @@ -1707,7 +1708,7 @@ static const char* QZ_SetVideoMode(uint width, uint height, bool fullscreen) _cocoa_video_data.issetting = true; if (fullscreen) { /* Setup full screen video */ - ret = QZ_SetVideoFullScreen(width, height); + ret = QZ_SetVideoToggleFullscreen(width, height); } else { /* Setup windowed video */ ret = QZ_SetVideoWindowed(width, height); @@ -1970,7 +1971,9 @@ static void setupApplication() * Video driver interface * ******************************************************************************/ -static void CocoaVideoStop() +static FVideoDriver_Cocoa iFVideoDriver_Cocoa; + +void VideoDriver_Cocoa::Stop() { if (!_cocoa_video_started) return; @@ -1981,7 +1984,7 @@ static void CocoaVideoStop() _cocoa_video_started = false; } -static const char *CocoaVideoStart(const char * const *parm) +const char *VideoDriver_Cocoa::Start(const char * const *parm) { const char *ret; @@ -1998,12 +2001,12 @@ static const char *CocoaVideoStart(const char * const *parm) QZ_VideoInit(); ret = QZ_SetVideoMode(_cur_resolution[0], _cur_resolution[1], _fullscreen); - if (ret != NULL) CocoaVideoStop(); + if (ret != NULL) VideoDriver_Cocoa::Stop(); return ret; } -static void CocoaVideoMakeDirty(int left, int top, int width, int height) +void VideoDriver_Cocoa::MakeDirty(int left, int top, int width, int height) { if (_cocoa_video_data.num_dirty_rects < MAX_DIRTY_RECTS) { _cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].left = left; @@ -2014,41 +2017,32 @@ static void CocoaVideoMakeDirty(int left, int top, int width, int height) _cocoa_video_data.num_dirty_rects++; } -static void CocoaVideoMainLoop() +void VideoDriver_Cocoa::MainLoop() { /* Start the main event loop */ [NSApp run]; } -static bool CocoaVideoChangeRes(int w, int h) +bool VideoDriver_Cocoa::ChangeResolution(int w, int h) { const char *ret = QZ_SetVideoModeAndRestoreOnFailure((uint)w, (uint)h, _cocoa_video_data.fullscreen); if (ret != NULL) { - DEBUG(driver, 0, "cocoa_v: CocoaVideoChangeRes failed with message: %s", ret); + DEBUG(driver, 0, "cocoa_v: VideoDriver_Cocoa::ChangeResolution failed with message: %s", ret); } return ret == NULL; } -static void CocoaVideoFullScreen(bool full_screen) +void VideoDriver_Cocoa::ToggleFullscreen(bool full_screen) { const char *ret = QZ_SetVideoModeAndRestoreOnFailure(_cocoa_video_data.width, _cocoa_video_data.height, full_screen); if (ret != NULL) { - DEBUG(driver, 0, "cocoa_v: CocoaVideoFullScreen failed with message: %s", ret); + DEBUG(driver, 0, "cocoa_v: VideoDriver_Cocoa::ToggleFullscreen failed with message: %s", ret); } _fullscreen = _cocoa_video_data.fullscreen; } -const HalVideoDriver _cocoa_video_driver = { - CocoaVideoStart, - CocoaVideoStop, - CocoaVideoMakeDirty, - CocoaVideoMainLoop, - CocoaVideoChangeRes, - CocoaVideoFullScreen, -}; - /* This is needed since sometimes assert is called before the videodriver is initialized */ void CocoaDialog(const char* title, const char* message, const char* buttonLabel) @@ -2058,14 +2052,14 @@ void CocoaDialog(const char* title, const char* message, const char* buttonLabel _cocoa_video_dialog = true; wasstarted = _cocoa_video_started; - if (!_cocoa_video_started && CocoaVideoStart(NULL) != NULL) { + if (!_cocoa_video_started && VideoDriver_Cocoa::Start(NULL) != NULL) { fprintf(stderr, "%s: %s\n", title, message); return; } NSRunAlertPanel([NSString stringWithCString: title], [NSString stringWithCString: message], [NSString stringWithCString: buttonLabel], nil, nil); - if (!wasstarted) CocoaVideoStop(); + if (!wasstarted) VideoDriver_Cocoa::Stop(); _cocoa_video_dialog = false; } diff --git a/src/video/dedicated_v.cpp b/src/video/dedicated_v.cpp index cbcbaef3b..d4bfa7cbc 100644 --- a/src/video/dedicated_v.cpp +++ b/src/video/dedicated_v.cpp @@ -119,8 +119,10 @@ static void *_dedicated_video_mem; extern bool SafeSaveOrLoad(const char *filename, int mode, int newgm, Subdirectory subdir); extern void SwitchMode(int new_mode); +static FVideoDriver_Dedicated iFVideoDriver_Dedicated; -static const char *DedicatedVideoStart(const char * const *parm) + +const char *VideoDriver_Dedicated::Start(const char * const *parm) { int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(); if (bpp == 0) _dedicated_video_mem = NULL; @@ -147,7 +149,7 @@ static const char *DedicatedVideoStart(const char * const *parm) return NULL; } -static void DedicatedVideoStop() +void VideoDriver_Dedicated::Stop() { #ifdef WIN32 CloseWindowsConsoleThread(); @@ -155,9 +157,9 @@ static void DedicatedVideoStop() free(_dedicated_video_mem); } -static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {} -static bool DedicatedVideoChangeRes(int w, int h) { return false; } -static void DedicatedVideoFullScreen(bool fs) {} +void VideoDriver_Dedicated::MakeDirty(int left, int top, int width, int height) {} +bool VideoDriver_Dedicated::ChangeResolution(int w, int h) { return false; } +void VideoDriver_Dedicated::ToggleFullscreen(bool fs) {} #if defined(UNIX) || defined(__OS2__) || defined(PSP) static bool InputWaiting() @@ -232,7 +234,7 @@ static void DedicatedHandleKeyInput() IConsoleCmdExec(input_line); // execute command } -static void DedicatedVideoMainLoop() +void VideoDriver_Dedicated::MainLoop() { uint32 cur_ticks = GetTime(); uint32 next_tick = cur_ticks + 30; @@ -295,13 +297,4 @@ static void DedicatedVideoMainLoop() } } -const HalVideoDriver _dedicated_video_driver = { - DedicatedVideoStart, - DedicatedVideoStop, - DedicatedVideoMakeDirty, - DedicatedVideoMainLoop, - DedicatedVideoChangeRes, - DedicatedVideoFullScreen, -}; - #endif /* ENABLE_NETWORK */ diff --git a/src/video/dedicated_v.h b/src/video/dedicated_v.h index 5b752643e..d1dccd0c9 100644 --- a/src/video/dedicated_v.h +++ b/src/video/dedicated_v.h @@ -3,8 +3,30 @@ #ifndef VIDEO_DEDICATED_H #define VIDEO_DEDICATED_H -#include "../hal.h" +#include "video_driver.hpp" -extern const HalVideoDriver _dedicated_video_driver; +class VideoDriver_Dedicated: public VideoDriver { +public: + /* virtual */ bool CanProbe() { return false; } + + /* virtual */ const char *Start(const char * const *param); + + /* virtual */ void Stop(); + + /* virtual */ void MakeDirty(int left, int top, int width, int height); + + /* virtual */ void MainLoop(); + + /* virtual */ bool ChangeResolution(int w, int h); + + /* virtual */ void ToggleFullscreen(bool fullscreen); +}; + +class FVideoDriver_Dedicated: public VideoDriverFactory { +public: + /* virtual */ const char *GetName() { return "dedicated"; } + /* virtual */ const char *GetDescription() { return "Dedicated Video Driver"; } + /* virtual */ Driver *CreateInstance() { return new VideoDriver_Dedicated(); } +}; #endif /* VIDEO_DEDICATED_H */ diff --git a/src/video/null_v.cpp b/src/video/null_v.cpp index d6afc7af4..0a01eb6f9 100644 --- a/src/video/null_v.cpp +++ b/src/video/null_v.cpp @@ -9,7 +9,9 @@ #include "../blitter/factory.hpp" #include "null_v.h" -static const char* NullVideoStart(const char* const* parm) +static FVideoDriver_Null iFVideoDriver_Null; + +const char *VideoDriver_Null::Start(const char* const *parm) { _screen.width = _screen.pitch = _cur_resolution[0]; _screen.height = _cur_resolution[1]; @@ -19,11 +21,11 @@ static const char* NullVideoStart(const char* const* parm) return NULL; } -static void NullVideoStop() { } +void VideoDriver_Null::Stop() { } -static void NullVideoMakeDirty(int left, int top, int width, int height) {} +void VideoDriver_Null::MakeDirty(int left, int top, int width, int height) {} -static void NullVideoMainLoop() +void VideoDriver_Null::MainLoop() { uint i; @@ -34,14 +36,6 @@ static void NullVideoMainLoop() } } -static bool NullVideoChangeRes(int w, int h) { return false; } -static void NullVideoFullScreen(bool fs) {} - -const HalVideoDriver _null_video_driver = { - NullVideoStart, - NullVideoStop, - NullVideoMakeDirty, - NullVideoMainLoop, - NullVideoChangeRes, - NullVideoFullScreen, -}; +bool VideoDriver_Null::ChangeResolution(int w, int h) { return false; } + +void VideoDriver_Null::ToggleFullscreen(bool fs) {} diff --git a/src/video/null_v.h b/src/video/null_v.h index 21f1af7a9..0e65933e8 100644 --- a/src/video/null_v.h +++ b/src/video/null_v.h @@ -3,8 +3,30 @@ #ifndef VIDEO_NULL_H #define VIDEO_NULL_H -#include "../hal.h" +#include "video_driver.hpp" -extern const HalVideoDriver _null_video_driver; +class VideoDriver_Null: public VideoDriver { +public: + /* virtual */ bool CanProbe() { return false; } + + /* virtual */ const char *Start(const char * const *param); + + /* virtual */ void Stop(); + + /* virtual */ void MakeDirty(int left, int top, int width, int height); + + /* virtual */ void MainLoop(); + + /* virtual */ bool ChangeResolution(int w, int h); + + /* virtual */ void ToggleFullscreen(bool fullscreen); +}; + +class FVideoDriver_Null: public VideoDriverFactory { +public: + /* virtual */ const char *GetName() { return "null"; } + /* virtual */ const char *GetDescription() { return "Null Video Driver"; } + /* virtual */ Driver *CreateInstance() { return new VideoDriver_Null(); } +}; #endif /* VIDEO_NULL_H */ diff --git a/src/video/sdl_v.cpp b/src/video/sdl_v.cpp index bc12edb92..b6cc38a64 100644 --- a/src/video/sdl_v.cpp +++ b/src/video/sdl_v.cpp @@ -17,6 +17,8 @@ #include "sdl_v.h" #include +static FVideoDriver_SDL iFVideoDriver_SDL; + static SDL_Surface *_sdl_screen; static bool _all_modes; @@ -24,7 +26,7 @@ static bool _all_modes; static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS]; static int _num_dirty_rects; -static void SdlVideoMakeDirty(int left, int top, int width, int height) +void VideoDriver_SDL::MakeDirty(int left, int top, int width, int height) { if (_num_dirty_rects < MAX_DIRTY_RECTS) { _dirty_rects[_num_dirty_rects].x = left; @@ -415,7 +417,7 @@ static int PollEvent() return -1; } -static const char *SdlVideoStart(const char * const *parm) +const char *VideoDriver_SDL::Start(const char * const *parm) { char buf[30]; @@ -434,12 +436,12 @@ static const char *SdlVideoStart(const char * const *parm) return NULL; } -static void SdlVideoStop() +void VideoDriver_SDL::Stop() { SdlClose(SDL_INIT_VIDEO); } -static void SdlVideoMainLoop() +void VideoDriver_SDL::MainLoop() { uint32 cur_ticks = SDL_CALL SDL_GetTicks(); uint32 last_cur_ticks = cur_ticks; @@ -505,28 +507,19 @@ static void SdlVideoMainLoop() } } -static bool SdlVideoChangeRes(int w, int h) +bool VideoDriver_SDL::ChangeResolution(int w, int h) { return CreateMainSurface(w, h); } -static void SdlVideoFullScreen(bool full_screen) +void VideoDriver_SDL::ToggleFullscreen(bool fullscreen) { - _fullscreen = full_screen; + _fullscreen = fullscreen; GetVideoModes(); // get the list of available video modes - if (_num_resolutions == 0 || !_video_driver->change_resolution(_cur_resolution[0], _cur_resolution[1])) { + if (_num_resolutions == 0 || !this->ChangeResolution(_cur_resolution[0], _cur_resolution[1])) { // switching resolution failed, put back full_screen to original status _fullscreen ^= true; } } -const HalVideoDriver _sdl_video_driver = { - SdlVideoStart, - SdlVideoStop, - SdlVideoMakeDirty, - SdlVideoMainLoop, - SdlVideoChangeRes, - SdlVideoFullScreen, -}; - #endif /* WITH_SDL */ diff --git a/src/video/sdl_v.h b/src/video/sdl_v.h index 2868b1c5c..ff494919d 100644 --- a/src/video/sdl_v.h +++ b/src/video/sdl_v.h @@ -3,8 +3,30 @@ #ifndef VIDEO_SDL_H #define VIDEO_SDL_H -#include "../hal.h" +#include "video_driver.hpp" -extern const HalVideoDriver _sdl_video_driver; +class VideoDriver_SDL: public VideoDriver { +public: + /* virtual */ bool CanProbe() { return true; } + + /* virtual */ const char *Start(const char * const *param); + + /* virtual */ void Stop(); + + /* virtual */ void MakeDirty(int left, int top, int width, int height); + + /* virtual */ void MainLoop(); + + /* virtual */ bool ChangeResolution(int w, int h); + + /* virtual */ void ToggleFullscreen(bool fullscreen); +}; + +class FVideoDriver_SDL: public VideoDriverFactory { +public: + /* virtual */ const char *GetName() { return "sdl"; } + /* virtual */ const char *GetDescription() { return "SDL Video Driver"; } + /* virtual */ Driver *CreateInstance() { return new VideoDriver_SDL(); } +}; #endif /* VIDEO_SDL_H */ diff --git a/src/video/video_driver.hpp b/src/video/video_driver.hpp new file mode 100644 index 000000000..eab438491 --- /dev/null +++ b/src/video/video_driver.hpp @@ -0,0 +1,35 @@ +/* $Id$ */ + +#ifndef VIDEO_VIDEO_DRIVER_HPP +#define VIDEO_VIDEO_DRIVER_HPP + +#include "../driver.h" + +class VideoDriver: public Driver { +public: + virtual void MakeDirty(int left, int top, int width, int height) = 0; + + virtual void MainLoop() = 0; + + virtual bool ChangeResolution(int w, int h) = 0; + + virtual void ToggleFullscreen(bool fullscreen) = 0; +}; + +class VideoDriverFactoryBase: public DriverFactoryBase { +}; + +template +class VideoDriverFactory: public VideoDriverFactoryBase { +public: + VideoDriverFactory() { this->RegisterDriver(((T *)this)->GetName(), Driver::DT_VIDEO); } + + /** + * Get the long, human readable, name for the Driver-class. + */ + const char *GetName(); +}; + +extern VideoDriver *_video_driver; + +#endif /* VIDEO_VIDEO_DRIVER_HPP */ diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp index 7fa306ff3..a1a987565 100644 --- a/src/video/win32_v.cpp +++ b/src/video/win32_v.cpp @@ -751,8 +751,9 @@ static void FindResolutions() SortResolutions(_num_resolutions); } +static FVideoDriver_Win32 iFVideoDriver_Win32; -static const char *Win32GdiStart(const char * const *parm) +const char *VideoDriver_Win32::Start(const char * const *parm) { memset(&_wnd, 0, sizeof(_wnd)); @@ -774,7 +775,7 @@ static const char *Win32GdiStart(const char * const *parm) return NULL; } -static void Win32GdiStop() +void VideoDriver_Win32::Stop() { DeleteObject(_wnd.gdi_palette); DeleteObject(_wnd.dib_sect); @@ -786,7 +787,7 @@ static void Win32GdiStop() MyShowCursor(true); } -static void Win32GdiMakeDirty(int left, int top, int width, int height) +void VideoDriver_Win32::MakeDirty(int left, int top, int width, int height) { RECT r = { left, top, left + width, top + height }; @@ -800,7 +801,7 @@ static void CheckPaletteAnim() InvalidateRect(_wnd.main_wnd, NULL, FALSE); } -static void Win32GdiMainLoop() +void VideoDriver_Win32::MainLoop() { MSG mesg; uint32 cur_ticks = GetTickCount(); @@ -873,7 +874,7 @@ static void Win32GdiMainLoop() } } -static bool Win32GdiChangeRes(int w, int h) +bool VideoDriver_Win32::ChangeResolution(int w, int h) { _wnd.width = _wnd.width_org = w; _wnd.height = _wnd.height_org = h; @@ -883,16 +884,7 @@ static bool Win32GdiChangeRes(int w, int h) return true; } -static void Win32GdiFullScreen(bool full_screen) +void VideoDriver_Win32::ToggleFullscreen(bool full_screen) { MakeWindow(full_screen); } - -const HalVideoDriver _win32_video_driver = { - Win32GdiStart, - Win32GdiStop, - Win32GdiMakeDirty, - Win32GdiMainLoop, - Win32GdiChangeRes, - Win32GdiFullScreen, -}; diff --git a/src/video/win32_v.h b/src/video/win32_v.h index d63a526f8..afd33a88c 100644 --- a/src/video/win32_v.h +++ b/src/video/win32_v.h @@ -3,8 +3,30 @@ #ifndef VIDEO_WIN32_H #define VIDEO_WIN32_H -#include "../hal.h" +#include "video_driver.hpp" -extern const HalVideoDriver _win32_video_driver; +class VideoDriver_Win32: public VideoDriver { +public: + /* virtual */ bool CanProbe() { return true; } + + /* virtual */ const char *Start(const char * const *param); + + /* virtual */ void Stop(); + + /* virtual */ void MakeDirty(int left, int top, int width, int height); + + /* virtual */ void MainLoop(); + + /* virtual */ bool ChangeResolution(int w, int h); + + /* virtual */ void ToggleFullscreen(bool fullscreen); +}; + +class FVideoDriver_Win32: public VideoDriverFactory { +public: + /* virtual */ const char *GetName() { return "win32"; } + /* virtual */ const char *GetDescription() { return "Win32 Video Driver"; } + /* virtual */ Driver *CreateInstance() { return new VideoDriver_Win32(); } +}; #endif /* VIDEO_WIN32_H */ -- cgit v1.2.3-54-g00ecf