Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Console.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_CONSOLE_H
22 #define GF_CONSOLE_H
23 
24 #include <cstdint>
25 #include <string_view>
26 
27 #include "Alignment.h"
28 #include "Array2D.h"
29 #include "Blend.h"
30 #include "ConsoleFont.h"
31 #include "Flags.h"
32 #include "GraphicsApi.h"
33 #include "Path.h"
34 #include "Portability.h"
35 #include "Texture.h"
36 #include "Transformable.h"
37 #include "Vector.h"
38 
39 namespace gf {
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
41 inline namespace v1 {
42 #endif
43 
44 
51  enum class ConsoleAlignment {
52  Left,
53  Center,
54  Right,
55  };
56 
63  using ConsoleColorControl = char;
64 
72 
80 
88 
96 
104 
112 
113 
114 
128  class GF_GRAPHICS_API ConsoleEffect {
129  public:
130 
134  enum Kind : uint32_t {
136  Set,
143  Add,
149  };
150 
159  constexpr ConsoleEffect(Kind kind)
160  : m_kind(kind)
161  , m_alpha(0.0f)
162  {
163 
164  }
165 
175  constexpr ConsoleEffect(Kind kind, float alpha)
176  : m_kind(kind)
177  , m_alpha(alpha)
178  {
179 
180  }
181 
185  constexpr Kind getKind() const noexcept {
186  return m_kind;
187  }
188 
192  constexpr bool isDefault() const noexcept {
193  return m_kind == Default;
194  }
195 
199  constexpr float getAlpha() const noexcept {
200  return m_alpha;
201  }
202 
203  private:
204  Kind m_kind;
205  float m_alpha;
206  };
207 
226  class GF_GRAPHICS_API Console : public Transformable {
227  public:
231  enum class PrintAction {
232  None,
233  Clear,
234  };
235 
242  Console(const ConsoleFont& font, Vector2i size);
243 
247  int getWidth() const {
248  return m_data.getSize().width;
249  }
250 
254  int getHeight() const {
255  return m_data.getSize().height;
256  }
257 
269  void setDefaultBackground(const Color4f& color) {
270  m_background = color;
271  }
272 
279  const Color4f& getDefaultBackground() const {
280  return m_background;
281  }
282 
289  void setDefaultForeground(const Color4f& color) {
290  m_foreground = color;
291  }
292 
299  const Color4f& getDefaultForeground() const {
300  return m_foreground;
301  }
302 
311  void clear();
312 
322  void setCharBackground(Vector2i position, const Color4f& color, ConsoleEffect effect = ConsoleEffect::Set);
323 
332  const Color4f& getCharBackground(Vector2i position) const;
333 
342  void setCharForeground(Vector2i position, const Color4f& color);
343 
352  const Color4f& getCharForeground(Vector2i position) const;
353 
362  void setChar(Vector2i position, char16_t c);
363 
372  char16_t getChar(Vector2i position) const;
373 
386  void putChar(Vector2i position, char16_t c, ConsoleEffect effect = ConsoleEffect::Default);
387 
401  void putChar(Vector2i position, char16_t c, const Color4f& foreground, const Color4f& background);
402 
420  if (!effect.isDefault()) {
421  m_effect = effect;
422  }
423  }
424 
433  return m_effect;
434  }
435 
444  m_alignment = alignment;
445  }
446 
455  return m_alignment;
456  }
457 
472  void print(Vector2i position, const char *fmt, ...) GF_FORMAT(3, 4);
473 
490  void print(Vector2i position, ConsoleEffect effect, ConsoleAlignment alignment, const char *fmt, ...) GF_FORMAT(5, 6);
491 
504  int printRect(const RectI& rect, const char *fmt, ...) GF_FORMAT(3, 4);
505 
520  int printRect(const RectI& rect, ConsoleEffect effect, ConsoleAlignment alignment, const char *fmt, ...) GF_FORMAT(5, 6);
521 
534  int getHeight(const RectI& rect, const char *fmt, ...) GF_FORMAT(3, 4);
535 
549  void setColorControl(ConsoleColorControl ctrl, const Color4f& foreground, const Color4f& background);
550 
571  void drawRectangle(const RectI& rect, PrintAction action = PrintAction::None, ConsoleEffect effect = ConsoleEffect::Default);
572 
582  void drawHorizontalLine(Vector2i left, int width, ConsoleEffect effect = ConsoleEffect::Default);
583 
593  void drawVerticalLine(Vector2i top, int height, ConsoleEffect effect = ConsoleEffect::Default);
594 
607  void drawFrame(const RectI& rect, PrintAction action = PrintAction::None, ConsoleEffect effect = ConsoleEffect::Default, const char *title = nullptr, ...) GF_FORMAT(5, 6);
608 
624  void setFade(float amount, const Color4f& color) {
625  m_fadingAmount = amount;
626  m_fadingColor = color;
627  }
628 
634  float getFadingAmount() const {
635  return m_fadingAmount;
636  }
637 
643  const Color4f& getFadingColor() const {
644  return m_fadingColor;
645  }
646 
660  void blit(const RectI& src, Console& con, Vector2i dst, float foregroundAlpha = 1.0f, float backgroundAlpha = 1.0f) const;
661 
662  virtual void draw(RenderTarget& target, const RenderStates& states) override;
663 
664  private:
665  Color4f computeColor(ConsoleEffect effect, const Color4f& existing, const Color4f& current);
666 
667  int putWord(Vector2i position, ConsoleEffect effect, std::string_view message, const Color4f& foreground, const Color4f& background);
668 
669  enum class PrintOption {
670  Split = 0x01,
671  CountOnly = 0x02,
672  };
673 
674  int printInternal(const RectI& rect, ConsoleEffect effect, ConsoleAlignment alignment, const std::string& message, Flags<PrintOption> flags = None);
675 
676  private:
677  struct Cell {
678  Color4f fg;
679  Color4f bg;
680  char16_t c;
681  };
682 
683  const ConsoleFont *m_font;
684  Array2D<Cell, int> m_data;
685  Color4f m_background;
686  Color4f m_foreground;
687 
688  ConsoleEffect m_effect;
689  ConsoleAlignment m_alignment;
690 
691  struct ColorControl {
692  Color4f fg;
693  Color4f bg;
694  };
695 
696  static constexpr char ColorControlCount = 5;
697  ColorControl m_controls[ColorControlCount];
698 
699  float m_fadingAmount;
700  Color4f m_fadingColor;
701  };
702 
703 #ifndef DOXYGEN_SHOULD_SKIP_THIS
704 }
705 #endif
706 }
707 
708 #endif // GF_CONSOLE_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
Definition: Console.h:146
PrintAction
An action when printing.
Definition: Console.h:231
const Color4f & getDefaultForeground() const
Get the default foreground color.
Definition: Console.h:299
void setDefaultForeground(const Color4f &color)
Set the default foreground color.
Definition: Console.h:289
Kind
The kind of console effect.
Definition: Console.h:134
constexpr ConsoleColorControl ConsoleColorControl3
The constant for color control #3.
Definition: Console.h:87
void setDefaultConsoleEffect(ConsoleEffect effect)
Set the default console effect.
Definition: Console.h:419
constexpr ConsoleColorControl ConsoleColorControl5
The constant for color control #5.
Definition: Console.h:103
A virtual console.
Definition: Console.h:226
Left alignement.
Definition: Console.h:137
constexpr ConsoleEffect(Kind kind)
Constructor with a kind only.
Definition: Console.h:159
Definition: Console.h:145
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
Bitfield relying on an enumeration.
Definition: Flags.h:46
The default widget state.
constexpr ConsoleColorControl ConsoleColorControlStop
The constant for color control stop.
Definition: Console.h:111
const Color4f & getFadingColor() const
Get the fading color.
Definition: Console.h:643
constexpr ConsoleEffect(Kind kind, float alpha)
Constructor with a kind and .
Definition: Console.h:175
Definition: Console.h:142
Use the default console effect.
Definition: Console.h:148
constexpr float getAlpha() const noexcept
Get the alpha value.
Definition: Console.h:199
Definition: Console.h:141
constexpr bool isDefault() const noexcept
Check if the effect if Console::Default.
Definition: Console.h:192
void setFade(float amount, const Color4f &color)
Set the fading parameters.
Definition: Console.h:624
Definition: Console.h:147
Definition: Console.h:139
Definition: Console.h:140
constexpr ConsoleColorControl ConsoleColorControl1
The constant for color control #1.
Definition: Console.h:71
Do not change the background color.
Definition: Console.h:135
Centered alignment.
constexpr ConsoleColorControl ConsoleColorControl2
The constant for color control #2.
Definition: Console.h:79
constexpr ConsoleColorControl ConsoleColorControl4
The constant for color control #4.
Definition: Console.h:95
The namespace for gf classes.
Definition: Action.h:35
ConsoleAlignment
The alignment of the text in the console.
Definition: Console.h:51
ConsoleEffect getDefaultConsoleEffect() const
Get the default console effect.
Definition: Console.h:432
A 4D vector.
Definition: Vector.h:852
Definition: Console.h:143
void setDefaultBackground(const Color4f &color)
Set the default background color.
Definition: Console.h:269
float getFadingAmount() const
Get the fading amount.
Definition: Console.h:634
Definition: Console.h:138
A console font.
Definition: ConsoleFont.h:111
Definition: Console.h:144
Right alignement.
Definition: Console.h:136
A console effect on the background color.
Definition: Console.h:128
ConsoleAlignment getDefaultAlignment() const
Get the default alignment.
Definition: Console.h:454
No alignement.
constexpr Kind getKind() const noexcept
Get the kind of effect.
Definition: Console.h:185
void setDefaultAlignment(ConsoleAlignment alignment)
Set the default alignment.
Definition: Console.h:443
int getHeight() const
Get the height of the console.
Definition: Console.h:254
const Color4f & getDefaultBackground() const
Get the default background color.
Definition: Console.h:279
int getWidth() const
Get the width of the console.
Definition: Console.h:247
char ConsoleColorControl
A type for color controls in a console.
Definition: Console.h:63