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 "ags/lib/allegro/gfx.h"
24 #include "ags/lib/allegro/color.h"
25 #include "ags/lib/allegro/flood.h"
26 #include "ags/lib/allegro/rotate.h"
27 #include "ags/ags.h"
28 #include "ags/globals.h"
29 #include "common/textconsole.h"
30 #include "common/util.h"
31 #include "graphics/screen.h"
32 
33 namespace AGS3 {
34 
35 int color_conversion;
36 
37 /*-------------------------------------------------------------------*/
38 
set_color_conversion(int mode)39 void set_color_conversion(int mode) {
40 	color_conversion = mode;
41 }
42 
get_color_conversion()43 int get_color_conversion() {
44 	return color_conversion;
45 }
46 
set_gfx_mode(int card,int w,int h,int depth)47 int set_gfx_mode(int card, int w, int h, int depth) {
48 	// Graphics shutdown can be ignored
49 	if (card != -1) {
50 		assert(card == SCUMMVM_ID);
51 		::AGS::g_vm->setGraphicsMode(w, h, depth);
52 	}
53 	return 0;
54 }
55 
set_clip_rect(BITMAP * bitmap,int x1,int y1,int x2,int y2)56 void set_clip_rect(BITMAP *bitmap, int x1, int y1, int x2, int y2) {
57 	// The rect passed to the function in inclusive-inclusive, but
58 	// internally the clipping rect in BITMAP is inclusive-exclusive.
59 	bitmap->cl = CLIP(x1, 0, (int)bitmap->w - 1);
60 	bitmap->ct = CLIP(y1, 0, (int)bitmap->h - 1);
61 	bitmap->cr = CLIP(x2 + 1, 0, (int)bitmap->w);
62 	bitmap->cb = CLIP(y2 + 1, 0, (int)bitmap->h);
63 }
64 
get_clip_rect(BITMAP * bitmap,int * x1,int * y1,int * x2,int * y2)65 void get_clip_rect(BITMAP *bitmap, int *x1, int *y1, int *x2, int *y2) {
66 	if (x1)
67 		*x1 = bitmap->cl;
68 	if (y1)
69 		*y1 = bitmap->ct;
70 	if (x2)
71 		*x2 = bitmap->cr - 1;
72 	if (y2)
73 		*y2 = bitmap->cb - 1;
74 }
75 
acquire_bitmap(BITMAP * bitmap)76 void acquire_bitmap(BITMAP *bitmap) {
77 	// No implementation needed
78 }
79 
release_bitmap(BITMAP * bitmap)80 void release_bitmap(BITMAP *bitmap) {
81 	// No implementation needed
82 }
83 
clear_to_color(BITMAP * bitmap,int color)84 void clear_to_color(BITMAP *bitmap, int color) {
85 	Graphics::ManagedSurface &surf = **bitmap;
86 
87 	surf.clear(color);
88 }
89 
bitmap_color_depth(BITMAP * bmp)90 int bitmap_color_depth(BITMAP *bmp) {
91 	Graphics::ManagedSurface &surf = **bmp;
92 
93 	return (surf.format.bytesPerPixel == 1) ? 8 : surf.format.bpp();
94 }
95 
bitmap_mask_color(BITMAP * bmp)96 int bitmap_mask_color(BITMAP *bmp) {
97 	// For paletted sprites this is 0.
98 	// For other color depths this is bright pink (RGB 255, 0, 255).
99 	// The alpha chanel should be 0.
100 	//if (bmp-format.bytesPerPixel == 1)
101 	//  return 0;
102 	//return bmp->format.AGRBToColor(0, 255, 0, 255);
103 	return bmp->getTransparentColor();
104 }
105 
blit(const BITMAP * src,BITMAP * dest,int src_x,int src_y,int dst_x,int dst_y,int width,int height)106 void blit(const BITMAP *src, BITMAP *dest, int src_x, int src_y, int dst_x, int dst_y, int width, int height) {
107 	dest->draw(src, Common::Rect(src_x, src_y, src_x + width, src_y + height),
108 	           dst_x, dst_y, false, false, false, -1);
109 }
110 
stretch_blit(const BITMAP * src,BITMAP * dest,int source_x,int source_y,int source_width,int source_height,int dest_x,int dest_y,int dest_width,int dest_height)111 void stretch_blit(const BITMAP *src, BITMAP *dest,
112                   int source_x, int source_y, int source_width, int source_height,
113                   int dest_x, int dest_y, int dest_width, int dest_height) {
114 	dest->stretchDraw(src,
115 	                  Common::Rect(source_x, source_y, source_x + source_width, source_y + source_height),
116 	                  Common::Rect(dest_x, dest_y, dest_x + dest_width, dest_y + dest_height),
117 	                  false, -1);
118 }
119 
masked_blit(const BITMAP * src,BITMAP * dest,int src_x,int src_y,int dst_x,int dst_y,int width,int height)120 void masked_blit(const BITMAP *src, BITMAP *dest, int src_x, int src_y, int dst_x, int dst_y, int width, int height) {
121 	assert(src->format == dest->format);
122 
123 	dest->draw(src, Common::Rect(src_x, src_y, src_x + width, src_y + height),
124 	           dst_x, dst_y, false, false, true, -1);
125 }
126 
masked_stretch_blit(const BITMAP * src,BITMAP * dest,int source_x,int source_y,int source_width,int source_height,int dest_x,int dest_y,int dest_width,int dest_height)127 void masked_stretch_blit(const BITMAP *src, BITMAP *dest,
128                          int source_x, int source_y, int source_width, int source_height,
129                          int dest_x, int dest_y, int dest_width, int dest_height) {
130 	dest->stretchDraw(src,
131 	                  Common::Rect(source_x, source_y, source_x + source_width, source_y + source_height),
132 	                  Common::Rect(dest_x, dest_y, dest_x + dest_width, dest_y + dest_height),
133 	                  true, -1);
134 }
135 
draw_sprite(BITMAP * bmp,const BITMAP * sprite,int x,int y)136 void draw_sprite(BITMAP *bmp, const BITMAP *sprite, int x, int y) {
137 	bmp->draw(sprite, Common::Rect(0, 0, sprite->w, sprite->h),
138 	          x, y, false, false, true, -1);
139 }
140 
stretch_sprite(BITMAP * bmp,const BITMAP * sprite,int x,int y,int w,int h)141 void stretch_sprite(BITMAP *bmp, const BITMAP *sprite, int x, int y, int w, int h) {
142 	bmp->stretchDraw(sprite, Common::Rect(0, 0, sprite->w, sprite->h),
143 	                 Common::Rect(x, y, x + w, y + h),
144 	                 true, -1);
145 }
146 
draw_trans_sprite(BITMAP * bmp,const BITMAP * sprite,int x,int y)147 void draw_trans_sprite(BITMAP *bmp, const BITMAP *sprite, int x, int y) {
148 	bmp->draw(sprite, Common::Rect(0, 0, sprite->w, sprite->h),
149 	          x, y, false, false, true, _G(trans_blend_alpha));
150 }
151 
draw_lit_sprite(BITMAP * bmp,const BITMAP * sprite,int x,int y,int color)152 void draw_lit_sprite(BITMAP *bmp, const BITMAP *sprite, int x, int y, int color) {
153 	bmp->draw(sprite, Common::Rect(0, 0, sprite->w, sprite->h),
154 	          x, y, false, false, true, color,
155 	          _G(trans_blend_red), _G(trans_blend_green), _G(trans_blend_blue));
156 }
157 
draw_sprite_h_flip(BITMAP * bmp,const BITMAP * sprite,int x,int y)158 void draw_sprite_h_flip(BITMAP *bmp, const BITMAP *sprite, int x, int y) {
159 	bmp->draw(sprite, Common::Rect(0, 0, sprite->w, sprite->h),
160 	          x, y, true, false, true, -1);
161 }
162 
draw_sprite_v_flip(BITMAP * bmp,const BITMAP * sprite,int x,int y)163 void draw_sprite_v_flip(BITMAP *bmp, const BITMAP *sprite, int x, int y) {
164 	bmp->draw(sprite, Common::Rect(0, 0, sprite->w, sprite->h),
165 	          x, y, false, true, true, -1);
166 }
167 
draw_sprite_vh_flip(BITMAP * bmp,const BITMAP * sprite,int x,int y)168 void draw_sprite_vh_flip(BITMAP *bmp, const BITMAP *sprite, int x, int y) {
169 	bmp->draw(sprite, Common::Rect(0, 0, sprite->w, sprite->h),
170 	          x, y, true, true, true, -1);
171 }
172 
rotate_sprite(BITMAP * bmp,const BITMAP * sprite,int x,int y,fixed angle)173 void rotate_sprite(BITMAP *bmp, const BITMAP *sprite, int x, int y, fixed angle) {
174 	pivot_scaled_sprite(bmp, sprite, (x<<16) + (sprite->w * 0x10000) / 2, (y<<16) + (sprite->h * 0x10000) / 2, sprite->w << 15, sprite->h << 15, angle, 0x10000);
175 }
176 
pivot_sprite(BITMAP * bmp,const BITMAP * sprite,int x,int y,int cx,int cy,fixed angle)177 void pivot_sprite(BITMAP *bmp, const BITMAP *sprite, int x, int y, int cx, int cy, fixed angle) {
178 	pivot_scaled_sprite(bmp, sprite, x<<16, y<<16, cx<<16, cy<<16, angle, 0x10000);
179 }
180 
181 
is_screen_bitmap(BITMAP * bmp)182 bool is_screen_bitmap(BITMAP *bmp) {
183 	return dynamic_cast<Graphics::Screen *>(bmp) != nullptr;
184 }
185 
is_video_bitmap(BITMAP * bmp)186 bool is_video_bitmap(BITMAP *bmp) {
187 	return dynamic_cast<Graphics::Screen *>(bmp) != nullptr;
188 }
189 
is_planar_bitmap(BITMAP * bmp)190 bool is_planar_bitmap(BITMAP *bmp) {
191 	return false;
192 }
193 
is_linear_bitmap(BITMAP * bmp)194 bool is_linear_bitmap(BITMAP *bmp) {
195 	return true;
196 }
197 
bmp_select(BITMAP * bmp)198 void bmp_select(BITMAP *bmp) {
199 	// No implementation needed
200 }
201 
bmp_write_line(BITMAP * bmp,int line)202 byte *bmp_write_line(BITMAP *bmp, int line) {
203 	return bmp->line[line];
204 }
205 
bmp_unwrite_line(BITMAP * bmp)206 void bmp_unwrite_line(BITMAP *bmp) {
207 	// No implementation needed
208 }
209 
bmp_write8(byte * addr,int color)210 void bmp_write8(byte *addr, int color) {
211 	*addr = color;
212 }
213 
bmp_write15(byte * addr,int color)214 void bmp_write15(byte *addr, int color) {
215 	*((uint16 *)addr) = color;
216 }
217 
bmp_write16(byte * addr,int color)218 void bmp_write16(byte *addr, int color) {
219 	*((uint16 *)addr) = color;
220 }
221 
bmp_write24(byte * addr,int color)222 void bmp_write24(byte *addr, int color) {
223 	*addr = (color & 0xff);
224 	*(addr + 1) = ((color >> 8) & 0xff);
225 	*(addr + 2) = ((color >> 16) & 0xff);
226 }
227 
bmp_write32(byte * addr,int color)228 void bmp_write32(byte *addr, int color) {
229 	*((uint32 *)addr) = color;
230 }
231 
memory_putpixel(BITMAP * bmp,int x,int y,int color)232 void memory_putpixel(BITMAP *bmp, int x, int y, int color) {
233 	putpixel(bmp, x, y, color);
234 }
235 
putpixel(BITMAP * bmp,int x,int y,int color)236 void putpixel(BITMAP *bmp, int x, int y, int color) {
237 	Graphics::ManagedSurface &surf = **bmp;
238 	void *p = surf.getBasePtr(x, y);
239 
240 	switch (surf.format.bytesPerPixel) {
241 	case 1:
242 		*((uint8 *)p) = color;
243 		break;
244 	case 2:
245 		*((uint16 *)p) = color;
246 		break;
247 	case 4:
248 		*((uint32 *)p) = color;
249 		break;
250 	default:
251 		break;
252 	}
253 }
254 
_putpixel(BITMAP * bmp,int x,int y,int color)255 void _putpixel(BITMAP *bmp, int x, int y, int color) {
256 	Graphics::ManagedSurface &surf = **bmp;
257 	void *p = surf.getBasePtr(x, y);
258 	*((uint8 *)p) = color;
259 }
260 
_putpixel15(BITMAP * bmp,int x,int y,int color)261 void _putpixel15(BITMAP *bmp, int x, int y, int color) {
262 	error("Unsupported bpp");
263 }
264 
_putpixel16(BITMAP * bmp,int x,int y,int color)265 void _putpixel16(BITMAP *bmp, int x, int y, int color) {
266 	Graphics::ManagedSurface &surf = **bmp;
267 	void *p = surf.getBasePtr(x, y);
268 	*((uint16 *)p) = color;
269 }
270 
_putpixel24(BITMAP * bmp,int x,int y,int color)271 void _putpixel24(BITMAP *bmp, int x, int y, int color) {
272 	error("Unsupported bpp");
273 }
274 
_putpixel32(BITMAP * bmp,int x,int y,int color)275 void _putpixel32(BITMAP *bmp, int x, int y, int color) {
276 	Graphics::ManagedSurface &surf = **bmp;
277 	void *p = surf.getBasePtr(x, y);
278 	*((uint32 *)p) = color;
279 }
280 
getpixel(const BITMAP * bmp,int x,int y)281 int getpixel(const BITMAP *bmp, int x, int y) {
282 	Graphics::ManagedSurface &surf = **bmp;
283 
284 	// Allegro returns -1 if the pixel lies outside the bitmap
285 	if (x < 0 || y < 0 || x >= surf.w || y >= surf.h)
286 		return -1;
287 
288 	void *p = surf.getBasePtr(x, y);
289 
290 	switch (surf.format.bytesPerPixel) {
291 	case 1:
292 		return *((uint8 *)p);
293 	case 2:
294 		return *((uint16 *)p);
295 	case 4:
296 		return *((uint32 *)p);
297 	default:
298 		break;
299 	}
300 
301 	error("Unsupported bpp");
302 }
303 
_getpixel(const BITMAP * bmp,int x,int y)304 int _getpixel(const BITMAP *bmp, int x, int y) {
305 	Graphics::ManagedSurface &surf = **bmp;
306 	if (x < 0 || y < 0 || x >= surf.w || y >= surf.h)
307 		return -1;
308 	void *p = surf.getBasePtr(x, y);
309 	return *((uint8 *)p);
310 }
311 
_getpixel15(const BITMAP * bmp,int x,int y)312 int _getpixel15(const BITMAP *bmp, int x, int y) {
313 	error("Unsupported bpp");
314 }
315 
_getpixel16(const BITMAP * bmp,int x,int y)316 int _getpixel16(const BITMAP *bmp, int x, int y) {
317 	Graphics::ManagedSurface &surf = **bmp;
318 	if (x < 0 || y < 0 || x >= surf.w || y >= surf.h)
319 		return -1;
320 	void *p = surf.getBasePtr(x, y);
321 	return *((uint16 *)p);
322 }
323 
_getpixel24(const BITMAP * bmp,int x,int y)324 int _getpixel24(const BITMAP *bmp, int x, int y) {
325 	error("Unsupported bpp");
326 }
327 
_getpixel32(const BITMAP * bmp,int x,int y)328 int _getpixel32(const BITMAP *bmp, int x, int y) {
329 	Graphics::ManagedSurface &surf = **bmp;
330 	if (x < 0 || y < 0 || x >= surf.w || y >= surf.h)
331 		return -1;
332 	void *p = surf.getBasePtr(x, y);
333 	return *((uint32 *)p);
334 }
335 
line(BITMAP * bmp,int x1,int y1,int x2,int y2,int color)336 void line(BITMAP *bmp, int x1, int y1, int x2, int y2, int color) {
337 	Graphics::ManagedSurface &surf = **bmp;
338 	surf.drawLine(x1, y1, x2, y2, color);
339 }
340 
rect(BITMAP * bmp,int x1,int y1,int x2,int y2,int color)341 void rect(BITMAP *bmp, int x1, int y1, int x2, int y2, int color) {
342 	Graphics::ManagedSurface &surf = **bmp;
343 	if (x1 > x2)
344 		SWAP(x1, x2);
345 	if (y1 > y2)
346 		SWAP(y1, y2);
347 	surf.frameRect(Common::Rect(x1, y1, x2 + 1, y2 + 1), color);
348 }
349 
rectfill(BITMAP * bmp,int x1,int y1,int x2,int y2,int color)350 void rectfill(BITMAP *bmp, int x1, int y1, int x2, int y2, int color) {
351 	Graphics::ManagedSurface &surf = **bmp;
352 	if (x1 > x2)
353 		SWAP(x1, x2);
354 	if (y1 > y2)
355 		SWAP(y1, y2);
356 	surf.fillRect(Common::Rect(x1, y1, x2 + 1, y2 + 1), color);
357 }
358 
triangle(BITMAP * bmp,int x1,int y1,int x2,int y2,int x3,int y3,int color)359 void triangle(BITMAP *bmp, int x1, int y1, int x2, int y2, int x3, int y3, int color) {
360 	Graphics::ManagedSurface &surf = **bmp;
361 	surf.drawLine(x1, y1, x2, y2, color);
362 	surf.drawLine(x2, y2, x3, y3, color);
363 	surf.drawLine(x3, y3, x1, y1, color);
364 }
365 
circlefill(BITMAP * bmp,int x,int y,int radius,int color)366 void circlefill(BITMAP *bmp, int x, int y, int radius, int color) {
367 	bmp->circlefill(x, y, radius, color);
368 }
369 
clear_bitmap(BITMAP * bmp)370 void clear_bitmap(BITMAP *bmp) {
371 	bmp->clear();
372 }
373 
374 } // namespace AGS3
375