OpenTTD Source: src/driver.h Source File

OpenTTD Source 14.1

driver.h
Go to the documentation of this file.
1 /*
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/>.
6 */
7
10 #ifndef DRIVER_H
11 #define DRIVER_H
12
13 #include "core/enum_type.hpp"
14 #include "string_type.h"
15
16 const char *GetDriverParam(const StringList &parm, const char *name);
17 bool GetDriverParamBool(const StringList &parm, const char *name);
18 int GetDriverParamInt(const StringList &parm, const char *name, int def);
19
21 class Driver {
22 public:
28 virtual const char *Start(const StringList &parm) = 0;
29
33 virtual void Stop() = 0;
34
35 virtual ~Driver() = default;
36
38 enum Type {
39 DT_BEGIN = 0,
40 DT_MUSIC = 0,
41 DT_SOUND,
42 DT_VIDEO,
43 DT_END,
44 };
45
50 virtual const char *GetName() const = 0;
51 };
52
53 DECLARE_POSTFIX_INCREMENT(Driver::Type)
54
55
56
57 class DriverFactoryBase {
58 private:
59 friend class MusicDriver;
60 friend class SoundDriver;
61 friend class VideoDriver;
62
63 Driver::Type type;
64 int priority;
65 const char *name;
66 const char *description;
67
68 typedef std::map<std::string, DriverFactoryBase *> Drivers;
69
73 static Drivers &GetDrivers()
74 {
75 static Drivers &s_drivers = *new Drivers();
76 return s_drivers;
77 }
78
84 static Driver **GetActiveDriver(Driver::Type type)
85 {
86 static Driver *s_driver[3] = { nullptr, nullptr, nullptr };
87 return &s_driver[type];
88 }
89
95 static const char *GetDriverTypeName(Driver::Type type)
96 {
97 static const char * const driver_type_name[] = { "music", "sound", "video" };
98 return driver_type_name[type];
99 }
100
101 static bool SelectDriverImpl(const std::string &name, Driver::Type type);
102
103 static void MarkVideoDriverOperational();
104
105 protected:
106 DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
107
108 virtual ~DriverFactoryBase();
109
114 virtual bool UsesHardwareAcceleration() const
115 {
116 return false;
117 }
118
119 public:
123 static void ShutdownDrivers()
124 {
125 for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
126 Driver *driver = *GetActiveDriver(dt);
127 if (driver != nullptr) driver->Stop();
128 }
129 }
130
131 static void SelectDriver(const std::string &name, Driver::Type type);
132 static void GetDriversInfo(std::back_insert_iterator<std::string> &output_iterator);
133
138 const char *GetDescription() const
139 {
140 return this->description;
141 }
142
147 virtual Driver *CreateInstance() const = 0;
148 };
149
150 #endif /* DRIVER_H */
DriverFactoryBase::GetDriverTypeName
static const char * GetDriverTypeName(Driver::Type type)
Get the driver type name.
Definition: driver.h:95
DriverFactoryBase::ShutdownDrivers
static void ShutdownDrivers()
Shuts down all active drivers.
Definition: driver.h:123
DriverFactoryBase::GetActiveDriver
static Driver ** GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition: driver.h:84
Driver::GetName
virtual const char * GetName() const =0
Get the name of this driver.
VideoDriver
The base of all video drivers.
Definition: video_driver.hpp:34
DriverFactoryBase::name
const char * name
The name of the drivers of this factory.
Definition: driver.h:65
DriverFactoryBase::GetDrivers
static Drivers & GetDrivers()
Get the map with drivers.
Definition: driver.h:73
DECLARE_POSTFIX_INCREMENT
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
Definition: enum_type.hpp:14
DriverFactoryBase::description
const char * description
The description of this driver.
Definition: driver.h:66
DriverFactoryBase::priority
int priority
The priority of this factory.
Definition: driver.h:64
DriverFactoryBase::Drivers
std::map< std::string, DriverFactoryBase * > Drivers
Type for a map of drivers.
Definition: driver.h:68
Driver::Start
virtual const char * Start(const StringList &parm)=0
Start this driver.
DriverFactoryBase::type
Driver::Type type
The type of driver.
Definition: driver.h:63
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:60
GetDriverParamInt
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:82
string_type.h
Driver::Type
Type
The type of driver.
Definition: driver.h:38
Driver::DT_END
@ DT_END
Helper for iteration.
Definition: driver.h:43
Driver::DT_SOUND
@ DT_SOUND
A sound driver.
Definition: driver.h:41
Driver::DT_BEGIN
@ DT_BEGIN
Helper for iteration.
Definition: driver.h:39
Driver::DT_VIDEO
@ DT_VIDEO
A video driver.
Definition: driver.h:42
DriverFactoryBase::UsesHardwareAcceleration
virtual bool UsesHardwareAcceleration() const
Does the driver use hardware acceleration (video-drivers only).
Definition: driver.h:114
DriverFactoryBase::GetDescription
const char * GetDescription() const
Get a nice description of the driver-class.
Definition: driver.h:138
SoundDriver
Base for all sound drivers.
Definition: sound_driver.hpp:16
enum_type.hpp
GetDriverParamBool
bool GetDriverParamBool(const StringList &parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:70
MusicDriver
Driver for all music playback.
Definition: music_driver.hpp:18
Driver
A driver for communicating with the user.
Definition: driver.h:21
Driver::DT_MUSIC
@ DT_MUSIC
A music driver, needs to be before sound to properly shut down extmidi forked music players.
Definition: driver.h:40
GetDriverParam
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
Definition: driver.cpp:50
Driver::Stop
virtual void Stop()=0
Stop this driver.
DriverFactoryBase
Base for all driver factories.
Definition: driver.h:57