24
|
1 |
unit uLandObjects;
|
|
2 |
interface
|
|
3 |
uses SDLh;
|
|
4 |
{$include options.inc}
|
|
5 |
|
|
6 |
procedure AddObjects(Surface: PSDL_Surface);
|
|
7 |
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface);
|
|
8 |
|
|
9 |
implementation
|
|
10 |
uses uLand, uStore, uConsts, uMisc, uConsole, uRandom;
|
27
|
11 |
const MaxRects = 256;
|
|
12 |
MAXOBJECTRECTS = 16;
|
24
|
13 |
type PRectArray = ^TRectsArray;
|
|
14 |
TRectsArray = array[0..MaxRects] of TSDL_rect;
|
|
15 |
|
|
16 |
type TThemeObject = record
|
|
17 |
Surf: PSDL_Surface;
|
|
18 |
inland: TSDL_Rect;
|
|
19 |
outland: array[1..MAXOBJECTRECTS] of TSDL_Rect;
|
|
20 |
rectcnt: Longword;
|
|
21 |
Width, Height: Longword;
|
|
22 |
end;
|
|
23 |
|
|
24 |
var Rects: PRectArray;
|
|
25 |
RectCount: Longword;
|
|
26 |
|
|
27 |
procedure BlitImageAndGenerateCollisionInfo(cpX, cpY: Longword; Image, Surface: PSDL_Surface);
|
|
28 |
var i, p: LongWord;
|
|
29 |
x, y: Longword;
|
|
30 |
bpp: integer;
|
|
31 |
r: TSDL_Rect;
|
|
32 |
begin
|
|
33 |
r.x:= cpX;
|
|
34 |
r.y:= cpY;
|
|
35 |
SDL_UpperBlit(Image, nil, Surface, @r);
|
|
36 |
WriteToConsole('Generating collision info... ');
|
|
37 |
|
|
38 |
if SDL_MustLock(Image) then
|
|
39 |
SDLTry(SDL_LockSurface(Image) >= 0, true);
|
|
40 |
|
|
41 |
bpp:= Image.format.BytesPerPixel;
|
|
42 |
WriteToConsole('('+inttostr(bpp)+') ');
|
|
43 |
p:= LongWord(Image.pixels);
|
|
44 |
case bpp of
|
|
45 |
1: OutError('We don''t work with 8 bit surfaces', true);
|
|
46 |
2: for y:= 0 to Pred(Image.h) do
|
|
47 |
begin
|
|
48 |
i:= Longword(@Land[cpY + y, cpX]);
|
|
49 |
for x:= 0 to Pred(Image.w) do
|
|
50 |
if PWord(p + x * 2)^ <> 0 then PLongWord(i + x * 4)^:= $FFFFFF;
|
|
51 |
inc(p, Image.pitch);
|
|
52 |
end;
|
|
53 |
3: for y:= 0 to Pred(Image.h) do
|
|
54 |
begin
|
|
55 |
i:= Longword(@Land[cpY + y, cpX]);
|
|
56 |
for x:= 0 to Pred(Image.w) do
|
|
57 |
if (PByte(p + x * 3 + 0)^ <> 0)
|
|
58 |
or (PByte(p + x * 3 + 1)^ <> 0)
|
|
59 |
or (PByte(p + x * 3 + 2)^ <> 0) then PLongWord(i + x * 4)^:= $FFFFFF;
|
|
60 |
inc(p, Image.pitch);
|
|
61 |
end;
|
|
62 |
4: for y:= 0 to Pred(Image.h) do
|
|
63 |
begin
|
|
64 |
i:= Longword(@Land[cpY + y, cpX]);
|
|
65 |
for x:= 0 to Pred(Image.w) do
|
|
66 |
if PLongword(p + x * 4)^ <> 0 then PLongWord(i + x * 4)^:= $FFFFFF;
|
|
67 |
inc(p, Image.pitch);
|
|
68 |
end;
|
|
69 |
end;
|
|
70 |
if SDL_MustLock(Image) then
|
|
71 |
SDL_UnlockSurface(Image);
|
|
72 |
WriteLnToConsole(msgOK)
|
|
73 |
end;
|
|
74 |
|
|
75 |
procedure AddRect(x1, y1, w1, h1: integer);
|
|
76 |
begin
|
|
77 |
with Rects[RectCount] do
|
|
78 |
begin
|
|
79 |
x:= x1;
|
|
80 |
y:= y1;
|
|
81 |
w:= w1;
|
|
82 |
h:= h1
|
|
83 |
end;
|
|
84 |
inc(RectCount);
|
|
85 |
TryDo(RectCount < MaxRects, 'AddRect: overflow', true)
|
|
86 |
end;
|
|
87 |
|
|
88 |
procedure InitRects;
|
|
89 |
begin
|
|
90 |
RectCount:= 0;
|
|
91 |
New(Rects)
|
|
92 |
end;
|
|
93 |
|
|
94 |
procedure FreeRects;
|
|
95 |
begin
|
|
96 |
Dispose(rects)
|
|
97 |
end;
|
|
98 |
|
|
99 |
function CheckIntersect(x1, y1, w1, h1: integer): boolean;
|
|
100 |
var i: Longword;
|
|
101 |
begin
|
|
102 |
Result:= false;
|
|
103 |
i:= 0;
|
|
104 |
if RectCount > 0 then
|
|
105 |
repeat
|
|
106 |
with Rects[i] do
|
|
107 |
Result:= (x < x1 + w1) and (x1 < x + w) and
|
|
108 |
(y < y1 + h1) and (y1 < y + h);
|
|
109 |
inc(i)
|
|
110 |
until (i = RectCount) or (Result)
|
|
111 |
end;
|
|
112 |
|
27
|
113 |
function AddGirder(gX: integer; Surface: PSDL_Surface): boolean;
|
24
|
114 |
var tmpsurf: PSDL_Surface;
|
|
115 |
x1, x2, y, k, i: integer;
|
|
116 |
r, rr: TSDL_Rect;
|
|
117 |
|
|
118 |
function CountNonZeroz(x, y: integer): Longword;
|
|
119 |
var i: integer;
|
|
120 |
begin
|
|
121 |
Result:= 0;
|
|
122 |
for i:= y to y + 15 do
|
|
123 |
if Land[i, x] <> 0 then inc(Result)
|
|
124 |
end;
|
|
125 |
|
|
126 |
begin
|
27
|
127 |
y:= 150;
|
24
|
128 |
repeat
|
|
129 |
inc(y, 24);
|
27
|
130 |
x1:= gX;
|
|
131 |
x2:= gX;
|
24
|
132 |
while (x1 > 100) and (CountNonZeroz(x1, y) = 0) do dec(x1, 2);
|
|
133 |
i:= x1 - 12;
|
|
134 |
repeat
|
|
135 |
k:= CountNonZeroz(x1, y);
|
|
136 |
dec(x1, 2)
|
|
137 |
until (x1 < 100) or (k = 0) or (k = 16) or (x1 < i);
|
|
138 |
inc(x1, 2);
|
|
139 |
if k = 16 then
|
|
140 |
begin
|
|
141 |
while (x2 < 1900) and (CountNonZeroz(x2, y) = 0) do inc(x2, 2);
|
|
142 |
i:= x2 + 12;
|
|
143 |
repeat
|
|
144 |
k:= CountNonZeroz(x2, y);
|
|
145 |
inc(x2, 2)
|
|
146 |
until (x2 > 1900) or (k = 0) or (k = 16) or (x2 > i);
|
|
147 |
if (x2 < 1900) and (k = 16) and (x2 - x1 > 250)
|
|
148 |
and not CheckIntersect(x1, y, x2 - x1, 16) then break;
|
|
149 |
end;
|
|
150 |
x1:= 0;
|
|
151 |
until y > 900;
|
|
152 |
if x1 > 0 then
|
|
153 |
begin
|
27
|
154 |
Result:= true;
|
24
|
155 |
tmpsurf:= LoadImage(Pathz[ptGraphics] + 'Girder.png');
|
|
156 |
rr.x:= x1;
|
|
157 |
rr.y:= y;
|
|
158 |
while rr.x + 100 < x2 do
|
|
159 |
begin
|
|
160 |
SDL_UpperBlit(tmpsurf, nil, Surface, @rr);
|
|
161 |
inc(rr.x, 100);
|
|
162 |
end;
|
|
163 |
r.x:= 0;
|
|
164 |
r.y:= 0;
|
|
165 |
r.w:= x2 - rr.x;
|
|
166 |
r.h:= 16;
|
|
167 |
SDL_UpperBlit(tmpsurf, @r, Surface, @rr);
|
|
168 |
SDL_FreeSurface(tmpsurf);
|
27
|
169 |
AddRect(x1 - 8, y - 8, x2 - x1 + 16, 72);
|
24
|
170 |
for k:= y to y + 15 do
|
|
171 |
for i:= x1 to x2 do Land[k, i]:= $FFFFFF
|
27
|
172 |
end else Result:= false
|
24
|
173 |
end;
|
|
174 |
|
|
175 |
function CheckLand(rect: TSDL_Rect; dX, dY, Color: Longword): boolean;
|
|
176 |
var i: Longword;
|
|
177 |
begin
|
|
178 |
Result:= true;
|
|
179 |
inc(rect.x, dX);
|
|
180 |
inc(rect.y, dY);
|
|
181 |
i:= 0;
|
|
182 |
{$WARNINGS OFF}
|
|
183 |
while (i <= rect.w) and Result do
|
|
184 |
begin
|
|
185 |
Result:= (Land[rect.y, rect.x + i] = Color) and (Land[rect.y + rect.h, rect.x + i] = Color);
|
|
186 |
inc(i)
|
|
187 |
end;
|
|
188 |
i:= 0;
|
|
189 |
while (i <= rect.h) and Result do
|
|
190 |
begin
|
27
|
191 |
Result:= (Land[rect.y + i, rect.x] = Color) and (Land[rect.y + i, rect.x + rect.w] = Color);
|
24
|
192 |
inc(i)
|
|
193 |
end;
|
|
194 |
{$WARNINGS ON}
|
|
195 |
end;
|
|
196 |
|
|
197 |
function CheckCanPlace(x, y: Longword; var Obj: TThemeObject): boolean;
|
|
198 |
var i: Longword;
|
|
199 |
begin
|
|
200 |
with Obj do
|
|
201 |
if CheckLand(inland, x, y, $FFFFFF) then
|
|
202 |
begin
|
|
203 |
Result:= true;
|
|
204 |
i:= 1;
|
|
205 |
while Result and (i <= rectcnt) do
|
|
206 |
begin
|
|
207 |
Result:= CheckLand(outland[i], x, y, 0);
|
|
208 |
inc(i)
|
|
209 |
end;
|
|
210 |
if Result then
|
|
211 |
Result:= not CheckIntersect(x, y, Width, Height)
|
|
212 |
end else
|
|
213 |
Result:= false
|
|
214 |
end;
|
|
215 |
|
|
216 |
function TryPut(var Obj: TThemeObject; Surface: PSDL_Surface): boolean;
|
|
217 |
const MaxPointsIndex = 2047;
|
|
218 |
var x, y: Longword;
|
|
219 |
ar: array[0..MaxPointsIndex] of TPoint;
|
|
220 |
cnt, i: Longword;
|
|
221 |
begin
|
|
222 |
cnt:= 0;
|
|
223 |
with Obj do
|
|
224 |
begin
|
|
225 |
x:= 0;
|
|
226 |
repeat
|
|
227 |
y:= 0;
|
|
228 |
repeat
|
|
229 |
if CheckCanPlace(x, y, Obj) then
|
|
230 |
begin
|
|
231 |
ar[cnt].x:= x;
|
|
232 |
ar[cnt].y:= y;
|
|
233 |
inc(cnt);
|
|
234 |
if cnt > MaxPointsIndex then // buffer is full, do not check the rest land
|
|
235 |
begin
|
|
236 |
y:= 5000;
|
|
237 |
x:= 5000;
|
|
238 |
end
|
|
239 |
end;
|
27
|
240 |
inc(y, 3);
|
24
|
241 |
until y > 1023 - Height;
|
27
|
242 |
inc(x, getrandom(6) + 3)
|
24
|
243 |
until x > 2047 - Width;
|
|
244 |
Result:= cnt <> 0;
|
|
245 |
if Result then
|
|
246 |
begin
|
|
247 |
i:= getrandom(cnt);
|
|
248 |
BlitImageAndGenerateCollisionInfo(ar[i].x, ar[i].y, Obj.Surf, Surface);
|
|
249 |
AddRect(ar[i].x, ar[i].y, Width, Height);
|
|
250 |
end
|
|
251 |
end
|
|
252 |
end;
|
|
253 |
|
|
254 |
procedure AddThemeObjects(Surface: PSDL_Surface; MaxCount: Longword);
|
27
|
255 |
const MAXTHEMEOBJECTS = 32;
|
24
|
256 |
var f: textfile;
|
|
257 |
s: string;
|
|
258 |
ThemeObjects: array[1..MAXTHEMEOBJECTS] of TThemeObject;
|
|
259 |
i, ii, t, n: Longword;
|
|
260 |
b: boolean;
|
|
261 |
begin
|
|
262 |
s:= Pathz[ptThemeCurrent] + cThemeCFGFilename;
|
|
263 |
WriteLnToConsole('Adding objects...');
|
|
264 |
AssignFile(f, s);
|
|
265 |
{$I-}
|
|
266 |
Reset(f);
|
|
267 |
Readln(f, s); // skip color
|
|
268 |
Readln(f, n);
|
|
269 |
for i:= 1 to n do
|
|
270 |
begin
|
|
271 |
Readln(f, s); // filename
|
|
272 |
with ThemeObjects[i] do
|
|
273 |
begin
|
|
274 |
Surf:= LoadImage(Pathz[ptThemeCurrent] + s + '.png');
|
|
275 |
Read(f, Width, Height);
|
|
276 |
with inland do Read(f, x, y, w, h);
|
|
277 |
Read(f, rectcnt);
|
|
278 |
for ii:= 1 to rectcnt do
|
|
279 |
with outland[ii] do Read(f, x, y, w, h);
|
|
280 |
ReadLn(f)
|
|
281 |
end;
|
|
282 |
end;
|
|
283 |
Closefile(f);
|
|
284 |
{$I+}
|
|
285 |
TryDo(IOResult = 0, 'Bad data or cannot access file', true);
|
|
286 |
|
|
287 |
// loaded objects, try to put on land
|
|
288 |
if n = 0 then exit;
|
|
289 |
i:= 1;
|
|
290 |
repeat
|
|
291 |
t:= getrandom(n) + 1;
|
|
292 |
ii:= t;
|
|
293 |
repeat
|
|
294 |
inc(ii);
|
|
295 |
if ii > n then ii:= 1;
|
|
296 |
b:= TryPut(ThemeObjects[ii], Surface)
|
|
297 |
until b or (ii = t);
|
|
298 |
inc(i)
|
|
299 |
until (i > MaxCount) or not b
|
|
300 |
end;
|
|
301 |
|
|
302 |
procedure AddObjects(Surface: PSDL_Surface);
|
|
303 |
begin
|
|
304 |
InitRects;
|
27
|
305 |
AddGirder(512, Surface);
|
|
306 |
AddGirder(1024, Surface);
|
|
307 |
AddGirder(1300, Surface);
|
|
308 |
AddGirder(1536, Surface);
|
|
309 |
AddThemeObjects(Surface, 8);
|
24
|
310 |
FreeRects
|
|
311 |
end;
|
|
312 |
|
|
313 |
end.
|