aboutsummaryrefslogtreecommitdiff
path: root/sword2/driver/d_draw.cpp
blob: cba4bb1e6d59ca4522f0074c59a748fc61c844ed (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
/* Copyright (C) 1994-2003 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

#include "stdafx.h"
#include "sound/mixer.h"
#include "bs2/driver/driver96.h"
#include "bs2/driver/rdwin.h"
#include "bs2/driver/d_draw.h"
#include "bs2/driver/palette.h"
#include "bs2/driver/render.h"
#include "bs2/header.h"		// HACK: For cutscenes instruction message
#include "bs2/maketext.h"	// HACK: For cutscenes instruction message
#include "bs2/sword2.h"

namespace Sword2 {

byte *lpBackBuffer;

// Game screen metrics
int16 screenDeep;
int16 screenWide;

// Scroll variables.  scrollx and scrolly hold the current scroll position, 

int16 scrollx;
int16 scrolly;

int32 renderCaps = 0;

int32 PlotDots(int16 x, int16 y, int16 count) {

	warning("stub PlotDots( %d, %d, %d )", x, y, count);
/*
	int16			i;
	uint8			*dst;

	DDSURFACEDESC	ddDescription;
	HRESULT			hr;

	ddDescription.dwSize = sizeof(ddDescription);
	
	hr = IDirectDrawSurface2_Lock(lpBackBuffer, NULL, &ddDescription, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);
	if (hr != DD_OK)
	{
		hr = IDirectDrawSurface2_Lock(lpBackBuffer, NULL, &ddDescription, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);
	}

	if (hr == DD_OK)
	{

		dst = (uint8 *) ddDescription.lpSurface + y * ddDescription.lPitch + x;

		for (i=0; i<=count; i++)
		{
			*dst = 184;
			dst += 2;
		}
		dst = (uint8 *) ddDescription.lpSurface + (y+1) * ddDescription.lPitch + x;
		for (i=0; i<=count/10; i++)
		{
			*dst = 184;
			dst += 20;
		}
		IDirectDrawSurface2_Unlock(lpBackBuffer, ddDescription.lpSurface);
	}
*/

	return RD_OK;
}

/**
 * Initialise the display with the sizes passed in.
 * @return RD_OK, or an error code if the display cannot be set up.
 */

int32 InitialiseDisplay(int16 width, int16 height) {
	g_system->init_size(width, height);

	screenWide = width;
	screenDeep = height;

	lpBackBuffer = (byte *) malloc(screenWide * screenDeep);
	if (!lpBackBuffer)
		return RDERR_OUTOFMEMORY;

	return RD_OK;
}

// FIXME: Clean up this mess. I don't want to add any new flags, but some of
// them should be renamed. Or maybe we should abandon the whole renderCaps
// thing and simply check the numeric value of the graphics quality setting
// instead.

// Note that SetTransFx() actually clears a bit. That's intentional.

void SetTransFx(void) {
	renderCaps &= ~RDBLTFX_ALLHARDWARE;
}

void ClearTransFx(void) {
	renderCaps |= RDBLTFX_ALLHARDWARE;
}

/**
 * Sets the edge blend and arithmetic stretching effects.
 */

void SetBltFx(void) {
	renderCaps |= (RDBLTFX_EDGEBLEND | RDBLTFX_ARITHMETICSTRETCH);
}

/**
 * Clears the edge blend and arithmetic stretching effects.
 */

void ClearBltFx(void) { 
	renderCaps &= ~(RDBLTFX_EDGEBLEND | RDBLTFX_ARITHMETICSTRETCH);
}

void SetShadowFx(void) {
	renderCaps |= RDBLTFX_SHADOWBLEND;
}

void ClearShadowFx(void) {
	renderCaps &= ~RDBLTFX_SHADOWBLEND;
}

/**
 * @return the graphics detail setting
 */

int32 GetRenderType(void) {
	if (renderCaps & RDBLTFX_ALLHARDWARE)
		return 0;

	if (renderCaps & (RDBLTFX_EDGEBLEND | RDBLTFX_ARITHMETICSTRETCH))
		return 3;

	if (renderCaps & RDBLTFX_SHADOWBLEND)
		return 2;

	return 1;
}

/**
 * Fill the screen buffer with palette colour zero. Note that it does not
 * touch the menu areas of the screen.
 */

int32 EraseBackBuffer( void ) {
	memset(lpBackBuffer + MENUDEEP * screenWide, 0, screenWide * RENDERDEEP);
	return RD_OK;
}


int32 NextSmackerFrame(void) {
	warning("stub NextSmackerFrame");
	return RD_OK;
}


static uint8 *textSurface = NULL;

void OpenTextObject(_movieTextObject *obj) {
	if (obj->textSprite)
		CreateSurface(obj->textSprite, &textSurface);
}

