Pyrogenesis  trunk
CConsole.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 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 <stdarg.h>
26 #include <string>
27 #include <deque>
28 #include <map>
29 
31 #include "lib/file/vfs/vfs_path.h"
32 #include "lib/input.h"
33 #include "ps/CStr.h"
34 #include "ps/ThreadUtil.h"
35 
36 class CTextRenderer;
37 
38 #define CONSOLE_BUFFER_SIZE 1024 // for text being typed into the console
39 #define CONSOLE_MESSAGE_SIZE 1024 // for messages being printed into the console
40 
41 #define CONSOLE_FONT "mono-10"
42 
43 /**
44  * In-game console.
45  *
46  * Thread-safety:
47  * - Expected to be constructed/destructed in the main thread.
48  * - InsertMessage may be called from any thread while the object is alive.
49  */
50 class CConsole
51 {
53 
54 public:
55  CConsole();
56  ~CConsole();
57 
58  void SetSize(float X = 300, float Y = 0, float W = 800, float H = 600);
59  void UpdateScreenSize(int w, int h);
60 
61  void ToggleVisible();
62  void SetVisible(bool visible);
63 
64  void SetCursorBlinkRate(double rate);
65 
66  /**
67  * @param deltaRealTime Elapsed real time since the last frame.
68  */
69  void Update(const float deltaRealTime);
70 
71  void Render();
72 
73  void InsertChar(const int szChar, const wchar_t cooked);
74 
75  void InsertMessage(const std::string& message);
76 
77  void SetBuffer(const wchar_t* szMessage);
78 
79  void UseHistoryFile(const VfsPath& filename, int historysize);
80 
81  // Only returns a pointer to the buffer; copy out of here if you want to keep it.
82  const wchar_t* GetBuffer();
83  void FlushBuffer();
84 
85  bool IsActive() { return m_bVisible; }
86 
89  int m_iFontOffset; // distance to move up before drawing
91 
92 private:
93  // Lock for all state modified by InsertMessage
95 
96  float m_fX;
97  float m_fY;
98 
99  float m_fHeight;
100  float m_fWidth;
101 
102  // "position" in show/hide animation, how visible the console is (0..1).
103  // allows implementing other animations than sliding, e.g. fading in/out.
105 
106  std::deque<std::wstring> m_deqMsgHistory; // protected by m_Mutex
107  std::deque<std::wstring> m_deqBufHistory;
108 
110 
111  wchar_t* m_szBuffer;
114 
117 
118  bool m_bVisible; // console is to be drawn
119  bool m_bToggle; // show/hide animation is currently active
120  double m_prevTime; // the previous time the cursor draw state changed (used for blinking cursor)
121  bool m_bCursorVisState; // if the cursor should be drawn or not
122  double m_cursorBlinkRate; // cursor blink rate in seconds, if greater than 0.0
123 
124  void DrawWindow(CShaderProgramPtr& shader);
125  void DrawHistory(CTextRenderer& textRenderer);
126  void DrawBuffer(CTextRenderer& textRenderer);
127  void DrawCursor(CTextRenderer& textRenderer);
128 
129  bool IsEOB() { return (m_iBufferPos == m_iBufferLength); } // Is end of Buffer?
130  bool IsBOB() { return (m_iBufferPos == 0); } // Is beginning of Buffer?
131  bool IsFull() { return (m_iBufferLength == CONSOLE_BUFFER_SIZE); }
132  bool IsEmpty() { return (m_iBufferLength == 0); }
133 
134  void ProcessBuffer(const wchar_t* szLine);
135 
136  void LoadHistory();
137  void SaveHistory();
138 };
139 
140 extern CConsole* g_Console;
141 
142 extern InReaction conInputHandler(const SDL_Event_* ev);
143 
144 #endif
Definition: Decompose.h:22
std::deque< std::wstring > m_deqBufHistory
Definition: CConsole.h:107
bool IsBOB()
Definition: CConsole.h:130
int m_iFontOffset
Definition: CConsole.h:89
VfsPath m_sHistoryFile
Definition: CConsole.h:115
void FlushBuffer()
Definition: CConsole.cpp:122
float m_fY
Definition: CConsole.h:97
float m_fWidth
Definition: CConsole.h:100
int m_iFontWidth
Definition: CConsole.h:88
double m_prevTime
Definition: CConsole.h:120
void DrawHistory(CTextRenderer &textRenderer)
Definition: CConsole.cpp:235
void DrawCursor(CTextRenderer &textRenderer)
Definition: CConsole.cpp:285
float m_fVisibleFrac
Definition: CConsole.h:104
Definition: Decompose.h:22
int m_iFontHeight
Definition: CConsole.h:87
bool IsActive()
Definition: CConsole.h:85
A non-recursive mutual exclusion lock.
Definition: ThreadUtil.h:45
Definition: libsdl.h:51
Definition: TextRenderer.h:28
const wchar_t * GetBuffer()
Definition: CConsole.cpp:519
size_t m_charsPerPage
Definition: CConsole.h:90
void SetCursorBlinkRate(double rate)
Definition: CConsole.cpp:117
wchar_t * m_szBuffer
Definition: CConsole.h:111
void SetSize(float X=300, float Y=0, float W=800, float H=600)
Definition: CConsole.cpp:78
void ToggleVisible()
Definition: CConsole.cpp:93
CConsole()
Definition: CConsole.cpp:51
Definition: path.h:77
void UseHistoryFile(const VfsPath &filename, int historysize)
Definition: CConsole.cpp:537
NONCOPYABLE(CConsole)
InReaction
Definition: input.h:34
int m_iBufferPos
Definition: CConsole.h:112
void InsertMessage(const std::string &message)
Definition: CConsole.cpp:478
bool m_bCursorVisState
Definition: CConsole.h:121
void Render()
Definition: CConsole.cpp:158
int m_iMsgHistPos
Definition: CConsole.h:109
~CConsole()
Definition: CConsole.cpp:72
std::deque< std::wstring > m_deqMsgHistory
Definition: CConsole.h:106
void DrawWindow(CShaderProgramPtr &shader)
Definition: CConsole.cpp:199
void ProcessBuffer(const wchar_t *szLine)
Definition: CConsole.cpp:545
void Update(const float deltaRealTime)
Definition: CConsole.cpp:130
void LoadHistory()
Definition: CConsole.cpp:568
bool IsEOB()
Definition: CConsole.h:129
int m_MaxHistoryLines
Definition: CConsole.h:116
#define X(id)
Definition: CStrIntern.cpp:89
void SaveHistory()
Definition: CConsole.cpp:599
void SetBuffer(const wchar_t *szMessage)
Definition: CConsole.cpp:525
bool m_bVisible
Definition: CConsole.h:118
#define CONSOLE_BUFFER_SIZE
Definition: CConsole.h:38
CConsole * g_Console
Definition: CConsole.cpp:49
In-game console.
Definition: CConsole.h:50
float m_fX
Definition: CConsole.h:96
void DrawBuffer(CTextRenderer &textRenderer)
Definition: CConsole.cpp:258
InReaction conInputHandler(const SDL_Event_ *ev)
Definition: CConsole.cpp:635
double m_cursorBlinkRate
Definition: CConsole.h:122
float m_fHeight
Definition: CConsole.h:99
int m_iBufferLength
Definition: CConsole.h:113
CMutex m_Mutex
Definition: CConsole.h:94
void SetVisible(bool visible)
Definition: CConsole.cpp:105
void UpdateScreenSize(int w, int h)
Definition: CConsole.cpp:86
std::shared_ptr< CShaderProgram > CShaderProgramPtr
Definition: ShaderProgramPtr.h:25
void InsertChar(const int szChar, const wchar_t cooked)
Definition: CConsole.cpp:318
bool m_bToggle
Definition: CConsole.h:119
bool IsFull()
Definition: CConsole.h:131
bool IsEmpty()
Definition: CConsole.h:132