author | unc0rr |
Tue, 22 Apr 2008 12:41:14 +0000 | |
changeset 857 | 43912f139db1 |
parent 806 | d397c502a5dd |
child 883 | 07a568ba44e0 |
permissions | -rw-r--r-- |
184 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
393 | 3 |
* Copyright (c) 2005-2007 Andrey Korotaev <unC0Rr@gmail.com> |
184 | 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 |
||
19 |
unit uLandObjects; |
|
20 |
interface |
|
21 |
uses SDLh; |
|
22 |
{$include options.inc} |
|
23 |
||
24 |
procedure AddObjects(InSurface, Surface: PSDL_Surface); |
|
25 |
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface); |
|
26 |
||
27 |
implementation |
|
805 | 28 |
uses uLand, uStore, uConsts, uMisc, uConsole, uRandom, uVisualGears, uFloat; |
184 | 29 |
const MaxRects = 256; |
30 |
MAXOBJECTRECTS = 16; |
|
31 |
MAXTHEMEOBJECTS = 32; |
|
32 |
||
33 |
type PRectArray = ^TRectsArray; |
|
34 |
TRectsArray = array[0..MaxRects] of TSDL_Rect; |
|
35 |
TThemeObject = record |
|
36 |
Surf: PSDL_Surface; |
|
37 |
inland: TSDL_Rect; |
|
38 |
outland: array[0..Pred(MAXOBJECTRECTS)] of TSDL_Rect; |
|
39 |
rectcnt: Longword; |
|
40 |
Width, Height: Longword; |
|
41 |
Maxcnt: Longword; |
|
42 |
end; |
|
43 |
TThemeObjects = record |
|
371 | 44 |
Count: LongInt; |
184 | 45 |
objs: array[0..Pred(MAXTHEMEOBJECTS)] of TThemeObject; |
46 |
end; |
|
47 |
TSprayObject = record |
|
48 |
Surf: PSDL_Surface; |
|
49 |
Width, Height: Longword; |
|
50 |
Maxcnt: Longword; |
|
51 |
end; |
|
52 |
TSprayObjects = record |
|
371 | 53 |
Count: LongInt; |
184 | 54 |
objs: array[0..Pred(MAXTHEMEOBJECTS)] of TSprayObject |
55 |
end; |
|
56 |
||
57 |
var Rects: PRectArray; |
|
58 |
RectCount: Longword; |
|
59 |
||
60 |
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface); |
|
61 |
var p: PByteArray; |
|
62 |
x, y: Longword; |
|
371 | 63 |
bpp: LongInt; |
184 | 64 |
r: TSDL_Rect; |
65 |
begin |
|
66 |
r.x:= cpX; |
|
67 |
r.y:= cpY; |
|
68 |
SDL_UpperBlit(Image, nil, Surface, @r); |
|
69 |
WriteToConsole('Generating collision info... '); |
|
70 |
||
71 |
if SDL_MustLock(Image) then |
|
72 |
SDLTry(SDL_LockSurface(Image) >= 0, true); |
|
73 |
||
351 | 74 |
bpp:= Image^.format^.BytesPerPixel; |
184 | 75 |
WriteToConsole('('+inttostr(bpp)+') '); |
351 | 76 |
p:= Image^.pixels; |
184 | 77 |
case bpp of |
78 |
1: OutError('We don''t work with 8 bit surfaces', true); |
|
351 | 79 |
2: for y:= 0 to Pred(Image^.h) do |
184 | 80 |
begin |
351 | 81 |
for x:= 0 to Pred(Image^.w) do |
82 |
if PWord(@(p^[x * 2]))^ <> 0 then Land[cpY + y, cpX + x]:= COLOR_LAND; |
|
83 |
p:= @(p^[Image^.pitch]); |
|
184 | 84 |
end; |
351 | 85 |
3: for y:= 0 to Pred(Image^.h) do |
184 | 86 |
begin |
351 | 87 |
for x:= 0 to Pred(Image^.w) do |
88 |
if (p^[x * 3 + 0] <> 0) |
|
89 |
or (p^[x * 3 + 1] <> 0) |
|
90 |
or (p^[x * 3 + 2] <> 0) then Land[cpY + y, cpX + x]:= COLOR_LAND; |
|
91 |
p:= @(p^[Image^.pitch]); |
|
184 | 92 |
end; |
351 | 93 |
4: for y:= 0 to Pred(Image^.h) do |
184 | 94 |
begin |
351 | 95 |
for x:= 0 to Pred(Image^.w) do |
96 |
if PLongword(@(p^[x * 4]))^ <> 0 then Land[cpY + y, cpX + x]:= COLOR_LAND; |
|
97 |
p:= @(p^[Image^.pitch]); |
|
184 | 98 |
end; |
99 |
end; |
|
100 |
if SDL_MustLock(Image) then |
|
101 |
SDL_UnlockSurface(Image); |
|
102 |
WriteLnToConsole(msgOK) |
|
103 |
end; |
|
104 |
||
371 | 105 |
procedure AddRect(x1, y1, w1, h1: LongInt); |
184 | 106 |
begin |
351 | 107 |
with Rects^[RectCount] do |
184 | 108 |
begin |
109 |
x:= x1; |
|
110 |
y:= y1; |
|
111 |
w:= w1; |
|
112 |
h:= h1 |
|
113 |
end; |
|
114 |
inc(RectCount); |
|
115 |
TryDo(RectCount < MaxRects, 'AddRect: overflow', true) |
|
116 |
end; |
|
117 |
||
118 |
procedure InitRects; |
|
119 |
begin |
|
120 |
RectCount:= 0; |
|
121 |
New(Rects) |
|
122 |
end; |
|
123 |
||
124 |
procedure FreeRects; |
|
125 |
begin |
|
126 |
Dispose(rects) |
|
127 |
end; |
|
128 |
||
371 | 129 |
function CheckIntersect(x1, y1, w1, h1: LongInt): boolean; |
184 | 130 |
var i: Longword; |
351 | 131 |
Result: boolean; |
184 | 132 |
begin |
133 |
Result:= false; |
|
134 |
i:= 0; |
|
135 |
if RectCount > 0 then |
|
136 |
repeat |
|
351 | 137 |
with Rects^[i] do |
184 | 138 |
Result:= (x < x1 + w1) and (x1 < x + w) and |
139 |
(y < y1 + h1) and (y1 < y + h); |
|
140 |
inc(i) |
|
351 | 141 |
until (i = RectCount) or (Result); |
142 |
CheckIntersect:= Result |
|
184 | 143 |
end; |
144 |
||
371 | 145 |
function AddGirder(gX: LongInt; Surface: PSDL_Surface): boolean; |
184 | 146 |
var tmpsurf: PSDL_Surface; |
371 | 147 |
x1, x2, y, k, i: LongInt; |
184 | 148 |
r, rr: TSDL_Rect; |
351 | 149 |
Result: boolean; |
184 | 150 |
|
371 | 151 |
function CountNonZeroz(x, y: LongInt): Longword; |
152 |
var i: LongInt; |
|
351 | 153 |
Result: Longword; |
184 | 154 |
begin |
155 |
Result:= 0; |
|
156 |
for i:= y to y + 15 do |
|
351 | 157 |
if Land[i, x] <> 0 then inc(Result); |
158 |
CountNonZeroz:= Result |
|
184 | 159 |
end; |
160 |
||
161 |
begin |
|
162 |
y:= 150; |
|
163 |
repeat |
|
164 |
inc(y, 24); |
|
165 |
x1:= gX; |
|
166 |
x2:= gX; |
|
167 |
while (x1 > 100) and (CountNonZeroz(x1, y) = 0) do dec(x1, 2); |
|
168 |
i:= x1 - 12; |
|
169 |
repeat |
|
170 |
k:= CountNonZeroz(x1, y); |
|
171 |
dec(x1, 2) |
|
172 |
until (x1 < 100) or (k = 0) or (k = 16) or (x1 < i); |
|
173 |
inc(x1, 2); |
|
174 |
if k = 16 then |
|
175 |
begin |
|
176 |
while (x2 < 1900) and (CountNonZeroz(x2, y) = 0) do inc(x2, 2); |
|
177 |
i:= x2 + 12; |
|
178 |
repeat |
|
179 |
k:= CountNonZeroz(x2, y); |
|
180 |
inc(x2, 2) |
|
181 |
until (x2 > 1900) or (k = 0) or (k = 16) or (x2 > i); |
|
182 |
if (x2 < 1900) and (k = 16) and (x2 - x1 > 250) |
|
183 |
and not CheckIntersect(x1 - 32, y - 64, x2 - x1 + 64, 144) then break; |
|
184 |
end; |
|
185 |
x1:= 0; |
|
186 |
until y > 900; |
|
187 |
if x1 > 0 then |
|
188 |
begin |
|
189 |
Result:= true; |
|
351 | 190 |
tmpsurf:= LoadImage(Pathz[ptGraphics] + '/Girder', false, true, true); |
184 | 191 |
rr.x:= x1; |
192 |
rr.y:= y; |
|
193 |
while rr.x + 100 < x2 do |
|
194 |
begin |
|
195 |
SDL_UpperBlit(tmpsurf, nil, Surface, @rr); |
|
196 |
inc(rr.x, 100); |
|
197 |
end; |
|
198 |
r.x:= 0; |
|
199 |
r.y:= 0; |
|
200 |
r.w:= x2 - rr.x; |
|
201 |
r.h:= 16; |
|
202 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr); |
|
203 |
SDL_FreeSurface(tmpsurf); |
|
204 |
AddRect(x1 - 8, y - 32, x2 - x1 + 16, 80); |
|
205 |
for k:= y to y + 15 do |
|
206 |
for i:= x1 to x2 do Land[k, i]:= $FFFFFF |
|
351 | 207 |
end else Result:= false; |
208 |
AddGirder:= Result |
|
184 | 209 |
end; |
210 |
||
211 |
function CheckLand(rect: TSDL_Rect; dX, dY, Color: Longword): boolean; |
|
212 |
var i: Longword; |
|
351 | 213 |
Result: boolean; |
184 | 214 |
begin |
215 |
Result:= true; |
|
216 |
inc(rect.x, dX); |
|
217 |
inc(rect.y, dY); |
|
218 |
i:= 0; |
|
219 |
{$WARNINGS OFF} |
|
220 |
while (i <= rect.w) and Result do |
|
221 |
begin |
|
222 |
Result:= (Land[rect.y, rect.x + i] = Color) and (Land[rect.y + rect.h, rect.x + i] = Color); |
|
223 |
inc(i) |
|
224 |
end; |
|
225 |
i:= 0; |
|
226 |
while (i <= rect.h) and Result do |
|
227 |
begin |
|
228 |
Result:= (Land[rect.y + i, rect.x] = Color) and (Land[rect.y + i, rect.x + rect.w] = Color); |
|
229 |
inc(i) |
|
230 |
end; |
|
231 |
{$WARNINGS ON} |
|
351 | 232 |
CheckLand:= Result |
184 | 233 |
end; |
234 |
||
235 |
function CheckCanPlace(x, y: Longword; var Obj: TThemeObject): boolean; |
|
236 |
var i: Longword; |
|
351 | 237 |
Result: boolean; |
184 | 238 |
begin |
239 |
with Obj do |
|
240 |
if CheckLand(inland, x, y, $FFFFFF) then |
|
241 |
begin |
|
242 |
Result:= true; |
|
243 |
i:= 1; |
|
244 |
while Result and (i <= rectcnt) do |
|
245 |
begin |
|
246 |
Result:= CheckLand(outland[i], x, y, 0); |
|
247 |
inc(i) |
|
248 |
end; |
|
249 |
if Result then |
|
250 |
Result:= not CheckIntersect(x, y, Width, Height) |
|
251 |
end else |
|
351 | 252 |
Result:= false; |
253 |
CheckCanPlace:= Result |
|
184 | 254 |
end; |
255 |
||
256 |
function TryPut(var Obj: TThemeObject; Surface: PSDL_Surface): boolean; overload; |
|
257 |
const MaxPointsIndex = 2047; |
|
258 |
var x, y: Longword; |
|
259 |
ar: array[0..MaxPointsIndex] of TPoint; |
|
260 |
cnt, i: Longword; |
|
351 | 261 |
Result: boolean; |
184 | 262 |
begin |
263 |
cnt:= 0; |
|
264 |
with Obj do |
|
265 |
begin |
|
266 |
if Maxcnt = 0 then |
|
351 | 267 |
exit(false); |
184 | 268 |
x:= 0; |
269 |
repeat |
|
270 |
y:= 0; |
|
271 |
repeat |
|
272 |
if CheckCanPlace(x, y, Obj) then |
|
273 |
begin |
|
274 |
ar[cnt].x:= x; |
|
275 |
ar[cnt].y:= y; |
|
276 |
inc(cnt); |
|
277 |
if cnt > MaxPointsIndex then // buffer is full, do not check the rest land |
|
278 |
begin |
|
279 |
y:= 5000; |
|
280 |
x:= 5000; |
|
281 |
end |
|
282 |
end; |
|
283 |
inc(y, 3); |
|
284 |
until y > 1023 - Height; |
|
285 |
inc(x, getrandom(6) + 3) |
|
286 |
until x > 2047 - Width; |
|
287 |
Result:= cnt <> 0; |
|
288 |
if Result then |
|
289 |
begin |
|
290 |
i:= getrandom(cnt); |
|
291 |
BlitImageAndGenerateCollisionInfo(ar[i].x, ar[i].y, Obj.Surf, Surface); |
|
292 |
AddRect(ar[i].x, ar[i].y, Width, Height); |
|
293 |
dec(Maxcnt) |
|
294 |
end else Maxcnt:= 0 |
|
351 | 295 |
end; |
296 |
TryPut:= Result |
|
184 | 297 |
end; |
298 |
||
299 |
function TryPut(var Obj: TSprayObject; Surface: PSDL_Surface): boolean; overload; |
|
300 |
const MaxPointsIndex = 8095; |
|
301 |
var x, y: Longword; |
|
302 |
ar: array[0..MaxPointsIndex] of TPoint; |
|
303 |
cnt, i: Longword; |
|
304 |
r: TSDL_Rect; |
|
351 | 305 |
Result: boolean; |
184 | 306 |
begin |
307 |
cnt:= 0; |
|
308 |
with Obj do |
|
309 |
begin |
|
310 |
if Maxcnt = 0 then |
|
351 | 311 |
exit(false); |
184 | 312 |
x:= 0; |
313 |
r.x:= 0; |
|
314 |
r.y:= 0; |
|
315 |
r.w:= Width; |
|
316 |
r.h:= Height + 16; |
|
317 |
repeat |
|
318 |
y:= 8; |
|
319 |
repeat |
|
320 |
if CheckLand(r, x, y - 8, $FFFFFF) |
|
321 |
and not CheckIntersect(x, y, Width, Height) then |
|
322 |
begin |
|
323 |
ar[cnt].x:= x; |
|
324 |
ar[cnt].y:= y; |
|
325 |
inc(cnt); |
|
326 |
if cnt > MaxPointsIndex then // buffer is full, do not check the rest land |
|
327 |
begin |
|
328 |
y:= 5000; |
|
329 |
x:= 5000; |
|
330 |
end |
|
331 |
end; |
|
332 |
inc(y, 12); |
|
333 |
until y > 1023 - Height - 8; |
|
334 |
inc(x, getrandom(12) + 12) |
|
335 |
until x > 2047 - Width; |
|
336 |
Result:= cnt <> 0; |
|
337 |
if Result then |
|
338 |
begin |
|
339 |
i:= getrandom(cnt); |
|
340 |
r.x:= ar[i].X; |
|
341 |
r.y:= ar[i].Y; |
|
342 |
r.w:= Width; |
|
343 |
r.h:= Height; |
|
344 |
SDL_UpperBlit(Obj.Surf, nil, Surface, @r); |
|
345 |
AddRect(ar[i].x - 32, ar[i].y - 32, Width + 64, Height + 64); |
|
346 |
dec(Maxcnt) |
|
347 |
end else Maxcnt:= 0 |
|
351 | 348 |
end; |
349 |
TryPut:= Result |
|
184 | 350 |
end; |
351 |
||
352 |
procedure ReadThemeInfo(var ThemeObjects: TThemeObjects; var SprayObjects: TSprayObjects); |
|
353 |
var s: string; |
|
354 |
f: textfile; |
|
371 | 355 |
i, ii: LongInt; |
805 | 356 |
vobcount: Longword; |
184 | 357 |
begin |
358 |
s:= Pathz[ptCurrTheme] + '/' + cThemeCFGFilename; |
|
359 |
WriteLnToConsole('Reading objects info...'); |
|
351 | 360 |
Assign(f, s); |
184 | 361 |
{$I-} |
362 |
Reset(f); |
|
780 | 363 |
Readln(f, s); // skip sky color |
364 |
Readln(f, s); // skip border color |
|
184 | 365 |
Readln(f, ThemeObjects.Count); |
366 |
for i:= 0 to Pred(ThemeObjects.Count) do |
|
367 |
begin |
|
368 |
Readln(f, s); // filename |
|
369 |
with ThemeObjects.objs[i] do |
|
370 |
begin |
|
351 | 371 |
Surf:= LoadImage(Pathz[ptCurrTheme] + '/' + s, false, true, true); |
372 |
Width:= Surf^.w; |
|
373 |
Height:= Surf^.h; |
|
184 | 374 |
with inland do Read(f, x, y, w, h); |
375 |
Read(f, rectcnt); |
|
376 |
for ii:= 1 to rectcnt do |
|
377 |
with outland[ii] do Read(f, x, y, w, h); |
|
378 |
Maxcnt:= 3; |
|
379 |
ReadLn(f) |
|
380 |
end; |
|
381 |
end; |
|
382 |
||
802
ed5450a89b96
Start implementing 'visual gears' - gears, that don't need to be synchronized (clouds and flakes)
unc0rr
parents:
780
diff
changeset
|
383 |
// sprays |
184 | 384 |
Readln(f, SprayObjects.Count); |
385 |
for i:= 0 to Pred(SprayObjects.Count) do |
|
386 |
begin |
|
387 |
Readln(f, s); // filename |
|
388 |
with SprayObjects.objs[i] do |
|
389 |
begin |
|
351 | 390 |
Surf:= LoadImage(Pathz[ptCurrTheme] + '/' + s, false, true, true); |
391 |
Width:= Surf^.w; |
|
392 |
Height:= Surf^.h; |
|
184 | 393 |
ReadLn(f, Maxcnt) |
394 |
end; |
|
395 |
end; |
|
802
ed5450a89b96
Start implementing 'visual gears' - gears, that don't need to be synchronized (clouds and flakes)
unc0rr
parents:
780
diff
changeset
|
396 |
|
ed5450a89b96
Start implementing 'visual gears' - gears, that don't need to be synchronized (clouds and flakes)
unc0rr
parents:
780
diff
changeset
|
397 |
// snowflakes |
805 | 398 |
Readln(f, vobCount); |
399 |
if vobCount > 0 then |
|
400 |
Readln(f, vobFramesCount, vobFrameTicks, vobVelocity, vobFallSpeed); |
|
401 |
||
402 |
for i:= 0 to Pred(vobCount) do |
|
806 | 403 |
AddVisualGear( -cScreenWidth + random(cScreenWidth * 2 + 2048), random(1200) - 100, vgtFlake); |
805 | 404 |
|
351 | 405 |
Close(f); |
184 | 406 |
{$I+} |
407 |
TryDo(IOResult = 0, 'Bad data or cannot access file ' + cThemeCFGFilename, true) |
|
408 |
end; |
|
409 |
||
371 | 410 |
procedure AddThemeObjects(Surface: PSDL_Surface; var ThemeObjects: TThemeObjects; MaxCount: LongInt); |
411 |
var i, ii, t: LongInt; |
|
184 | 412 |
b: boolean; |
413 |
begin |
|
414 |
if ThemeObjects.Count = 0 then exit; |
|
415 |
WriteLnToConsole('Adding theme objects...'); |
|
416 |
i:= 1; |
|
417 |
repeat |
|
418 |
t:= getrandom(ThemeObjects.Count); |
|
419 |
ii:= t; |
|
420 |
repeat |
|
421 |
inc(ii); |
|
422 |
if ii = ThemeObjects.Count then ii:= 0; |
|
423 |
b:= TryPut(ThemeObjects.objs[ii], Surface) |
|
424 |
until b or (ii = t); |
|
425 |
inc(i) |
|
426 |
until (i > MaxCount) or not b; |
|
427 |
end; |
|
428 |
||
429 |
procedure AddSprayObjects(Surface: PSDL_Surface; var SprayObjects: TSprayObjects; MaxCount: Longword); |
|
430 |
var i: Longword; |
|
371 | 431 |
ii, t: LongInt; |
184 | 432 |
b: boolean; |
433 |
begin |
|
434 |
if SprayObjects.Count = 0 then exit; |
|
435 |
WriteLnToConsole('Adding spray objects...'); |
|
436 |
i:= 1; |
|
437 |
repeat |
|
438 |
t:= getrandom(SprayObjects.Count); |
|
439 |
ii:= t; |
|
440 |
repeat |
|
441 |
inc(ii); |
|
442 |
if ii = SprayObjects.Count then ii:= 0; |
|
443 |
b:= TryPut(SprayObjects.objs[ii], Surface) |
|
444 |
until b or (ii = t); |
|
445 |
inc(i) |
|
446 |
until (i > MaxCount) or not b; |
|
447 |
end; |
|
448 |
||
449 |
procedure AddObjects(InSurface, Surface: PSDL_Surface); |
|
450 |
var ThemeObjects: TThemeObjects; |
|
451 |
SprayObjects: TSprayObjects; |
|
452 |
begin |
|
453 |
InitRects; |
|
454 |
AddGirder(256, Surface); |
|
455 |
AddGirder(512, Surface); |
|
456 |
AddGirder(768, Surface); |
|
457 |
AddGirder(1024, Surface); |
|
458 |
AddGirder(1280, Surface); |
|
459 |
AddGirder(1536, Surface); |
|
460 |
AddGirder(1792, Surface); |
|
461 |
ReadThemeInfo(ThemeObjects, SprayObjects); |
|
462 |
AddThemeObjects(Surface, ThemeObjects, 8); |
|
463 |
AddProgress; |
|
464 |
SDL_UpperBlit(InSurface, nil, Surface, nil); |
|
465 |
AddSprayObjects(Surface, SprayObjects, 10); |
|
466 |
FreeRects |
|
467 |
end; |
|
468 |
||
469 |
end. |