Gorgon Game Engine
Console.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include "../Geometry/Size.h"
5 #include "../Geometry/Point.h"
6 #include "../Graphics/Color.h"
7 #include <assert.h>
8 
9 namespace Gorgon { namespace Utils {
10 
11  class ConsoleBackend;
12 
14  class Console {
15  Console StdOutConsole();
16  public:
18  Console() = default;
19 
21  Console(ConsoleBackend &backend) : backend(&backend), refcount(new int{1}) {
22  }
23 
25  Console(const Console &other) : backend(other.backend), refcount(other.refcount) {
26  (*refcount)++;
27  }
28 
30  Console &operator =(const Console &other);
31 
33 
37  None = 0,
38 
41 
43  RGB
44  };
45 
47  enum Color {
51  Red,
56  Green
57  };
58 
61 
63  bool IsColorSupported() const {
64  return ColorSupport() != None;
65  }
66 
68  bool IsReady() const {
69  return backend != nullptr;
70  }
71 
73  explicit operator bool() const { return IsReady(); }
74 
76  bool IsStylesSupported() const;
77 
80  void SetColor(Color color);
81 
84  void SetColor(Graphics::RGBA color);
85 
87  void SetBackground(Color color);
88 
91 
93  void Reset();
94 
96  void SetBold(bool bold=true);
97 
99  void SetUnderline(bool underline=true);
100 
102  void SetItalic(bool italic=true);
103 
105  void SetNegative(bool negative=true);
106 
109 
111  int GetWidth() const;
112 
114  int GetHeight() const;
115 
117  void GotoXY(Geometry::Point location);
118 
120  inline void GotoXY(int x, int y) { GotoXY({x,y}); }
121 
123  void ClearScreen();
124 
126  void HideCaret();
127 
129  void ShowCaret();
130 
131  std::ostream &OutStream() const;
132 
133  private:
134  ConsoleBackend *backend = nullptr;
135  int *refcount = nullptr;
136  };
137 
139  class ConsoleBackend {
140  public:
141  virtual ~ConsoleBackend() { }
142 
143  virtual Console::ColorSupportLevel ColorSupport() const = 0;
144 
145  virtual bool IsStylesSupported() const = 0;
146 
147  virtual void SetColor(Console::Color color) = 0;
148 
149  virtual void SetColor(Graphics::RGBA color) = 0;
150 
151  virtual void SetBackground(Console::Color color) = 0;
152 
153  virtual void SetBackground(Graphics::RGBA color) = 0;
154 
155  virtual void Reset() = 0;
156 
157  virtual void SetBold(bool value) = 0;
158 
159  virtual void SetUnderline(bool value) = 0;
160 
161  virtual void SetItalic(bool value) = 0;
162 
163  virtual void SetNegative(bool value) = 0;
164 
165  virtual Geometry::Size GetSize() const = 0;
166 
167  virtual void GotoXY(Geometry::Point location) = 0;
168 
169  virtual void ClearScreen() = 0;
170 
171  virtual void HideCaret() = 0;
172 
173  virtual void ShowCaret() = 0;
174 
175  virtual std::ostream &OutStream() const = 0;
176  };
177 
178  class StdOutBackend : public ConsoleBackend {
179  public:
180  virtual Console::ColorSupportLevel ColorSupport() const override;
181 
182  virtual bool IsStylesSupported() const override;
183 
184  virtual void SetColor(Console::Color color) override;
185 
186  virtual void SetColor(Graphics::RGBA color) override;
187 
188  virtual void SetBackground(Console::Color color) override;
189 
190  virtual void SetBackground(Graphics::RGBA color) override;
191 
192  virtual void Reset() override;
193 
194  virtual void SetBold(bool value) override;
195 
196  virtual void SetUnderline(bool value) override;
197 
198  virtual void SetItalic(bool value) override;
199 
200  virtual void SetNegative(bool value) override;
201 
202  virtual Geometry::Size GetSize() const override;
203 
204  virtual void GotoXY(Geometry::Point location) override;
205 
206  virtual void ClearScreen() override;
207 
208  virtual void HideCaret() override;
209 
210  virtual void ShowCaret() override;
211 
212  virtual std::ostream &OutStream() const override { return std::cout; }
213  };
214 
216  inline Console StdConsole() {
217  static Console stdconsole = {*new StdOutBackend};
218 
219  return stdconsole;
220  }
221 
222 
223 #define __mychk assert(refcount)
224 
225 
226  inline Console &Console::operator=(const Console &other) {
227  if(&other == this) return *this;
228 
229  if(refcount) {
230  if(--(*refcount) <= 0) {
231  delete backend;
232  delete refcount;
233  }
234  }
235 
236  backend = other.backend;
237  refcount = other.refcount;
238  if(refcount)
239  (*refcount)++;
240 
241  return *this;
242  }
243 
244  inline Console::~Console() {
245  if(!refcount) return;
246 
247  if(--(*refcount) <= 0) {
248  delete backend;
249  delete refcount;
250  }
251  }
252 
254  __mychk; return backend->ColorSupport();
255  }
256 
257  inline bool Console::IsStylesSupported() const {
258  __mychk; return backend->IsStylesSupported();
259  }
260 
261  inline void Console::SetColor(Color color) {
262  __mychk; backend->SetColor(color);
263  }
264 
265  inline void Console::SetColor(Graphics::RGBA color) {
266  __mychk; backend->SetColor(color);
267  }
268 
269  inline void Console::SetBackground(Color color) {
270  __mychk; backend->SetBackground(color);
271  }
272 
273  inline void Console::SetBackground(Graphics::RGBA color) {
274  __mychk; backend->SetBackground(color);
275  }
276 
277  inline void Console::Reset() {
278  __mychk; backend->Reset();
279  }
280 
281  inline void Console::SetBold(bool bold) {
282  __mychk; backend->SetBold(bold);
283  }
284 
285  inline void Console::SetUnderline(bool underline) {
286  __mychk; backend->SetUnderline(underline);
287  }
288 
289  inline void Console::SetItalic(bool italic) {
290  __mychk; backend->SetUnderline(italic);
291  }
292 
293  inline void Console::SetNegative(bool negative) {
294  __mychk; backend->SetNegative(negative);
295  }
296 
297  inline Geometry::Size Console::GetSize() const {
298  __mychk; return backend->GetSize();
299  }
300 
301  inline int Console::GetWidth() const {
302  __mychk; return GetSize().Width;
303  }
304 
305  inline int Console::GetHeight() const {
306  __mychk; return GetSize().Height;
307  }
308 
309  inline void Console::GotoXY(Geometry::Point location) {
310  __mychk; backend->GotoXY(location);
311  }
312 
313  inline void Console::ClearScreen() {
314  __mychk; backend->ClearScreen();
315  }
316 
317  inline void Console::HideCaret() {
318  __mychk; backend->HideCaret();
319  }
320 
321  inline void Console::ShowCaret() {
322  __mychk; backend->ShowCaret();
323  }
324 
325 
326  inline std::ostream &Console::OutStream() const {
327  __mychk; return backend->OutStream();
328  }
329 
330 #undef __mychk
331 
333 } }
Gorgon::Utils::Console::GetSize
Geometry::Size GetSize() const
Returns the size of the console window in cols/rows.
Gorgon::Utils::Console::ColorSupportLevel
ColorSupportLevel
Level of support for color.
Definition: Console.h:35
Gorgon::Utils::Console::SetColor
void SetColor(Graphics::RGBA color)
Sets the color to the given value, avoid, black and white as console can have its background color re...
Gorgon::Graphics::RGBA
This class represents a color information.
Definition: Color.h:91
Gorgon::Utils::Console::SetUnderline
void SetUnderline(bool underline=true)
Enable/disable underline. Not all consoles support underline.
Gorgon::Filesystem::GetFilename
std::string GetFilename(std::string path)
Returns the filename portion of a file path.
Definition: Filesystem.h:183
Gorgon::Utils::Console::GetHeight
int GetHeight() const
Returns the size of the console window in cols/rows.
Gorgon::Utils::Console::~Console
~Console()
Gorgon::Utils::Console::Color
Color
The colors that can be used for console coloring. This is a safe list.
Definition: Console.h:47
Gorgon::Utils::Console::Reset
void Reset()
Resets terminal attributes.
Gorgon::Utils::Console::IsReady
bool IsReady() const
Returns if the console is usable.
Definition: Console.h:68
Gorgon::Utils::Console::GotoXY
void GotoXY(int x, int y)
Changes the position of the caret to the given position.
Definition: Console.h:120
Gorgon::Geometry::basic_Size::Height
T_ Height
Height of this size object.
Definition: Size.h:261
Gorgon::Utils::Console::SetColor
void SetColor(Color color)
Sets the color to the given value, avoid, black and white as console can have its background color re...
Gorgon::Utils::Console::Red
@ Red
Definition: Console.h:51
Gorgon::Utils::Console::None
@ None
Color is not supported.
Definition: Console.h:37
Gorgon::Filesystem::GetDirectory
std::string GetDirectory(std::string filepath)
Returns the directory portion of a file path.
Definition: Filesystem.h:167
Gorgon::Utils::Console::Safelist
@ Safelist
Only colors in the safelist can be used.
Definition: Console.h:40
Gorgon::Utils::Console::IsStylesSupported
bool IsStylesSupported() const
Returns if color is supported in this terminal. Even if this is false, bold can be emulated.
Gorgon::Geometry::Size
basic_Size< int > Size
Definition: Size.h:385
Gorgon::Utils::Console::SetBold
void SetBold(bool bold=true)
Sets terminal font to bold or normal.
Gorgon::Utils::Console::OutStream
std::ostream & OutStream() const
Gorgon::Utils::Console::White
@ White
Definition: Console.h:50
Gorgon::Utils::Console::ClearScreen
void ClearScreen()
Clears the console screen.
Gorgon::Utils::Console::GetWidth
int GetWidth() const
Returns the size of the console window in cols/rows.
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::basic_Size
This class represents a 2D geometric size.
Definition: Size.h:23
Gorgon::Utils::Console
Console manipulation functions. Not thread safe. Current only std::cout is supported.
Definition: Console.h:14
Gorgon::Geometry::Point
basic_Point< int > Point
Definition: Point.h:598
Gorgon::Utils::Console::Blue
@ Blue
Definition: Console.h:53
Gorgon::Utils::Console::SetBackground
void SetBackground(Graphics::RGBA color)
Sets the background color to the given value. Use Default to set it to default color.
Gorgon::Utils::Console::SetNegative
void SetNegative(bool negative=true)
Background/foreground is switched.
Gorgon::Geometry::basic_Point
This class represents a 2D point.
Definition: Point.h:32
Gorgon::Utils::Console::IsColorSupported
bool IsColorSupported() const
Returns if the color is supported by this console.
Definition: Console.h:63
Gorgon::Utils::Console::HideCaret
void HideCaret()
Hides input cursor.
Gorgon::Utils::Console::Cyan
@ Cyan
Definition: Console.h:52
Gorgon::Utils::Console::Yellow
@ Yellow
Definition: Console.h:54
Gorgon::Utils::Console::RGB
@ RGB
Graphics::RGBA can be used for color.
Definition: Console.h:43
Gorgon::Filesystem::ExePath
std::string ExePath()
Returns the the full path of the application.
Definition: Linux.cpp:226
Gorgon::Geometry::basic_Size::Width
T_ Width
Width of this size object.
Definition: Size.h:258
Gorgon::end
std::vector< T_ >::const_iterator end(enum_type_id< T_ >)
Definition: Enum.h:288
Gorgon::Utils::Console::Default
@ Default
Definition: Console.h:48
Gorgon::Utils::Console::Black
@ Black
Definition: Console.h:49
Gorgon::Utils::Console::Console
Console(const Console &other)
Console objects can be copied. They are reference counted.
Definition: Console.h:25
Gorgon::Utils::Console::SetItalic
void SetItalic(bool italic=true)
Enable/disable italic. Not all consoles support italic.
Gorgon::Utils::Console::GotoXY
void GotoXY(Geometry::Point location)
Changes the position of the caret to the given position.
Gorgon::Filesystem::Canonical
std::string Canonical(const std::string &path)
Canonicalizes a given relative path.
Definition: Linux.cpp:94
Gorgon::Utils::Console::SetBackground
void SetBackground(Color color)
Sets the background color to the given value. Use Default to set it to default color.
Gorgon::Utils::Console::Console
Console(ConsoleBackend &backend)
Creates a new console with the specified backend.
Definition: Console.h:21
Gorgon::Utils::Console::ShowCaret
void ShowCaret()
Shows input cursor.
Gorgon::Utils::Console::Green
@ Green
Definition: Console.h:56
Gorgon::Utils::demangle
std::string demangle(const std::string &name)
Definition: Compiler_GCC.cpp:8
Gorgon::Utils::Console::operator=
Console & operator=(const Console &other)
Console objects can be copied. They are reference counted.
Gorgon::Utils::Console::ColorSupport
ColorSupportLevel ColorSupport() const
Returns if color is supported in this terminal.
Assert.h
Gorgon::Utils::Console::Console
Console()=default
Empty console, nothing can be done with it.
Gorgon::Utils::Console::Magenta
@ Magenta
Definition: Console.h:55