Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Console.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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 "Color.h"
31#include "ConsoleFont.h"
32#include "Flags.h"
33#include "GraphicsApi.h"
34#include "Path.h"
35#include "Portability.h"
36#include "Texture.h"
37#include "Transformable.h"
38#include "Vector.h"
39
40namespace gf {
41#ifndef DOXYGEN_SHOULD_SKIP_THIS
42inline namespace v1 {
43#endif
44
45
52 enum class ConsoleAlignment {
53 Left,
54 Center,
55 Right,
56 };
57
64 using ConsoleColorControl = char;
65
73
81
89
97
105
113
127 class GF_GRAPHICS_API ConsoleEffect {
128 public:
129
133 enum Kind : uint32_t {
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 float getAlpha() const noexcept {
191 return m_alpha;
192 }
193
194 private:
195 Kind m_kind;
196 float m_alpha;
197 };
198
204 struct GF_GRAPHICS_API ConsoleStyle {
205 Color4f foreground = Color::White;
206 Color4f background = Color::Black;
209 };
210
221 class GF_GRAPHICS_API Console : public Transformable {
222 public:
226 enum class PrintAction {
227 None,
228 Clear,
229 };
230
237 Console(const ConsoleFont& font, Vector2i size);
238
242 int getWidth() const {
243 return m_data.getSize().width;
244 }
245
249 int getHeight() const {
250 return m_data.getSize().height;
251 }
252
266 void clear(const ConsoleStyle& style = ConsoleStyle());
267
278
288
297 void setCharForeground(Vector2i position, Color4f color);
298
308
317 void setChar(Vector2i position, char16_t c);
318
327 char16_t getChar(Vector2i position) const;
328
341 void putChar(Vector2i position, char16_t c, const ConsoleStyle& style = ConsoleStyle());
342
356 void putChar(Vector2i position, char16_t c, Color4f foreground, Color4f background);
357
382 void print(Vector2i position, const ConsoleStyle& style, const char *fmt, ...) GF_FORMAT(4, 5);
383
397 int printRect(const RectI& rect, const ConsoleStyle& style, const char *fmt, ...) GF_FORMAT(4, 5);
398
411 int getHeight(const RectI& rect, const char *fmt, ...) GF_FORMAT(3, 4);
412
426 void setColorControl(ConsoleColorControl ctrl, Color4f foreground, Color4f background);
427
448 void drawRectangle(const RectI& rect, const ConsoleStyle& style, PrintAction action = PrintAction::None);
449
459 void drawHorizontalLine(Vector2i left, int width, const ConsoleStyle& style);
460
470 void drawVerticalLine(Vector2i top, int height, const ConsoleStyle& style);
471
484 void drawFrame(const RectI& rect, const ConsoleStyle& style, PrintAction action = PrintAction::None, const char *title = nullptr, ...) GF_FORMAT(5, 6);
485
501 void setFade(float amount, Color4f color) {
502 m_fadingAmount = amount;
503 m_fadingColor = color;
504 }
505
511 float getFadingAmount() const {
512 return m_fadingAmount;
513 }
514
521 return m_fadingColor;
522 }
523
537 void blit(const RectI& src, Console& con, Vector2i dst, float foregroundAlpha = 1.0f, float backgroundAlpha = 1.0f) const;
538
539 virtual void draw(RenderTarget& target, const RenderStates& states) override;
540
541 private:
542 Color4f computeColor(ConsoleEffect effect, Color4f existing, Color4f current);
543
544 int putWord(Vector2i position, std::string_view message, const ConsoleStyle& style);
545
546 enum class PrintOption {
547 Split = 0x01,
548 CountOnly = 0x02,
549 };
550
551 int printInternal(const RectI& rect, const std::string& message, const ConsoleStyle& style, Flags<PrintOption> flags = None);
552
553 private:
554 struct Cell {
555 Color4f fg;
556 Color4f bg;
557 char16_t c;
558 };
559
560 const ConsoleFont *m_font;
561 Array2D<Cell, int> m_data;
562
563 struct ColorControl {
564 Color4f fg;
565 Color4f bg;
566 };
567
568 static constexpr char ColorControlCount = 5;
569 ColorControl m_controls[ColorControlCount];
570
571 float m_fadingAmount;
572 Color4f m_fadingColor;
573 };
574
575#ifndef DOXYGEN_SHOULD_SKIP_THIS
576}
577#endif
578}
579
580#endif // GF_CONSOLE_H
A console effect on the background color.
Definition: Console.h:127
constexpr Kind getKind() const noexcept
Get the kind of effect.
Definition: Console.h:183
constexpr float getAlpha() const noexcept
Get the alpha value.
Definition: Console.h:190
constexpr ConsoleEffect(Kind kind, float alpha)
Constructor with a kind and .
Definition: Console.h:173
Kind
The kind of console effect.
Definition: Console.h:133
@ Darken
Definition: Console.h:138
@ None
Do not change the background color.
Definition: Console.h:134
@ Lighten
Definition: Console.h:137
@ Overlay
Definition: Console.h:145
@ Set
Definition: Console.h:135
@ Burn
Definition: Console.h:144
@ ColorBurn
Definition: Console.h:141
@ Screen
Definition: Console.h:139
@ Alpha
Definition: Console.h:146
@ Multiply
Definition: Console.h:136
@ ColorDodge
Definition: Console.h:140
@ Add
Definition: Console.h:142
@ AddAlpha
Definition: Console.h:143
constexpr ConsoleEffect(Kind kind)
Constructor with a kind only.
Definition: Console.h:157
A console font.
Definition: ConsoleFont.h:111
A virtual console.
Definition: Console.h:221
void clear(const ConsoleStyle &style=ConsoleStyle())
Clear the console.
char16_t getChar(Vector2i position) const
Get a character.
Color4f getCharBackground(Vector2i position) const
Get the character background color.
int getHeight() const
Get the height of the console.
Definition: Console.h:249
Console(const ConsoleFont &font, Vector2i size)
Constructor.
int getWidth() const
Get the width of the console.
Definition: Console.h:242
Color4f getFadingColor() const
Get the fading color.
Definition: Console.h:520
PrintAction
An action when printing.
Definition: Console.h:226
virtual void draw(RenderTarget &target, const RenderStates &states) override
Draw the object to a render target.
void setCharBackground(Vector2i position, Color4f color, ConsoleEffect effect=ConsoleEffect::Set)
Set the character background color.
void putChar(Vector2i position, char16_t c, const ConsoleStyle &style=ConsoleStyle())
Modify a cell in the console.
Color4f getCharForeground(Vector2i position) const
Get the character foreground color.
void setCharForeground(Vector2i position, Color4f color)
Set the character foreground color.
void setChar(Vector2i position, char16_t c)
Set a character.
void print(Vector2i position, const ConsoleStyle &style, const char *fmt,...)
Print a formatted string.
float getFadingAmount() const
Get the fading amount.
Definition: Console.h:511
void blit(const RectI &src, Console &con, Vector2i dst, float foregroundAlpha=1.0f, float backgroundAlpha=1.0f) const
Blit a console on another console.
void putChar(Vector2i position, char16_t c, Color4f foreground, Color4f background)
Modify a cell in the console.
Bitfield relying on an enumeration.
Definition: Flags.h:48
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
Color4< float > Color4f
A float color vector with 4 components.
Definition: Vector.h:1287
constexpr NoneType None
Constant to represent "none".
Definition: Types.h:45
constexpr ConsoleColorControl ConsoleColorControl5
The constant for color control #5.
Definition: Console.h:104
char ConsoleColorControl
A type for color controls in a console.
Definition: Console.h:64
ConsoleAlignment
The alignment of the text in the console.
Definition: Console.h:52
constexpr ConsoleColorControl ConsoleColorControl1
The constant for color control #1.
Definition: Console.h:72
constexpr ConsoleColorControl ConsoleColorControl4
The constant for color control #4.
Definition: Console.h:96
constexpr ConsoleColorControl ConsoleColorControl3
The constant for color control #3.
Definition: Console.h:88
constexpr ConsoleColorControl ConsoleColorControl2
The constant for color control #2.
Definition: Console.h:80
constexpr ConsoleColorControl ConsoleColorControlStop
The constant for color control stop.
Definition: Console.h:112
@ Left
Left alignment.
@ Center
Centered alignment.
@ Right
Right alignement.
@ Left
Left alignement.
The namespace for gf classes.
static constexpr Color4< T > White
White predefined color.
Definition: Color.h:168
static constexpr Color4< T > Black
Black predefined color.
Definition: Color.h:163
A console style.
Definition: Console.h:204
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A 4D vector.
Definition: Vector.h:852