OpenTTD Source  1.11.0-beta1
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  /*
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 
76 public:
77  virtual ~BlitterFactory()
78  {
79  GetBlitters().erase(this->name);
80  if (GetBlitters().empty()) delete &GetBlitters();
81  }
82 
88  static Blitter *SelectBlitter(const std::string &name)
89  {
91  if (b == nullptr) return nullptr;
92 
93  Blitter *newb = b->CreateInstance();
94  delete *GetActiveBlitter();
95  *GetActiveBlitter() = newb;
96 
97  DEBUG(driver, 1, "Successfully %s blitter '%s'", name.empty() ? "probed" : "loaded", newb->GetName());
98  return newb;
99  }
100 
106  static BlitterFactory *GetBlitterFactory(const std::string &name)
107  {
108 #if defined(DEDICATED)
109  const char *default_blitter = "null";
110 #elif defined(WITH_COCOA)
111  const char *default_blitter = "32bpp-anim";
112 #else
113  const char *default_blitter = "8bpp-optimized";
114 #endif
115  if (GetBlitters().size() == 0) return nullptr;
116  const char *bname = name.empty() ? default_blitter : name.c_str();
117 
118  Blitters::iterator it = GetBlitters().begin();
119  for (; it != GetBlitters().end(); it++) {
120  BlitterFactory *b = (*it).second;
121  if (strcasecmp(bname, b->name.c_str()) == 0) {
122  return b;
123  }
124  }
125  return nullptr;
126  }
127 
132  {
133  return *GetActiveBlitter();
134  }
135 
142  static char *GetBlittersInfo(char *p, const char *last)
143  {
144  p += seprintf(p, last, "List of blitters:\n");
145  Blitters::iterator it = GetBlitters().begin();
146  for (; it != GetBlitters().end(); it++) {
147  BlitterFactory *b = (*it).second;
148  p += seprintf(p, last, "%18s: %s\n", b->name.c_str(), b->GetDescription().c_str());
149  }
150  p += seprintf(p, last, "\n");
151 
152  return p;
153  }
154 
158  const std::string &GetName() const
159  {
160  return this->name;
161  }
162 
166  const std::string &GetDescription() const
167  {
168  return this->description;
169  }
170 
174  virtual Blitter *CreateInstance() = 0;
175 };
176 
177 extern std::string _ini_blitter;
178 extern bool _blitter_autodetected;
179 
180 #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:166
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:88
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:131
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:31
_ini_blitter
std::string _ini_blitter
The blitter as stored in the configuration file.
Definition: driver.cpp:30
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:106
base.hpp
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:158
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:142