aboutsummaryrefslogtreecommitdiff
path: root/saga/render.cpp
blob: f93dadbf7268fdf71cc7eb5b4e974803a7083330 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004-2005 The ScummVM project
 *
 * The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

// Main rendering loop
#include "saga/saga.h"

#include "saga/actor.h"
#include "saga/font.h"
#include "saga/gfx.h"
#include "saga/interface.h"
#include "saga/objectmap.h"
#include "saga/puzzle.h"
#include "saga/render.h"
#include "saga/scene.h"
#include "saga/text.h"

#include "common/timer.h"
#include "common/system.h"

namespace Saga {

const char *test_txt = "The quick brown fox jumped over the lazy dog. She sells sea shells down by the sea shore.";

Render::Render(SagaEngine *vm, OSystem *system) {
	_vm = vm;
	_system = system;
	_initialized = false;

	int tmp_w, tmp_h, tmp_bytepp;


	// Initialize FPS timer callback
	Common::g_timer->installTimerProc(&fpsTimerCallback, 1000000, this);

	// Create background buffer 
	_bg_buf_w = _vm->getDisplayWidth();
	_bg_buf_h = _vm->getDisplayHeight();
	_bg_buf = (byte *)calloc(_vm->getDisplayWidth(), _vm->getDisplayHeight());

	if (_bg_buf == NULL) {
		memoryError("Render::Render");
	}

	// Allocate temp buffer for animation decoding, 
	// graphics scalers (2xSaI), etc.
	tmp_w = _vm->getDisplayWidth();
	tmp_h = _vm->getDisplayHeight() + 4; // BG unbanking requres extra rows
	tmp_bytepp = 1;

	_tmp_buf = (byte *)calloc(1, tmp_w * tmp_h * tmp_bytepp);
	if (_tmp_buf == NULL) {
		free(_bg_buf);
		return;
	}

	_tmp_buf_w = tmp_w;
	_tmp_buf_h = tmp_h;

	_backbuf_surface = _vm->_gfx->getBackBuffer();
	_flags = 0;

	_initialized = true;
}

Render::~Render(void) {
	Common::g_timer->removeTimerProc(&fpsTimerCallback);
	free(_bg_buf);
	free(_tmp_buf);

	_initialized = false;
}

bool Render::initialized() {
	return _initialized;
}

int Render::drawScene() {
	SURFACE *backbuf_surface;
	char txt_buf[20];
	int fps_width;
	Point mouse_pt;

	if (!_initialized) {
		return FAILURE;
	}

	_framecount++;

	backbuf_surface = _backbuf_surface;

	// Get mouse coordinates
	mouse_pt = _vm->mousePos();

	if (/*_vm->_interface->getMode() != kPanelPlacard*/!(_flags & (RF_PLACARD | RF_MAP))) {
		// Display scene background
		_vm->_scene->draw();

		if (_vm->_interface->getFadeMode() != kFadeOut) {
			if (_vm->_puzzle->isActive()) {
				_vm->_puzzle->movePiece(mouse_pt);
				_vm->_actor->drawSpeech();
			} else {
				// Draw queued actors
				_vm->_actor->drawActors();
			}

			if (getFlags() & RF_OBJECTMAP_TEST) {
				if (_vm->_scene->_objectMap)
					_vm->_scene->_objectMap->draw(backbuf_surface, mouse_pt, kITEColorBrightWhite, kITEColorBlack);
				if (_vm->_scene->_actionMap)
					_vm->_scene->_actionMap->draw(backbuf_surface, mouse_pt, kITEColorRed, kITEColorBlack);
			}
			if (getFlags() & RF_ACTOR_PATH_TEST) {
				_vm->_actor->drawPathTest();
			}
		}
	}

	if (_flags & RF_MAP)
		_vm->_interface->mapPanelDrawCrossHair();

	if ((_vm->_interface->getMode() == kPanelOption) || 
		(_vm->_interface->getMode() == kPanelQuit) ||
		(_vm->_interface->getMode() == kPanelLoad) ||
		(_vm->_interface->getMode() == kPanelSave)) {
		_vm->_interface->drawOption();

		if (_vm->_interface->getMode() == kPanelQuit) {
			_vm->_interface->drawQuit();
		}
		if (_vm->_interface->getMode() == kPanelLoad) {
			_vm->_interface->drawLoad();
		}
		if (_vm->_interface->getMode() == kPanelSave) {
			_vm->_interface->drawSave();
		}
	}

	// Draw queued text strings
	_vm->textDrawList(_vm->_scene->_textList, backbuf_surface);

	// Handle user input
	_vm->processInput();

	// Display rendering information
	if (_flags & RF_SHOW_FPS) {
		sprintf(txt_buf, "%d", _fps);
		fps_width = _vm->_font->getStringWidth(SMALL_FONT_ID, txt_buf, 0, FONT_NORMAL);
		_vm->_font->draw(SMALL_FONT_ID, backbuf_surface, txt_buf, 0, backbuf_surface->w - fps_width, 2,
					kITEColorBrightWhite, kITEColorBlack, FONT_OUTLINE);
	}

	// Display "paused game" message, if applicable
	if (_flags & RF_RENDERPAUSE) {
		int msg_len = strlen(PAUSEGAME_MSG);
		int msg_w = _vm->_font->getStringWidth(BIG_FONT_ID, PAUSEGAME_MSG, msg_len, FONT_OUTLINE);
		_vm->_font->draw(BIG_FONT_ID, backbuf_surface, PAUSEGAME_MSG, msg_len,
				(backbuf_surface->w - msg_w) / 2, 90, kITEColorBrightWhite, kITEColorBlack, FONT_OUTLINE);
	}

	// Update user interface

	_vm->_interface->update(mouse_pt, UPDATE_MOUSEMOVE);

	// Display text formatting test, if applicable
	if (_flags & RF_TEXT_TEST) {
		_vm->textDraw(MEDIUM_FONT_ID, backbuf_surface, test_txt, mouse_pt.x, mouse_pt.y,
				kITEColorBrightWhite, kITEColorBlack, FONT_OUTLINE | FONT_CENTERED);
	}

	// Display palette test, if applicable
	if (_flags & RF_PALETTE_TEST) {
		drawPalette(backbuf_surface);
	}

	_system->copyRectToScreen((byte *)backbuf_surface->pixels, backbuf_surface->w, 0, 0, 
							  backbuf_surface->w, backbuf_surface->h);

	_system->updateScreen();
	return SUCCESS;
}

unsigned int Render::getFrameCount() {
	return _framecount;
}

unsigned int Render::resetFrameCount() {
	unsigned int framecount = _framecount;

	_framecount = 0;

	return framecount;
}

void Render::fpsTimerCallback(void *refCon) {
	((Render *)refCon)->fpsTimer();
}

void Render::fpsTimer(void) {
	_fps = _framecount;
	_framecount = 0;
}

unsigned int Render::getFlags() {
	return _flags;
}

void Render::setFlag(unsigned int flag) {
	_flags |= flag;
}

void Render::clearFlag(unsigned int flag) {
	_flags &= ~flag;
}

void Render::toggleFlag(unsigned int flag) {
	_flags ^= flag;
}

int Render::getBufferInfo(BUFFER_INFO *bufinfo) {
	assert(bufinfo != NULL);

	bufinfo->bg_buf = _bg_buf;
	bufinfo->bg_buf_w = _bg_buf_w;
	bufinfo->bg_buf_h = _bg_buf_h;

	bufinfo->tmp_buf = _tmp_buf;
	bufinfo->tmp_buf_w = _tmp_buf_w;
	bufinfo->tmp_buf_h = _tmp_buf_h;

	return SUCCESS;
}

} // End of namespace Saga