void CloseTextObject(_movieTextObject *obj) {
	if (textSurface) {
		DeleteSurface(textSurface);
		textSurface = 0;
	}
}

void DrawTextObject(_movieTextObject *obj) {
	if (obj->textSprite && textSurface)
		DrawSurface(obj->textSprite, textSurface);
}

/**
 * Plays an animated cutscene.
 * @param filename the file name of the cutscene file
 * @param text the subtitles and voiceovers for the cutscene
 * @param musicOut lead-out music
 */

int32 PlaySmacker(char *filename, _movieTextObject *text[], uint8 *musicOut) {
	warning("semi-stub PlaySmacker %s", filename);

	// WORKAROUND: For now, we just do the voice-over parts of the
	// movies, since they're separate from the actual smacker files.

	if (text) {
		uint8 oldPal[1024];
		uint8 tmpPal[1024];

		EraseBackBuffer();

		// HACK: Draw instructions
		//
		// I'm using the the menu area, because that's unlikely to be
		// touched by anything else during the cutscene.

		memset(lpBackBuffer, 0, screenWide * MENUDEEP);

		uint8 msg[] = "Cutscene - Press ESC to exit";
		mem *data = MakeTextSprite(msg, 640, 255, speech_font_id);
		_frameHeader *frame = (_frameHeader *) data->ad;
		_spriteInfo msgSprite;
		uint8 *msgSurface;

		msgSprite.x = screenWide / 2 - frame->width / 2;
		msgSprite.y = RDMENU_MENUDEEP / 2 - frame->height / 2;
		msgSprite.w = frame->width;
		msgSprite.h = frame->height;
		msgSprite.type = RDSPR_DISPLAYALIGN | RDSPR_NOCOMPRESSION | RDSPR_TRANS;
		msgSprite.data = data->ad + sizeof(_frameHeader);

		CreateSurface(&msgSprite, &msgSurface);
		DrawSurface(&msgSprite, msgSurface);
		DeleteSurface(msgSurface);
		memory.freeMemory(data);

		// In case the cutscene has a long lead-in, start just before
		// the first line of text.

		int frameCounter = text[0]->startFrame - 12;
		int textCounter = 0;

		// Fake a palette that will hopefully make the text visible.
		// In the opening cutscene it seems to use colours 1 (black?)
		// and 255 (white?).
		//
		// The text should probably be colored the same as the rest of
		// the in-game text.

		memcpy(oldPal, palCopy, 1024);
		memset(tmpPal, 0, 1024);
		tmpPal[255 * 4 + 0] = 255;
		tmpPal[255 * 4 + 1] = 255;
		tmpPal[255 * 4 + 2] = 255;
		BS2_SetPalette(0, 256, tmpPal, RDPAL_INSTANT);

		PlayingSoundHandle handle = 0;

		bool skipCutscene = false;

		while (1) {
			if (!text[textCounter])
				break;

			if (frameCounter == text[textCounter]->startFrame) {
				EraseBackBuffer();
				OpenTextObject(text[textCounter]);
				DrawTextObject(text[textCounter]);
				if (text[textCounter]->speech) {
					g_sword2->_mixer->playRaw(&handle, text[textCounter]->speech, text[textCounter]->speechBufferSize, 22050, SoundMixer::FLAG_16BITS);
				}
			}

			if (frameCounter == text[textCounter]->endFrame) {
				CloseTextObject(text[textCounter]);
				EraseBackBuffer();
				textCounter++;
			}

			frameCounter++;

			ServiceWindows();

			_keyboardEvent ke;

			if (ReadKey(&ke) == RD_OK && ke.keycode == 27) {
				g_sword2->_mixer->stopHandle(handle);
				skipCutscene = true;
				break;
			}

			// Simulate ~12 frames per second. I don't know what
			// frame rate the original movies had, or even if it
			// was constant, but this seems to work reasonably.

			g_system->delay_msecs(90);
		}

		CloseTextObject(text[textCounter]);

		EraseBackBuffer();
		SetNeedRedraw();

		// HACK: Remove the instructions created above
		Common::Rect r;

		memset(lpBackBuffer, 0, screenWide * MENUDEEP);
		r.left = r.top = 0;
		r.right = screenWide;
		r.bottom = MENUDEEP;
		UploadRect(&r);

		// FIXME: For now, only play the lead-out music for cutscenes
		// that have subtitles.

		if (!skipCutscene)
			g_sound->playLeadOut(musicOut);

		BS2_SetPalette(0, 256, oldPal, RDPAL_INSTANT);
	}

	// Lead-in and lead-out music are, as far as I can tell, only used for
	// the animated cut-scenes, so this seems like a good place to close
	// both of them.

	g_sound->closeFx(-1);
	g_sound->closeFx(-2);

	return RD_OK;
}

} // End of namespace Sword2