(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / driver.h
blob10a6863bc2d080beebd698751946d343de1b98b0
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file driver.h Base for all drivers (video, sound, music, etc). */
12 #ifndef DRIVER_H
13 #define DRIVER_H
15 #include "core/enum_type.hpp"
16 #include "core/string_compare_type.hpp"
17 #include <map>
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. */
24 class Driver {
25 public:
26 /**
27 * Start this driver.
28 * @param parm Parameters passed to the driver.
29 * @return NULL if everything went okay, otherwise an error message.
31 virtual const char *Start(const char * const *parm) = 0;
33 /**
34 * Stop this driver.
36 virtual void Stop() = 0;
38 virtual ~Driver() { }
40 /** The type of driver */
41 enum Type {
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
49 /**
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 {
61 private:
62 Driver::Type type; ///< The type of driver.
63 const char *name; ///< The name of the drivers of this factory.
64 int priority; ///< The priority of this factory.
66 typedef std::map<const char *, DriverFactoryBase *, StringCompare> Drivers; ///< Type for a map of drivers.
68 /**
69 * Get the map with drivers.
71 static Drivers &GetDrivers()
73 static Drivers &s_drivers = *new Drivers();
74 return s_drivers;
77 /**
78 * Get the active driver for the given type.
79 * @param type The type to get the driver for.
80 * @return The active driver.
82 static Driver **GetActiveDriver(Driver::Type type)
84 static Driver *s_driver[3] = { NULL, NULL, NULL };
85 return &s_driver[type];
88 /**
89 * Get the driver type name.
90 * @param type The type of driver to get the name of.
91 * @return The name of the type.
93 static const char *GetDriverTypeName(Driver::Type type)
95 static const char * const driver_type_name[] = { "music", "sound", "video" };
96 return driver_type_name[type];
99 protected:
100 void RegisterDriver(const char *name, Driver::Type type, int priority);
102 public:
103 DriverFactoryBase() :
104 name(NULL)
107 virtual ~DriverFactoryBase();
110 * Shuts down all active drivers
112 static void ShutdownDrivers()
114 for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
115 Driver *driver = *GetActiveDriver(dt);
116 if (driver != NULL) driver->Stop();
120 static Driver *SelectDriver(const char *name, Driver::Type type);
121 static char *GetDriversInfo(char *p, const char *last);
124 * Get a nice description of the driver-class.
125 * @return The description.
127 virtual const char *GetDescription() = 0;
130 * Create an instance of this driver-class.
131 * @return The instance.
133 virtual Driver *CreateInstance() = 0;
136 #endif /* DRIVER_H */