Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CConsole.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 
30 #include "lib/input.h"
31 #include "lib/file/vfs/vfs_path.h"
32 #include "ps/CStr.h"
33 #include "ps/ThreadUtil.h"
34 
36 typedef shared_ptr<CShaderProgram> CShaderProgramPtr;
37 
38 class CTextRenderer;
39 
40 #define CONSOLE_BUFFER_SIZE 1024 // for text being typed into the console
41 #define CONSOLE_MESSAGE_SIZE 1024 // for messages being printed into the console
42 
43 #define CONSOLE_FONT L"mono-10"
44 
45 /**
46  * In-game console.
47  *
48  * Thread-safety:
49  * - Expected to be constructed/destructed in the main thread.
50  * - InsertMessage may be called from any thread while the object is alive.
51  */
52 class CConsole
53 {
55 
56 public:
57  CConsole();
58  ~CConsole();
59 
60  void SetSize(float X = 300, float Y = 0, float W = 800, float H = 600);
61  void UpdateScreenSize(int w, int h);
62 
63  void ToggleVisible();
64  void SetVisible(bool visible);
65 
66  void SetCursorBlinkRate(double rate);
67 
68  /**
69  * @param deltaRealTime Elapsed real time since the last frame.
70  */
71  void Update(const float deltaRealTime);
72 
73  void Render();
74 
75  void InsertMessage(const wchar_t* szMessage, ...) WPRINTF_ARGS(2);
76  void InsertChar(const int szChar, const wchar_t cooked);
77 
78  void ReceivedChatMessage(const wchar_t *pSender, const wchar_t *szMessage);
79 
80  void SetBuffer(const wchar_t* szMessage);
81 
82  void UseHistoryFile(const VfsPath& filename, int historysize);
83 
84  // Only returns a pointer to the buffer; copy out of here if you want to keep it.
85  const wchar_t* GetBuffer();
86  void FlushBuffer();
87 
88  bool IsActive() { return m_bVisible; }
89 
92  int m_iFontOffset; // distance to move up before drawing
94 
95 private:
96  // Lock for all state modified by InsertMessage
98 
99  float m_fX;
100  float m_fY;
101 
102  float m_fHeight;
103  float m_fWidth;
104 
105  // "position" in show/hide animation, how visible the console is (0..1).
106  // allows implementing other animations than sliding, e.g. fading in/out.
108 
109  std::deque<std::wstring> m_deqMsgHistory; // protected by m_Mutex
110  std::deque<std::wstring> m_deqBufHistory;
111 
113 
114  wchar_t* m_szBuffer;
117 
120 
121  bool m_bFocus;
122  bool m_bVisible; // console is to be drawn
123  bool m_bToggle; // show/hide animation is currently active
124  double m_prevTime; // the previous time the cursor draw state changed (used for blinking cursor)
125  bool m_bCursorVisState; // if the cursor should be drawn or not
126  double m_cursorBlinkRate; // cursor blink rate in seconds, if greater than 0.0
127 
128  void ToLower(wchar_t* szMessage, size_t iSize = 0);
129  void Trim(wchar_t* szMessage, const wchar_t cChar = 32, size_t iSize = 0);
130 
131  void DrawWindow(CShaderProgramPtr& shader);
132  void DrawHistory(CTextRenderer& textRenderer);
133  void DrawBuffer(CTextRenderer& textRenderer);
134  void DrawCursor(CTextRenderer& textRenderer);
135 
136  bool IsEOB() { return (m_iBufferPos == m_iBufferLength); } // Is end of Buffer?
137  bool IsBOB() { return (m_iBufferPos == 0); } // Is beginning of Buffer?
139  bool IsEmpty() { return (m_iBufferLength == 0); }
140 
141  void ProcessBuffer(const wchar_t* szLine);
142 
143  // Insert message without printf-style formatting, and without length limits
144  void InsertMessageRaw(const CStrW& message);
145 
146  void LoadHistory();
147  void SaveHistory();
148 };
149 
150 extern CConsole* g_Console;
151 
152 extern InReaction conInputHandler(const SDL_Event_* ev);
153 
154 #endif
Definition: Decompose.h:22
std::deque< std::wstring > m_deqBufHistory
Definition: CConsole.h:110
void ReceivedChatMessage(const wchar_t *pSender, const wchar_t *szMessage)
Definition: CConsole.cpp:660
Definition: Decompose.h:22
bool IsBOB()
Definition: CConsole.h:137
int m_iFontOffset
Definition: CConsole.h:92
VfsPath m_sHistoryFile
Definition: CConsole.h:118
void FlushBuffer()
Definition: CConsole.cpp:113
float m_fY
Definition: CConsole.h:100
float m_fWidth
Definition: CConsole.h:103
int m_iFontWidth
Definition: CConsole.h:91
double m_prevTime
Definition: CConsole.h:124
void DrawHistory(CTextRenderer &textRenderer)
Definition: CConsole.cpp:267
bool m_bFocus
Definition: CConsole.h:121
void DrawCursor(CTextRenderer &textRenderer)
Definition: CConsole.cpp:317
float m_fVisibleFrac
Definition: CConsole.h:107
Definition: Decompose.h:22
int m_iFontHeight
Definition: CConsole.h:90
bool IsActive()
Definition: CConsole.h:88
A non-recursive mutual exclusion lock.
Definition: ThreadUtil.h:45
const wchar_t * GetBuffer()
Definition: CConsole.cpp:568
size_t m_charsPerPage
Definition: CConsole.h:93
void SetCursorBlinkRate(double rate)
Definition: CConsole.cpp:108
wchar_t * m_szBuffer
Definition: CConsole.h:114
void SetSize(float X=300, float Y=0, float W=800, float H=600)
Definition: CConsole.cpp:75
void ToggleVisible()
Definition: CConsole.cpp:90
CConsole()
Definition: CConsole.cpp:48
CConsole * g_Console
Definition: CConsole.cpp:46
Definition: path.h:75
void UseHistoryFile(const VfsPath &filename, int historysize)
Definition: CConsole.cpp:586
NONCOPYABLE(CConsole)
InReaction
Definition: input.h:34
int m_iBufferPos
Definition: CConsole.h:115
bool m_bCursorVisState
Definition: CConsole.h:125
void Render()
Definition: CConsole.cpp:190
int m_iMsgHistPos
Definition: CConsole.h:112
~CConsole()
Definition: CConsole.cpp:69
void InsertMessageRaw(const CStrW &message)
Definition: CConsole.cpp:527
std::deque< std::wstring > m_deqMsgHistory
Definition: CConsole.h:109
void DrawWindow(CShaderProgramPtr &shader)
Definition: CConsole.cpp:231
void ProcessBuffer(const wchar_t *szLine)
Definition: CConsole.cpp:594
void Trim(wchar_t *szMessage, const wchar_t cChar=32, size_t iSize=0)
Definition: CConsole.cpp:134
void Update(const float deltaRealTime)
Definition: CConsole.cpp:162
void LoadHistory()
Definition: CConsole.cpp:612
bool IsEOB()
Definition: CConsole.h:136
int m_MaxHistoryLines
Definition: CConsole.h:119
#define WPRINTF_ARGS(fmtpos)
void SaveHistory()
Definition: CConsole.cpp:643
void SetBuffer(const wchar_t *szMessage)
Definition: CConsole.cpp:574
bool m_bVisible
Definition: CConsole.h:122
#define CONSOLE_BUFFER_SIZE
Definition: CConsole.h:40
In-game console.
Definition: CConsole.h:52
float m_fX
Definition: CConsole.h:99
void DrawBuffer(CTextRenderer &textRenderer)
Definition: CConsole.cpp:290
double m_cursorBlinkRate
Definition: CConsole.h:126
float m_fHeight
Definition: CConsole.h:102
void ToLower(wchar_t *szMessage, size_t iSize=0)
Definition: CConsole.cpp:121
A compiled vertex+fragment shader program.
Definition: ShaderProgram.h:65
int m_iBufferLength
Definition: CConsole.h:116
CMutex m_Mutex
Definition: CConsole.h:97
void SetVisible(bool visible)
Definition: CConsole.cpp:96
shared_ptr< CShaderProgram > CShaderProgramPtr
void UpdateScreenSize(int w, int h)
Definition: CConsole.cpp:83
void InsertMessage(const wchar_t *szMessage,...) WPRINTF_ARGS(2)
Definition: CConsole.cpp:508
void InsertChar(const int szChar, const wchar_t cooked)
Definition: CConsole.cpp:350
bool m_bToggle
Definition: CConsole.h:123
InReaction conInputHandler(const SDL_Event_ *ev)
Definition: CConsole.cpp:694
bool IsFull()
Definition: CConsole.h:138
bool IsEmpty()
Definition: CConsole.h:139