-
Notifications
You must be signed in to change notification settings - Fork 0
/
Console.h
175 lines (126 loc) · 3.78 KB
/
Console.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef CONSOLE_H
#define CONSOLE_H
#include <string>
#include <string_view>
#include "TextInput.h"
#include "ConsoleHistory.h"
#include "CommandArgs.h"
#include "GAPI/BufferObject.h"
#include "GAPI/Vertex.h"
#include "Misc.h"
#include "thrid_party/ggformat/ggformat.h"
#include "BMfont.h"
class Console final
{
private:
static constexpr char COLOR_DEFAULT = 0;
static constexpr char COLOR_RED = 1;
static constexpr char COLOR_GREEN = 2;
static constexpr char COLOR_BLUE = 3;
static constexpr char COLOR_GOLD = 4;
static constexpr unsigned int FORMAT_BUFF_SIZE = 2048;
static constexpr unsigned int BUFF_SIZE = 0x80000;
static constexpr unsigned int MAX_LINES_DISPLAY = 32;
unsigned int LINE_WIDTH = 0;
unsigned int TOTAL_LINES = 0;
TextInput textInput;
ConsoleHistory history;
std::basic_string<uint16_t> text;
bool opened = false;
unsigned int currentLine = 0;
unsigned int x = 0;
unsigned int display = 0;
std::uint32_t consoleTexIndexToDraw = 0;
std::uint32_t textInputIndexToDraw = 0;
BMfont font;
VertexArrayObject vao;
Buffer indexBuffer;
Buffer vertexBuffer;
std::uint32_t vertsCount = 0;
std::uint32_t textInputStringBaseVertex = 0;
std::uint32_t backgroundBaseVertex = 0;
std::uint32_t cursorBaseVertex = 0;
int cursorBlinkTime = 0;
bool updateTCoord = true;
void handleKeyEvent( const SDL_Event &ev );
void scrollUp();
void scrollDown();
void home();
void end();
void lineFeed();
void updateTextInputVertexData( Vertex2d *verts, vec2f pos );
void updateConsoleTextVertexData( Vertex2d *verts, vec2f pos );
void updateCursorVertexData( Vertex2d *verts, vec2f pos );
std::pair<int, int> getBeginEndRowsIndex() const;
void printAutoComplete();
public:
Console();
~Console() = default;
void Init();
void Open();
void Close();
bool IsOpened() const;
void HandleEvent( const SDL_Event &ev );
void Update( const unsigned int dt );
void SetFont( const BMfont &newFont );
const BMfont &GetFont() const;
template <typename... Args>
void Printf( std::string format, const Args &... args );
template <typename... Args>
void WPrintf( std::string format, const Args &... args );
template <typename... Args>
void EPrintf( std::string format, const Args &... args );
template <typename... Args>
void IPrintf( std::string format, const Args &... args );
void Print( std::string_view str );
void Draw();
private:
friend void Clear_f( const CommandArgs &args );
friend void Dump_f( const CommandArgs &args );
};
template <typename... Args>
inline void Console::EPrintf( std::string format, const Args &... args )
{
format.insert( 0, "ERROR: " );
format.insert( 0, 1, COLOR_RED );
Printf( format, args... );
}
template <typename... Args>
inline void Console::WPrintf( std::string format, const Args &... args )
{
format.insert( 0, "WARNING: " );
format.insert( 0, 1, COLOR_GOLD );
Printf( format, args... );
}
template <typename... Args>
inline void Console::IPrintf( std::string format, const Args &... args )
{
format.insert( 0, "INFO: " );
format.insert( 0, 1, COLOR_BLUE );
Printf( format, args... );
}
template <typename... Args>
inline void Console::Printf( std::string format, const Args &... args )
{
char buff[FORMAT_BUFF_SIZE];
auto len = ggformat( buff, FORMAT_BUFF_SIZE, format.c_str(), args... );
Print( { buff, len } );
}
inline void Console::Close()
{
opened = false;
}
inline bool Console::IsOpened() const
{
return opened;
}
inline void Console::SetFont( const BMfont &newFont )
{
font = newFont;
}
inline const BMfont &Console::GetFont() const
{
return font;
}
inline Console con;
#endif // CONSOLE_H