Gamedev Framework (gf)  0.12.0
A C++14 framework for 2D games
Console.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 
26 #include "Alignment.h"
27 #include "Array2D.h"
28 #include "Blend.h"
29 #include "ConsoleFont.h"
30 #include "Flags.h"
31 #include "Path.h"
32 #include "Portability.h"
33 #include "StringRef.h"
34 #include "Texture.h"
35 #include "Transformable.h"
36 #include "Vector.h"
37 
38 namespace gf {
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 inline namespace v1 {
41 #endif
42 
43 
50  enum class ConsoleAlignment {
51  Left,
52  Center,
53  Right,
54  };
55 
62  using ConsoleColorControl = char;
63 
71 
79 
87 
95 
103 
111 
112 
113 
126  class GF_API ConsoleEffect {
127  public:
128 
132  enum Kind : uint32_t {
134  Set,
141  Add,
147  };
148 
157  constexpr ConsoleEffect(Kind kind)
158  : m_kind(kind)
159  , m_alpha(0.0f)
160  {
161 
162  }
163 
173  constexpr ConsoleEffect(Kind kind, float alpha)
174  : m_kind(kind)
175  , m_alpha(alpha)
176  {
177 
178  }
179 
183  constexpr Kind getKind() const noexcept {
184  return m_kind;
185  }
186 
190  constexpr bool isDefault() const noexcept {
191  return m_kind == Default;
192  }
193 
197  constexpr float getAlpha() const noexcept {
198  return m_alpha;
199  }
200 
201  private:
202  Kind m_kind;
203  float m_alpha;
204  };
205 
224  class GF_API Console : public Transformable {
225  public:
229  enum class PrintAction {
230  None,
231  Clear,
232  };
233 
240  Console(const ConsoleFont& font, Vector2i size);
241 
245  int getWidth() const {
246  return m_data.getSize().width;
247  }
248 
252  int getHeight() const {
253  return m_data.getSize().height;
254  }
255 
267  void setDefaultBackground(const Color4f& color) {
268  m_background = color;
269  }
270 
277  const Color4f& getDefaultBackground() const {
278  return m_background;
279  }
280 
287  void setDefaultForeground(const Color4f& color) {
288  m_foreground = color;
289  }
290 
297  const Color4f& getDefaultForeground() const {
298  return m_foreground;
299  }
300 
309  void clear();
310 
320  void setCharBackground(Vector2i position, const Color4f& color, ConsoleEffect effect = ConsoleEffect::Set);
321 
330  const Color4f& getCharBackground(Vector2i position) const;
331 
340  void setCharForeground(Vector2i position, const Color4f& color);
341 
350  const Color4f& getCharForeground(Vector2i position) const;
351 
360  void setChar(Vector2i position, char16_t c);
361 
370  char16_t getChar(Vector2i position) const;
371 
384  void putChar(Vector2i position, char16_t c, ConsoleEffect effect = ConsoleEffect::Default);
385 
399  void putChar(Vector2i position, char16_t c, const Color4f& foreground, const Color4f& background);
400 
418  if (!effect.isDefault()) {
419  m_effect = effect;
420  }
421  }
422 
431  return m_effect;
432  }
433 
442  m_alignment = alignment;
443  }
444 
453  return m_alignment;
454  }
455 
470  void print(Vector2i position, const char *fmt, ...) GF_FORMAT(3, 4);
471 
488  void print(Vector2i position, ConsoleEffect effect, ConsoleAlignment alignment, const char *fmt, ...) GF_FORMAT(5, 6);
489 
502  int printRect(const RectI& rect, const char *fmt, ...) GF_FORMAT(3, 4);
503 
518  int printRect(const RectI& rect, ConsoleEffect effect, ConsoleAlignment alignment, const char *fmt, ...) GF_FORMAT(5, 6);
519 
532  int getHeight(const RectI& rect, const char *fmt, ...) GF_FORMAT(3, 4);
533 
552  void setColorControl(ConsoleColorControl ctrl, const Color4f& foreground, const Color4f& background);
553 
574  void drawRectangle(const RectI& rect, PrintAction action = PrintAction::None, ConsoleEffect effect = ConsoleEffect::Default);
575 
585  void drawHorizontalLine(Vector2i left, int width, ConsoleEffect effect = ConsoleEffect::Default);
586 
596  void drawVerticalLine(Vector2i top, int height, ConsoleEffect effect = ConsoleEffect::Default);
597 
610  void drawFrame(const RectI& rect, PrintAction action = PrintAction::None, ConsoleEffect effect = ConsoleEffect::Default, const char *title = nullptr, ...) GF_FORMAT(5, 6);
611 
627  void setFade(float amount, const Color4f& color) {
628  m_fadingAmount = amount;
629  m_fadingColor = color;
630  }
631 
637  float getFadingAmount() const {
638  return m_fadingAmount;
639  }
640 
646  const Color4f& getFadingColor() const {
647  return m_fadingColor;
648  }
649 
663  void blit(const RectI& src, Console& con, Vector2i dst, float foregroundAlpha = 1.0f, float backgroundAlpha = 1.0f) const;
664 
665  virtual void draw(RenderTarget& target, const RenderStates& states) override;
666 
667  private:
668  Color4f computeColor(ConsoleEffect effect, const Color4f& existing, const Color4f& current);
669 
670  int putWord(Vector2i position, ConsoleEffect effect, StringRef message, const Color4f& foreground, const Color4f& background);
671 
672  enum class PrintOption {
673  Split = 0x01,
674  CountOnly = 0x02,
675  };
676 
678 
679  int printInternal(const RectI& rect, ConsoleEffect effect, ConsoleAlignment alignment, const std::string& message, PrintOptionFlags flags = None);
680 
681  private:
682  struct Cell {
683  Color4f fg;
684  Color4f bg;
685  char16_t c;
686  };
687 
688  const ConsoleFont *m_font;
689  Array2D<Cell, int> m_data;
690  Color4f m_background;
691  Color4f m_foreground;
692 
693  ConsoleEffect m_effect;
694  ConsoleAlignment m_alignment;
695 
696  struct ColorControl {
697  Color4f fg;
698  Color4f bg;
699  };
700 
701  static constexpr char ColorControlCount = 5;
702  ColorControl m_controls[ColorControlCount];
703 
704  float m_fadingAmount;
705  Color4f m_fadingColor;
706  };
707 
708 #ifndef DOXYGEN_SHOULD_SKIP_THIS
709 }
710 #endif
711 }
712 
713 #endif // GF_CONSOLE_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
Definition: Console.h:144
PrintAction
An action when printing.
Definition: Console.h:229
const Color4f & getDefaultForeground() const
Get the default foreground color.
Definition: Console.h:297
void setDefaultForeground(const Color4f &color)
Set the default foreground color.
Definition: Console.h:287
Kind
The kind of console effect.
Definition: Console.h:132
void setDefaultConsoleEffect(ConsoleEffect effect)
Set the default console effect.
Definition: Console.h:417
The button is active once.
A virtual console.
Definition: Console.h:224
constexpr ConsoleColorControl ConsoleColorControlStop
The constant for color control stop.
Definition: Console.h:110
No alignement.
Definition: Console.h:135
constexpr ConsoleEffect(Kind kind)
Constructor with a kind only.
Definition: Console.h:157
Definition: Console.h:143
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
Bitfield relying on an enumeration.
Definition: Flags.h:68
const Color4f & getFadingColor() const
Get the fading color.
Definition: Console.h:646
constexpr ConsoleColorControl ConsoleColorControl3
The constant for color control #3.
Definition: Console.h:86
constexpr ConsoleEffect(Kind kind, float alpha)
Constructor with a kind and .
Definition: Console.h:173
constexpr ConsoleColorControl ConsoleColorControl5
The constant for color control #5.
Definition: Console.h:102
constexpr ConsoleColorControl ConsoleColorControl2
The constant for color control #2.
Definition: Console.h:78
Definition: Console.h:140
Use the default console effect.
Definition: Console.h:146
constexpr float getAlpha() const noexcept
Get the alpha value.
Definition: Console.h:197
Definition: Console.h:139
constexpr bool isDefault() const noexcept
Check if the effect if Console::Default.
Definition: Console.h:190
void setFade(float amount, const Color4f &color)
Set the fading parameters.
Definition: Console.h:627
Definition: Console.h:145
Definition: Console.h:137
ConsoleAlignment
The alignment of the text in the console.
Definition: Console.h:50
Definition: Console.h:138
Do not change the background color.
Definition: Console.h:133
The namespace for gf classes.
Definition: Action.h:35
ConsoleEffect getDefaultConsoleEffect() const
Get the default console effect.
Definition: Console.h:430
constexpr ConsoleColorControl ConsoleColorControl1
The constant for color control #1.
Definition: Console.h:70
constexpr ConsoleColorControl ConsoleColorControl4
The constant for color control #4.
Definition: Console.h:94
Definition: Console.h:141
void setDefaultBackground(const Color4f &color)
Set the default background color.
Definition: Console.h:267
float getFadingAmount() const
Get the fading amount.
Definition: Console.h:637
Definition: Console.h:136
A console font.
Definition: ConsoleFont.h:110
Definition: Console.h:142
char ConsoleColorControl
A type for color controls in a console.
Definition: Console.h:62
A constant reference to a string and its size.
Definition: StringRef.h:41
Definition: Console.h:134
Left alignement.
A console effect on the background color.
Definition: Console.h:126
ConsoleAlignment getDefaultAlignment() const
Get the default alignment.
Definition: Console.h:452
Right alignement.
constexpr Kind getKind() const noexcept
Get the kind of effect.
Definition: Console.h:183
void setDefaultAlignment(ConsoleAlignment alignment)
Set the default alignment.
Definition: Console.h:441
int getHeight() const
Get the height of the console.
Definition: Console.h:252
const Color4f & getDefaultBackground() const
Get the default background color.
Definition: Console.h:277
Centered alignment.
int getWidth() const
Get the width of the console.
Definition: Console.h:245