Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CConsole.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18/*
19 * Implements the in-game console with scripting support.
20 */
21
22#ifndef INCLUDED_CCONSOLE
23#define INCLUDED_CCONSOLE
24
25#include "lib/code_annotation.h"
27#include "ps/Input.h"
28
29#include <cstddef>
30#include <deque>
31#include <memory>
32#include <mutex>
33#include <string>
34
35class CCanvas2D;
36class CTextRenderer;
37
38/**
39 * In-game console.
40 *
41 * Thread-safety:
42 * - Expected to be constructed/destructed in the main thread.
43 * - InsertMessage may be called from any thread while the object is alive.
44 */
46{
48
49public:
50 CConsole();
52
53 void Init();
54
55 void UpdateScreenSize(int w, int h);
56
57 void SetVisible(bool visible);
58
59 /**
60 * @param deltaRealTime Elapsed real time since the last frame.
61 */
62 void Update(const float deltaRealTime);
63
64 void Render(CCanvas2D& canvas);
65
66 void InsertMessage(const std::string& message);
67
68 void SetBuffer(const wchar_t* szMessage);
69
70 void FlushBuffer();
71
72 bool IsActive() const { return m_Visible; }
73
74private:
75 // Lock for all state modified by InsertMessage
76 std::mutex m_Mutex;
77
80 float m_FontOffset; // distance to move up before drawing
82
83 float m_X;
84 float m_Y;
85 float m_Height;
86 float m_Width;
87
88 // "position" in show/hide animation, how visible the console is (0..1).
89 // allows implementing other animations than sliding, e.g. fading in/out.
91
92 std::deque<std::wstring> m_MsgHistory; // protected by m_Mutex
93 std::deque<std::wstring> m_BufHistory;
94
96
97 std::unique_ptr<wchar_t[]> m_Buffer;
100
104 std::string m_consoleFont;
105
106 bool m_Visible; // console is to be drawn
107 bool m_Toggle; // show/hide animation is currently active
108 double m_PrevTime; // the previous time the cursor draw state changed (used for blinking cursor)
109 bool m_CursorVisState; // if the cursor should be drawn or not
110 bool m_QuitHotkeyWasShown; // show console.toggle hotkey values at first time
111 double m_CursorBlinkRate; // cursor blink rate in seconds, if greater than 0.0
112
113 void InsertChar(const int szChar, const wchar_t cooked);
114
115 void DrawWindow(CCanvas2D& canvas);
116 void DrawHistory(CTextRenderer& textRenderer);
117 void DrawBuffer(CTextRenderer& textRenderer);
118 void DrawCursor(CTextRenderer& textRenderer);
119
120 // Is end of Buffer?
121 bool IsEOB() const;
122 // Is beginning of Buffer?
123 bool IsBOB() const;
124 bool IsFull() const;
125 bool IsEmpty() const;
126
127 void ProcessBuffer(const wchar_t* szLine);
128
129 void LoadHistory();
130 void SaveHistory();
131
133 {
135 Input::Reaction operator()(const SDL_Event& ev);
136 };
138};
139
140extern CConsole* g_Console;
141
142#endif // INCLUDED_CCONSOLE
CConsole * g_Console
Definition: CConsole.cpp:115
Definition: Canvas2D.h:36
In-game console.
Definition: CConsole.h:46
VfsPath m_HistoryFile
Definition: CConsole.h:101
void FlushBuffer()
Definition: CConsole.cpp:189
bool IsActive() const
Definition: CConsole.h:72
float m_Y
Definition: CConsole.h:84
float m_FontWidth
Definition: CConsole.h:79
int m_BufferPos
Definition: CConsole.h:98
bool IsEmpty() const
Definition: CConsole.cpp:373
void UpdateScreenSize(int w, int h)
Definition: CConsole.cpp:168
void DrawHistory(CTextRenderer &textRenderer)
Definition: CConsole.cpp:272
int m_MaxHistoryLines
Definition: CConsole.h:102
bool m_Toggle
Definition: CConsole.h:107
void Update(const float deltaRealTime)
Definition: CConsole.cpp:196
NONCOPYABLE(CConsole)
bool IsBOB() const
Definition: CConsole.cpp:363
void LoadHistory()
Definition: CConsole.cpp:622
void DrawBuffer(CTextRenderer &textRenderer)
Definition: CConsole.cpp:300
float m_FontHeight
Definition: CConsole.h:78
void SetVisible(bool visible)
Definition: CConsole.cpp:177
std::mutex m_Mutex
Definition: CConsole.h:76
std::deque< std::wstring > m_BufHistory
Definition: CConsole.h:93
std::deque< std::wstring > m_MsgHistory
Definition: CConsole.h:92
std::string m_consoleFont
Definition: CConsole.h:104
float m_Height
Definition: CConsole.h:85
void DrawCursor(CTextRenderer &textRenderer)
Definition: CConsole.cpp:327
float m_X
Definition: CConsole.h:83
bool IsEOB() const
Definition: CConsole.cpp:358
std::unique_ptr< wchar_t[]> m_Buffer
Definition: CConsole.h:97
void SaveHistory()
Definition: CConsole.cpp:653
void DrawWindow(CCanvas2D &canvas)
Definition: CConsole.cpp:244
CConsole()
Definition: CConsole.cpp:117
bool m_HistoryIgnoreDuplicates
Definition: CConsole.h:103
size_t m_CharsPerPage
Definition: CConsole.h:81
void InsertChar(const int szChar, const wchar_t cooked)
Definition: CConsole.cpp:379
bool IsFull() const
Definition: CConsole.cpp:368
void Init()
Definition: CConsole.cpp:143
bool m_Visible
Definition: CConsole.h:106
bool m_QuitHotkeyWasShown
Definition: CConsole.h:110
double m_PrevTime
Definition: CConsole.h:108
int m_MsgHistPos
Definition: CConsole.h:95
void InsertMessage(const std::string &message)
Definition: CConsole.cpp:544
void ProcessBuffer(const wchar_t *szLine)
Definition: CConsole.cpp:598
double m_CursorBlinkRate
Definition: CConsole.h:111
float m_Width
Definition: CConsole.h:86
float m_FontOffset
Definition: CConsole.h:80
bool m_CursorVisState
Definition: CConsole.h:109
Input::Handler< InputHandler > m_InputHandler
Definition: CConsole.h:137
void SetBuffer(const wchar_t *szMessage)
Definition: CConsole.cpp:586
void Render(CCanvas2D &canvas)
Definition: CConsole.cpp:223
float m_VisibleFrac
Definition: CConsole.h:90
int m_BufferLength
Definition: CConsole.h:99
Definition: TextRenderer.h:38
Usable type to register a handler to the associated Manager.
Definition: Input.h:180
Definition: path.h:84
Reaction
Definition: Input.h:39
Definition: CConsole.h:133
Input::Reaction operator()(const SDL_Event &ev)
Definition: CConsole.cpp:693
CConsole & console
Definition: CConsole.h:134