aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/gfx/gfx_driver.cpp
blob: 91c5fadc23ee341eb4e08741ae31b93c212fe51c (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
/* 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.
 *
 * 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/scummsys.h"
#include "common/system.h"
#include "graphics/cursorman.h"
#include "graphics/primitives.h"
#include "graphics/surface.h"

#include "sci/sci.h"
#include "sci/gfx/gfx_driver.h"
#include "sci/gfx/gfx_tools.h"


namespace Sci {

GfxDriver::GfxDriver(int xfact, int yfact, Graphics::PixelFormat format) {
	int i;

	_mode = gfx_new_mode(xfact, yfact, format, format.bytesPerPixel == 1 ? new Palette(256) : 0, 0);
	_mode->xsize = xfact * 320;
	_mode->ysize = yfact * 200;

	for (i = 0; i < 2; i++) {
		_priority[i] = gfx_pixmap_alloc_index_data(gfx_new_pixmap(_mode->xsize, _mode->ysize, GFX_RESID_NONE, -i, -777));
		if (!_priority[i]) {
			error("Out of memory: Could not allocate priority maps! (%dx%d)\n", _mode->xsize, _mode->ysize);
		}
	}
	// create the visual buffers
	for (i = 0; i < 2; i++) {
		_visual[i] = NULL;
		_visual[i] = new byte[_mode->xsize * _mode->ysize];
		if (!_visual[i]) {
			error("Out of memory: Could not allocate visual buffers! (%dx%d)\n", _mode->xsize, _mode->ysize);
		}
		memset(_visual[i], 0, _mode->xsize * _mode->ysize);
	}

	if (_mode->palette)
		_mode->palette->name = "global";
}

GfxDriver::~GfxDriver() {
	int i;
	for (i = 0; i < 2; i++) {
		gfx_free_pixmap(_priority[i]);
		_priority[i] = NULL;
	}

	for (i = 0; i < 2; i++) {
		delete[] _visual[i];
		_visual[i] = NULL;
	}
}


// Drawing operations

static void drawProc(int x, int y, int c, void *data) {
	GfxDriver *drv = (GfxDriver *)data;
	byte *p = drv->getVisual0();
	uint8 col = c;
	memcpy(p + (y * 320* drv->getMode()->scaleFactor + x), &col, 1);
}

void GfxDriver::drawLine(Common::Point start, Common::Point end, gfx_color_t color, 
						gfx_line_mode_t line_mode, gfx_line_style_t line_style) {
	uint32 scolor = color.visual.parent_index;
	int scaleFactor = (line_mode == GFX_LINE_MODE_FINE)? 1: _mode->scaleFactor;
	int xsize = _mode->xsize;
	int ysize = _mode->ysize;

	if (color.mask & GFX_MASK_VISUAL) {
		Common::Point nstart, nend;

		for (int xc = 0; xc < scaleFactor; xc++) {
			for (int yc = 0; yc < scaleFactor; yc++) {

				nstart.x = CLIP<int16>(start.x + xc, 0, xsize);
				nstart.y = CLIP<int16>(start.y + yc, 0, ysize);
				nend.x = CLIP<int16>(end.x + xc, 0, xsize - 1);
				nend.y = CLIP<int16>(end.y + yc, 0, ysize - 1);

				Graphics::drawLine(nstart.x, nstart.y, nend.x, nend.y, scolor, drawProc, this);

				if (color.mask & GFX_MASK_PRIORITY) {
					gfx_draw_line_pixmap_i(_priority[0], nstart, nend, color.priority);
				}
			}
		}
	}
}

void GfxDriver::drawFilledRect(rect_t rect, gfx_color_t color1, gfx_color_t color2,
	gfx_rectangle_fill_t shade_mode) {
	if (color1.mask & GFX_MASK_VISUAL) {
		for (int i = rect.y; i < rect.y + rect.height; i++) {
			memset(_visual[0] + (i * _mode->xsize + rect.x),
			       color1.visual.parent_index, rect.width);
		}
	}

	if (color1.mask & GFX_MASK_PRIORITY)
		gfx_draw_box_pixmap_i(_priority[0], rect, color1.priority);
}

// Pixmap operations

void GfxDriver::drawPixmap(gfx_pixmap_t *pxm, int priority, rect_t src, rect_t dest, gfx_buffer_t buffer) {
	int bufnr = (buffer == GFX_BUFFER_STATIC) ? 1 : 0;

	if (dest.width != src.width || dest.height != src.height) {
		warning("Attempt to scale pixmap (%dx%d)->(%dx%d): Not supported\n", src.width, src.height, dest.width, dest.height);
		return;
	}

	gfx_crossblit_pixmap(_mode, pxm, priority, src, dest, _visual[bufnr],
	                     _mode->xsize,
	                     _priority[bufnr]->index_data,
	                     _priority[bufnr]->index_width, 1, 0);
}

void GfxDriver::grabPixmap(rect_t src, gfx_pixmap_t *pxm, gfx_map_mask_t map) {
	if (src.x < 0 || src.y < 0)
		error("Attempt to grab pixmap from invalid coordinates (%d,%d)", src.x, src.y);

	if (!pxm->data)
		error("Attempt to grab pixmap to unallocated memory");

	switch (map) {

	case GFX_MASK_VISUAL:
		pxm->width = src.width;
		pxm->height = src.height;
		for (int i = 0; i < src.height; i++) {
			memcpy(pxm->data + i * src.width,
			       _visual[0] + ((i + src.y) * _mode->xsize + src.x),
			       src.width);
		}
		break;

	case GFX_MASK_PRIORITY:
		warning("FIXME: priority map grab not implemented yet");
		break;

	default:
		error("Attempt to grab pixmap from invalid map 0x%02x", map);
	}
}

// Buffer operations

void GfxDriver::update(rect_t src, Common::Point dest, gfx_buffer_t buffer) {
	//TODO

	/*
	if (src.x != dest.x || src.y != dest.y) {
		printf("Updating %d (%d,%d)(%dx%d) to (%d,%d) on %d\n", buffer, src.x, src.y, src.width, src.height, dest.x, dest.y, data_dest);
	} else {
		printf("Updating %d (%d,%d)(%dx%d) to %d\n", buffer, src.x, src.y, src.width, src.height, data_dest);
	}
	*/

	switch (buffer) {
	case GFX_BUFFER_BACK:
		for (int i = 0; i < src.height; i++) {
			memcpy(_visual[0] + ( (dest.y + i) * _mode->xsize + dest.x),
			       _visual[1] + ( (src.y + i) * _mode->xsize + src.x), src.width );
		}

		if ((src.x == dest.x) && (src.y == dest.y))
			gfx_copy_pixmap_box_i(_priority[0], _priority[1], src);
		break;
	case GFX_BUFFER_FRONT: {
		g_system->copyRectToScreen(_visual[0] + (src.x + src.y * _mode->xsize), _mode->xsize, dest.x, dest.y, src.width, src.height);
		g_system->updateScreen();
		break;
	}
	default:
		error("Invalid buffer %d in update", buffer);
	}
}

