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;
|
|
272 |
var Result: boolean;
|
|
273 |
X, Y, sY, bpp, h, w: LongInt;
|
|
274 |
p: PByteArray;
|
|
275 |
r, rr: TSDL_Rect;
|
|
276 |
Image: PSDL_Surface;
|
|
277 |
begin
|
|
278 |
Result:= true;
|
|
279 |
Image:= SpritesData[Obj].Surface;
|
|
280 |
w:= SpritesData[Obj].Width;
|
|
281 |
h:= SpritesData[Obj].Height;
|
|
282 |
|
|
283 |
if SDL_MustLock(Image) then
|
|
284 |
SDLTry(SDL_LockSurface(Image) >= 0, true);
|
|
285 |
|
|
286 |
bpp:= Image^.format^.BytesPerPixel;
|
|
287 |
TryDo(bpp <> 1, 'We don''t work with 8 bit surfaces', true);
|
|
288 |
// Check that sprites fits free space
|
|
289 |
p:= @(PByteArray(Image^.pixels)^[Image^.pitch * Frame * h]);
|
|
290 |
case bpp of
|
|
291 |
2: for y:= 0 to Pred(h) do
|
|
292 |
begin
|
|
293 |
for x:= 0 to Pred(w) do
|
|
294 |
if PWord(@(p^[x * 2]))^ <> 0 then
|
|
295 |
if (((cpY + y) and $FFFFFC00) <> 0) or
|
|
296 |
(((cpX + x) and $FFFFF800) <> 0) or
|
|
297 |
(Land[cpY + y, cpX + x] <> 0) then
|
|
298 |
begin
|
|
299 |
if SDL_MustLock(Image) then
|
|
300 |
SDL_UnlockSurface(Image);
|
|
301 |
exit(false)
|
|
302 |
end;
|
|
303 |
p:= @(p^[Image^.pitch]);
|
|
304 |
end;
|
|
305 |
3: for y:= 0 to Pred(h) do
|
|
306 |
begin
|
|
307 |
for x:= 0 to Pred(w) do
|
|
308 |
if (p^[x * 3 + 0] <> 0)
|
|
309 |
or (p^[x * 3 + 1] <> 0)
|
|
310 |
or (p^[x * 3 + 2] <> 0) then
|
|
311 |
if (((cpY + y) and $FFFFFC00) <> 0) or
|
|
312 |
(((cpX + x) and $FFFFF800) <> 0) or
|
|
313 |
(Land[cpY + y, cpX + x] <> 0) then
|
|
314 |
begin
|
|
315 |
if SDL_MustLock(Image) then
|
|
316 |
SDL_UnlockSurface(Image);
|
|
317 |
exit(false)
|
|
318 |
end;
|
|
319 |
p:= @(p^[Image^.pitch]);
|
|
320 |
end;
|
|
321 |
4: for y:= 0 to Pred(h) do
|
|
322 |
begin
|
|
323 |
for x:= 0 to Pred(w) do
|
|
324 |
if PLongword(@(p^[x * 4]))^ <> 0 then
|
|
325 |
if (((cpY + y) and $FFFFFC00) <> 0) or
|
|
326 |
(((cpX + x) and $FFFFF800) <> 0) or
|
|
327 |
(Land[cpY + y, cpX + x] <> 0) then
|
|
328 |
begin
|
|
329 |
if SDL_MustLock(Image) then
|
|
330 |
SDL_UnlockSurface(Image);
|
|
331 |
exit(false)
|
|
332 |
end;
|
|
333 |
p:= @(p^[Image^.pitch]);
|
|
334 |
end;
|
|
335 |
end;
|
|
336 |
|
|
337 |
// Checked, now place
|
|
338 |
p:= @(PByteArray(Image^.pixels)^[Image^.pitch * Frame * h]);
|
|
339 |
case bpp of
|
|
340 |
2: for y:= 0 to Pred(h) do
|
|
341 |
begin
|
|
342 |
for x:= 0 to Pred(w) do
|
|
343 |
if PWord(@(p^[x * 2]))^ <> 0 then Land[cpY + y, cpX + x]:= COLOR_LAND;
|
|
344 |
p:= @(p^[Image^.pitch]);
|
|
345 |
end;
|
|
346 |
3: for y:= 0 to Pred(h) do
|
|
347 |
begin
|
|
348 |
for x:= 0 to Pred(w) do
|
|
349 |
if (p^[x * 3 + 0] <> 0)
|
|
350 |
or (p^[x * 3 + 1] <> 0)
|
|
351 |
or (p^[x * 3 + 2] <> 0) then Land[cpY + y, cpX + x]:= COLOR_LAND;
|
|
352 |
p:= @(p^[Image^.pitch]);
|
|
353 |
end;
|
|
354 |
4: for y:= 0 to Pred(h) do
|
|
355 |
begin
|
|
356 |
for x:= 0 to Pred(w) do
|
|
357 |
if PLongword(@(p^[x * 4]))^ <> 0 then Land[cpY + y, cpX + x]:= COLOR_LAND;
|
|
358 |
p:= @(p^[Image^.pitch]);
|
|
359 |
end;
|
|
360 |
end;
|
|
361 |
if SDL_MustLock(Image) then
|
|
362 |
SDL_UnlockSurface(Image);
|
|
363 |
|
|
364 |
// Draw sprite on Land surface
|
|
365 |
r.x:= 0;
|
|
366 |
r.y:= SpritesData[Obj].Height * Frame;
|
|
367 |
r.w:= SpritesData[Obj].Width;
|
|
368 |
r.h:= SpritesData[Obj].Height;
|
|
369 |
rr.x:= cpX;
|
|
370 |
rr.y:= cpY;
|
|
371 |
SDL_UpperBlit(Image, @r, LandSurface, @rr);
|
|
372 |
|
|
373 |
TryPlaceOnLand:= true
|
|
374 |
end;
|
|
375 |
|
184
|
376 |
|
|
377 |
end.
|