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
|
409
|
21 |
uses uFloat, uConsts;
|
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 |
|
409
|
34 |
function TryPlaceOnLand(cpX, cpY: LongInt; Obj: TSprite; Frame: LongInt): boolean;
|
|
35 |
|
184
|
36 |
implementation
|
409
|
37 |
uses SDLh, uMisc, uLand;
|
184
|
38 |
|
371
|
39 |
procedure FillCircleLines(x, y, dx, dy: LongInt; Value: Longword);
|
|
40 |
var i: LongInt;
|
184
|
41 |
begin
|
|
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 - dy) and $FFFFFC00) = 0 then
|
|
45 |
for i:= max(x - dx, 0) to min(x + dx, 2047) do Land[y - dy, 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 |
if ((y - dx) and $FFFFFC00) = 0 then
|
|
49 |
for i:= max(x - dy, 0) to min(x + dy, 2047) do Land[y - dx, i]:= Value;
|
|
50 |
end;
|
|
51 |
|
371
|
52 |
procedure FillRoundInLand(X, Y, Radius: LongInt; Value: Longword);
|
|
53 |
var dx, dy, d: LongInt;
|
184
|
54 |
begin
|
|
55 |
dx:= 0;
|
|
56 |
dy:= Radius;
|
|
57 |
d:= 3 - 2 * Radius;
|
|
58 |
while (dx < dy) do
|
|
59 |
begin
|
|
60 |
FillCircleLines(x, y, dx, dy, Value);
|
|
61 |
if (d < 0)
|
|
62 |
then d:= d + 4 * dx + 6
|
|
63 |
else begin
|
|
64 |
d:= d + 4 * (dx - dy) + 10;
|
|
65 |
dec(dy)
|
|
66 |
end;
|
|
67 |
inc(dx)
|
|
68 |
end;
|
|
69 |
if (dx = dy) then FillCircleLines(x, y, dx, dy, Value);
|
|
70 |
end;
|
|
71 |
|
371
|
72 |
procedure ClearLandPixel(y, x: LongInt);
|
184
|
73 |
var p: PByteArray;
|
|
74 |
begin
|
351
|
75 |
p:= @PByteArray(LandSurface^.pixels)^[LandSurface^.pitch * y];
|
|
76 |
case LandSurface^.format^.BytesPerPixel of
|
|
77 |
2: PWord(@(p^[x * 2]))^:= 0;
|
184
|
78 |
3: begin
|
351
|
79 |
p^[x * 3 + 0]:= 0;
|
|
80 |
p^[x * 3 + 1]:= 0;
|
|
81 |
p^[x * 3 + 2]:= 0;
|
184
|
82 |
end;
|
351
|
83 |
4: PLongword(@(p^[x * 4]))^:= 0;
|
184
|
84 |
end
|
|
85 |
end;
|
|
86 |
|
371
|
87 |
procedure SetLandPixel(y, x: LongInt);
|
184
|
88 |
var p: PByteArray;
|
|
89 |
begin
|
351
|
90 |
p:= @PByteArray(LandSurface^.pixels)^[LandSurface^.pitch * y];
|
|
91 |
case LandSurface^.format^.BytesPerPixel of
|
|
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 |
|
409
|
271 |
function TryPlaceOnLand(cpX, cpY: LongInt; Obj: TSprite; Frame: LongInt): boolean;
|
495
|
272 |
var X, Y, bpp, h, w: LongInt;
|
409
|
273 |
p: PByteArray;
|
|
274 |
r, rr: TSDL_Rect;
|
|
275 |
Image: PSDL_Surface;
|
|
276 |
begin
|
|
277 |
Image:= SpritesData[Obj].Surface;
|
|
278 |
w:= SpritesData[Obj].Width;
|
|
279 |
h:= SpritesData[Obj].Height;
|
|
280 |
|
|
281 |
if SDL_MustLock(Image) then
|
|
282 |
SDLTry(SDL_LockSurface(Image) >= 0, true);
|
|
283 |
|
|
284 |
bpp:= Image^.format^.BytesPerPixel;
|
|
285 |
TryDo(bpp <> 1, 'We don''t work with 8 bit surfaces', true);
|
|
286 |
// Check that sprites fits free space
|
|
287 |
p:= @(PByteArray(Image^.pixels)^[Image^.pitch * Frame * h]);
|
|
288 |
case bpp of
|
|
289 |
2: for y:= 0 to Pred(h) do
|
|
290 |
begin
|
|
291 |
for x:= 0 to Pred(w) do
|
|
292 |
if PWord(@(p^[x * 2]))^ <> 0 then
|
|
293 |
if (((cpY + y) and $FFFFFC00) <> 0) or
|
|
294 |
(((cpX + x) and $FFFFF800) <> 0) or
|
|
295 |
(Land[cpY + y, cpX + x] <> 0) then
|
|
296 |
begin
|
|
297 |
if SDL_MustLock(Image) then
|
|
298 |
SDL_UnlockSurface(Image);
|
|
299 |
exit(false)
|
|
300 |
end;
|
|
301 |
p:= @(p^[Image^.pitch]);
|
|
302 |
end;
|
|
303 |
3: for y:= 0 to Pred(h) do
|
|
304 |
begin
|
|
305 |
for x:= 0 to Pred(w) do
|
|
306 |
if (p^[x * 3 + 0] <> 0)
|
|
307 |
or (p^[x * 3 + 1] <> 0)
|
|
308 |
or (p^[x * 3 + 2] <> 0) then
|
|
309 |
if (((cpY + y) and $FFFFFC00) <> 0) or
|
|
310 |
(((cpX + x) and $FFFFF800) <> 0) or
|
|
311 |
(Land[cpY + y, cpX + x] <> 0) then
|
|
312 |
begin
|
|
313 |
if SDL_MustLock(Image) then
|
|
314 |
SDL_UnlockSurface(Image);
|
|
315 |
exit(false)
|
|
316 |
end;
|
|
317 |
p:= @(p^[Image^.pitch]);
|
|
318 |
end;
|
|
319 |
4: for y:= 0 to Pred(h) do
|
|
320 |
begin
|
|
321 |
for x:= 0 to Pred(w) do
|
|
322 |
if PLongword(@(p^[x * 4]))^ <> 0 then
|
|
323 |
if (((cpY + y) and $FFFFFC00) <> 0) or
|
|
324 |
(((cpX + x) and $FFFFF800) <> 0) or
|
|
325 |
(Land[cpY + y, cpX + x] <> 0) then
|
|
326 |
begin
|
|
327 |
if SDL_MustLock(Image) then
|
|
328 |
SDL_UnlockSurface(Image);
|
|
329 |
exit(false)
|
|
330 |
end;
|
|
331 |
p:= @(p^[Image^.pitch]);
|
|
332 |
end;
|
|
333 |
end;
|
|
334 |
|
|
335 |
// Checked, now place
|
|
336 |
p:= @(PByteArray(Image^.pixels)^[Image^.pitch * Frame * h]);
|
|
337 |
case bpp of
|
|
338 |
2: for y:= 0 to Pred(h) do
|
|
339 |
begin
|
|
340 |
for x:= 0 to Pred(w) do
|
|
341 |
if PWord(@(p^[x * 2]))^ <> 0 then Land[cpY + y, cpX + x]:= COLOR_LAND;
|
|
342 |
p:= @(p^[Image^.pitch]);
|
|
343 |
end;
|
|
344 |
3: for y:= 0 to Pred(h) do
|
|
345 |
begin
|
|
346 |
for x:= 0 to Pred(w) do
|
|
347 |
if (p^[x * 3 + 0] <> 0)
|
|
348 |
or (p^[x * 3 + 1] <> 0)
|
|
349 |
or (p^[x * 3 + 2] <> 0) then Land[cpY + y, cpX + x]:= COLOR_LAND;
|
|
350 |
p:= @(p^[Image^.pitch]);
|
|
351 |
end;
|
|
352 |
4: for y:= 0 to Pred(h) do
|
|
353 |
begin
|
|
354 |
for x:= 0 to Pred(w) do
|
|
355 |
if PLongword(@(p^[x * 4]))^ <> 0 then Land[cpY + y, cpX + x]:= COLOR_LAND;
|
|
356 |
p:= @(p^[Image^.pitch]);
|
|
357 |
end;
|
|
358 |
end;
|
|
359 |
if SDL_MustLock(Image) then
|
|
360 |
SDL_UnlockSurface(Image);
|
|
361 |
|
|
362 |
// Draw sprite on Land surface
|
|
363 |
r.x:= 0;
|
|
364 |
r.y:= SpritesData[Obj].Height * Frame;
|
|
365 |
r.w:= SpritesData[Obj].Width;
|
|
366 |
r.h:= SpritesData[Obj].Height;
|
|
367 |
rr.x:= cpX;
|
|
368 |
rr.y:= cpY;
|
|
369 |
SDL_UpperBlit(Image, @r, LandSurface, @rr);
|
|
370 |
|
|
371 |
TryPlaceOnLand:= true
|
|
372 |
end;
|
|
373 |
|
184
|
374 |
|
|
375 |
end.
|