void GfxDriver::setStaticBuffer(gfx_pixmap_t *pic, gfx_pixmap_t *priority) {
	memcpy(_visual[1], pic->data, _mode->xsize * _mode->ysize);
	gfx_copy_pixmap_box_i(_priority[1], priority, gfx_rect(0, 0, _mode->xsize, _mode->ysize));
}

// Mouse pointer operations

// Scale cursor and map its colors to the global palette
byte *GfxDriver::createCursor(gfx_pixmap_t *pointer) {
	int linewidth = pointer->width;
	int lines = pointer->height;
	byte *data = new byte[linewidth*lines];
	byte *linebase = data, *pos;
	byte *src = pointer->index_data;

	for (int yc = 0; yc < pointer->index_height; yc++) {
		pos = linebase;

		for (int xc = 0; xc < pointer->index_width; xc++) {
			byte color = *src;
			// FIXME: The palette size check is a workaround for cursors using non-palette colour GFX_CURSOR_TRANSPARENT
			// Note that some cursors don't have a palette in SQ5
			if (pointer->palette && color < pointer->palette->size())
				color = pointer->palette->getColor(color).parent_index;
			for (int scalectr = 0; scalectr < _mode->scaleFactor; scalectr++) {
				*pos++ = color;
			}
			src++;
		}
		for (int scalectr = 1; scalectr < _mode->scaleFactor; scalectr++)
			memcpy(linebase + linewidth * scalectr, linebase, linewidth);
		linebase += linewidth * _mode->scaleFactor;
	}
	return data;
}


void GfxDriver::setPointer(gfx_pixmap_t *pointer, Common::Point *hotspot) {
	if ((pointer == NULL) || (hotspot == NULL)) {
		CursorMan.showMouse(false);
	} else {
		byte *cursorData = createCursor(pointer);

		// FIXME: The palette size check is a workaround for cursors using non-palette color GFX_CURSOR_TRANSPARENT
		// Note that some cursors don't have a palette (e.g. in SQ5 and QFG3)
		byte color_key = GFX_CURSOR_TRANSPARENT;
		if ((pointer->color_key != GFX_PIXMAP_COLOR_KEY_NONE) && (pointer->palette && (unsigned int)pointer->color_key < pointer->palette->size()))
			color_key = pointer->palette->getColor(pointer->color_key).parent_index;
		// Some cursors don't have a palette, so we set the color key directly
		if (!pointer->palette)
			color_key = pointer->color_key;

		CursorMan.replaceCursor(cursorData, pointer->width, pointer->height, hotspot->x, hotspot->y, color_key);
		CursorMan.showMouse(true);

		delete[] cursorData;
		cursorData = 0;
	}
}

} // End of namespace Sci