aboutsummaryrefslogtreecommitdiff
path: root/engines/sword2/debug.cpp
blob: f1f93c4731f6711c10a7738a888e83e067042033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * Additional copyright for this file:
 * Copyright (C) 1994-1998 Revolution Software Ltd.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 */

#include "common/stdafx.h"
#include "common/rect.h"

#include "sword2/sword2.h"
#include "sword2/defs.h"
#include "sword2/header.h"
#include "sword2/console.h"
#include "sword2/logic.h"
#include "sword2/maketext.h"
#include "sword2/memory.h"
#include "sword2/mouse.h"
#include "sword2/resman.h"
#include "sword2/router.h"
#include "sword2/screen.h"

namespace Sword2 {

void Debugger::clearDebugTextBlocks() {
	uint8 blockNo = 0;

	while (blockNo < MAX_DEBUG_TEXTS && _debugTextBlocks[blockNo] > 0) {
		// kill the system text block
		_vm->_fontRenderer->killTextBloc(_debugTextBlocks[blockNo]);

		// clear this element of our array of block numbers
		_debugTextBlocks[blockNo] = 0;

		blockNo++;
	}
}

void Debugger::makeDebugTextBlock(char *text, int16 x, int16 y) {
	uint8 blockNo = 0;

	while (blockNo < MAX_DEBUG_TEXTS && _debugTextBlocks[blockNo] > 0)
		blockNo++;

	assert(blockNo < MAX_DEBUG_TEXTS);

	_debugTextBlocks[blockNo] = _vm->_fontRenderer->buildNewBloc((byte *)text, x, y, 640 - x, 0, RDSPR_DISPLAYALIGN, CONSOLE_FONT_ID, NO_JUSTIFICATION);
}

void Debugger::buildDebugText() {
	char buf[128];

	int32 showVarNo;		// for variable watching
	int32 showVarPos;
	int32 varNo;

	ScreenInfo *screenInfo = _vm->_screen->getScreenInfo();

	// clear the array of text block numbers for the debug text
	clearDebugTextBlocks();

	// mouse coords
	// print mouse coords beside mouse-marker, if it's being displayed
	if (_displayMouseMarker) {
		int mouseX, mouseY;

		_vm->_mouse->getPos(mouseX, mouseY);

		sprintf(buf, "%d,%d", mouseX + screenInfo->scroll_offset_x, mouseY + screenInfo->scroll_offset_y);
		if (mouseX > 560)
			makeDebugTextBlock(buf, mouseX - 50, mouseY - 15);
		else
			makeDebugTextBlock(buf, mouseX + 5, mouseY - 15);
	}

	// mouse area coords

	// defining a mouse area the easy way, by creating a box on-screen
	if (_draggingRectangle || _vm->_logic->readVar(SYSTEM_TESTING_ANIMS)) {
		// so we can see what's behind the lines
		_rectFlicker = !_rectFlicker;

		sprintf(buf, "x1=%d", _rectX1);
		makeDebugTextBlock(buf, 0, 120);

		sprintf(buf, "y1=%d", _rectY1);
		makeDebugTextBlock(buf, 0, 135);

		sprintf(buf, "x2=%d", _rectX2);
		makeDebugTextBlock(buf, 0, 150);

		sprintf(buf, "y2=%d", _rectY2);
		makeDebugTextBlock(buf, 0, 165);
	}

	// testingSnR indicator

	if (_testingSnR) {		// see fnAddHuman()
		sprintf(buf, "TESTING LOGIC STABILITY!");
		makeDebugTextBlock(buf, 0, 105);
	}

#ifdef SWORD2_DEBUG
	// speed-up indicator

	if (_vm->_renderSkip) {		// see sword2.cpp
		sprintf(buf, "SKIPPING FRAMES FOR SPEED-UP!");
		makeDebugTextBlock(buf, 0, 120);
	}
#endif

	// debug info at top of screen - enabled/disabled as one complete unit

	if (_displayTime) {
		int32 time = _vm->getMillis();

		if ((time - _startTime) / 1000 >= 10000)
			_startTime = time;

		time -= _startTime;
		sprintf(buf, "Time %.2d:%.2d:%.2d.%.3d", (time / 3600000) % 60, (time / 60000) % 60, (time / 1000) % 60, time % 1000);
		makeDebugTextBlock(buf, 500, 360);
		sprintf(buf, "Game %d", _vm->_gameCycle);
		makeDebugTextBlock(buf, 500, 380);
	}

   	// current text number & speech-sample resource id

	if (_displayTextNumbers) {
		if (_textNumber) {
			if (_vm->_logic->readVar(SYSTEM_TESTING_TEXT)) {
				if (_vm->_logic->readVar(SYSTEM_WANT_PREVIOUS_LINE))
					sprintf(buf, "backwards");
				else
 					sprintf(buf, "forwards");

				makeDebugTextBlock(buf, 0, 340);
			}

			sprintf(buf, "res: %d", _textNumber / SIZE);
			makeDebugTextBlock(buf, 0, 355);

			sprintf(buf, "pos: %d", _textNumber & 0xffff);
			makeDebugTextBlock(buf, 0, 370);

 			sprintf(buf, "TEXT: %d", _vm->_logic->_officialTextNumber);
			makeDebugTextBlock(buf, 0, 385);
		}
	}

	// resource number currently being checking for animation

	if (_vm->_logic->readVar(SYSTEM_TESTING_ANIMS)) {
		sprintf(buf, "trying resource %d", _vm->_logic->readVar(SYSTEM_TESTING_ANIMS));
		makeDebugTextBlock(buf, 0, 90);
	}

	// general debug info

	if (_displayDebugText) {
/*
		// CD in use
		sprintf(buf, "CD-%d", currentCD);
		makeDebugTextBlock(buf, 0, 0);
*/

		// mouse coords & object pointed to

		if (_vm->_logic->readVar(CLICKED_ID))
			sprintf(buf, "last click at %d,%d (id %d: %s)",
				_vm->_logic->readVar(MOUSE_X),
				_vm->_logic->readVar(MOUSE_Y),
				_vm->_logic->readVar(CLICKED_ID),
				_vm->_resman->fetchName(_vm->_logic->readVar(CLICKED_ID)));
		else
			sprintf(buf, "last click at %d,%d (---)",
				_vm->_logic->readVar(MOUSE_X),
				_vm->_logic->readVar(MOUSE_Y));

 		makeDebugTextBlock(buf, 0, 15);

		uint32 mouseTouching = _vm->_mouse->getMouseTouching();

		int mouseX, mouseY;

		_vm->_mouse->getPos(mouseX, mouseY);

		if (mouseTouching)
			sprintf(buf, "mouse %d,%d (id %d: %s)",
				mouseX + screenInfo->scroll_offset_x,
				mouseY + screenInfo->scroll_offset_y,
				mouseTouching,
				_vm->_resman->fetchName(mouseTouching));
		else
			sprintf(buf, "mouse %d,%d (not touching)",
				mouseX + screenInfo->scroll_offset_x,
				mouseY + screenInfo->scroll_offset_y);

		makeDebugTextBlock(buf, 0, 30);

 		// player coords & graphic info
		// if player objct has a graphic

		if (_graphAnimRes)
			sprintf(buf, "player %d,%d %s (%d) #%d/%d",
				screenInfo->player_feet_x,
				screenInfo->player_feet_y,
				_vm->_resman->fetchName(_graphAnimRes),
				_graphAnimRes,
				_graphAnimPc,
				_graphNoFrames);
		else
			sprintf(buf, "player %d,%d --- %d",
				screenInfo->player_feet_x,
				screenInfo->player_feet_y,
				_graphAnimPc);

		makeDebugTextBlock(buf, 0, 45);

 		// frames-per-second counter

		sprintf(buf, "fps %d", _vm->_screen->getFps());
		makeDebugTextBlock(buf, 440, 0);

 		// location number

		sprintf(buf, "location=%d", _vm->_logic->readVar(LOCATION));
		makeDebugTextBlock(buf, 440, 15);

 		// "result" variable

		sprintf(buf, "result=%d", _vm->_logic->readVar(RESULT));
		makeDebugTextBlock(buf, 440, 30);

 		// no. of events in event list

		sprintf(buf, "events=%d", _vm->_logic->countEvents());
		makeDebugTextBlock(buf, 440, 45);

		// sprite list usage

		sprintf(buf, "bgp0: %d/%d", _vm->_screen->getCurBgp0(), MAX_bgp0_sprites);
		makeDebugTextBlock(buf, 560, 0);

		sprintf(buf, "bgp1: %d/%d", _vm->_screen->getCurBgp1(), MAX_bgp1_sprites);
		makeDebugTextBlock(buf, 560, 15);

		sprintf(buf, "back: %d/%d", _vm->_screen->getCurBack(), MAX_back_sprites);
		makeDebugTextBlock(buf, 560, 30);

		sprintf(buf, "sort: %d/%d", _vm->_screen->getCurSort(), MAX_sort_sprites);
		makeDebugTextBlock(buf, 560, 45);

		sprintf(buf, "fore: %d/%d", _vm->_screen->getCurFore(), MAX_fore_sprites);
		makeDebugTextBlock(buf, 560, 60);

		sprintf(buf, "fgp0: %d/%d", _vm->_screen->getCurFgp0(), MAX_fgp0_sprites);
		makeDebugTextBlock(buf, 560, 75);

		sprintf(buf, "fgp1: %d/%d", _vm->_screen->getCurFgp1(), MAX_fgp1_sprites);
		makeDebugTextBlock(buf, 560, 90);

		// largest layer & sprite

		// NB. Strings already constructed in Build_display.cpp
		makeDebugTextBlock(_vm->_screen->getLargestLayerInfo(), 0, 60);
		makeDebugTextBlock(_vm->_screen->getLargestSpriteInfo(), 0, 75);

		// "waiting for person" indicator - set form fnTheyDo and
		// fnTheyDoWeWait

		if (_speechScriptWaiting) {
			sprintf(buf, "script waiting for %s (%d)",
				_vm->_resman->fetchName(_speechScriptWaiting),
				_speechScriptWaiting);
			makeDebugTextBlock(buf, 0, 90);
		}

		// variable watch display

		showVarPos = 115;	// y-coord for first showVar

		for (showVarNo = 0; showVarNo < MAX_SHOWVARS; showVarNo++) {
			varNo = _showVar[showVarNo];	// get variable number

			// if non-zero ie. cannot watch 'id' but not needed
			// anyway because it changes throughout the logic loop

			if (varNo) {
				sprintf(buf, "var(%d) = %d", varNo, _vm->_logic->readVar(varNo));
				makeDebugTextBlock(buf, 530, showVarPos);
				showVarPos += 15;	// next line down
			}
		}

		// memory indicator - this should come last, to show all the
		// sprite blocks above!

		uint32 totAlloc = _vm->_memory->getTotAlloc();
		int16 numBlocks = _vm->_memory->getNumBlocks();

		if (totAlloc < 1024)
			sprintf(buf, "%u bytes in %d memory blocks", totAlloc, numBlocks);
		else if (totAlloc < 1024 * 1024)
			sprintf(buf, "%uK in %d memory blocks", totAlloc / 1024, numBlocks);
		else
			sprintf(buf, "%.02fM in %d memory blocks", totAlloc / 1048576., numBlocks);

		makeDebugTextBlock(buf, 0, 0);
	}
}

void Debugger::drawDebugGraphics() {
	ScreenInfo *screenInfo = _vm->_screen->getScreenInfo();
	// walk-grid

	if (_displayWalkGrid)
		_vm->_logic->_router->plotWalkGrid();

	// player feet coord marker

	if (_displayPlayerMarker)
		plotCrossHair(screenInfo->player_feet_x, screenInfo->player_feet_y, 215);

	// mouse marker & coords

	if (_displayMouseMarker) {
		int mouseX, mouseY;

		_vm->_mouse->getPos(mouseX, mouseY);

		plotCrossHair(mouseX + screenInfo->scroll_offset_x, mouseY + screenInfo->scroll_offset_y, 215);
	}

   	// mouse area rectangle / sprite box rectangle when testing anims

	if (_vm->_logic->readVar(SYSTEM_TESTING_ANIMS)) {
		// draw box around current frame
		drawRect(_rectX1, _rectY1, _rectX2, _rectY2, 184);
	} else if (_draggingRectangle) {
		// defining a mouse area the easy way, by creating a box
		// on-screen
		if (_rectFlicker)
			drawRect(_rectX1, _rectY1, _rectX2, _rectY2, 184);
	}
}

void Debugger::plotCrossHair(int16 x, int16 y, uint8 pen) {
	_vm->_screen->plotPoint(x, y, pen);

	_vm->_screen->drawLine(x - 2, y, x - 5, y, pen);
	_vm->_screen->drawLine(x + 2, y, x + 5, y, pen);

	_vm->_screen->drawLine(x, y - 2, x, y - 5, pen);
	_vm->_screen->drawLine(x, y + 2, x, y + 5, pen);
}

void Debugger::drawRect(int16 x1, int16 y1, int16 x2, int16 y2, uint8 pen) {
	_vm->_screen->drawLine(x1, y1, x2, y1, pen);	// top edge
	_vm->_screen->drawLine(x1, y2, x2, y2, pen);	// bottom edge
	_vm->_screen->drawLine(x1, y1, x1, y2, pen);	// left edge
	_vm->_screen->drawLine(x2, y1, x2, y2, pen);	// right edge
}

} // End of namespace Sword2