OpenTTD Source: src/blitter/factory.hpp Source File

OpenTTD Source 1.11.0-RC1

factory.hpp
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 BLITTER_FACTORY_HPP
11 #define BLITTER_FACTORY_HPP
12
13 #include "base.hpp"
14 #include "../debug.h"
15 #include "../string_func.h"
16 #include "../core/string_compare_type.hpp"
17 #include <map>
18
19
23 class BlitterFactory {
24 private:
25 const std::string name;
26 const std::string description;
27
28 typedef std::map<std::string, BlitterFactory *> Blitters;
29
34 static Blitters &GetBlitters()
35 {
36 static Blitters &s_blitters = *new Blitters();
37 return s_blitters;
38 }
39
44 static Blitter **GetActiveBlitter()
45 {
46 static Blitter *s_blitter = nullptr;
47 return &s_blitter;
48 }
49
50 protected:
60 BlitterFactory(const char *name, const char *description, bool usable = true) :
61 name(name), description(description)
62 {
63 if (usable) {
64 /*
65 * Only add when the blitter is usable. Do not bail out or
66 * do more special things since the blitters are always
67 * instantiated upon start anyhow and freed upon shutdown.
68 */
69 std::pair<Blitters::iterator, bool> P = GetBlitters().insert(Blitters::value_type(this->name, this));
70 assert(P.second);
71 } else {
72 DEBUG(driver, 1, "Not registering blitter %s as it is not usable", name);
73 }
74 }
75
80 virtual bool IsUsable() const
81 {
82 return true;
83 }
84
85 public:
86 virtual ~BlitterFactory()
87 {
88 GetBlitters().erase(this->name);
89 if (GetBlitters().empty()) delete &GetBlitters();
90 }
91
97 static Blitter *SelectBlitter(const std::string &name)
98 {
99 BlitterFactory *b = GetBlitterFactory(name);
100 if (b == nullptr) return nullptr;
101
102 Blitter *newb = b->CreateInstance();
103 delete *GetActiveBlitter();
104 *GetActiveBlitter() = newb;
105
106 DEBUG(driver, 1, "Successfully %s blitter '%s'", name.empty() ? "probed" : "loaded", newb->GetName());
107 return newb;
108 }
109
115 static BlitterFactory *GetBlitterFactory(const std::string &name)
116 {
117 #if defined(DEDICATED)
118 const char *default_blitter = "null";
119 #elif defined(WITH_COCOA)
120 const char *default_blitter = "32bpp-anim";
121 #else
122 const char *default_blitter = "8bpp-optimized";
123 #endif
124 if (GetBlitters().size() == 0) return nullptr;
125 const char *bname = name.empty() ? default_blitter : name.c_str();
126
127 Blitters::iterator it = GetBlitters().begin();
128 for (; it != GetBlitters().end(); it++) {
129 BlitterFactory *b = (*it).second;
130 if (strcasecmp(bname, b->name.c_str()) == 0) {
131 return b->IsUsable() ? b : nullptr;
132 }
133 }
134 return nullptr;
135 }
136
140 static Blitter *GetCurrentBlitter()
141 {
142 return *GetActiveBlitter();
143 }
144
151 static char *GetBlittersInfo(char *p, const char *last)
152 {
153 p += seprintf(p, last, "List of blitters:\n");
154 Blitters::iterator it = GetBlitters().begin();
155 for (; it != GetBlitters().end(); it++) {
156 BlitterFactory *b = (*it).second;
157 p += seprintf(p, last, "%18s: %s\n", b->name.c_str(), b->GetDescription().c_str());
158 }
159 p += seprintf(p, last, "\n");
160
161 return p;
162 }
163
167 const std::string &GetName() const
168 {
169 return this->name;
170 }
171
175 const std::string &GetDescription() const
176 {
177 return this->description;
178 }
179
183 virtual Blitter *CreateInstance() = 0;
184 };
185
186 extern std::string _ini_blitter;
187 extern bool _blitter_autodetected;
188
189 #endif /* BLITTER_FACTORY_HPP */
BlitterFactory::CreateInstance
virtual Blitter * CreateInstance()=0
Create an instance of this Blitter-class.
BlitterFactory::GetBlitters
static Blitters & GetBlitters()
Get the map with currently known blitters.
Definition: factory.hpp:34
Blitter
How all blitters should look like.
Definition: base.hpp:28
BlitterFactory::GetDescription
const std::string & GetDescription() const
Get a nice description of the blitter-class.
Definition: factory.hpp:175
BlitterFactory
The base factory, keeping track of all blitters.
Definition: factory.hpp:23
BlitterFactory::SelectBlitter
static Blitter * SelectBlitter(const std::string &name)
Find the requested blitter and return his class.
Definition: factory.hpp:97
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
BlitterFactory::GetActiveBlitter
static Blitter ** GetActiveBlitter()
Get the currently active blitter.
Definition: factory.hpp:44
BlitterFactory::Blitters
std::map< std::string, BlitterFactory * > Blitters
Map of blitter factories.
Definition: factory.hpp:28
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:140
BlitterFactory::BlitterFactory
BlitterFactory(const char *name, const char *description, bool usable=true)
Construct the blitter, and register it.
Definition: factory.hpp:60
Blitter::GetName
virtual const char * GetName()=0
Get the name of the blitter, the same as the Factory-instance returns.
_blitter_autodetected
bool _blitter_autodetected
Was the blitter autodetected or specified by the user?
Definition: driver.cpp:33
_ini_blitter
std::string _ini_blitter
The blitter as stored in the configuration file.
Definition: driver.cpp:32
BlitterFactory::description
const std::string description
The description of the blitter.
Definition: factory.hpp:26
BlitterFactory::GetBlitterFactory
static BlitterFactory * GetBlitterFactory(const std::string &name)
Get the blitter factory with the given name.
Definition: factory.hpp:115
base.hpp
BlitterFactory::IsUsable
virtual bool IsUsable() const
Is the blitter usable with the current drivers and hardware config?
Definition: factory.hpp:80
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:442
BlitterFactory::GetName
const std::string & GetName() const
Get the long, human readable, name for the Blitter-class.
Definition: factory.hpp:167
BlitterFactory::name
const std::string name
The name of the blitter factory.
Definition: factory.hpp:25
BlitterFactory::GetBlittersInfo
static char * GetBlittersInfo(char *p, const char *last)
Fill a buffer with information about the blitters.
Definition: factory.hpp:151