aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/driver_vga.cpp
blob: e9830cf1973c5ef5710f43075cbe4204a31daaa8 (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
/* 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/endian.h"
#include "graphics/primitives.h"

#include "gob/driver_vga.h"

namespace Gob {

static void plotPixel(int x, int y, int color, void *data) {
	SurfaceDesc *dest = (SurfaceDesc *)data;

	if ((x >= 0) && (x < dest->getWidth()) &&
	    (y >= 0) && (y < dest->getHeight()))
		dest->getVidMem()[(y * dest->getWidth()) + x] = color;
}

void VGAVideoDriver::putPixel(int16 x, int16 y, byte color, SurfaceDesc *dest) {
	if ((x >= 0) && (x < dest->getWidth()) &&
	    (y >= 0) && (y < dest->getHeight()))
		dest->getVidMem()[(y * dest->getWidth()) + x] = color;
}

void VGAVideoDriver::drawLine(SurfaceDesc *dest, int16 x0, int16 y0, int16 x1,
		int16 y1, byte color) {

	Graphics::drawLine(x0, y0, x1, y1, color, &plotPixel, dest);
}

void VGAVideoDriver::fillRect(SurfaceDesc *dest, int16 left, int16 top,
		int16 right, int16 bottom, byte color) {

	if ((left >= dest->getWidth()) || (right >= dest->getWidth()) ||
	    (top >= dest->getHeight()) || (bottom >= dest->getHeight()))
		return;

	byte *pos = dest->getVidMem() + (top * dest->getWidth()) + left;
	int16 width = (right - left) + 1;
	int16 height = (bottom - top) + 1;

	while (height--) {
		for (int16 i = 0; i < width; ++i)
			pos[i] = color;

		pos += dest->getWidth();
	}
}

void VGAVideoDriver::drawLetter(unsigned char item, int16 x, int16 y,
		Video::FontDesc *fontDesc, byte color1, byte color2,
		byte transp, SurfaceDesc *dest) {
	byte *src, *dst;
	uint16 data;

	src = fontDesc->dataPtr +
		(item - fontDesc->startItem) * (fontDesc->itemSize & 0xFF);
	dst = dest->getVidMem() + x + dest->getWidth() * y;

	int nWidth = fontDesc->itemWidth;

	if (nWidth & 7)
		nWidth = (nWidth & 0xF8) + 8;

	nWidth >>= 3;

	for (int i = 0; i < fontDesc->itemHeight; i++) {
		int width = fontDesc->itemWidth;

		for (int k = 0; k < nWidth; k++) {
	
			data = *src++;
			for (int j = 0; j < MIN(8, width); j++) {
				if (data & 0x80)
					*dst = color2;
				else if (color1 == 0)
					*dst = transp;

				dst++;
				data <<= 1;
			}

			width -= 8;

		}

		dst += dest->getWidth() - fontDesc->itemWidth;
	}
}

void VGAVideoDriver::drawSprite(SurfaceDesc *source, SurfaceDesc *dest,
		int16 left, int16 top, int16 right, int16 bottom,
		int16 x, int16 y, int16 transp) {

	if ((x >= dest->getWidth()) || (x < 0) ||
	    (y >= dest->getHeight()) || (y < 0))
		return;

	int16 width = MIN((right - left) + 1, (int) dest->getWidth());
	int16 height = MIN((bottom - top) + 1, (int) dest->getHeight());

	if ((width < 1) || (height < 1))
		return;

	const byte *srcPos = source->getVidMem() + (top * source->getWidth()) + left;
	byte *destPos = dest->getVidMem() + (y * dest->getWidth()) + x;

	uint32 size = width * height;
	
	if (transp) {

		while (height--) {
			for (int16 i = 0; i < width; ++i) {
				if (srcPos[i])
					destPos[i] = srcPos[i];
			}

			srcPos += source->getWidth();
			destPos += dest->getWidth();
		}

	} else if (((srcPos  >= destPos) && (srcPos  <= (destPos + size))) ||
	           ((destPos >= srcPos)  && (destPos <= (srcPos  + size)))) {

		while (height--) {
			memmove(destPos, srcPos, width);

			srcPos += source->getWidth();
			destPos += dest->getWidth();
		}

	} else {

		while (height--) {
			memcpy(destPos, srcPos, width);

			srcPos += source->getWidth();
			destPos += dest->getWidth();
		}

	}
}

void VGAVideoDriver::drawPackedSprite(byte *sprBuf, int16 width, int16 height,
		int16 x, int16 y, byte transp, SurfaceDesc *dest) {
	int destRight = x + width;
	int destBottom = y + height;

	byte *dst = dest->getVidMem() + x + dest->getWidth() * y;

	int curx = x;
	int cury = y;

	while (1) {
		uint8 val = *sprBuf++;
		unsigned int repeat = val & 7;
		val &= 0xF8;

		if (!(val & 8)) {
			repeat <<= 8;
			repeat |= *sprBuf++;
		}
		repeat++;
		val >>= 4;

		for (unsigned int i = 0; i < repeat; ++i) {
			if (curx < dest->getWidth() && cury < dest->getHeight())
				if (!transp || val)
					*dst = val;

			dst++;
			curx++;
			if (curx == destRight) {
				dst += dest->getWidth() + x - curx;
				curx = x;
				cury++;
				if (cury == destBottom)
					return;
			}
		}

	}

}

}