(svn r27707) -Fix: Make the result of NewGRF's CARGO_NAME string code reliably print...
[openttd.git] / src / driver.h
blob12ca4474d4014d2e9e348bf8c0be5ed6d4585e7b
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 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.
73 /**
74 * Get the map with drivers.
76 static Drivers &GetDrivers()
78 static Drivers &s_drivers = *new Drivers();
79 return s_drivers;
82 /**
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] = { NULL, NULL, NULL };
90 return &s_driver[type];
93 /**
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);
106 protected:
107 DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
109 virtual ~DriverFactoryBase();
111 public:
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 != NULL) 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 */