393
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2005-2007 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*)
|
|
18 |
|
184
|
19 |
unit uLandGraphics;
|
|
20 |
interface
|
351
|
21 |
uses uFloat;
|
345
|
22 |
{$INCLUDE options.inc}
|
184
|
23 |
|
|
24 |
type PRangeArray = ^TRangeArray;
|
|
25 |
TRangeArray = array[0..31] of record
|
371
|
26 |
Left, Right: LongInt;
|
184
|
27 |
end;
|
|
28 |
|
371
|
29 |
procedure DrawExplosion(X, Y, Radius: LongInt);
|
|
30 |
procedure DrawHLinesExplosions(ar: PRangeArray; Radius: LongInt; y, dY: LongInt; Count: Byte);
|
|
31 |
procedure DrawTunnel(X, Y, dX, dY: hwFloat; ticks, HalfWidth: LongInt);
|
|
32 |
procedure FillRoundInLand(X, Y, Radius: LongInt; Value: Longword);
|
184
|
33 |
|
|
34 |
implementation
|
196
|
35 |
uses SDLh, uMisc, uLand, uConsts;
|
184
|
36 |
|
371
|
37 |
procedure FillCircleLines(x, y, dx, dy: LongInt; Value: Longword);
|
|
38 |
var i: LongInt;
|
184
|
39 |
begin
|
|
40 |
if ((y + dy) and $FFFFFC00) = 0 then
|
|
41 |
for i:= max(x - dx, 0) to min(x + dx, 2047) do Land[y + dy, i]:= Value;
|
|
42 |
if ((y - dy) and $FFFFFC00) = 0 then
|
|
43 |
for i:= max(x - dx, 0) to min(x + dx, 2047) do Land[y - dy, i]:= Value;
|
|
44 |
if ((y + dx) and $FFFFFC00) = 0 then
|
|
45 |
for i:= max(x - dy, 0) to min(x + dy, 2047) do Land[y + dx, i]:= Value;
|
|
46 |
if ((y - dx) and $FFFFFC00) = 0 then
|
|
47 |
for i:= max(x - dy, 0) to min(x + dy, 2047) do Land[y - dx, i]:= Value;
|
|
48 |
end;
|
|
49 |
|
371
|
50 |
procedure FillRoundInLand(X, Y, Radius: LongInt; Value: Longword);
|
|
51 |
var dx, dy, d: LongInt;
|
184
|
52 |
begin
|
|
53 |
dx:= 0;
|
|
54 |
dy:= Radius;
|
|
55 |
d:= 3 - 2 * Radius;
|
|
56 |
while (dx < dy) do
|
|
57 |
begin
|
|
58 |
FillCircleLines(x, y, dx, dy, Value);
|
|
59 |
if (d < 0)
|
|
60 |
then d:= d + 4 * dx + 6
|
|
61 |
else begin
|
|
62 |
d:= d + 4 * (dx - dy) + 10;
|
|
63 |
dec(dy)
|
|
64 |
end;
|
|
65 |
inc(dx)
|
|
66 |
end;
|
|
67 |
if (dx = dy) then FillCircleLines(x, y, dx, dy, Value);
|
|
68 |
end;
|
|
69 |
|
371
|
70 |
procedure ClearLandPixel(y, x: LongInt);
|
184
|
71 |
var p: PByteArray;
|
|
72 |
begin
|
351
|
73 |
p:= @PByteArray(LandSurface^.pixels)^[LandSurface^.pitch * y];
|
|
74 |
case LandSurface^.format^.BytesPerPixel of
|
184
|
75 |
1: ;// not supported
|
351
|
76 |
2: PWord(@(p^[x * 2]))^:= 0;
|
184
|
77 |
3: begin
|
351
|
78 |
p^[x * 3 + 0]:= 0;
|
|
79 |
p^[x * 3 + 1]:= 0;
|
|
80 |
p^[x * 3 + 2]:= 0;
|
184
|
81 |
end;
|
351
|
82 |
4: PLongword(@(p^[x * 4]))^:= 0;
|
184
|
83 |
end
|
|
84 |
end;
|
|
85 |
|
371
|
86 |
procedure SetLandPixel(y, x: LongInt);
|
184
|
87 |
var p: PByteArray;
|
|
88 |
begin
|
351
|
89 |
p:= @PByteArray(LandSurface^.pixels)^[LandSurface^.pitch * y];
|
|
90 |
case LandSurface^.format^.BytesPerPixel of
|
184
|
91 |
1: ;// not supported
|
351
|
92 |
2: PWord(@(p^[x * 2]))^:= cExplosionBorderColor;
|
184
|
93 |
3: begin
|
351
|
94 |
p^[x * 3 + 0]:= cExplosionBorderColor and $FF;
|
|
95 |
p^[x * 3 + 1]:= (cExplosionBorderColor shr 8) and $FF;
|
|
96 |
p^[x * 3 + 2]:= cExplosionBorderColor shr 16;
|
184
|
97 |
end;
|
351
|
98 |
4: PLongword(@(p^[x * 4]))^:= cExplosionBorderColor;
|
184
|
99 |
end
|
|
100 |
end;
|
|
101 |
|
371
|
102 |
procedure FillLandCircleLines0(x, y, dx, dy: LongInt);
|
|
103 |
var i: LongInt;
|
184
|
104 |
begin
|
|
105 |
if ((y + dy) and $FFFFFC00) = 0 then
|
|
106 |
for i:= max(x - dx, 0) to min(x + dx, 2047) do ClearLandPixel(y + dy, i);
|
|
107 |
if ((y - dy) and $FFFFFC00) = 0 then
|
|
108 |
for i:= max(x - dx, 0) to min(x + dx, 2047) do ClearLandPixel(y - dy, i);
|
|
109 |
if ((y + dx) and $FFFFFC00) = 0 then
|
|
110 |
for i:= max(x - dy, 0) to min(x + dy, 2047) do ClearLandPixel(y + dx, i);
|
|
111 |
if ((y - dx) and $FFFFFC00) = 0 then
|
|
112 |
for i:= max(x - dy, 0) to min(x + dy, 2047) do ClearLandPixel(y - dx, i);
|
|
113 |
end;
|
|
114 |
|
371
|
115 |
procedure FillLandCircleLinesEBC(x, y, dx, dy: LongInt);
|
|
116 |
var i: LongInt;
|
184
|
117 |
begin
|
|
118 |
if ((y + dy) and $FFFFFC00) = 0 then
|
|
119 |
for i:= max(x - dx, 0) to min(x + dx, 2047) do
|
|
120 |
if Land[y + dy, i] = COLOR_LAND then SetLandPixel(y + dy, i);
|
|
121 |
if ((y - dy) and $FFFFFC00) = 0 then
|
|
122 |
for i:= max(x - dx, 0) to min(x + dx, 2047) do
|
|
123 |
if Land[y - dy, i] = COLOR_LAND then SetLandPixel(y - dy, i);
|
|
124 |
if ((y + dx) and $FFFFFC00) = 0 then
|
|
125 |
for i:= max(x - dy, 0) to min(x + dy, 2047) do
|
|
126 |
if Land[y + dx, i] = COLOR_LAND then SetLandPixel(y + dx, i);
|
|
127 |
if ((y - dx) and $FFFFFC00) = 0 then
|
|
128 |
for i:= max(x - dy, 0) to min(x + dy, 2047) do
|
|
129 |
if Land[y - dx, i] = COLOR_LAND then SetLandPixel(y - dx, i);
|
|
130 |
end;
|
|
131 |
|
371
|
132 |
procedure DrawExplosion(X, Y, Radius: LongInt);
|
|
133 |
var dx, dy, d: LongInt;
|
184
|
134 |
begin
|
|
135 |
FillRoundInLand(X, Y, Radius, 0);
|
|
136 |
|
|
137 |
if SDL_MustLock(LandSurface) then
|
|
138 |
SDLTry(SDL_LockSurface(LandSurface) >= 0, true);
|
|
139 |
|
|
140 |
dx:= 0;
|
|
141 |
dy:= Radius;
|
|
142 |
d:= 3 - 2 * Radius;
|
|
143 |
while (dx < dy) do
|
|
144 |
begin
|
|
145 |
FillLandCircleLines0(x, y, dx, dy);
|
|
146 |
if (d < 0)
|
|
147 |
then d:= d + 4 * dx + 6
|
|
148 |
else begin
|
|
149 |
d:= d + 4 * (dx - dy) + 10;
|
|
150 |
dec(dy)
|
|
151 |
end;
|
|
152 |
inc(dx)
|
|
153 |
end;
|
|
154 |
if (dx = dy) then FillLandCircleLines0(x, y, dx, dy);
|
|
155 |
inc(Radius, 4);
|
|
156 |
dx:= 0;
|
|
157 |
dy:= Radius;
|
|
158 |
d:= 3 - 2 * Radius;
|
|
159 |
while (dx < dy) do
|
|
160 |
begin
|
|
161 |
FillLandCircleLinesEBC(x, y, dx, dy);
|
|
162 |
if (d < 0)
|
|
163 |
then d:= d + 4 * dx + 6
|
|
164 |
else begin
|
|
165 |
d:= d + 4 * (dx - dy) + 10;
|
|
166 |
dec(dy)
|
|
167 |
end;
|
|
168 |
inc(dx)
|
|
169 |
end;
|
351
|
170 |
if (dx = dy) then FillLandCircleLinesEBC(x, y, dx, dy);
|
|
171 |
|
184
|
172 |
if SDL_MustLock(LandSurface) then
|
|
173 |
SDL_UnlockSurface(LandSurface);
|
|
174 |
end;
|
|
175 |
|
371
|
176 |
procedure DrawHLinesExplosions(ar: PRangeArray; Radius: LongInt; y, dY: LongInt; Count: Byte);
|
184
|
177 |
var tx, ty, i: LongInt;
|
|
178 |
begin
|
|
179 |
if SDL_MustLock(LandSurface) then
|
|
180 |
SDL_LockSurface(LandSurface);
|
|
181 |
|
|
182 |
for i:= 0 to Pred(Count) do
|
|
183 |
begin
|
188
|
184 |
for ty:= max(y - Radius, 0) to min(y + Radius, 1023) do
|
351
|
185 |
for tx:= max(0, ar^[i].Left - Radius) to min(2047, ar^[i].Right + Radius) do
|
188
|
186 |
ClearLandPixel(ty, tx);
|
184
|
187 |
inc(y, dY)
|
|
188 |
end;
|
|
189 |
|
|
190 |
inc(Radius, 4);
|
351
|
191 |
dec(y, Count * dY);
|
184
|
192 |
|
|
193 |
for i:= 0 to Pred(Count) do
|
|
194 |
begin
|
188
|
195 |
for ty:= max(y - Radius, 0) to min(y + Radius, 1023) do
|
351
|
196 |
for tx:= max(0, ar^[i].Left - Radius) to min(2047, ar^[i].Right + Radius) do
|
188
|
197 |
if Land[ty, tx] = $FFFFFF then
|
|
198 |
SetLandPixel(ty, tx);
|
184
|
199 |
inc(y, dY)
|
|
200 |
end;
|
|
201 |
|
|
202 |
if SDL_MustLock(LandSurface) then
|
|
203 |
SDL_UnlockSurface(LandSurface);
|
|
204 |
end;
|
|
205 |
|
|
206 |
//
|
|
207 |
// - (dX, dY) - direction, vector of length = 0.5
|
|
208 |
//
|
371
|
209 |
procedure DrawTunnel(X, Y, dX, dY: hwFloat; ticks, HalfWidth: LongInt);
|
358
|
210 |
var nx, ny, dX8, dY8: hwFloat;
|
184
|
211 |
i, t, tx, ty: Longint;
|
|
212 |
begin // (-dY, dX) is (dX, dY) rotated by PI/2
|
|
213 |
if SDL_MustLock(LandSurface) then
|
|
214 |
SDL_LockSurface(LandSurface);
|
|
215 |
|
|
216 |
nx:= X + dY * (HalfWidth + 8);
|
|
217 |
ny:= Y - dX * (HalfWidth + 8);
|
|
218 |
|
358
|
219 |
dX8:= dX * 8;
|
|
220 |
dY8:= dY * 8;
|
184
|
221 |
for i:= 0 to 7 do
|
|
222 |
begin
|
358
|
223 |
X:= nx - dX8;
|
|
224 |
Y:= ny - dY8;
|
184
|
225 |
for t:= -8 to ticks + 8 do
|
|
226 |
{$include tunsetborder.inc}
|
|
227 |
nx:= nx - dY;
|
|
228 |
ny:= ny + dX;
|
|
229 |
end;
|
|
230 |
|
|
231 |
for i:= -HalfWidth to HalfWidth do
|
|
232 |
begin
|
358
|
233 |
X:= nx - dX8;
|
|
234 |
Y:= ny - dY8;
|
184
|
235 |
for t:= 0 to 7 do
|
|
236 |
{$include tunsetborder.inc}
|
|
237 |
X:= nx;
|
|
238 |
Y:= ny;
|
|
239 |
for t:= 0 to ticks do
|
|
240 |
begin
|
|
241 |
X:= X + dX;
|
|
242 |
Y:= Y + dY;
|
351
|
243 |
tx:= hwRound(X);
|
|
244 |
ty:= hwRound(Y);
|
184
|
245 |
if ((ty and $FFFFFC00) = 0) and ((tx and $FFFFF800) = 0) then
|
|
246 |
begin
|
|
247 |
Land[ty, tx]:= 0;
|
|
248 |
ClearLandPixel(ty, tx);
|
|
249 |
end
|
|
250 |
end;
|
|
251 |
for t:= 0 to 7 do
|
|
252 |
{$include tunsetborder.inc}
|
|
253 |
nx:= nx - dY;
|
|
254 |
ny:= ny + dX;
|
|
255 |
end;
|
|
256 |
|
|
257 |
for i:= 0 to 7 do
|
|
258 |
begin
|
358
|
259 |
X:= nx - dX8;
|
|
260 |
Y:= ny - dY8;
|
184
|
261 |
for t:= -8 to ticks + 8 do
|
|
262 |
{$include tunsetborder.inc}
|
|
263 |
nx:= nx - dY;
|
|
264 |
ny:= ny + dX;
|
|
265 |
end;
|
|
266 |
|
|
267 |
if SDL_MustLock(LandSurface) then
|
|
268 |
SDL_UnlockSurface(LandSurface)
|
|
269 |
end;
|
|
270 |
|
|
271 |
|
|
272 |
end.
|