aboutsummaryrefslogtreecommitdiff
path: root/engines/sherlock/graphics.cpp
blob: 8c096e3c729c8f8fc8c993cd81a0464c7ca2d500 (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
/* 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.
 *
 */

#include "sherlock/graphics.h"
#include "sherlock/sherlock.h"
#include "common/system.h"
#include "graphics/palette.h"

namespace Sherlock {

Surface::Surface(uint16 width, uint16 height): _freePixels(true) {
	create(width, height, Graphics::PixelFormat::createFormatCLUT8());
}

Surface::Surface(Surface &src, const Common::Rect &r) : _freePixels(false) {
	setPixels(src.getBasePtr(r.left, r.top));
	w = r.width();
	h = r.height();
	pitch = src.pitch;
	format = Graphics::PixelFormat::createFormatCLUT8();
}


Surface::~Surface() {
	if (_freePixels)
		free();
}

/**
 * Copy a surface into this one
 */
void Surface::blitFrom(const Graphics::Surface &src) {
	blitFrom(src, Common::Point(0, 0));
}

/**
 * Draws a surface at a given position within this surface
 */
void Surface::blitFrom(const Graphics::Surface &src, const Common::Point &pt) {
	Common::Rect drawRect(0, 0, src.w, src.h);
	Common::Point destPt = pt;
	
	if (destPt.x < 0) {
		drawRect.left += -destPt.x;
		destPt.x = 0;
	}
	if (destPt.y < 0) {
		drawRect.top += -destPt.y;
		destPt.y = 0;
	}
	int right = destPt.x + src.w;
	if (right > this->w) {
		drawRect.right -= (right - this->w);
	}
	int bottom = destPt.y + src.h;
	if (bottom > this->h) {
		drawRect.bottom -= (bottom - this->h);
	}

	if (drawRect.isValidRect())
		blitFrom(src, destPt, drawRect);
}

/**
 * Draws a sub-section of a surface at a given position within this surface
 */
void Surface::blitFrom(const Graphics::Surface &src, const Common::Point &pt,
		const Common::Rect &srcBounds) {
	addDirtyRect(Common::Rect(pt.x, pt.y, pt.x + srcBounds.width(),
		pt.y + srcBounds.height()));
	copyRectToSurface(src, pt.x, pt.y, srcBounds);
}

/**
* Draws a surface at a given position within this surface with transparency
*/
void Surface::transBlitFrom(const Graphics::Surface &src, const Common::Point &pt,
		bool flipped, int overrideColor) {
	Common::Rect drawRect(0, 0, src.w, src.h);
	Common::Point destPt = pt;

	if (destPt.x < 0) {
		drawRect.left += -destPt.x;
		destPt.x = 0;
	}
	if (destPt.y < 0) {
		drawRect.top += -destPt.y;
		destPt.y = 0;
	}
	int right = destPt.x + src.w;
	if (right > this->w) {
		drawRect.right -= (right - this->w);
	}
	int bottom = destPt.y + src.h;
	if (bottom > this->h) {
		drawRect.bottom -= (bottom - this->h);
	}

	if (!drawRect.isValidRect())
		return;

	if (flipped)
		drawRect = Common::Rect(src.w - drawRect.right, src.h - drawRect.bottom,
			src.w - drawRect.left, src.h - drawRect.top);

	addDirtyRect(Common::Rect(destPt.x, destPt.y, destPt.x + drawRect.width(),
		destPt.y + drawRect.height()));

	// Draw loop
	const int TRANSPARENCY = 0xFF;
	for (int yp = 0; yp < drawRect.height(); ++yp) {
		const byte *srcP = (const byte *)src.getBasePtr(
			flipped ? drawRect.right : drawRect.left, drawRect.top + yp);
		byte *destP = (byte *)getBasePtr(destPt.x, destPt.y + yp);

		for (int xp = 0; xp < drawRect.width(); ++xp, ++destP) {
			if (*srcP != TRANSPARENCY)
				*destP = overrideColor ? overrideColor : *srcP;

			srcP = flipped ? srcP - 1 : srcP + 1;
		}
	}
}

/**
 * Fill a given area of the surface with a given color
 */
void Surface::fillRect(int x1, int y1, int x2, int y2, byte color) {
    Graphics::Surface::fillRect(Common::Rect(x1, y1, x2, y2), color);
}

/**
 * Return a sub-area of the surface as a new surface object. The surfaces
 * are shared in common, so changes in the sub-surface affects the original.
 */
Surface Surface::getSubArea(const Common::Rect &r) {
	return Surface(*this, r);
}

} // End of namespace Sherlock