2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file driver.h Base for all drivers (video, sound, music, etc). */
13 #include "core/enum_type.hpp"
14 #include "core/string_compare_type.hpp"
15 #include "string_type.h"
18 const char *GetDriverParam(const StringList
&parm
, const char *name
);
19 bool GetDriverParamBool(const StringList
&parm
, const char *name
);
20 int GetDriverParamInt(const StringList
&parm
, const char *name
, int def
);
22 /** A driver for communicating with the user. */
27 * @param parm Parameters passed to the driver.
28 * @return nullptr if everything went okay, otherwise an error message.
30 virtual const char *Start(const StringList
&parm
) = 0;
35 virtual void Stop() = 0;
39 /** The type of driver */
41 DT_BEGIN
= 0, ///< Helper for iteration
42 DT_MUSIC
= 0, ///< A music driver, needs to be before sound to properly shut down extmidi forked music players
43 DT_SOUND
, ///< A sound driver
44 DT_VIDEO
, ///< A video driver
45 DT_END
, ///< Helper for iteration
49 * Get the name of this driver.
50 * @return The name of the driver.
52 virtual const char *GetName() const = 0;
55 DECLARE_POSTFIX_INCREMENT(Driver::Type
)
58 /** Base for all driver factories. */
59 class DriverFactoryBase
{
61 friend class MusicDriver
;
62 friend class SoundDriver
;
63 friend class VideoDriver
;
65 Driver::Type type
; ///< The type of driver.
66 int priority
; ///< The priority of this factory.
67 const char *name
; ///< The name of the drivers of this factory.
68 const char *description
; ///< The description of this driver.
70 typedef std::map
<std::string
, DriverFactoryBase
*> Drivers
; ///< Type for a map of drivers.
73 * Get the map with drivers.
75 static Drivers
&GetDrivers()
77 static Drivers
&s_drivers
= *new Drivers();
82 * Get the active driver for the given type.
83 * @param type The type to get the driver for.
84 * @return The active driver.
86 static Driver
**GetActiveDriver(Driver::Type type
)
88 static Driver
*s_driver
[3] = { nullptr, nullptr, nullptr };
89 return &s_driver
[type
];
93 * Get the driver type name.
94 * @param type The type of driver to get the name of.
95 * @return The name of the type.
97 static const char *GetDriverTypeName(Driver::Type type
)
99 static const char * const driver_type_name
[] = { "music", "sound", "video" };
100 return driver_type_name
[type
];
103 static bool SelectDriverImpl(const std::string
&name
, Driver::Type type
);
106 DriverFactoryBase(Driver::Type type
, int priority
, const char *name
, const char *description
);
108 virtual ~DriverFactoryBase();
111 * Does the driver use hardware acceleration (video-drivers only).
112 * @return True if the driver uses hardware acceleration.
114 virtual bool UsesHardwareAcceleration() const
121 * Shuts down all active drivers
123 static void ShutdownDrivers()
125 for (Driver::Type dt
= Driver::DT_BEGIN
; dt
< Driver::DT_END
; dt
++) {
126 Driver
*driver
= *GetActiveDriver(dt
);
127 if (driver
!= nullptr) driver
->Stop();
131 static void SelectDriver(const std::string
&name
, Driver::Type type
);
132 static char *GetDriversInfo(char *p
, const char *last
);
135 * Get a nice description of the driver-class.
136 * @return The description.
138 const char *GetDescription() const
140 return this->description
;
144 * Create an instance of this driver-class.
145 * @return The instance.
147 virtual Driver
*CreateInstance() const = 0;
150 #endif /* DRIVER_H */