Fix the removal of implicit conversions in libfmt 10 by using explicit casts.
[0ad.git] / source / ps / CConsole.h
blob37e3e63d1b711965070dbf0aeb0c2d1d1347ab54
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
19 * Implements the in-game console with scripting support.
22 #ifndef INCLUDED_CCONSOLE
23 #define INCLUDED_CCONSOLE
25 #include "lib/file/vfs/vfs_path.h"
26 #include "lib/input.h"
28 #include <deque>
29 #include <memory>
30 #include <mutex>
31 #include <string>
33 class CCanvas2D;
34 class CTextRenderer;
36 /**
37 * In-game console.
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.
43 class CConsole
45 NONCOPYABLE(CConsole);
47 public:
48 CConsole();
49 ~CConsole();
51 void Init();
53 void UpdateScreenSize(int w, int h);
55 void ToggleVisible();
56 void SetVisible(bool visible);
58 /**
59 * @param deltaRealTime Elapsed real time since the last frame.
61 void Update(const float deltaRealTime);
63 void Render(CCanvas2D& canvas);
65 void InsertChar(const int szChar, const wchar_t cooked);
67 void InsertMessage(const std::string& message);
69 void SetBuffer(const wchar_t* szMessage);
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();
75 bool IsActive() const { return m_Visible; }
77 private:
78 // Lock for all state modified by InsertMessage
79 std::mutex m_Mutex;
81 int m_FontHeight;
82 int m_FontWidth;
83 int m_FontOffset; // distance to move up before drawing
84 size_t m_CharsPerPage;
86 float m_X;
87 float m_Y;
88 float m_Height;
89 float m_Width;
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.
93 float m_VisibleFrac;
95 std::deque<std::wstring> m_MsgHistory; // protected by m_Mutex
96 std::deque<std::wstring> m_BufHistory;
98 int m_MsgHistPos;
100 std::unique_ptr<wchar_t[]> m_Buffer;
101 int m_BufferPos;
102 int m_BufferLength;
104 VfsPath m_HistoryFile;
105 int m_MaxHistoryLines;
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
114 void DrawWindow(CCanvas2D& canvas);
115 void DrawHistory(CTextRenderer& textRenderer);
116 void DrawBuffer(CTextRenderer& textRenderer);
117 void DrawCursor(CTextRenderer& textRenderer);
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;
126 void ProcessBuffer(const wchar_t* szLine);
128 void LoadHistory();
129 void SaveHistory();
130 void ShowQuitHotkeys();
133 extern CConsole* g_Console;
135 extern InReaction conInputHandler(const SDL_Event_* ev);
137 #endif // INCLUDED_CCONSOLE