File: warper.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 (185 lines) | stat: -rw-r--r-- 5,244 bytes parent folder | download | duplicates (3)
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
/* 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
 * 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 "common/util.h"
#include "ags/plugins/ags_waves/ags_waves.h"

namespace AGS3 {
namespace Plugins {
namespace AGSWaves {


void AGSWaves::ReturnNewHeight(ScriptMethodParams &params) {
	params._result = _newHeight;
}

void AGSWaves::ReturnNewWidth(ScriptMethodParams &params) {
	params._result = _newWidth;
}

void AGSWaves::Warper(ScriptMethodParams &params) {
	PARAMS5(int, swarp, int, sadjust, int, x1, int, y1, int, x2);

	ix = 0.0;
	iy = 0.0;
	ua = 0.0;
	// some precautions against non-positive values for width and height

	float ax = float(x1), ay = float(y1);
	float bx = float(x2), by = float(_y2);
	float cx = float(_x3), cy = float(_y3);
	float dx = float(_x4), dy = float(_y4);

	int w = int(max4(ax, bx, cx, dx)) + 1;
	int h = int(max4(ay, by, cy, dy)) + 1;

	BITMAP *refsrc = _engine->GetSpriteGraphic(swarp);
	int32 refsrc_width = 640;
	int32 refsrc_height = 360;
	int32 refsrc_depth = 32;
	_engine->GetBitmapDimensions(refsrc, &refsrc_width, &refsrc_height, &refsrc_depth);
	unsigned int **refsprite_pixels = (unsigned int **)_engine->GetRawBitmapSurface(refsrc);
	_engine->ReleaseBitmapSurface(refsrc);


	// create temporary sprite holding the warped version
	BITMAP *resizeb = _engine->GetSpriteGraphic(sadjust);
	int32 src_width = 640;
	int32 src_height = 360;
	int32 src_depth = 32;
	_engine->GetBitmapDimensions(resizeb, &src_width, &src_height, &src_depth);
	unsigned int **sprite_pixels = (unsigned int **)_engine->GetRawBitmapSurface(resizeb);


	int ow = refsrc_width, oh = refsrc_height;

	int x, y;  // pixel coords
	float fx, fy; // original sprite's in between pixel coords

	int il;

	// calculate intersections of opposing sides
	float orx_x, orx_y, ory_x, ory_y;
	bool xp = false, yp = false; // parallel sides?

	// AC and BD to get intersection of all "vertical lines"

	il = IntersectLines(ax, ay, cx, cy, bx, by, dx, dy);
	if (il == 0) {
		// parallel sides, store directional vector
		orx_x = cx - ax;
		orx_y = cy - ay;
		xp = true;
	} else {
		// store intersection of sides
		orx_x = ix;
		orx_y = iy;
	}
	// AB and CD to get intersection of all "horizontal lines"
	il = IntersectLines(ax, ay, bx, by, cx, cy, dx, dy);
	if (il == 0) {
		// parallel sides, store directional vector
		ory_x = bx - ax;
		ory_y = by - ay;
		yp = true;
	} else {
		// store intersection of sides
		ory_x = ix;
		ory_y = iy;
	}

	int xm = int(min4(ax, bx, cx, dx)); // x loop starts here

	y = int(min4(ay, by, cy, dy));
	while (y < h) {
		x = xm;
		while (x < w) {

			// calculate original pixel

			// x:
			if (xp) il = IntersectLines(ax, ay, bx, by, float(x), float(y), float(x) + orx_x, float(y) + orx_y);
			else il = IntersectLines(ax, ay, bx, by, float(x), float(y), orx_x, orx_y);
			fx = float(ow - 1) * ua;

			float ux = ua;

			// y:
			if (yp) il = IntersectLines(ax, ay, cx, cy, float(x), float(y), float(x) + ory_x, float(y) + ory_y);
			else il = IntersectLines(ax, ay, cx, cy, float(x), float(y), ory_x, ory_y);
			fy = float(oh - 1) * ua;

			// only draw if within original sprite
			if (ux >= 0.0 && ux <= 1.0 && ua >= 0.0 && ua <= 1.0) {
				int refY = (int)CLIP(fy, (float)0.0, float(refsrc_height - 1));
				int refX = (int)CLIP(fx, (float)0.0, float(refsrc_width - 1));

				int setcolor = refsprite_pixels[refY][refX];

				int setY = (int)CLIP((float)y, (float)0.0, (float)(src_height - 1));
				int setX = (int)CLIP((float)x, (float)0.0, (float)(src_width - 1));

				sprite_pixels[setY][setX] = setcolor;
			}

			x++;
		}

		y++;
	}

	_newWidth = w;
	_newHeight = h;
	_engine->ReleaseBitmapSurface(resizeb);
}

void AGSWaves::SetWarper(ScriptMethodParams &params) {
	//PARAMS5(int, y2x, int, x3x, int, y3x, int, x4x, int, y4x);
}


int AGSWaves::IntersectLines(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
	// check a
	if (x1 == x2 && y1 == y2)
		return -1;
	// check b
	if (x3 == x4 && y3 == y4)
		return -1;
	float den = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
	float num12 = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
	float num34 = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);

	if (den == 0.0) {  // no intersection
		if (num12 == 0.0 && num34 == 0.0)
			return 2;
		return 0;
	}

	ua = num12 / den;
	ix = x1 + ua * (x2 - x1);
	iy = y1 + ua * (y2 - y1);

	return 1;
}

} // namespace AGSWaves
} // namespace Plugins
} // namespace AGS3