Pyrogenesis  trunk
CConsole.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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/file/vfs/vfs_path.h"
26 #include "lib/input.h"
27 
28 #include <deque>
29 #include <memory>
30 #include <mutex>
31 #include <string>
32 
33 class CCanvas2D;
34 class CTextRenderer;
35 
36 /**
37  * In-game console.
38  *
39  * Thread-safety:
40  * - Expected to be constructed/destructed in the main thread.
41  * - InsertMessage may be called from any thread while the object is alive.
42  */
43 class CConsole
44 {
46 
47 public:
48  CConsole();
49  ~CConsole();
50 
51  void Init();
52 
53  void UpdateScreenSize(int w, int h);
54 
55  void ToggleVisible();
56  void SetVisible(bool visible);
57 
58  /**
59  * @param deltaRealTime Elapsed real time since the last frame.
60  */
61  void Update(const float deltaRealTime);
62 
63  void Render(CCanvas2D& canvas);
64 
65  void InsertChar(const int szChar, const wchar_t cooked);
66 
67  void InsertMessage(const std::string& message);
68 
69  void SetBuffer(const wchar_t* szMessage);
70 
71  // Only returns a pointer to the buffer; copy out of here if you want to keep it.
72  const wchar_t* GetBuffer();
73  void FlushBuffer();
74 
75  bool IsActive() const { return m_Visible; }
76 
77 private:
78  // Lock for all state modified by InsertMessage
79  std::mutex m_Mutex;
80 
83  int m_FontOffset; // distance to move up before drawing
85 
86  float m_X;
87  float m_Y;
88  float m_Height;
89  float m_Width;
90 
91  // "position" in show/hide animation, how visible the console is (0..1).
92  // allows implementing other animations than sliding, e.g. fading in/out.
94 
95  std::deque<std::wstring> m_MsgHistory; // protected by m_Mutex
96  std::deque<std::wstring> m_BufHistory;
97 
99 
100  std::unique_ptr<wchar_t[]> m_Buffer;
103 
106 
107  bool m_Visible; // console is to be drawn
108  bool m_Toggle; // show/hide animation is currently active
109  double m_PrevTime; // the previous time the cursor draw state changed (used for blinking cursor)
110  bool m_CursorVisState; // if the cursor should be drawn or not
111  bool m_QuitHotkeyWasShown; // show console.toggle hotkey values at first time
112  double m_CursorBlinkRate; // cursor blink rate in seconds, if greater than 0.0
113 
114  void DrawWindow(CCanvas2D& canvas);
115  void DrawHistory(CTextRenderer& textRenderer);
116  void DrawBuffer(CTextRenderer& textRenderer);
117  void DrawCursor(CTextRenderer& textRenderer);
118 
119  // Is end of Buffer?
120  bool IsEOB() const;
121  // Is beginning of Buffer?
122  bool IsBOB() const;
123  bool IsFull() const;
124  bool IsEmpty() const;
125 
126  void ProcessBuffer(const wchar_t* szLine);
127 
128  void LoadHistory();
129  void SaveHistory();
130  void ShowQuitHotkeys();
131 };
132 
133 extern CConsole* g_Console;
134 
135 extern InReaction conInputHandler(const SDL_Event_* ev);
136 
137 #endif // INCLUDED_CCONSOLE
size_t m_CharsPerPage
Definition: CConsole.h:84
void FlushBuffer()
Definition: CConsole.cpp:160
std::unique_ptr< wchar_t[]> m_Buffer
Definition: CConsole.h:100
int m_BufferPos
Definition: CConsole.h:101
bool m_Toggle
Definition: CConsole.h:108
void DrawHistory(CTextRenderer &textRenderer)
Definition: CConsole.cpp:243
void DrawCursor(CTextRenderer &textRenderer)
Definition: CConsole.cpp:298
void ShowQuitHotkeys()
Definition: CConsole.cpp:117
Definition: libsdl.h:52
float m_X
Definition: CConsole.h:86
Definition: TextRenderer.h:33
const wchar_t * GetBuffer()
Definition: CConsole.cpp:557
std::deque< std::wstring > m_MsgHistory
Definition: CConsole.h:95
void ToggleVisible()
Definition: CConsole.cpp:133
std::mutex m_Mutex
Definition: CConsole.h:79
CConsole()
Definition: CConsole.cpp:60
int m_MsgHistPos
Definition: CConsole.h:98
int m_FontHeight
Definition: CConsole.h:81
Definition: path.h:79
NONCOPYABLE(CConsole)
double m_CursorBlinkRate
Definition: CConsole.h:112
InReaction
Definition: input.h:34
Definition: Canvas2D.h:35
VfsPath m_HistoryFile
Definition: CConsole.h:104
float m_Height
Definition: CConsole.h:88
void Render(CCanvas2D &canvas)
Definition: CConsole.cpp:194
void InsertMessage(const std::string &message)
Definition: CConsole.cpp:515
float m_Y
Definition: CConsole.h:87
int m_BufferLength
Definition: CConsole.h:102
bool IsFull() const
Definition: CConsole.cpp:339
std::deque< std::wstring > m_BufHistory
Definition: CConsole.h:96
double m_PrevTime
Definition: CConsole.h:109
void ProcessBuffer(const wchar_t *szLine)
Definition: CConsole.cpp:575
void Update(const float deltaRealTime)
Definition: CConsole.cpp:167
bool IsEmpty() const
Definition: CConsole.cpp:344
void LoadHistory()
Definition: CConsole.cpp:596
int m_MaxHistoryLines
Definition: CConsole.h:105
bool IsActive() const
Definition: CConsole.h:75
void SaveHistory()
Definition: CConsole.cpp:627
void SetBuffer(const wchar_t *szMessage)
Definition: CConsole.cpp:563
bool IsBOB() const
Definition: CConsole.cpp:334
float m_Width
Definition: CConsole.h:89
CConsole * g_Console
Definition: CConsole.cpp:58
In-game console.
Definition: CConsole.h:43
float m_VisibleFrac
Definition: CConsole.h:93
void DrawBuffer(CTextRenderer &textRenderer)
Definition: CConsole.cpp:271
InReaction conInputHandler(const SDL_Event_ *ev)
Definition: CConsole.cpp:667
bool m_QuitHotkeyWasShown
Definition: CConsole.h:111
bool m_Visible
Definition: CConsole.h:107
void Init()
Definition: CConsole.cpp:85
void SetVisible(bool visible)
Definition: CConsole.cpp:148
int m_FontOffset
Definition: CConsole.h:83
void UpdateScreenSize(int w, int h)
Definition: CConsole.cpp:108
bool IsEOB() const
Definition: CConsole.cpp:329
void DrawWindow(CCanvas2D &canvas)
Definition: CConsole.cpp:215
void InsertChar(const int szChar, const wchar_t cooked)
Definition: CConsole.cpp:350
int m_FontWidth
Definition: CConsole.h:82
bool m_CursorVisState
Definition: CConsole.h:110