OpenTTD Source  12.0-beta2
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 
24 private:
25  const std::string name;
26  const std::string description;
27 
28  typedef std::map<std::string, BlitterFactory *> Blitters;
29 
35  {
36  static Blitters &s_blitters = *new Blitters();
37  return s_blitters;
38  }
39 
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) :
62  {
63  if (usable) {
64  Blitters &blitters = GetBlitters();
65  assert(blitters.find(this->name) == blitters.end());
66  /*
67  * Only add when the blitter is usable. Do not bail out or
68  * do more special things since the blitters are always
69  * instantiated upon start anyhow and freed upon shutdown.
70  */
71  blitters.insert(Blitters::value_type(this->name, this));
72  } else {
73  Debug(driver, 1, "Not registering blitter {} as it is not usable", name);
74  }
75  }
76 
81  virtual bool IsUsable() const
82  {
83  return true;
84  }
85 
86 public:
87  virtual ~BlitterFactory()
88  {
89  GetBlitters().erase(this->name);
90  if (GetBlitters().empty()) delete &GetBlitters();
91  }
92 
98  static Blitter *SelectBlitter(const std::string &name)
99  {
101  if (b == nullptr) return nullptr;
102 
103  Blitter *newb = b->CreateInstance();
104  delete *GetActiveBlitter();
105  *GetActiveBlitter() = newb;
106 
107  Debug(driver, 1, "Successfully {} blitter '{}'", name.empty() ? "probed" : "loaded", newb->GetName());
108  return newb;
109  }
110 
116  static BlitterFactory *GetBlitterFactory(const std::string &name)
117  {
118 #if defined(DEDICATED)
119  const char *default_blitter = "null";
120 #elif defined(WITH_COCOA)
121  const char *default_blitter = "32bpp-anim";
122 #else
123  const char *default_blitter = "8bpp-optimized";
124 #endif
125  if (GetBlitters().size() == 0) return nullptr;
126  const char *bname = name.empty() ? default_blitter : name.c_str();
127 
128  Blitters::iterator it = GetBlitters().begin();
129  for (; it != GetBlitters().end(); it++) {
130  BlitterFactory *b = (*it).second;
131  if (strcasecmp(bname, b->name.c_str()) == 0) {
132  return b->IsUsable() ? b : nullptr;
133  }
134  }
135  return nullptr;
136  }
137 
142  {
143  return *GetActiveBlitter();
144  }
145 
152  static char *GetBlittersInfo(char *p, const char *last)
153  {
154  p += seprintf(p, last, "List of blitters:\n");
155  Blitters::iterator it = GetBlitters().begin();
156  for (; it != GetBlitters().end(); it++) {
157  BlitterFactory *b = (*it).second;
158  p += seprintf(p, last, "%18s: %s\n", b->name.c_str(), b->GetDescription().c_str());
159  }
160  p += seprintf(p, last, "\n");
161 
162  return p;
163  }
164 
168  const std::string &GetName() const
169  {
170  return this->name;
171  }
172 
176  const std::string &GetDescription() const
177  {
178  return this->description;
179  }
180 
184  virtual Blitter *CreateInstance() = 0;
185 };
186 
187 extern std::string _ini_blitter;
188 extern bool _blitter_autodetected;
189 
190 #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:176
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 its class.
Definition: factory.hpp:98
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:141
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:116
base.hpp
BlitterFactory::IsUsable
virtual bool IsUsable() const
Is the blitter usable with the current drivers and hardware config?
Definition: factory.hpp:81
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
BlitterFactory::GetName
const std::string & GetName() const
Get the long, human readable, name for the Blitter-class.
Definition: factory.hpp:168
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:152