4
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* Distributed under the terms of the BSD-modified licence:
|
|
6 |
*
|
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8 |
* of this software and associated documentation files (the "Software"), to deal
|
|
9 |
* with the Software without restriction, including without limitation the
|
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
12 |
* furnished to do so, subject to the following conditions:
|
|
13 |
*
|
|
14 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
15 |
* this list of conditions and the following disclaimer.
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
17 |
* this list of conditions and the following disclaimer in the documentation
|
|
18 |
* and/or other materials provided with the distribution.
|
|
19 |
* 3. The name of the author may not be used to endorse or promote products
|
|
20 |
* derived from this software without specific prior written permission.
|
|
21 |
*
|
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32 |
*)
|
|
33 |
|
|
34 |
unit uStore;
|
|
35 |
interface
|
|
36 |
uses uConsts, uTeams, SDLh;
|
|
37 |
{$INCLUDE options.inc}
|
|
38 |
|
|
39 |
type PRangeArray = ^TRangeArray;
|
|
40 |
TRangeArray = array[byte] of record
|
|
41 |
Left, Right: integer;
|
|
42 |
end;
|
|
43 |
|
|
44 |
procedure StoreInit;
|
|
45 |
procedure StoreLoad;
|
|
46 |
procedure StoreRelease;
|
|
47 |
procedure DrawGear(Stuff : TStuff; X, Y: integer; Surface: PSDL_Surface);
|
|
48 |
procedure DrawSpriteFromRect(r: TSDL_Rect; X, Y, Height, Position: integer; Surface: PSDL_Surface);
|
|
49 |
procedure DrawSprite (Sprite: TSprite; X, Y, Position: integer; Surface: PSDL_Surface);
|
|
50 |
procedure DrawLand (X, Y: integer; Surface: PSDL_Surface);
|
|
51 |
procedure DXOutText(X, Y: Integer; Font: THWFont; s: string; Surface: PSDL_Surface);
|
|
52 |
procedure DrawCaption(X, Y: integer; Rect: TSDL_Rect; Surface: PSDL_Surface; const fromTempSurf: boolean = false);
|
|
53 |
procedure DrawHedgehog(X, Y: integer; Dir: integer; Pos, Step: LongWord; Surface: PSDL_Surface);
|
|
54 |
procedure DrawExplosion(X, Y, Radius: integer);
|
38
|
55 |
procedure DrawHLineExplosions(ar: PRangeArray; Radius: Longword; y, dY: integer; Count: Byte);
|
|
56 |
procedure DrawTunnel(X, Y, dX, dY: real; ticks, HalfWidth: integer);
|
4
|
57 |
procedure RenderHealth(var Hedgehog: THedgehog);
|
|
58 |
function RenderString(var s: shortstring; Color, Pos: integer): TSDL_Rect;
|
|
59 |
procedure AddProgress;
|
35
|
60 |
function LoadImage(filename: string; hasAlpha: boolean): PSDL_Surface;
|
4
|
61 |
|
|
62 |
var PixelFormat: PSDL_PixelFormat;
|
|
63 |
SDLPrimSurface: PSDL_Surface;
|
|
64 |
|
|
65 |
implementation
|
|
66 |
uses uMisc, uIO, uConsole, uLand;
|
|
67 |
|
|
68 |
var StoreSurface,
|
|
69 |
TempSurface,
|
|
70 |
HHSurface: PSDL_Surface;
|
|
71 |
|
|
72 |
procedure DrawExplosion(X, Y, Radius: integer);
|
38
|
73 |
var ty, tx, p: integer;
|
4
|
74 |
begin
|
|
75 |
for ty:= max(-Radius, -y) to min(radius, 1023 - y) do
|
|
76 |
for tx:= max(0, round(x-radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(x+radius*sqrt(1-sqr(ty/radius)))) do
|
|
77 |
Land[ty + y, tx]:= 0;
|
|
78 |
|
|
79 |
if SDL_MustLock(LandSurface) then
|
|
80 |
SDLTry(SDL_LockSurface(LandSurface) >= 0, true);
|
|
81 |
|
38
|
82 |
p:= integer(LandSurface.pixels);
|
4
|
83 |
case LandSurface.format.BytesPerPixel of
|
|
84 |
1: ;// not supported
|
|
85 |
2: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
86 |
for tx:= max(0, round(x-radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(x+radius*sqrt(1-sqr(ty/radius)))) do
|
|
87 |
PWord(p + LandSurface.pitch*(y + ty) + tx * 2)^:= 0;
|
|
88 |
3: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
89 |
for tx:= max(0, round(x-radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(x+radius*sqrt(1-sqr(ty/radius)))) do
|
|
90 |
begin
|
|
91 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 0)^:= 0;
|
|
92 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 1)^:= 0;
|
|
93 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 2)^:= 0;
|
|
94 |
end;
|
|
95 |
4: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
96 |
for tx:= max(0, round(x-radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(x+radius*sqrt(1-sqr(ty/radius)))) do
|
|
97 |
PLongword(p + LandSurface.pitch*(y + ty) + tx * 4)^:= 0;
|
|
98 |
end;
|
|
99 |
|
|
100 |
inc(Radius, 4);
|
|
101 |
|
|
102 |
case LandSurface.format.BytesPerPixel of
|
|
103 |
1: ;// not supported
|
|
104 |
2: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
105 |
for tx:= max(0, round(x-radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(x+radius*sqrt(1-sqr(ty/radius)))) do
|
38
|
106 |
if Land[y + ty, tx] <> 0 then
|
4
|
107 |
PWord(p + LandSurface.pitch*(y + ty) + tx * 2)^:= cExplosionBorderColor;
|
|
108 |
3: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
109 |
for tx:= max(0, round(x-radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(x+radius*sqrt(1-sqr(ty/radius)))) do
|
38
|
110 |
if Land[y + ty, tx] <> 0 then
|
|
111 |
begin
|
4
|
112 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 0)^:= cExplosionBorderColor and $FF;
|
|
113 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 1)^:= (cExplosionBorderColor shr 8) and $FF;
|
|
114 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 2)^:= (cExplosionBorderColor shr 16);
|
|
115 |
end;
|
|
116 |
4: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
117 |
for tx:= max(0, round(x-radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(x+radius*sqrt(1-sqr(ty/radius)))) do
|
38
|
118 |
if Land[y + ty, tx] <> 0 then
|
4
|
119 |
PLongword(p + LandSurface.pitch*(y + ty) + tx * 4)^:= cExplosionBorderColor;
|
|
120 |
end;
|
|
121 |
|
|
122 |
if SDL_MustLock(LandSurface) then
|
|
123 |
SDL_UnlockSurface(LandSurface);
|
|
124 |
|
|
125 |
SDL_UpdateRect(LandSurface, X - Radius, Y - Radius, Radius * 2, Radius * 2)
|
|
126 |
end;
|
|
127 |
|
38
|
128 |
procedure DrawHLineExplosions(ar: PRangeArray; Radius: Longword; y, dY: integer; Count: Byte);
|
4
|
129 |
var tx, ty, i, p: integer;
|
|
130 |
begin
|
|
131 |
if SDL_MustLock(LandSurface) then
|
|
132 |
SDL_LockSurface(LandSurface);
|
|
133 |
|
38
|
134 |
p:= integer(LandSurface.pixels);
|
4
|
135 |
for i:= 0 to Pred(Count) do
|
|
136 |
begin
|
|
137 |
case LandSurface.format.BytesPerPixel of
|
|
138 |
1: ;
|
|
139 |
2: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
140 |
for tx:= max(0, round(ar[i].Left - radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(ar[i].Right + radius*sqrt(1-sqr(ty/radius)))) do
|
|
141 |
PWord(p + LandSurface.pitch*(y + ty) + tx * 2)^:= 0;
|
|
142 |
3: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
143 |
for tx:= max(0, round(ar[i].Left - radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(ar[i].Right + radius*sqrt(1-sqr(ty/radius)))) do
|
|
144 |
begin
|
|
145 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 0)^:= 0;
|
|
146 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 1)^:= 0;
|
|
147 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 2)^:= 0;
|
|
148 |
end;
|
|
149 |
4: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
150 |
for tx:= max(0, round(ar[i].Left - radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(ar[i].Right + radius*sqrt(1-sqr(ty/radius)))) do
|
|
151 |
PLongword(p + LandSurface.pitch*(y + ty) + tx * 4)^:= 0;
|
|
152 |
end;
|
|
153 |
inc(y, dY)
|
|
154 |
end;
|
|
155 |
|
|
156 |
inc(Radius, 4);
|
|
157 |
dec(y, Count*dY);
|
|
158 |
|
|
159 |
for i:= 0 to Pred(Count) do
|
|
160 |
begin
|
|
161 |
case LandSurface.format.BytesPerPixel of
|
|
162 |
1: ;
|
|
163 |
2: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
164 |
for tx:= max(0, round(ar[i].Left - radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(ar[i].Right + radius*sqrt(1-sqr(ty/radius)))) do
|
38
|
165 |
if Land[y + ty, tx] <> 0 then
|
4
|
166 |
PWord(p + LandSurface.pitch*(y + ty) + tx * 2)^:= cExplosionBorderColor;
|
|
167 |
3: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
168 |
for tx:= max(0, round(ar[i].Left - radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(ar[i].Right + radius*sqrt(1-sqr(ty/radius)))) do
|
38
|
169 |
if Land[y + ty, tx] <> 0 then
|
|
170 |
begin
|
4
|
171 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 0)^:= cExplosionBorderColor and $FF;
|
|
172 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 1)^:= (cExplosionBorderColor shr 8) and $FF;
|
|
173 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 2)^:= (cExplosionBorderColor shr 16);
|
|
174 |
end;
|
|
175 |
4: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
176 |
for tx:= max(0, round(ar[i].Left - radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(ar[i].Right + radius*sqrt(1-sqr(ty/radius)))) do
|
38
|
177 |
if Land[y + ty, tx] <> 0 then
|
4
|
178 |
PLongword(p + LandSurface.pitch*(y + ty) + tx * 4)^:= cExplosionBorderColor;
|
|
179 |
end;
|
|
180 |
inc(y, dY)
|
|
181 |
end;
|
|
182 |
|
|
183 |
if SDL_MustLock(LandSurface) then
|
|
184 |
SDL_UnlockSurface(LandSurface);
|
|
185 |
end;
|
|
186 |
|
38
|
187 |
//
|
|
188 |
// - (dX, dY) - direction, vector of length = 0.5
|
|
189 |
//
|
|
190 |
procedure DrawTunnel(X, Y, dX, dY: real; ticks, HalfWidth: integer);
|
|
191 |
var nx, ny: real;
|
|
192 |
i, t, tx, ty, p: integer;
|
|
193 |
begin // (-dY, dX) is (dX, dY) turned by PI/2
|
|
194 |
if SDL_MustLock(LandSurface) then
|
|
195 |
SDL_LockSurface(LandSurface);
|
|
196 |
|
|
197 |
nx:= X + dY * (HalfWidth + 8);
|
|
198 |
ny:= Y - dX * (HalfWidth + 8);
|
|
199 |
p:= integer(LandSurface.pixels);
|
|
200 |
|
|
201 |
for i:= 0 to 7 do
|
|
202 |
begin
|
|
203 |
X:= nx - 8 * dX;
|
|
204 |
Y:= ny - 8 * dY;
|
|
205 |
for t:= -8 to ticks + 8 do
|
|
206 |
{$include tunsetborder.inc}
|
|
207 |
nx:= nx - dY;
|
|
208 |
ny:= ny + dX;
|
|
209 |
end;
|
|
210 |
|
|
211 |
for i:= -HalfWidth to HalfWidth do
|
|
212 |
begin
|
|
213 |
X:= nx - dX * 8;
|
|
214 |
Y:= ny - dY * 8;
|
|
215 |
for t:= 0 to 7 do
|
|
216 |
{$include tunsetborder.inc}
|
|
217 |
X:= nx;
|
|
218 |
Y:= ny;
|
|
219 |
for t:= 0 to ticks do
|
|
220 |
begin
|
|
221 |
X:= X + dX;
|
|
222 |
Y:= Y + dY;
|
|
223 |
tx:= round(X);
|
|
224 |
ty:= round(Y);
|
|
225 |
if ((ty and $FFFFFC00) = 0) and ((tx and $FFFFF800) = 0) then
|
|
226 |
begin
|
|
227 |
Land[ty, tx]:= 0;
|
|
228 |
case LandSurface.format.BytesPerPixel of
|
|
229 |
1: ;
|
|
230 |
2: PWord(p + LandSurface.pitch * ty + tx * 2)^:= 0;
|
|
231 |
3: begin
|
|
232 |
PByte(p + LandSurface.pitch * ty + tx * 3 + 0)^:= 0;
|
|
233 |
PByte(p + LandSurface.pitch * ty + tx * 3 + 1)^:= 0;
|
|
234 |
PByte(p + LandSurface.pitch * ty + tx * 3 + 2)^:= 0;
|
|
235 |
end;
|
|
236 |
4: PLongword(p + LandSurface.pitch * ty + tx * 4)^:= 0;
|
|
237 |
end
|
|
238 |
end
|
|
239 |
end;
|
|
240 |
for t:= 0 to 7 do
|
|
241 |
{$include tunsetborder.inc}
|
|
242 |
nx:= nx - dY;
|
|
243 |
ny:= ny + dX;
|
|
244 |
end;
|
|
245 |
|
|
246 |
for i:= 0 to 7 do
|
|
247 |
begin
|
|
248 |
X:= nx - 8 * dX;
|
|
249 |
Y:= ny - 8 * dY;
|
|
250 |
for t:= -8 to ticks + 8 do
|
|
251 |
{$include tunsetborder.inc}
|
|
252 |
nx:= nx - dY;
|
|
253 |
ny:= ny + dX;
|
|
254 |
end;
|
|
255 |
|
|
256 |
if SDL_MustLock(LandSurface) then
|
|
257 |
SDL_UnlockSurface(LandSurface)
|
|
258 |
end;
|
|
259 |
|
4
|
260 |
procedure StoreInit;
|
|
261 |
begin
|
|
262 |
StoreSurface := SDL_CreateRGBSurface(SDL_HWSURFACE, 576, 1024, cBits, PixelFormat.RMask, PixelFormat.GMask, PixelFormat.BMask, 0);
|
|
263 |
TryDo( StoreSurface <> nil, errmsgCreateSurface + ': store' , true);
|
37
|
264 |
SDL_FillRect(StoreSurface, nil, 0);
|
4
|
265 |
|
|
266 |
TempSurface := SDL_CreateRGBSurface(SDL_HWSURFACE, 724, 320, cBits, PixelFormat.RMask, PixelFormat.GMask, PixelFormat.BMask, 0);
|
|
267 |
TryDo( TempSurface <> nil, errmsgCreateSurface + ': temp' , true);
|
|
268 |
|
|
269 |
TryDo(SDL_SetColorKey( StoreSurface, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true);
|
|
270 |
//TryDo(SDL_SetColorKey(SpriteSurface, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true);
|
|
271 |
TryDo(SDL_SetColorKey( TempSurface, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true);
|
|
272 |
end;
|
|
273 |
|
|
274 |
procedure LoadToSurface(Filename: String; Surface: PSDL_Surface; X, Y: integer);
|
|
275 |
var tmpsurf: PSDL_Surface;
|
|
276 |
rr: TSDL_Rect;
|
|
277 |
begin
|
35
|
278 |
tmpsurf:= LoadImage(Filename, false);
|
4
|
279 |
rr.x:= X;
|
|
280 |
rr.y:= Y;
|
|
281 |
SDL_UpperBlit(tmpsurf, nil, Surface, @rr);
|
|
282 |
SDL_FreeSurface(tmpsurf);
|
|
283 |
end;
|
|
284 |
|
|
285 |
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: integer; Color: LongWord; Font: THWFont; s: string): TSDL_Rect;
|
|
286 |
var w, h: integer;
|
|
287 |
tmpsurf: PSDL_Surface;
|
|
288 |
clr: TSDL_Color;
|
|
289 |
begin
|
|
290 |
TTF_SizeText(Fontz[Font].Handle, PChar(s), w, h);
|
|
291 |
Result.x:= X;
|
|
292 |
Result.y:= Y;
|
|
293 |
Result.w:= w + 6;
|
|
294 |
Result.h:= h + 6;
|
|
295 |
SDL_FillRect(Surface, @Result, 0);
|
|
296 |
Result.w:= 1;
|
|
297 |
Result.y:= Y + 1;
|
|
298 |
Result.h:= h + 4;
|
|
299 |
SDL_FillRect(Surface, @Result, cWhiteColor);
|
|
300 |
Result.x:= X + w + 5;
|
|
301 |
SDL_FillRect(Surface, @Result, cWhiteColor);
|
|
302 |
Result.x:= X + 1;
|
|
303 |
Result.w:= w + 4;
|
|
304 |
Result.y:= Y;
|
|
305 |
Result.h:= 1;
|
|
306 |
SDL_FillRect(Surface, @Result, cWhiteColor);
|
|
307 |
Result.y:= Y + h + 5;
|
|
308 |
SDL_FillRect(Surface, @Result, cWhiteColor);
|
|
309 |
Result.x:= X + 1;
|
|
310 |
Result.y:= Y + 1;
|
|
311 |
Result.h:= h + 4;
|
|
312 |
SDL_FillRect(Surface, @Result, cColorNearBlack);
|
|
313 |
SDL_GetRGB(Color, Surface.format, @clr.r, @clr.g, @clr.b);
|
|
314 |
tmpsurf:= TTF_RenderText_Blended(Fontz[Font].Handle, PChar(s), clr);
|
|
315 |
Result.x:= X + 3;
|
|
316 |
Result.y:= Y + 3;
|
|
317 |
SDL_UpperBlit(tmpsurf, nil, Surface, @Result);
|
|
318 |
SDL_FreeSurface(tmpsurf);
|
|
319 |
Result.x:= X;
|
|
320 |
Result.y:= Y;
|
|
321 |
Result.w:= w + 6;
|
|
322 |
Result.h:= h + 6
|
|
323 |
end;
|
|
324 |
|
|
325 |
procedure StoreLoad;
|
|
326 |
var i: TStuff;
|
|
327 |
ii: TSprite;
|
|
328 |
fi: THWFont;
|
|
329 |
s: string;
|
|
330 |
tmpsurf: PSDL_Surface;
|
|
331 |
|
|
332 |
procedure WriteNames(Font: THWFont);
|
|
333 |
var Team: PTeam;
|
|
334 |
i: integer;
|
|
335 |
r: TSDL_Rect;
|
|
336 |
begin
|
|
337 |
r.x:= 0;
|
|
338 |
r.y:= 272;
|
|
339 |
Team:= TeamsList;
|
|
340 |
while Team<>nil do
|
|
341 |
begin
|
|
342 |
r.w:= 1968;
|
|
343 |
r:= WriteInRoundRect(StoreSurface, r.x, r.y, Team.Color, Font, Team.TeamName);
|
|
344 |
Team.NameRect:= r;
|
|
345 |
inc(r.y, r.h);
|
|
346 |
for i:= 0 to 7 do
|
|
347 |
if Team.Hedgehogs[i].Gear<>nil then
|
|
348 |
begin
|
|
349 |
r:= WriteInRoundRect(StoreSurface, r.x, r.y, Team.Color, Font, Team.Hedgehogs[i].Name);
|
|
350 |
Team.Hedgehogs[i].NameRect:= r;
|
|
351 |
inc(r.y, r.h)
|
|
352 |
end;
|
|
353 |
Team:= Team.Next
|
|
354 |
end;
|
|
355 |
end;
|
|
356 |
|
|
357 |
procedure MakeCrossHairs;
|
|
358 |
var Team: PTeam;
|
|
359 |
r: TSDL_Rect;
|
|
360 |
tmpsurf: PSDL_Surface;
|
|
361 |
s: string;
|
|
362 |
TransColor: Longword;
|
|
363 |
begin
|
|
364 |
r.x:= 0;
|
|
365 |
r.y:= 256;
|
|
366 |
r.w:= 16;
|
|
367 |
r.h:= 16;
|
|
368 |
s:= Pathz[ptGraphics] + cCHFileName;
|
|
369 |
WriteToConsole(msgLoading + s + ' ');
|
|
370 |
tmpsurf:= IMG_Load(PChar(s));
|
|
371 |
TryDo(tmpsurf <> nil, msgFailed, true);
|
|
372 |
WriteLnToConsole(msgOK);
|
|
373 |
TransColor:= SDL_MapRGB(tmpsurf.format, $FF, $FF, $FF);
|
|
374 |
TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, TransColor) = 0, errmsgTransparentSet, true);
|
|
375 |
|
|
376 |
Team:= TeamsList;
|
|
377 |
while Team<>nil do
|
|
378 |
begin
|
|
379 |
SDL_FillRect(StoreSurface, @r, Team.Color);
|
|
380 |
SDL_UpperBlit(tmpsurf, nil, StoreSurface, @r);
|
|
381 |
Team.CrossHairRect:= r;
|
|
382 |
inc(r.x, 16);
|
|
383 |
Team:= Team.Next
|
|
384 |
end;
|
|
385 |
|
|
386 |
SDL_FreeSurface(tmpsurf)
|
|
387 |
end;
|
|
388 |
|
|
389 |
procedure InitHealth;
|
|
390 |
var p: PTeam;
|
|
391 |
i, t: integer;
|
|
392 |
begin
|
|
393 |
p:= TeamsList;
|
|
394 |
t:= 0;
|
|
395 |
while p <> nil do
|
|
396 |
begin
|
|
397 |
for i:= 0 to cMaxHHIndex do
|
|
398 |
if p.Hedgehogs[i].Gear <> nil then
|
|
399 |
begin
|
|
400 |
p.Hedgehogs[i].HealthRect.y:= t;
|
|
401 |
RenderHealth(p.Hedgehogs[i]);
|
|
402 |
inc(t, p.Hedgehogs[i].HealthRect.h)
|
|
403 |
end;
|
|
404 |
p:= p.Next
|
|
405 |
end
|
|
406 |
end;
|
|
407 |
|
|
408 |
procedure LoadGraves;
|
|
409 |
var p: PTeam;
|
|
410 |
l: integer;
|
|
411 |
begin
|
|
412 |
p:= TeamsList;
|
|
413 |
l:= 512;
|
|
414 |
while p <> nil do
|
|
415 |
begin
|
|
416 |
dec(l, 32);
|
|
417 |
if p.GraveName = '' then p.GraveName:= 'Simple';
|
|
418 |
LoadToSurface(Pathz[ptGraves] + p.GraveName + '.png', StoreSurface, l, 512);
|
|
419 |
p.GraveRect.x:= l;
|
|
420 |
p.GraveRect.y:= 512;
|
|
421 |
p.GraveRect.w:= 32;
|
|
422 |
p.GraveRect.h:= 256;
|
|
423 |
p:= p.Next
|
|
424 |
end
|
|
425 |
end;
|
|
426 |
|
|
427 |
procedure GetSkyColor;
|
|
428 |
var p: Longword;
|
|
429 |
begin
|
|
430 |
if SDL_MustLock(StoreSurface) then
|
|
431 |
SDLTry(SDL_LockSurface(StoreSurface) >= 0, true);
|
|
432 |
p:= Longword(StoreSurface.pixels) + Word(StuffPoz[sSky].x) * StoreSurface.format.BytesPerPixel;
|
|
433 |
case StoreSurface.format.BytesPerPixel of
|
|
434 |
1: cSkyColor:= PByte(p)^;
|
|
435 |
2: cSkyColor:= PWord(p)^;
|
|
436 |
3: cSkyColor:= (PByte(p)^) or (PByte(p + 1)^ shl 8) or (PByte(p + 2)^ shl 16);
|
|
437 |
4: cSkyColor:= PLongword(p)^;
|
|
438 |
end;
|
|
439 |
if SDL_MustLock(StoreSurface) then
|
|
440 |
SDL_UnlockSurface(StoreSurface)
|
|
441 |
end;
|
|
442 |
|
|
443 |
procedure GetExplosionBorderColor;
|
|
444 |
var f: textfile;
|
|
445 |
c: integer;
|
|
446 |
begin
|
|
447 |
s:= Pathz[ptThemeCurrent] + cThemeCFGFilename;
|
|
448 |
WriteToConsole(msgLoading + s + ' ');
|
|
449 |
AssignFile(f, s);
|
|
450 |
{$I-}
|
|
451 |
Reset(f);
|
|
452 |
Readln(f, s);
|
|
453 |
Closefile(f);
|
|
454 |
{$I+}
|
|
455 |
TryDo(IOResult = 0, msgFailed, true);
|
|
456 |
WriteLnToConsole(msgOK);
|
|
457 |
val(s, cExplosionBorderColor, c);
|
|
458 |
if cFullScreen then
|
|
459 |
cExplosionBorderColor:= SDL_MapRGB(PixelFormat, (cExplosionBorderColor shr 16) and $FF,
|
|
460 |
(cExplosionBorderColor shr 8) and $FF,
|
|
461 |
cExplosionBorderColor and $FF)
|
|
462 |
else
|
|
463 |
cExplosionBorderColor:= SDL_MapRGB(LandSurface.format, (cExplosionBorderColor shr 16) and $FF,
|
|
464 |
(cExplosionBorderColor shr 8) and $FF,
|
|
465 |
cExplosionBorderColor and $FF)
|
|
466 |
end;
|
|
467 |
|
|
468 |
begin
|
|
469 |
for fi:= Low(THWFont) to High(THWFont) do
|
|
470 |
with Fontz[fi] do
|
|
471 |
begin
|
|
472 |
s:= Pathz[ptFonts] + Name;
|
|
473 |
WriteToConsole(msgLoading + s + ' ');
|
|
474 |
Handle:= TTF_OpenFont(PChar(s), Height);
|
|
475 |
TryDo(Handle <> nil, msgFailed, true);
|
|
476 |
WriteLnToConsole(msgOK)
|
|
477 |
end;
|
|
478 |
AddProgress;
|
35
|
479 |
//s:= Pathz[ptMapCurrent] + cLandFileName;
|
|
480 |
//WriteToConsole(msgLoading + s + ' ');
|
4
|
481 |
//tmpsurf:= IMG_Load(PChar(s));
|
|
482 |
tmpsurf:= LandSurface;
|
|
483 |
TryDo(tmpsurf <> nil, msgFailed, true);
|
|
484 |
if cFullScreen then
|
|
485 |
begin
|
|
486 |
LandSurface:= SDL_DisplayFormat(tmpsurf);
|
|
487 |
SDL_FreeSurface(tmpsurf);
|
|
488 |
end else LandSurface:= tmpsurf;
|
35
|
489 |
TryDo(SDL_SetColorKey(LandSurface, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true);
|
4
|
490 |
WriteLnToConsole(msgOK);
|
|
491 |
|
|
492 |
GetExplosionBorderColor;
|
|
493 |
|
|
494 |
AddProgress;
|
|
495 |
for i:= Low(TStuff) to High(TStuff) do
|
|
496 |
LoadToSurface(Pathz[StuffLoadData[i].Path] + StuffLoadData[i].FileName, StoreSurface, StuffPoz[i].x, StuffPoz[i].y);
|
|
497 |
|
|
498 |
AddProgress;
|
|
499 |
WriteNames(fnt16);
|
|
500 |
MakeCrosshairs;
|
|
501 |
LoadGraves;
|
|
502 |
|
|
503 |
GetSkyColor;
|
|
504 |
|
|
505 |
AddProgress;
|
|
506 |
for ii:= Low(TSprite) to High(TSprite) do
|
|
507 |
with SpritesData[ii] do
|
35
|
508 |
Surface:= LoadImage(Pathz[Path] + FileName, hasAlpha);
|
4
|
509 |
|
|
510 |
AddProgress;
|
35
|
511 |
tmpsurf:= LoadImage(Pathz[ptGraphics] + cHHFileName, false);
|
|
512 |
TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true);
|
4
|
513 |
HHSurface:= SDL_DisplayFormat(tmpsurf);
|
|
514 |
SDL_FreeSurface(tmpsurf);
|
|
515 |
|
|
516 |
InitHealth;
|
|
517 |
|
|
518 |
{$IFDEF DUMP}
|
|
519 |
SDL_SaveBMP_RW(LandSurface, SDL_RWFromFile('LandSurface.bmp', 'wb'), 1);
|
|
520 |
SDL_SaveBMP_RW(StoreSurface, SDL_RWFromFile('StoreSurface.bmp', 'wb'), 1);
|
|
521 |
SDL_SaveBMP_RW(TempSurface, SDL_RWFromFile('TempSurface.bmp', 'wb'), 1);
|
|
522 |
{$ENDIF}
|
|
523 |
end;
|
|
524 |
|
|
525 |
procedure DrawFromRect(X, Y: integer; r: PSDL_Rect; SourceSurface, DestSurface: PSDL_Surface);
|
|
526 |
var rr: TSDL_Rect;
|
|
527 |
begin
|
|
528 |
rr.x:= X;
|
|
529 |
rr.y:= Y;
|
|
530 |
rr.w:= r.w;
|
|
531 |
rr.h:= r.h;
|
|
532 |
if SDL_UpperBlit(SourceSurface, r, DestSurface, @rr) < 0 then
|
|
533 |
begin
|
|
534 |
Writeln('Blit: ', SDL_GetError);
|
|
535 |
exit
|
|
536 |
end;
|
|
537 |
end;
|
|
538 |
|
|
539 |
procedure DrawGear(Stuff: TStuff; X, Y: integer; Surface: PSDL_Surface);
|
|
540 |
begin
|
|
541 |
DrawFromRect(X, Y, @StuffPoz[Stuff], StoreSurface, Surface)
|
|
542 |
end;
|
|
543 |
|
|
544 |
procedure DrawSpriteFromRect(r: TSDL_Rect; X, Y, Height, Position: integer; Surface: PSDL_Surface);
|
|
545 |
begin
|
|
546 |
r.y:= r.y + Height * Position;
|
|
547 |
r.h:= Height;
|
|
548 |
DrawFromRect(X, Y, @r, StoreSurface, Surface)
|
|
549 |
end;
|
|
550 |
|
|
551 |
procedure DrawSprite(Sprite: TSprite; X, Y, Position: integer; Surface: PSDL_Surface);
|
|
552 |
var r: TSDL_Rect;
|
|
553 |
begin
|
|
554 |
r.x:= 0;
|
|
555 |
r.w:= SpritesData[Sprite].Width;
|
|
556 |
r.y:= Position * SpritesData[Sprite].Height;
|
|
557 |
r.h:= SpritesData[Sprite].Height;
|
|
558 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Surface, Surface)
|
|
559 |
end;
|
|
560 |
|
|
561 |
procedure DXOutText(X, Y: Integer; Font: THWFont; s: string; Surface: PSDL_Surface);
|
|
562 |
var clr: TSDL_Color;
|
|
563 |
tmpsurf: PSDL_Surface;
|
|
564 |
r: TSDL_Rect;
|
|
565 |
begin
|
|
566 |
r.x:= X;
|
|
567 |
r.y:= Y;
|
|
568 |
SDL_GetRGB(cWhiteColor, PixelFormat, @clr.r, @clr.g, @clr.b);
|
|
569 |
tmpsurf:= TTF_RenderText_Solid(Fontz[Font].Handle, PChar(s), clr);
|
|
570 |
SDL_UpperBlit(tmpsurf, nil, Surface, @r);
|
|
571 |
SDL_FreeSurface(tmpsurf)
|
|
572 |
end;
|
|
573 |
|
|
574 |
procedure DrawLand(X, Y: integer; Surface: PSDL_Surface);
|
|
575 |
const r: TSDL_Rect = (x: 0; y: 0; w: 2048; h: 1024);
|
|
576 |
begin
|
|
577 |
DrawFromRect(X, Y, @r, LandSurface, Surface)
|
|
578 |
end;
|
|
579 |
|
|
580 |
procedure DrawCaption(X, Y: integer; Rect: TSDL_Rect; Surface: PSDL_Surface; const fromTempSurf: boolean = false);
|
|
581 |
begin
|
|
582 |
if fromTempSurf then DrawFromRect(X - (Rect.w) div 2, Y, @Rect, TempSurface, Surface)
|
|
583 |
else DrawFromRect(X - (Rect.w) div 2, Y, @Rect, StoreSurface, Surface)
|
|
584 |
end;
|
|
585 |
|
|
586 |
procedure DrawHedgehog(X, Y: integer; Dir: integer; Pos, Step: LongWord; Surface: PSDL_Surface);
|
|
587 |
var r: TSDL_Rect;
|
|
588 |
begin
|
|
589 |
r.x:= Step * 32;
|
|
590 |
r.y:= Pos * 32;
|
|
591 |
if Dir = -1 then r.x:= cHHSurfaceWidth - 32 - r.x;
|
|
592 |
r.w:= 32;
|
|
593 |
r.h:= 32;
|
|
594 |
DrawFromRect(X, Y, @r, HHSurface, Surface)
|
|
595 |
end;
|
|
596 |
|
|
597 |
procedure StoreRelease;
|
|
598 |
var ii: TSprite;
|
|
599 |
begin
|
|
600 |
for ii:= Low(TSprite) to High(TSprite) do
|
|
601 |
SDL_FreeSurface(SpritesData[ii].Surface);
|
|
602 |
SDL_FreeSurface( HHSurface );
|
|
603 |
SDL_FreeSurface(TempSurface );
|
|
604 |
SDL_FreeSurface(LandSurface );
|
|
605 |
SDL_FreeSurface(StoreSurface )
|
|
606 |
end;
|
|
607 |
|
|
608 |
procedure RenderHealth(var Hedgehog: THedgehog);
|
30
|
609 |
var s: string[15];
|
4
|
610 |
begin
|
|
611 |
str(Hedgehog.Gear.Health, s);
|
|
612 |
Hedgehog.HealthRect:= WriteInRoundRect(TempSurface, Hedgehog.HealthRect.x, Hedgehog.HealthRect.y, Hedgehog.Team.Color, fnt16, s);
|
|
613 |
if Hedgehog.Gear.Damage > 0 then
|
|
614 |
begin
|
|
615 |
str(Hedgehog.Gear.Damage, s);
|
|
616 |
Hedgehog.HealthTagRect:= WriteInRoundRect(TempSurface, Hedgehog.HealthRect.x + Hedgehog.HealthRect.w, Hedgehog.HealthRect.y, Hedgehog.Team.Color, fnt16, s)
|
|
617 |
end;
|
|
618 |
end;
|
|
619 |
|
|
620 |
function RenderString(var s: shortstring; Color, Pos: integer): TSDL_Rect;
|
|
621 |
begin
|
|
622 |
Result:= WriteInRoundRect(TempSurface, 64, Pos * Fontz[fntBig].Height, Color, fntBig, s);
|
|
623 |
end;
|
|
624 |
|
|
625 |
procedure AddProgress;
|
|
626 |
const Step: Longword = 0;
|
|
627 |
ProgrSurf: PSDL_Surface = nil;
|
|
628 |
MaxCalls = 10; // MaxCalls should be the count of calls to AddProgress to prevent memory leakage
|
|
629 |
var r: TSDL_Rect;
|
|
630 |
begin
|
|
631 |
if Step = 0 then
|
|
632 |
begin
|
|
633 |
WriteToConsole(msgLoading + 'progress sprite... ');
|
|
634 |
ProgrSurf:= IMG_Load(PChar(string('Data/Graphics/BigDigits.png')));
|
|
635 |
SDLTry(ProgrSurf <> nil, true);
|
|
636 |
WriteLnToConsole(msgOK)
|
|
637 |
end;
|
|
638 |
SDL_FillRect(SDLPrimSurface, nil, 0);
|
|
639 |
r.x:= 0;
|
|
640 |
r.w:= 32;
|
|
641 |
r.h:= 32;
|
|
642 |
r.y:= Step * 32;
|
|
643 |
DrawFromRect(cScreenWidth div 2 - 16, cScreenHeight div 2 - 16, @r, ProgrSurf, SDLPrimSurface);
|
|
644 |
SDL_Flip(SDLPrimSurface);
|
|
645 |
inc(Step);
|
|
646 |
if Step = MaxCalls then
|
|
647 |
begin
|
|
648 |
WriteLnToConsole('Freeing progress surface... ');
|
|
649 |
SDL_FreeSurface(ProgrSurf)
|
|
650 |
end;
|
|
651 |
end;
|
|
652 |
|
35
|
653 |
function LoadImage(filename: string; hasAlpha: boolean): PSDL_Surface;
|
30
|
654 |
var tmpsurf: PSDL_Surface;
|
4
|
655 |
begin
|
|
656 |
WriteToConsole(msgLoading + filename + '... ');
|
30
|
657 |
tmpsurf:= IMG_Load(PChar(filename));
|
|
658 |
TryDo(tmpsurf <> nil, msgFailed, true);
|
35
|
659 |
TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true);
|
30
|
660 |
if cFullScreen then
|
|
661 |
begin
|
35
|
662 |
if hasAlpha then Result:= SDL_DisplayFormatAlpha(tmpsurf)
|
|
663 |
else Result:= SDL_DisplayFormat(tmpsurf);
|
30
|
664 |
SDL_FreeSurface(tmpsurf);
|
|
665 |
end else Result:= tmpsurf;
|
4
|
666 |
WriteLnToConsole(msgOK)
|
|
667 |
end;
|
|
668 |
|
|
669 |
end.
|