OpenTTD
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 #if defined(WITH_COCOA)
20 bool QZ_CanDisplay8bpp();
21 #endif /* defined(WITH_COCOA) */
22 
27 private:
28  const char *name;
29  const char *description;
30 
31  typedef std::map<const char *, BlitterFactory *, StringCompare> Blitters;
32 
37  static Blitters &GetBlitters()
38  {
39  static Blitters &s_blitters = *new Blitters();
40  return s_blitters;
41  }
42 
48  {
49  static Blitter *s_blitter = nullptr;
50  return &s_blitter;
51  }
52 
53 protected:
63  BlitterFactory(const char *name, const char *description, bool usable = true) :
64  name(stredup(name)), description(stredup(description))
65  {
66  if (usable) {
67  /*
68  * Only add when the blitter is usable. Do not bail out or
69  * do more special things since the blitters are always
70  * instantiated upon start anyhow and freed upon shutdown.
71  */
72  std::pair<Blitters::iterator, bool> P = GetBlitters().insert(Blitters::value_type(this->name, this));
73  assert(P.second);
74  } else {
75  DEBUG(driver, 1, "Not registering blitter %s as it is not usable", name);
76  }
77  }
78 
79 public:
80  virtual ~BlitterFactory()
81  {
82  GetBlitters().erase(this->name);
83  if (GetBlitters().empty()) delete &GetBlitters();
84 
85  free(this->name);
86  free(this->description);
87  }
88 
94  static Blitter *SelectBlitter(const char *name)
95  {
97  if (b == nullptr) return nullptr;
98 
99  Blitter *newb = b->CreateInstance();
100  delete *GetActiveBlitter();
101  *GetActiveBlitter() = newb;
102 
103  DEBUG(driver, 1, "Successfully %s blitter '%s'", StrEmpty(name) ? "probed" : "loaded", newb->GetName());
104  return newb;
105  }
106 
112  static BlitterFactory *GetBlitterFactory(const char *name)
113  {
114 #if defined(DEDICATED)
115  const char *default_blitter = "null";
116 #else
117  const char *default_blitter = "8bpp-optimized";
118 
119 #if defined(WITH_COCOA)
120  /* Some people reported lack of fullscreen support in 8 bpp mode.
121  * While we prefer 8 bpp since it's faster, we will still have to test for support. */
122  if (!QZ_CanDisplay8bpp()) {
123  /* The main display can't go to 8 bpp fullscreen mode.
124  * We will have to switch to 32 bpp by default. */
125  default_blitter = "32bpp-anim";
126  }
127 #endif /* defined(WITH_COCOA) */
128 #endif /* defined(DEDICATED) */
129  if (GetBlitters().size() == 0) return nullptr;
130  const char *bname = (StrEmpty(name)) ? default_blitter : name;
131 
132  Blitters::iterator it = GetBlitters().begin();
133  for (; it != GetBlitters().end(); it++) {
134  BlitterFactory *b = (*it).second;
135  if (strcasecmp(bname, b->name) == 0) {
136  return b;
137  }
138  }
139  return nullptr;
140  }
141 
146  {
147  return *GetActiveBlitter();
148  }
149 
156  static char *GetBlittersInfo(char *p, const char *last)
157  {
158  p += seprintf(p, last, "List of blitters:\n");
159  Blitters::iterator it = GetBlitters().begin();
160  for (; it != GetBlitters().end(); it++) {
161  BlitterFactory *b = (*it).second;
162  p += seprintf(p, last, "%18s: %s\n", b->name, b->GetDescription());
163  }
164  p += seprintf(p, last, "\n");
165 
166  return p;
167  }
168 
172  const char *GetName() const
173  {
174  return this->name;
175  }
176 
180  const char *GetDescription() const
181  {
182  return this->description;
183  }
184 
188  virtual Blitter *CreateInstance() = 0;
189 };
190 
191 extern char *_ini_blitter;
192 extern bool _blitter_autodetected;
193 
194 #endif /* BLITTER_FACTORY_HPP */
bool _blitter_autodetected
Was the blitter autodetected or specified by the user?
Definition: driver.cpp:29
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:407
const char * GetName() const
Get the long, human readable, name for the Blitter-class.
Definition: factory.hpp:172
virtual Blitter * CreateInstance()=0
Create an instance of this Blitter-class.
How all blitters should look like.
Definition: base.hpp:28
const char * GetDescription() const
Get a nice description of the blitter-class.
Definition: factory.hpp:180
virtual const char * GetName()=0
Get the name of the blitter, the same as the Factory-instance returns.
static char * GetBlittersInfo(char *p, const char *last)
Fill a buffer with information about the blitters.
Definition: factory.hpp:156
char * _ini_blitter
The blitter as stored in the configuration file.
Definition: driver.cpp:28
BlitterFactory(const char *name, const char *description, bool usable=true)
Construct the blitter, and register it.
Definition: factory.hpp:63
static Blitter * SelectBlitter(const char *name)
Find the requested blitter and return his class.
Definition: factory.hpp:94
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
Base for all blitters.
std::map< const char *, BlitterFactory *, StringCompare > Blitters
Map of blitter factories.
Definition: factory.hpp:31
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:145
const char * description
The description of the blitter.
Definition: factory.hpp:29
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:57
static Blitter ** GetActiveBlitter()
Get the currently active blitter.
Definition: factory.hpp:47
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
static BlitterFactory * GetBlitterFactory(const char *name)
Get the blitter factory with the given name.
Definition: factory.hpp:112
The base factory, keeping track of all blitters.
Definition: factory.hpp:26
const char * name
The name of the blitter factory.
Definition: factory.hpp:28
static Blitters & GetBlitters()
Get the map with currently known blitters.
Definition: factory.hpp:37