File: global_drawing_surface.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (300 lines) | stat: -rw-r--r-- 11,542 bytes parent folder | download | duplicates (2)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ags/shared/ac/common.h"
#include "ags/engine/ac/display.h"
#include "ags/engine/ac/draw.h"
#include "ags/engine/ac/game.h"
#include "ags/shared/ac/game_setup_struct.h"
#include "ags/engine/ac/game_state.h"
#include "ags/engine/ac/global_drawing_surface.h"
#include "ags/engine/ac/global_translation.h"
#include "ags/engine/ac/string.h"
#include "ags/engine/debugging/debug_log.h"
#include "ags/shared/font/fonts.h"
#include "ags/shared/game/room_struct.h"
#include "ags/shared/gui/gui_defines.h"
#include "ags/shared/ac/sprite_cache.h"
#include "ags/shared/gfx/gfx_def.h"
#include "ags/engine/gfx/gfx_util.h"

namespace AGS3 {

using namespace AGS::Shared;
using namespace AGS::Engine;

// Raw screen writing routines - similar to old CapturedStuff
#define RAW_START() _GP(play).raw_drawing_surface = _GP(thisroom).BgFrames[_GP(play).bg_frame].Graphic; _GP(play).raw_modified[_GP(play).bg_frame] = 1
#define RAW_END()
#define RAW_SURFACE() (_GP(play).raw_drawing_surface.get())

// RawSaveScreen: copy the current screen to a backup bitmap
void RawSaveScreen() {
	auto source = _GP(thisroom).BgFrames[_GP(play).bg_frame].Graphic;
	_G(raw_saved_screen).reset(BitmapHelper::CreateBitmapCopy(source.get()));
}
// RawRestoreScreen: copy backup bitmap back to screen; we
// deliberately don't free the Bitmap *cos they can multiple restore
// and it gets freed on room exit anyway
void RawRestoreScreen() {
	if (_G(raw_saved_screen) == nullptr) {
		debug_script_warn("RawRestoreScreen: unable to restore, since the screen hasn't been saved previously.");
		return;
	}
	auto deston = _GP(thisroom).BgFrames[_GP(play).bg_frame].Graphic;
	deston->Blit(_G(raw_saved_screen).get(), 0, 0, 0, 0, deston->GetWidth(), deston->GetHeight());
	invalidate_screen();
	mark_current_background_dirty();
}
// Restores the backup bitmap, but tints it to the specified level
void RawRestoreScreenTinted(int red, int green, int blue, int opacity) {
	if (_G(raw_saved_screen) == nullptr) {
		debug_script_warn("RawRestoreScreenTinted: unable to restore, since the screen hasn't been saved previously.");
		return;
	}
	if ((red < 0) || (green < 0) || (blue < 0) ||
	        (red > 255) || (green > 255) || (blue > 255) ||
	        (opacity < 1) || (opacity > 100))
		quit("!RawRestoreScreenTinted: invalid parameter. R,G,B must be 0-255, opacity 1-100");

	debug_script_log("RawRestoreTinted RGB(%d,%d,%d) %d%%", red, green, blue, opacity);

	PBitmap deston = _GP(thisroom).BgFrames[_GP(play).bg_frame].Graphic;
	tint_image(deston.get(), _G(raw_saved_screen).get(), red, green, blue, opacity);
	invalidate_screen();
	mark_current_background_dirty();
}

void RawDrawFrameTransparent(int frame, int translev) {
	if ((frame < 0) || ((size_t)frame >= _GP(thisroom).BgFrameCount) ||
	        (translev < 0) || (translev > 99))
		quit("!RawDrawFrameTransparent: invalid parameter (transparency must be 0-99, frame a valid BG frame)");

	PBitmap bg = _GP(thisroom).BgFrames[frame].Graphic;
	if (bg->GetColorDepth() <= 8)
		quit("!RawDrawFrameTransparent: 256-colour backgrounds not supported");

	if (frame == _GP(play).bg_frame)
		quit("!RawDrawFrameTransparent: cannot draw current background onto itself");

	RAW_START();
	if (translev == 0) {
		// just draw it over the top, no transparency
		RAW_SURFACE()->Blit(bg.get(), 0, 0, 0, 0, bg->GetWidth(), bg->GetHeight());
	} else {
		// Draw it transparently
		GfxUtil::DrawSpriteWithTransparency(RAW_SURFACE(), bg.get(), 0, 0,
		                                    GfxDef::Trans100ToAlpha255(translev));
	}
	invalidate_screen();
	mark_current_background_dirty();
	RAW_END();
}

void RawClear(int clr) {
	RAW_START();
	clr = RAW_SURFACE()->GetCompatibleColor(clr);
	RAW_SURFACE()->Clear(clr);
	invalidate_screen();
	mark_current_background_dirty();
}
void RawSetColor(int clr) {
	// set the colour at the appropriate depth for the background
	_GP(play).raw_color = MakeColor(clr);
}
void RawSetColorRGB(int red, int grn, int blu) {
	if ((red < 0) || (red > 255) || (grn < 0) || (grn > 255) ||
	        (blu < 0) || (blu > 255))
		quit("!RawSetColorRGB: colour values must be 0-255");

	_GP(play).raw_color = makecol_depth(_GP(thisroom).BgFrames[_GP(play).bg_frame].Graphic->GetColorDepth(), red, grn, blu);
}
void RawPrint(int xx, int yy, const char *text) {
	RAW_START();
	// don't use wtextcolor because it will do a 16->32 conversion
	color_t text_color = _GP(play).raw_color;
	if ((RAW_SURFACE()->GetColorDepth() <= 8) && (_GP(play).raw_color > 255)) {
		text_color = RAW_SURFACE()->GetCompatibleColor(1);
		debug_script_warn("RawPrint: Attempted to use hi-color on 256-col background");
	}
	data_to_game_coords(&xx, &yy);
	wouttext_outline(RAW_SURFACE(), xx, yy, _GP(play).normal_font, text_color, text);
	// we must invalidate the entire screen because these are room
	// co-ordinates, not screen co-ords which it works with
	invalidate_screen();
	mark_current_background_dirty();
	RAW_END();
}
void RawPrintMessageWrapped(int xx, int yy, int wid, int font, int msgm) {
	char displbuf[3000];
	const int linespacing = get_font_linespacing(font);
	data_to_game_coords(&xx, &yy);
	wid = data_to_game_coord(wid);

	get_message_text(msgm, displbuf);
	// it's probably too late but check anyway
	if (strlen(displbuf) > 2899)
		quit("!RawPrintMessageWrapped: message too long");
	if (break_up_text_into_lines(displbuf, _GP(Lines), wid, font) == 0)
		return;

	RAW_START();
	color_t text_color = _GP(play).raw_color;
	for (size_t i = 0; i < _GP(Lines).Count(); i++)
		wouttext_outline(RAW_SURFACE(), xx, yy + linespacing * i, font, text_color, _GP(Lines)[i].GetCStr());
	invalidate_screen();
	mark_current_background_dirty();
	RAW_END();
}

void RawDrawImageCore(int xx, int yy, int slot, int alpha) {
	if ((slot < 0) || (!_GP(spriteset).DoesSpriteExist(slot)))
		quit("!RawDrawImage: invalid sprite slot number specified");
	RAW_START();

	Bitmap *sprite = _GP(spriteset)[slot];
	if (sprite->GetColorDepth() != RAW_SURFACE()->GetColorDepth()) {
		debug_script_warn("RawDrawImage: Sprite %d colour depth %d-bit not same as background depth %d-bit", slot, sprite->GetColorDepth(), RAW_SURFACE()->GetColorDepth());
	}

	draw_sprite_slot_support_alpha(RAW_SURFACE(), false, xx, yy, slot, kBlendMode_Alpha, alpha);
	invalidate_screen();
	mark_current_background_dirty();
	RAW_END();
}

void RawDrawImage(int xx, int yy, int slot) {
	data_to_game_coords(&xx, &yy);
	RawDrawImageCore(xx, yy, slot);
}

void RawDrawImageTrans(int xx, int yy, int slot, int alpha) {
	data_to_game_coords(&xx, &yy);
	RawDrawImageCore(xx, yy, slot, alpha);
}

void RawDrawImageOffset(int xx, int yy, int slot) {
	// This function takes coordinates in real game coordinates as opposed to script coordinates
	defgame_to_finalgame_coords(xx, yy);
	RawDrawImageCore(xx, yy, slot);
}

void RawDrawImageTransparent(int xx, int yy, int slot, int legacy_transparency) {
	if ((legacy_transparency < 0) || (legacy_transparency > 100))
		quit("!RawDrawImageTransparent: invalid transparency setting");

	// WARNING: the previous versions of AGS actually had a bug:
	// although manual stated that RawDrawImageTransparent takes % of transparency
	// as an argument, that value was used improperly when setting up an Allegro's
	// trans_blender, which caused it to act about as % of opacity instead, but
	// with a twist.
	//
	// It was converted to 255-ranged "transparency" parameter:
	// int transparency = (trans * 255) / 100;
	//
	// Note by CJ:
	// Transparency is a bit counter-intuitive
	// 0=not transparent, 255=invisible, 1..254 barely visible .. mostly visible
	//
	// In order to support this backward-compatible behavior, we convert the
	// opacity into proper alpha this way:
	// 0      => alpha 255
	// 100    => alpha 0
	// 1 - 99 => alpha 1 - 244
	//
	RawDrawImageTrans(xx, yy, slot, GfxDef::LegacyTrans100ToAlpha255(legacy_transparency));
}
void RawDrawImageResized(int xx, int yy, int gotSlot, int width, int height) {
	if ((gotSlot < 0) || (!_GP(spriteset).DoesSpriteExist(gotSlot)))
		quit("!RawDrawImageResized: invalid sprite slot number specified");
	// very small, don't draw it
	if ((width < 1) || (height < 1))
		return;

	data_to_game_coords(&xx, &yy);
	data_to_game_coords(&width, &height);

	// resize the sprite to the requested size
	Bitmap *sprite = _GP(spriteset)[gotSlot];
	Bitmap *newPic = BitmapHelper::CreateBitmap(width, height, sprite->GetColorDepth());
	newPic->StretchBlt(sprite,
					   RectWH(0, 0, _GP(game).SpriteInfos[gotSlot].Width, _GP(game).SpriteInfos[gotSlot].Height),
					   RectWH(0, 0, width, height));

	RAW_START();
	if (newPic->GetColorDepth() != RAW_SURFACE()->GetColorDepth())
		quit("!RawDrawImageResized: image colour depth mismatch: the background image must have the same colour depth as the sprite being drawn");

	GfxUtil::DrawSpriteWithTransparency(RAW_SURFACE(), newPic, xx, yy);
	delete newPic;
	invalidate_screen();
	mark_current_background_dirty();
	RAW_END();
}
void RawDrawLine(int fromx, int fromy, int tox, int toy) {
	data_to_game_coords(&fromx, &fromy);
	data_to_game_coords(&tox, &toy);

	_GP(play).raw_modified[_GP(play).bg_frame] = 1;
	int ii, jj;
	// draw a line thick enough to look the same at all resolutions
	PBitmap bg = _GP(thisroom).BgFrames[_GP(play).bg_frame].Graphic;
	color_t draw_color = _GP(play).raw_color;
	for (ii = 0; ii < get_fixed_pixel_size(1); ii++) {
		for (jj = 0; jj < get_fixed_pixel_size(1); jj++)
			bg->DrawLine(Line(fromx + ii, fromy + jj, tox + ii, toy + jj), draw_color);
	}
	invalidate_screen();
	mark_current_background_dirty();
}
void RawDrawCircle(int xx, int yy, int rad) {
	data_to_game_coords(&xx, &yy);
	rad = data_to_game_coord(rad);

	_GP(play).raw_modified[_GP(play).bg_frame] = 1;
	PBitmap bg = _GP(thisroom).BgFrames[_GP(play).bg_frame].Graphic;
	bg->FillCircle(Circle(xx, yy, rad), _GP(play).raw_color);
	invalidate_screen();
	mark_current_background_dirty();
}
void RawDrawRectangle(int x1, int y1, int x2, int y2) {
	_GP(play).raw_modified[_GP(play).bg_frame] = 1;
	data_to_game_coords(&x1, &y1);
	data_to_game_round_up(&x2, &y2);

	PBitmap bg = _GP(thisroom).BgFrames[_GP(play).bg_frame].Graphic;
	bg->FillRect(Rect(x1, y1, x2, y2), _GP(play).raw_color);
	invalidate_screen();
	mark_current_background_dirty();
}
void RawDrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
	_GP(play).raw_modified[_GP(play).bg_frame] = 1;
	data_to_game_coords(&x1, &y1);
	data_to_game_coords(&x2, &y2);
	data_to_game_coords(&x3, &y3);

	PBitmap bg = _GP(thisroom).BgFrames[_GP(play).bg_frame].Graphic;
	bg->DrawTriangle(Triangle(x1, y1, x2, y2, x3, y3), _GP(play).raw_color);
	invalidate_screen();
	mark_current_background_dirty();
}

} // namespace AGS3