Wing Commander II
Reconstructed Win32 source (1996)
Loading...
Searching...
No Matches
debug.cpp
Go to the documentation of this file.
1/*
2 * Win32 developer overlay console.
3 *
4 * Retail object 0x45AC50-0x45B7DF (link order; see docs/ORDER.md).
5 */
6#include "game.h"
7
8#include <stdarg.h>
9
10int g_nDebugOverlayConsoleCount = 0; /* 0x0049cb24 */
11DWORD g_dwDebugOverlayKey = 0; /* 0x0049cb28 */
12DWORD g_dwDebugOverlayKeyLatch = 0; /* 0x0049cb2c */
13HHOOK g_hDebugKeyboardHook = 0; /* 0x0049cb30 */
14
15#pragma function(memcpy, memset, strcpy)
16/* Function start: 0x45AC50 */
18 HWND targetWindow,
19 int columnCount,
20 int rowCount,
21 int waitMode)
22{
23 TEXTMETRICA textMetrics;
24 HDC dc;
25 HGDIOBJ font;
26
27 busyWait = waitMode;
29 window = targetWindow;
30 columns = columnCount;
31 rows = rowCount;
32 cursorRow = 0;
34 textBuffer = (char *)malloc(rows * columns);
35 dirtyLines = (char *)malloc(rows);
36 memset(textBuffer, ' ', rows * columns);
37 memset(dirtyLines, 1, rows);
38
39 dc = GetDC(window);
40 font = CreateFontA(10, 10, 0, 0, 400, 0, 0, 0, 0, 2, 0, 0,
41 0x30, "Courier");
42 SelectObject(dc, font);
43 textColor = 0xffffff;
45 backgroundMode = OPAQUE;
46 GetTextMetricsA(dc, &textMetrics);
47 SetTextColor(dc, 0xffffff);
48 SetBkColor(dc, 0);
49 ReleaseDC(window, dc);
50 characterWidth = textMetrics.tmMaxCharWidth;
51 characterHeight = textMetrics.tmHeight;
52
53 if (g_hDebugKeyboardHook == 0) {
55 SetWindowsHookExA(WH_KEYBOARD, (HOOKPROC)DebugKeyboardHookProc,
56 module, 0);
57 }
58 reverseVideo = 0;
59 mutex = CreateMutexA(0, FALSE, 0);
60 spinnerIndex = 0;
62 spinnerCharacters = (char *)malloc(5);
63 strcpy(spinnerCharacters, "-\\|/");
64}
65
66/* Function start: 0x45AE6C */
68{
69 int shutdownKey;
70
72 shutdownKey = 0x103;
73 if (--g_nDebugOverlayConsoleCount == 0) {
74 UnhookWindowsHookEx(g_hDebugKeyboardHook);
75 }
76 free(textBuffer);
77 free(dirtyLines);
79 (void)shutdownKey;
80 return;
81}
82
83#pragma function(strlen)
84/* Function start: 0x45AEE4 */
85extern "C" DWORD WINAPI DebugOverlayWorkerProc(void *parameter)
86{
87 int done;
88 DebugOverlayConsole *context;
89 /* Retail compares against this stack slot before its first assignment. */
90 int lastTick;
91 DWORD waited;
92 HDC dc;
93
94 context = (DebugOverlayConsole *)parameter;
95 done = 0;
96 while (done == 0) {
97 if (context->animationState == 2)
98 break;
99 if ((int)timeGetTime() > lastTick + 500) {
100 waited = WaitForSingleObject(context->mutex, 500);
101 if (waited == WAIT_FAILED) {
102 ExitThread(1);
103 } else if (waited == WAIT_TIMEOUT) {
104 continue;
105 }
106 if (context->animationState != 0) {
107 dc = GetDC(context->window);
108 TextOutA(dc,
109 context->characterWidth * context->cursorColumn,
110 context->characterHeight * context->cursorRow,
111 context->spinnerCharacters + context->spinnerIndex,
112 1);
113 context->spinnerIndex++;
114 if ((int)strlen(context->spinnerCharacters) ==
115 context->spinnerIndex) {
116 context->spinnerIndex = 0;
117 }
118 ReleaseDC(context->window, dc);
119 }
120 ReleaseMutex(context->mutex);
121 lastTick = timeGetTime();
122 }
123 }
124 ExitThread(0);
125 return 0;
126}
127
128/* Function start: 0x45B04E */
129extern "C" LRESULT CALLBACK DebugKeyboardHookProc(int code, WPARAM key,
130 LPARAM flags)
131{
132 if (code < 0)
133 return CallNextHookEx(g_hDebugKeyboardHook,
134 code, key, flags);
135 if ((flags & 0x40000000) != 0) {
138 }
139 return CallNextHookEx(g_hDebugKeyboardHook,
140 code, key, flags);
141}
142
143WINBASEAPI BOOL WINAPI Beep(DWORD frequency, DWORD duration);
144
145#pragma function(strcpy, strlen)
146/* Function start: 0x45B0BB */
148 const char *format, ...)
149{
150 int position;
151 int length;
152 va_list arguments;
153
154 va_start(arguments, format);
155 if (format != 0)
156 vsprintf(console->formatBuffer, format, arguments);
157 else
158 strcpy(console->formatBuffer, "\n");
159 va_end(arguments);
160 length = strlen(console->formatBuffer);
161 for (position = 0; length > position; position++) {
162 console->dirtyLines[console->cursorRow] = 1;
163 if (console->formatBuffer[position] >= ' ' &&
164 console->formatBuffer[position] <= '~') {
165 console->textBuffer[console->cursorRow * console->columns +
166 console->cursorColumn] =
167 console->formatBuffer[position];
168 console->cursorColumn++;
169 if (console->cursorColumn == console->columns) {
170 console->cursorColumn = 0;
171 console->cursorRow++;
172 if (console->rows == console->cursorRow)
173 console->Scroll();
174 }
175 } else {
176 switch (console->formatBuffer[position]) {
177 case '\n':
178 case '\r':
179 console->cursorColumn = 0;
180 console->cursorRow++;
181 if (console->rows == console->cursorRow)
182 console->Scroll();
183 break;
184 case '\a':
185 Beep(0, 0);
186 break;
187 case '\b':
188 console->cursorColumn--;
189 if (console->cursorColumn < 0) {
190 console->cursorColumn = 0;
191 console->cursorRow--;
192 if (console->cursorRow < 0)
193 console->cursorRow = 0;
194 }
195 console->textBuffer[
196 console->cursorRow * console->columns +
197 console->cursorColumn] = ' ';
198 break;
199 }
200 }
201 }
202 console->DrawPendingLines();
203}
204
205#pragma intrinsic(strcpy, strlen)
206/* Function start: 0x45B2E0 */
208{
209 cursorRow = 0;
211 memset(textBuffer, ' ', rows * columns);
212 memset(dirtyLines, 1, rows);
213 return;
214}
215
216/* Function start: 0x45B348 */
218{
219 memmove(textBuffer, columns + textBuffer, (rows - 1) * columns);
220 cursorRow = rows - 1;
221 memset((rows - 1) * columns + textBuffer, ' ', columns);
222 memset(dirtyLines, 1, rows);
223 return;
224}
225
226/* Function start: 0x45B3DC */
228{
229 HDC deviceContext;
230 int rowIndex;
231
232 deviceContext = GetDC(window);
233 for (rowIndex = 0; rows > rowIndex; rowIndex++) {
234 if (dirtyLines[rowIndex] != 0) {
235 TextOutA(deviceContext, 0, characterHeight * rowIndex,
236 columns * rowIndex + textBuffer, columns);
237 }
238 }
239 ReleaseDC(window, deviceContext);
240 memset(dirtyLines, 0, rows);
241 return;
242}
243
244/* Function start: 0x45B49C */
246{
247 MSG message;
248 DWORD key;
249
250 if (busyWait == 0) {
251 while (g_dwDebugOverlayKey == 0) {
252 while (PeekMessageA(&message, 0, 0, 0, 0) != 0) {
253 if (message.message == WM_QUIT)
254 return 0;
255 if (GetMessageA(&message, 0, 0, 0) != 0) {
256 TranslateMessage(&message);
257 DispatchMessageA(&message);
258 }
259 }
260 }
261 } else {
262 while (g_dwDebugOverlayKey == 0) {
263 }
264 }
267 return (char)key;
268}
269
270/* Function start: 0x45B55C */
272{
273 HDC deviceContext;
274
275 if (reverseVideo == 0) {
276 deviceContext = GetDC(window);
277 SetBkColor(deviceContext, textColor);
278 SetTextColor(deviceContext, backgroundColor);
279 reverseVideo = 1;
280 ReleaseDC(window, deviceContext);
281 }
282}
283
284/* Function start: 0x45B5D8 */
286{
287 HDC deviceContext;
288
289 if (reverseVideo != 0) {
290 deviceContext = GetDC(window);
291 SetTextColor(deviceContext, textColor);
292 SetBkColor(deviceContext, backgroundColor);
293 reverseVideo = 0;
294 ReleaseDC(window, deviceContext);
295 }
296}
297
298WINUSERAPI HDC WINAPI GetDC(HWND window);
299WINGDIAPI COLORREF WINAPI SetTextColor(HDC deviceContext, COLORREF colour);
300WINGDIAPI int WINAPI SetBkMode(HDC deviceContext, int mode);
301WINGDIAPI BOOL WINAPI TextOutA(HDC deviceContext, int x, int y, LPCSTR text,
302 int length);
303
304/* Function start: 0x45B654 */
305void DebugOverlayConsole::SetOverlayTextColor(int red, int green, int blue)
306{
307 HDC deviceContext;
308
309 deviceContext = GetDC(window);
310 textColor = (green << 8) + (blue << 16) + red;
311 SetTextColor(deviceContext, textColor);
312 ReleaseDC(window, deviceContext);
313}
314
315WINGDIAPI COLORREF WINAPI SetBkColor(HDC deviceContext, COLORREF colour);
316WINUSERAPI int WINAPI ReleaseDC(HWND window, HDC deviceContext);
317
318/* Function start: 0x45B6BB */
320 int blue)
321{
322 HDC deviceContext;
323
324 deviceContext = GetDC(window);
325 backgroundColor = (blue << 16) + (green << 8) + red;
326 SetBkColor(deviceContext, backgroundColor);
327 ReleaseDC(window, deviceContext);
328}
329
330/* Function start: 0x45B722 */
332{
333 HDC deviceContext;
334
335 backgroundMode = TRANSPARENT;
336 deviceContext = GetDC(window);
337 SetBkMode(deviceContext, backgroundMode);
338 ReleaseDC(window, deviceContext);
339}
340
341/* Function start: 0x45B77A */
342/* Nothing in the shipped executable calls this. */
344{
345 HDC deviceContext;
346
347 backgroundMode = OPAQUE;
348 deviceContext = GetDC(window);
349 SetBkMode(deviceContext, backgroundMode);
350 ReleaseDC(window, deviceContext);
351}
WINGDIAPI int WINAPI SetBkMode(HDC deviceContext, int mode)
WINUSERAPI HDC WINAPI GetDC(HWND window)
WINGDIAPI COLORREF WINAPI SetTextColor(HDC deviceContext, COLORREF colour)
WINBASEAPI BOOL WINAPI Beep(DWORD frequency, DWORD duration)
WINGDIAPI COLORREF WINAPI SetBkColor(HDC deviceContext, COLORREF colour)
WINUSERAPI int WINAPI ReleaseDC(HWND window, HDC deviceContext)
WINGDIAPI BOOL WINAPI TextOutA(HDC deviceContext, int x, int y, LPCSTR text, int length)
void(__cdecl *g_pIxFree)(void *)
DWORD WINAPI DebugOverlayWorkerProc(void *parameter)
Definition debug.cpp:85
LRESULT CALLBACK DebugKeyboardHookProc(int code, WPARAM key, LPARAM flags)
Definition debug.cpp:129
void DebugOverlayPrintf(DebugOverlayConsole *console, const char *format,...)
Definition debug.cpp:147
Shared Win32 declarations, types and library interfaces.
DWORD g_dwDebugOverlayKeyLatch
Definition debug.cpp:12
DWORD g_dwDebugOverlayKey
Definition debug.cpp:11
int g_nDebugOverlayConsoleCount
Definition debug.cpp:10
HHOOK g_hDebugKeyboardHook
Definition debug.cpp:13
COLORREF backgroundColor
Definition game.h:383
void SetOverlayTextColor(int red, int green, int blue)
Definition debug.cpp:305
char formatBuffer[0x400]
Definition game.h:375
DebugOverlayConsole(HINSTANCE module, HWND targetWindow, int columnCount, int rowCount, int waitMode)
Definition debug.cpp:17
void DrawPendingLines(void)
Definition debug.cpp:227
void SetOverlayBackgroundColor(int red, int green, int blue)
Definition debug.cpp:319
char * dirtyLines
Definition game.h:377
void DisableReverseVideo(void)
Definition debug.cpp:285
void SetOpaqueBackground(void)
Definition debug.cpp:343
~DebugOverlayConsole(void)
Definition debug.cpp:67
void Clear(void)
Definition debug.cpp:207
char WaitForKey(void)
Definition debug.cpp:245
char * textBuffer
Definition game.h:376
char * spinnerCharacters
Definition game.h:393
COLORREF textColor
Definition game.h:382
void Scroll(void)
Definition debug.cpp:217
void SetTransparentBackground(void)
Definition debug.cpp:331
void EnableReverseVideo(void)
Definition debug.cpp:271