1 /* $Id: driver.h 26108 2013-11-25 14:30:22Z rubidium $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file driver.h Base for all drivers (video, sound, music, etc). */
15 #include "core/enum_type.hpp"
16 #include "core/string_compare_type.hpp"
19 const char *GetDriverParam(const char * const *parm
, const char *name
);
20 bool GetDriverParamBool(const char * const *parm
, const char *name
);
21 int GetDriverParamInt(const char * const *parm
, const char *name
, int def
);
23 /** A driver for communicating with the user. */
28 * @param parm Parameters passed to the driver.
29 * @return nullptr if everything went okay, otherwise an error message.
31 virtual const char *Start(const char * const *parm
) = 0;
36 virtual void Stop() = 0;
40 /** The type of driver */
42 DT_BEGIN
= 0, ///< Helper for iteration
43 DT_MUSIC
= 0, ///< A music driver, needs to be before sound to properly shut down extmidi forked music players
44 DT_SOUND
, ///< A sound driver
45 DT_VIDEO
, ///< A video driver
46 DT_END
, ///< Helper for iteration
50 * Get the name of this driver.
51 * @return The name of the driver.
53 virtual const char *GetName() const = 0;
56 DECLARE_POSTFIX_INCREMENT(Driver::Type
)
59 /** Base for all driver factories. */
60 class DriverFactoryBase
{
62 friend class MusicDriver
;
63 friend class SoundDriver
;
64 friend class VideoDriver
;
66 Driver::Type type
; ///< The type of driver.
67 int priority
; ///< The priority of this factory.
68 const char *name
; ///< The name of the drivers of this factory.
69 const char *description
; ///< The description of this driver.
71 typedef std::map
<const char *, DriverFactoryBase
*, StringCompare
> Drivers
; ///< Type for a map of drivers.
74 * Get the map with drivers.
76 static Drivers
&GetDrivers()
78 static Drivers
&s_drivers
= *new Drivers();
83 * Get the active driver for the given type.
84 * @param type The type to get the driver for.
85 * @return The active driver.
87 static Driver
**GetActiveDriver(Driver::Type type
)
89 static Driver
*s_driver
[3] = { nullptr, nullptr, nullptr };
90 return &s_driver
[type
];
94 * Get the driver type name.
95 * @param type The type of driver to get the name of.
96 * @return The name of the type.
98 static const char *GetDriverTypeName(Driver::Type type
)
100 static const char * const driver_type_name
[] = { "music", "sound", "video" };
101 return driver_type_name
[type
];
104 static bool SelectDriverImpl(const char *name
, Driver::Type type
);
107 DriverFactoryBase(Driver::Type type
, int priority
, const char *name
, const char *description
);
109 virtual ~DriverFactoryBase();
113 * Shuts down all active drivers
115 static void ShutdownDrivers()
117 for (Driver::Type dt
= Driver::DT_BEGIN
; dt
< Driver::DT_END
; dt
++) {
118 Driver
*driver
= *GetActiveDriver(dt
);
119 if (driver
!= nullptr) driver
->Stop();
123 static void SelectDriver(const char *name
, Driver::Type type
);
124 static char *GetDriversInfo(char *p
, const char *last
);
127 * Get a nice description of the driver-class.
128 * @return The description.
130 const char *GetDescription() const
132 return this->description
;
136 * Create an instance of this driver-class.
137 * @return The instance.
139 virtual Driver
*CreateInstance() const = 0;
142 #endif /* DRIVER_H */