1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "glk/window_graphics.h"
24 #include "glk/conf.h"
25 #include "glk/glk.h"
26 #include "glk/screen.h"
27 
28 namespace Glk {
29 
GraphicsWindow(Windows * windows,uint rock)30 GraphicsWindow::GraphicsWindow(Windows *windows, uint rock) : Window(windows, rock),
31 	_w(0), _h(0), _dirty(false), _surface(nullptr) {
32 	_type = wintype_Graphics;
33 	_bgnd = _bgColor;
34 }
35 
~GraphicsWindow()36 GraphicsWindow::~GraphicsWindow() {
37 	delete _surface;
38 }
39 
rearrange(const Rect & box)40 void GraphicsWindow::rearrange(const Rect &box) {
41 	int newwid, newhgt;
42 	int bothwid, bothhgt;
43 	Graphics::ManagedSurface *newSurface;
44 
45 	_bbox = box;
46 
47 	newwid = box.width();
48 	newhgt = box.height();
49 
50 	if (newwid <= 0 || newhgt <= 0) {
51 		_w = 0;
52 		_h = 0;
53 		delete _surface;
54 		_surface = nullptr;
55 		return;
56 	}
57 
58 	bothwid = _w;
59 	if (newwid < bothwid)
60 		bothwid = newwid;
61 	bothhgt = _h;
62 	if (newhgt < bothhgt)
63 		bothhgt = newhgt;
64 
65 	// Create it
66 	Graphics::PixelFormat pixelFormat = g_system->getScreenFormat();
67 	newSurface = new Graphics::ManagedSurface(newwid, newhgt, pixelFormat);
68 	newSurface->clear(_bgnd);
69 
70 	// If the new surface is equal or bigger than the old one, copy it over
71 	if (_surface && bothwid && bothhgt)
72 		newSurface->blitFrom(*_surface);
73 
74 	delete _surface;
75 	_surface = newSurface;
76 	_w = newwid;
77 	_h = newhgt;
78 
79 	touch();
80 }
81 
touch()82 void GraphicsWindow::touch() {
83 	_dirty = true;
84 	_windows->repaint(_bbox);
85 }
86 
redraw()87 void GraphicsWindow::redraw() {
88 	Screen &screen = *g_vm->_screen;
89 	Window::redraw();
90 
91 	if (_dirty || Windows::_forceRedraw) {
92 		_dirty = 0;
93 
94 		if (_surface)
95 			screen.blitFrom(*_surface, Point(_bbox.left, _bbox.top));
96 	}
97 }
98 
drawPicture(uint image,int xpos,int ypos,int scale,uint imagewidth,uint imageheight)99 uint GraphicsWindow::drawPicture(uint image, int xpos, int ypos, int scale,
100                                    uint imagewidth, uint imageheight) {
101 	Picture *pic = g_vm->_pictures->load(image);
102 	uint hyperlink = _attr.hyper;
103 
104 	if (!pic)
105 		return false;
106 
107 	if (!_imageLoaded) {
108 		g_vm->_pictures->increment();
109 		_imageLoaded = true;
110 	}
111 
112 	if (!scale) {
113 		imagewidth = pic->w;
114 		imageheight = pic->h;
115 	}
116 
117 	drawPicture(pic, xpos, ypos, imagewidth, imageheight, hyperlink);
118 	touch();
119 
120 	return true;
121 }
122 
eraseRect(bool whole,const Rect & box)123 void GraphicsWindow::eraseRect(bool whole, const Rect &box) {
124 	int x0 = box.left, y0 = box.top, x1 = box.right, y1 = box.bottom;
125 	int hx0, hx1, hy0, hy1;
126 
127 	if (whole) {
128 		x0 = 0;
129 		y0 = 0;
130 		x1 = _w;
131 		y1 = _h;
132 	}
133 
134 	if (x0 < 0) x0 = 0;
135 	if (y0 < 0) y0 = 0;
136 	if (x1 < 0) x1 = 0;
137 	if (y1 < 0) y1 = 0;
138 	if ((uint)x0 >= _w) x0 = _w;
139 	if ((uint)y0 >= _h) y0 = _h;
140 	if ((uint)x1 >= _w) x1 = _w;
141 	if ((uint)y1 >= _h) y1 = _h;
142 
143 	hx0 = _bbox.left + x0;
144 	hx1 = _bbox.left + x1;
145 	hy0 = _bbox.top + y0;
146 	hy1 = _bbox.top + y1;
147 
148 	// zero out hyperlinks for these coordinates
149 	g_vm->_selection->putHyperlink(0, hx0, hy0, hx1, hy1);
150 
151 	_surface->fillRect(Rect(x0, y0, x1, y1), _bgnd);
152 	touch();
153 }
154 
fillRect(uint color,const Rect & box)155 void GraphicsWindow::fillRect(uint color, const Rect &box) {
156 	int x0 = box.left, y0 = box.top, x1 = box.right, y1 = box.bottom;
157 	int hx0, hx1, hy0, hy1;
158 
159 	if (x0 < 0) x0 = 0;
160 	if (y0 < 0) y0 = 0;
161 	if (x1 < 0) x1 = 0;
162 	if (y1 < 0) y1 = 0;
163 	if ((uint)x0 > _w) x0 = _w;
164 	if ((uint)y0 > _h) y0 = _h;
165 	if ((uint)x1 > _w) x1 = _w;
166 	if ((uint)y1 > _h) y1 = _h;
167 
168 	hx0 = _bbox.left + x0;
169 	hx1 = _bbox.left + x1;
170 	hy0 = _bbox.top + y0;
171 	hy1 = _bbox.top + y1;
172 
173 	// zero out hyperlinks for these coordinates
174 	g_vm->_selection->putHyperlink(0, hx0, hy0, hx1, hy1);
175 
176 	_surface->fillRect(Rect(x0, y0, x1, y1), color);
177 	touch();
178 }
179 
drawPicture(Picture * src,int x0,int y0,int width,int height,uint linkval)180 void GraphicsWindow::drawPicture(Picture *src,  int x0, int y0, int width, int height, uint linkval) {
181 	int dx1, dy1, x1, y1, sx0, sy0, sx1, sy1;
182 	int hx0, hx1, hy0, hy1;
183 	int w, h;
184 
185 	if (width != src->w || height != src->h) {
186 		src = g_vm->_pictures->scale(src, width, height);
187 		if (!src)
188 			return;
189 	}
190 
191 	sx0 = 0;
192 	sy0 = 0;
193 	sx1 = src->w;
194 	sy1 = src->h;
195 	dx1 = _w;
196 	dy1 = _h;
197 
198 	x1 = x0 + src->w;
199 	y1 = y0 + src->h;
200 
201 	if (x1 <= 0 || x0 >= dx1) return;
202 	if (y1 <= 0 || y0 >= dy1) return;
203 	if (x0 < 0) {
204 		sx0 -= x0;
205 		x0 = 0;
206 	}
207 	if (y0 < 0) {
208 		sy0 -= y0;
209 		y0 = 0;
210 	}
211 	if (x1 > dx1) {
212 		sx1 += dx1 - x1;
213 		x1 = dx1;
214 	}
215 	if (y1 > dy1) {
216 		sy1 += dy1 - y1;
217 		y1 = dy1;
218 	}
219 
220 	hx0 = _bbox.left + x0;
221 	hx1 = _bbox.left + x1;
222 	hy0 = _bbox.top + y0;
223 	hy1 = _bbox.top + y1;
224 
225 	// zero out or set hyperlink for these coordinates
226 	g_vm->_selection->putHyperlink(linkval, hx0, hy0, hx1, hy1);
227 
228 	w = sx1 - sx0;
229 	h = sy1 - sy0;
230 
231 	_surface->transBlitFrom(*src, Rect(sx0, sy0, sx0 + w, sy0 + h), Point(x0, y0), src->getTransparentColor());
232 }
233 
getSize(uint * width,uint * height) const234 void GraphicsWindow::getSize(uint *width, uint *height) const {
235 	*width = _bbox.width();
236 	*height = _bbox.height();
237 }
238 
setBackgroundColor(uint color)239 void GraphicsWindow::setBackgroundColor(uint color) {
240 	_bgnd = color;
241 }
242 
click(const Point & newPos)243 void GraphicsWindow::click(const Point &newPos) {
244 	Point diff = newPos - Point(_bbox.left, _bbox.top);
245 
246 	if (_mouseRequest) {
247 		g_vm->_events->store(evtype_MouseInput, this, diff.x, diff.y);
248 		_mouseRequest = false;
249 		if (g_conf->_safeClicks)
250 			g_vm->_events->_forceClick = true;
251 	}
252 
253 	if (_hyperRequest) {
254 		uint linkval = g_vm->_selection->getHyperlink(newPos);
255 		if (linkval) {
256 			g_vm->_events->store(evtype_Hyperlink, this, linkval, 0);
257 			_hyperRequest = false;
258 			if (g_conf->_safeClicks)
259 				g_vm->_events->_forceClick = 1;
260 		}
261 	}
262 }
263 
264 } // End of namespace Glk
265