GigaPasjans
Loading...
Searching...
No Matches
Terminal.h
1// Plik utworzony przez Piotra ChudziƄskiego w dniu 11.04.2025
2#pragma once
3
4#include <stack>
5
6#include <foundation/Math/Box.h>
7#include <foundation/String/String.h>
8#include <foundation/math/Int2.h>
9#include <stack>
10#include <vector>
11#include "Color.h"
12
13namespace Andromenda
14{
19 struct TerminalStyle
20 {
26
32
33 TerminalStyle() = default;
34
35 TerminalStyle(Color foreground, Color background) : Foreground(foreground), Background(background) {}
36 };
37
42 struct FramebufferEntry
43 {
49
55
61
67
68 FramebufferEntry() : Position(Int2::Zero), Style(TerminalStyle()), Character('\0'), UnicodeCharacter(L'\0') {}
69
70 FramebufferEntry(Int2 position, TerminalStyle style, char character) :
71 Style(style), Character(character), UnicodeCharacter(L'\0'), Position(position)
72 {
73 }
74
75 FramebufferEntry(Int2 position, TerminalStyle style, wchar_t character) :
76 Style(style), Character('\0'), UnicodeCharacter(character), Position(position)
77 {
78 }
79 };
80
85 class Framebuffer
86 {
87 private:
88 friend class TerminalImpl;
89
94 Int2 m_Size;
95
100 std::vector<FramebufferEntry> m_Entries;
101
106 FramebufferEntry &get(const Int2 &position);
107
108 public:
109 Framebuffer() = default;
110
116 void Resize(const Int2 &size);
117
122 void Clear();
123
128 void Put(Int2 position, TerminalStyle style, char character);
129
134 void Put(Int2 position, TerminalStyle style, wchar_t character);
135
140 Int2 GetSize() const;
141 };
142
147 class StyleStack
148 {
149 private:
154 std::stack<TerminalStyle> m_Stack;
155
156 public:
157 StyleStack() = default;
158
164 void Push(const TerminalStyle &style);
165
170 void Pop();
171
178 };
179
184 class ScopeStyle
185 {
186 private:
191 TerminalStyle m_Style;
192
197 StyleStack &m_StyleStack;
198
199 public:
200 ScopeStyle(const TerminalStyle &style, StyleStack &stack) : m_Style(style), m_StyleStack(stack)
201 {
202 m_StyleStack.Push(m_Style);
203 }
204
205 ~ScopeStyle() { m_StyleStack.Pop(); }
206 };
207
213 {
218 int32 Width;
219
224 int32 Height;
225 };
226
232 {
233 private:
238 inline static Int2 m_Size;
239
240 public:
246 static Int2 GetSize();
247
252 static void Update(const Int2 &size);
253
258 static void Draw(const Int2 &size, Framebuffer &framebuffer);
259
264 static int32 Getch();
265
270 static void ForceSizeUpdate();
271
277 static void WriteDebug(const String &message);
278
283 static void DestroyBuffers();
284 };
285} // namespace Andromenda
Represents a set of pixels in array with size of terminal.
Definition Terminal.h:86
void Put(Int2 position, TerminalStyle style, char character)
Puts UTF-8 character to given size and applies given style.
Definition TerminalBase.cpp:20
void Clear()
Sets all pixels to black foreground, black background and empty character.
Definition TerminalBase.cpp:11
Int2 GetSize() const
Returns size of framebuffer.
Definition TerminalBase.cpp:41
void Resize(const Int2 &size)
Sets size to given size and resizes array to m_Size.X * m_Size.Y.
Definition TerminalBase.cpp:13
Stack of terminal styles.
Definition Terminal.h:148
void Pop()
Pops style at the top.
Definition TerminalBase.cpp:45
void Push(const TerminalStyle &style)
Pushes given style to the top.
Definition TerminalBase.cpp:43
TerminalStyle & Peek()
Returns style at the top of the stack.
Definition TerminalBase.cpp:47
Platform-specific terminal functions implemented per platform.
Definition Terminal.h:232
static Int2 GetSize()
Returns size of the terminal. X is total terminal row, y is columns.
Definition TerminalImpl.cpp:53
static int32 Getch()
Returns pressed key. Blocks until any key is pressed.
Definition TerminalImpl.cpp:83
static void DestroyBuffers()
Destroys platform framebuffer.
Definition TerminalImpl.cpp:107
static void ForceSizeUpdate()
Updates size of the terminal.
Definition TerminalImpl.cpp:96
static void Draw(const Int2 &size, Framebuffer &framebuffer)
Puts pixel from framebuffer to the terminal.
Definition TerminalImpl.cpp:55
static void Update(const Int2 &size)
Resizes platform framebuffer to given size.
Definition TerminalImpl.cpp:81
static void WriteDebug(const String &message)
Writes message to the attached debugger.
Definition TerminalImpl.cpp:105
Represents a mutable sequence of characters, providing various member functions for string manipulati...
Definition String.h:16
Represents framebuffer single glyph.
Definition Terminal.h:43
TerminalStyle Style
Glyph terminal style.
Definition Terminal.h:48
Int2 Position
Position of glyph (X is row, Y is column)
Definition Terminal.h:66
wchar_t UnicodeCharacter
Glyph encoded in UTF-16. Not used when UTF-8 character is used.
Definition Terminal.h:60
char Character
Glyph encoded in UTF-8. Not used when UTF-16 character is used.
Definition Terminal.h:54
Represents a major terminal informations.
Definition Terminal.h:213
int32 Height
Columns of the terminal.
Definition Terminal.h:224
int32 Width
Rows of the terminal.
Definition Terminal.h:218
Represents terminal text style, described by background and foreground color.
Definition Terminal.h:20
Color Foreground
Text foreground.
Definition Terminal.h:25
Color Background
Text background.
Definition Terminal.h:31
Represents a two component vector. Components are 32-bit and they are signed.
Definition Int2.h:11