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);
|
|
55 |
procedure DrawLineExplosions(ar: PRangeArray; Radius: Longword; y, dY: integer; Count: Byte);
|
|
56 |
procedure RenderHealth(var Hedgehog: THedgehog);
|
|
57 |
function RenderString(var s: shortstring; Color, Pos: integer): TSDL_Rect;
|
|
58 |
procedure AddProgress;
|
35
|
59 |
function LoadImage(filename: string; hasAlpha: boolean): PSDL_Surface;
|
4
|
60 |
|
|
61 |
var PixelFormat: PSDL_PixelFormat;
|
|
62 |
SDLPrimSurface: PSDL_Surface;
|
|
63 |
|
|
64 |
implementation
|
|
65 |
uses uMisc, uIO, uConsole, uLand;
|
|
66 |
|
|
67 |
var StoreSurface,
|
|
68 |
TempSurface,
|
|
69 |
HHSurface: PSDL_Surface;
|
|
70 |
|
|
71 |
procedure DrawExplosion(X, Y, Radius: integer);
|
|
72 |
var ty, tx: integer;
|
|
73 |
p: integer;
|
|
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 |
|
|
82 |
p:= Longword(LandSurface.pixels);
|
|
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
|
|
106 |
if PWord(p + LandSurface.pitch*(y + ty) + tx * 2)^ <> 0 then
|
|
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
|
|
110 |
if (PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 0)^ <> 0)
|
|
111 |
or (PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 1)^ <> 0)
|
|
112 |
or (PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 2)^ <> 0)
|
|
113 |
then begin
|
|
114 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 0)^:= cExplosionBorderColor and $FF;
|
|
115 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 1)^:= (cExplosionBorderColor shr 8) and $FF;
|
|
116 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 2)^:= (cExplosionBorderColor shr 16);
|
|
117 |
end;
|
|
118 |
4: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
119 |
for tx:= max(0, round(x-radius*sqrt(1-sqr(ty/radius)))) to min(2047, round(x+radius*sqrt(1-sqr(ty/radius)))) do
|
|
120 |
if PLongword(p + LandSurface.pitch*(y + ty) + tx * 4)^ <> 0 then
|
|
121 |
PLongword(p + LandSurface.pitch*(y + ty) + tx * 4)^:= cExplosionBorderColor;
|
|
122 |
end;
|
|
123 |
|
|
124 |
if SDL_MustLock(LandSurface) then
|
|
125 |
SDL_UnlockSurface(LandSurface);
|
|
126 |
|
|
127 |
SDL_UpdateRect(LandSurface, X - Radius, Y - Radius, Radius * 2, Radius * 2)
|
|
128 |
end;
|
|
129 |
|
|
130 |
procedure DrawLineExplosions(ar: PRangeArray; Radius: Longword; y, dY: integer; Count: Byte);
|
|
131 |
var tx, ty, i, p: integer;
|
|
132 |
begin
|
|
133 |
if SDL_MustLock(LandSurface) then
|
|
134 |
SDL_LockSurface(LandSurface);
|
|
135 |
|
|
136 |
p:= Longword(LandSurface.pixels);
|
|
137 |
for i:= 0 to Pred(Count) do
|
|
138 |
begin
|
|
139 |
case LandSurface.format.BytesPerPixel of
|
|
140 |
1: ;
|
|
141 |
2: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
142 |
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
|
|
143 |
PWord(p + LandSurface.pitch*(y + ty) + tx * 2)^:= 0;
|
|
144 |
3: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
145 |
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
|
|
146 |
begin
|
|
147 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 0)^:= 0;
|
|
148 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 1)^:= 0;
|
|
149 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 2)^:= 0;
|
|
150 |
end;
|
|
151 |
4: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
152 |
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
|
|
153 |
PLongword(p + LandSurface.pitch*(y + ty) + tx * 4)^:= 0;
|
|
154 |
end;
|
|
155 |
inc(y, dY)
|
|
156 |
end;
|
|
157 |
|
|
158 |
inc(Radius, 4);
|
|
159 |
dec(y, Count*dY);
|
|
160 |
|
|
161 |
for i:= 0 to Pred(Count) do
|
|
162 |
begin
|
|
163 |
case LandSurface.format.BytesPerPixel of
|
|
164 |
1: ;
|
|
165 |
2: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
166 |
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
|
|
167 |
if PWord(p + LandSurface.pitch*(y + ty) + tx * 2)^ <> 0 then
|
|
168 |
PWord(p + LandSurface.pitch*(y + ty) + tx * 2)^:= cExplosionBorderColor;
|
|
169 |
3: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
170 |
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
|
|
171 |
if (PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 0)^ <> 0)
|
|
172 |
or (PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 1)^ <> 0)
|
|
173 |
or (PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 2)^ <> 0)
|
|
174 |
then begin
|
|
175 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 0)^:= cExplosionBorderColor and $FF;
|
|
176 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 1)^:= (cExplosionBorderColor shr 8) and $FF;
|
|
177 |
PByte(p + LandSurface.pitch*(y + ty) + tx * 3 + 2)^:= (cExplosionBorderColor shr 16);
|
|
178 |
end;
|
|
179 |
4: for ty:= max(-Radius, -y) to min(Radius, 1023 - y) do
|
|
180 |
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
|
|
181 |
if PLongword(p + LandSurface.pitch*(y + ty) + tx * 4)^ <> 0 then
|
|
182 |
PLongword(p + LandSurface.pitch*(y + ty) + tx * 4)^:= cExplosionBorderColor;
|
|
183 |
end;
|
|
184 |
inc(y, dY)
|
|
185 |
end;
|
|
186 |
|
|
187 |
if SDL_MustLock(LandSurface) then
|
|
188 |
SDL_UnlockSurface(LandSurface);
|
|
189 |
end;
|
|
190 |
|
|
191 |
procedure StoreInit;
|
|
192 |
begin
|
|
193 |
StoreSurface := SDL_CreateRGBSurface(SDL_HWSURFACE, 576, 1024, cBits, PixelFormat.RMask, PixelFormat.GMask, PixelFormat.BMask, 0);
|
|
194 |
TryDo( StoreSurface <> nil, errmsgCreateSurface + ': store' , true);
|
37
|
195 |
SDL_FillRect(StoreSurface, nil, 0);
|
4
|
196 |
|
|
197 |
TempSurface := SDL_CreateRGBSurface(SDL_HWSURFACE, 724, 320, cBits, PixelFormat.RMask, PixelFormat.GMask, PixelFormat.BMask, 0);
|
|
198 |
TryDo( TempSurface <> nil, errmsgCreateSurface + ': temp' , true);
|
|
199 |
|
|
200 |
TryDo(SDL_SetColorKey( StoreSurface, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true);
|
|
201 |
//TryDo(SDL_SetColorKey(SpriteSurface, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true);
|
|
202 |
TryDo(SDL_SetColorKey( TempSurface, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true);
|
|
203 |
end;
|
|
204 |
|
|
205 |
procedure LoadToSurface(Filename: String; Surface: PSDL_Surface; X, Y: integer);
|
|
206 |
var tmpsurf: PSDL_Surface;
|
|
207 |
rr: TSDL_Rect;
|
|
208 |
begin
|
35
|
209 |
tmpsurf:= LoadImage(Filename, false);
|
4
|
210 |
rr.x:= X;
|
|
211 |
rr.y:= Y;
|
|
212 |
SDL_UpperBlit(tmpsurf, nil, Surface, @rr);
|
|
213 |
SDL_FreeSurface(tmpsurf);
|
|
214 |
end;
|
|
215 |
|
|
216 |
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: integer; Color: LongWord; Font: THWFont; s: string): TSDL_Rect;
|
|
217 |
var w, h: integer;
|
|
218 |
tmpsurf: PSDL_Surface;
|
|
219 |
clr: TSDL_Color;
|
|
220 |
begin
|
|
221 |
TTF_SizeText(Fontz[Font].Handle, PChar(s), w, h);
|
|
222 |
Result.x:= X;
|
|
223 |
Result.y:= Y;
|
|
224 |
Result.w:= w + 6;
|
|
225 |
Result.h:= h + 6;
|
|
226 |
SDL_FillRect(Surface, @Result, 0);
|
|
227 |
Result.w:= 1;
|
|
228 |
Result.y:= Y + 1;
|
|
229 |
Result.h:= h + 4;
|
|
230 |
SDL_FillRect(Surface, @Result, cWhiteColor);
|
|
231 |
Result.x:= X + w + 5;
|
|
232 |
SDL_FillRect(Surface, @Result, cWhiteColor);
|
|
233 |
Result.x:= X + 1;
|
|
234 |
Result.w:= w + 4;
|
|
235 |
Result.y:= Y;
|
|
236 |
Result.h:= 1;
|
|
237 |
SDL_FillRect(Surface, @Result, cWhiteColor);
|
|
238 |
Result.y:= Y + h + 5;
|
|
239 |
SDL_FillRect(Surface, @Result, cWhiteColor);
|
|
240 |
Result.x:= X + 1;
|
|
241 |
Result.y:= Y + 1;
|
|
242 |
Result.h:= h + 4;
|
|
243 |
SDL_FillRect(Surface, @Result, cColorNearBlack);
|
|
244 |
SDL_GetRGB(Color, Surface.format, @clr.r, @clr.g, @clr.b);
|
|
245 |
tmpsurf:= TTF_RenderText_Blended(Fontz[Font].Handle, PChar(s), clr);
|
|
246 |
Result.x:= X + 3;
|
|
247 |
Result.y:= Y + 3;
|
|
248 |
SDL_UpperBlit(tmpsurf, nil, Surface, @Result);
|
|
249 |
SDL_FreeSurface(tmpsurf);
|
|
250 |
Result.x:= X;
|
|
251 |
Result.y:= Y;
|
|
252 |
Result.w:= w + 6;
|
|
253 |
Result.h:= h + 6
|
|
254 |
end;
|
|
255 |
|
|
256 |
procedure StoreLoad;
|
|
257 |
var i: TStuff;
|
|
258 |
ii: TSprite;
|
|
259 |
fi: THWFont;
|
|
260 |
s: string;
|
|
261 |
tmpsurf: PSDL_Surface;
|
|
262 |
|
|
263 |
procedure WriteNames(Font: THWFont);
|
|
264 |
var Team: PTeam;
|
|
265 |
i: integer;
|
|
266 |
r: TSDL_Rect;
|
|
267 |
begin
|
|
268 |
r.x:= 0;
|
|
269 |
r.y:= 272;
|
|
270 |
Team:= TeamsList;
|
|
271 |
while Team<>nil do
|
|
272 |
begin
|
|
273 |
r.w:= 1968;
|
|
274 |
r:= WriteInRoundRect(StoreSurface, r.x, r.y, Team.Color, Font, Team.TeamName);
|
|
275 |
Team.NameRect:= r;
|
|
276 |
inc(r.y, r.h);
|
|
277 |
for i:= 0 to 7 do
|
|
278 |
if Team.Hedgehogs[i].Gear<>nil then
|
|
279 |
begin
|
|
280 |
r:= WriteInRoundRect(StoreSurface, r.x, r.y, Team.Color, Font, Team.Hedgehogs[i].Name);
|
|
281 |
Team.Hedgehogs[i].NameRect:= r;
|
|
282 |
inc(r.y, r.h)
|
|
283 |
end;
|
|
284 |
Team:= Team.Next
|
|
285 |
end;
|
|
286 |
end;
|
|
287 |
|
|
288 |
procedure MakeCrossHairs;
|
|
289 |
var Team: PTeam;
|
|
290 |
r: TSDL_Rect;
|
|
291 |
tmpsurf: PSDL_Surface;
|
|
292 |
s: string;
|
|
293 |
TransColor: Longword;
|
|
294 |
begin
|
|
295 |
r.x:= 0;
|
|
296 |
r.y:= 256;
|
|
297 |
r.w:= 16;
|
|
298 |
r.h:= 16;
|
|
299 |
s:= Pathz[ptGraphics] + cCHFileName;
|
|
300 |
WriteToConsole(msgLoading + s + ' ');
|
|
301 |
tmpsurf:= IMG_Load(PChar(s));
|
|
302 |
TryDo(tmpsurf <> nil, msgFailed, true);
|
|
303 |
WriteLnToConsole(msgOK);
|
|
304 |
TransColor:= SDL_MapRGB(tmpsurf.format, $FF, $FF, $FF);
|
|
305 |
TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, TransColor) = 0, errmsgTransparentSet, true);
|
|
306 |
|
|
307 |
Team:= TeamsList;
|
|
308 |
while Team<>nil do
|
|
309 |
begin
|
|
310 |
SDL_FillRect(StoreSurface, @r, Team.Color);
|
|
311 |
SDL_UpperBlit(tmpsurf, nil, StoreSurface, @r);
|
|
312 |
Team.CrossHairRect:= r;
|
|
313 |
inc(r.x, 16);
|
|
314 |
Team:= Team.Next
|
|
315 |
end;
|
|
316 |
|
|
317 |
SDL_FreeSurface(tmpsurf)
|
|
318 |
end;
|
|
319 |
|
|
320 |
procedure InitHealth;
|
|
321 |
var p: PTeam;
|
|
322 |
i, t: integer;
|
|
323 |
begin
|
|
324 |
p:= TeamsList;
|
|
325 |
t:= 0;
|
|
326 |
while p <> nil do
|
|
327 |
begin
|
|
328 |
for i:= 0 to cMaxHHIndex do
|
|
329 |
if p.Hedgehogs[i].Gear <> nil then
|
|
330 |
begin
|
|
331 |
p.Hedgehogs[i].HealthRect.y:= t;
|
|
332 |
RenderHealth(p.Hedgehogs[i]);
|
|
333 |
inc(t, p.Hedgehogs[i].HealthRect.h)
|
|
334 |
end;
|
|
335 |
p:= p.Next
|
|
336 |
end
|
|
337 |
end;
|
|
338 |
|
|
339 |
procedure LoadGraves;
|
|
340 |
var p: PTeam;
|
|
341 |
l: integer;
|
|
342 |
begin
|
|
343 |
p:= TeamsList;
|
|
344 |
l:= 512;
|
|
345 |
while p <> nil do
|
|
346 |
begin
|
|
347 |
dec(l, 32);
|
|
348 |
if p.GraveName = '' then p.GraveName:= 'Simple';
|
|
349 |
LoadToSurface(Pathz[ptGraves] + p.GraveName + '.png', StoreSurface, l, 512);
|
|
350 |
p.GraveRect.x:= l;
|
|
351 |
p.GraveRect.y:= 512;
|
|
352 |
p.GraveRect.w:= 32;
|
|
353 |
p.GraveRect.h:= 256;
|
|
354 |
p:= p.Next
|
|
355 |
end
|
|
356 |
end;
|
|
357 |
|
|
358 |
procedure GetSkyColor;
|
|
359 |
var p: Longword;
|
|
360 |
begin
|
|
361 |
if SDL_MustLock(StoreSurface) then
|
|
362 |
SDLTry(SDL_LockSurface(StoreSurface) >= 0, true);
|
|
363 |
p:= Longword(StoreSurface.pixels) + Word(StuffPoz[sSky].x) * StoreSurface.format.BytesPerPixel;
|
|
364 |
case StoreSurface.format.BytesPerPixel of
|
|
365 |
1: cSkyColor:= PByte(p)^;
|
|
366 |
2: cSkyColor:= PWord(p)^;
|
|
367 |
3: cSkyColor:= (PByte(p)^) or (PByte(p + 1)^ shl 8) or (PByte(p + 2)^ shl 16);
|
|
368 |
4: cSkyColor:= PLongword(p)^;
|
|
369 |
end;
|
|
370 |
if SDL_MustLock(StoreSurface) then
|
|
371 |
SDL_UnlockSurface(StoreSurface)
|
|
372 |
end;
|
|
373 |
|
|
374 |
procedure GetExplosionBorderColor;
|
|
375 |
var f: textfile;
|
|
376 |
c: integer;
|
|
377 |
begin
|
|
378 |
s:= Pathz[ptThemeCurrent] + cThemeCFGFilename;
|
|
379 |
WriteToConsole(msgLoading + s + ' ');
|
|
380 |
AssignFile(f, s);
|
|
381 |
{$I-}
|
|
382 |
Reset(f);
|
|
383 |
Readln(f, s);
|
|
384 |
Closefile(f);
|
|
385 |
{$I+}
|
|
386 |
TryDo(IOResult = 0, msgFailed, true);
|
|
387 |
WriteLnToConsole(msgOK);
|
|
388 |
val(s, cExplosionBorderColor, c);
|
|
389 |
if cFullScreen then
|
|
390 |
cExplosionBorderColor:= SDL_MapRGB(PixelFormat, (cExplosionBorderColor shr 16) and $FF,
|
|
391 |
(cExplosionBorderColor shr 8) and $FF,
|
|
392 |
cExplosionBorderColor and $FF)
|
|
393 |
else
|
|
394 |
cExplosionBorderColor:= SDL_MapRGB(LandSurface.format, (cExplosionBorderColor shr 16) and $FF,
|
|
395 |
(cExplosionBorderColor shr 8) and $FF,
|
|
396 |
cExplosionBorderColor and $FF)
|
|
397 |
end;
|
|
398 |
|
|
399 |
begin
|
|
400 |
for fi:= Low(THWFont) to High(THWFont) do
|
|
401 |
with Fontz[fi] do
|
|
402 |
begin
|
|
403 |
s:= Pathz[ptFonts] + Name;
|
|
404 |
WriteToConsole(msgLoading + s + ' ');
|
|
405 |
Handle:= TTF_OpenFont(PChar(s), Height);
|
|
406 |
TryDo(Handle <> nil, msgFailed, true);
|
|
407 |
WriteLnToConsole(msgOK)
|
|
408 |
end;
|
|
409 |
AddProgress;
|
35
|
410 |
//s:= Pathz[ptMapCurrent] + cLandFileName;
|
|
411 |
//WriteToConsole(msgLoading + s + ' ');
|
4
|
412 |
//tmpsurf:= IMG_Load(PChar(s));
|
|
413 |
tmpsurf:= LandSurface;
|
|
414 |
TryDo(tmpsurf <> nil, msgFailed, true);
|
|
415 |
if cFullScreen then
|
|
416 |
begin
|
|
417 |
LandSurface:= SDL_DisplayFormat(tmpsurf);
|
|
418 |
SDL_FreeSurface(tmpsurf);
|
|
419 |
end else LandSurface:= tmpsurf;
|
35
|
420 |
TryDo(SDL_SetColorKey(LandSurface, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true);
|
4
|
421 |
WriteLnToConsole(msgOK);
|
|
422 |
|
|
423 |
GetExplosionBorderColor;
|
|
424 |
|
|
425 |
AddProgress;
|
|
426 |
for i:= Low(TStuff) to High(TStuff) do
|
|
427 |
LoadToSurface(Pathz[StuffLoadData[i].Path] + StuffLoadData[i].FileName, StoreSurface, StuffPoz[i].x, StuffPoz[i].y);
|
|
428 |
|
|
429 |
AddProgress;
|
|
430 |
WriteNames(fnt16);
|
|
431 |
MakeCrosshairs;
|
|
432 |
LoadGraves;
|
|
433 |
|
|
434 |
GetSkyColor;
|
|
435 |
|
|
436 |
AddProgress;
|
|
437 |
for ii:= Low(TSprite) to High(TSprite) do
|
|
438 |
with SpritesData[ii] do
|
35
|
439 |
Surface:= LoadImage(Pathz[Path] + FileName, hasAlpha);
|
4
|
440 |
|
|
441 |
AddProgress;
|
35
|
442 |
tmpsurf:= LoadImage(Pathz[ptGraphics] + cHHFileName, false);
|
|
443 |
TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true);
|
4
|
444 |
HHSurface:= SDL_DisplayFormat(tmpsurf);
|
|
445 |
SDL_FreeSurface(tmpsurf);
|
|
446 |
|
|
447 |
InitHealth;
|
|
448 |
|
|
449 |
{$IFDEF DUMP}
|
|
450 |
SDL_SaveBMP_RW(LandSurface, SDL_RWFromFile('LandSurface.bmp', 'wb'), 1);
|
|
451 |
SDL_SaveBMP_RW(StoreSurface, SDL_RWFromFile('StoreSurface.bmp', 'wb'), 1);
|
|
452 |
SDL_SaveBMP_RW(TempSurface, SDL_RWFromFile('TempSurface.bmp', 'wb'), 1);
|
|
453 |
{$ENDIF}
|
|
454 |
end;
|
|
455 |
|
|
456 |
procedure DrawFromRect(X, Y: integer; r: PSDL_Rect; SourceSurface, DestSurface: PSDL_Surface);
|
|
457 |
var rr: TSDL_Rect;
|
|
458 |
begin
|
|
459 |
rr.x:= X;
|
|
460 |
rr.y:= Y;
|
|
461 |
rr.w:= r.w;
|
|
462 |
rr.h:= r.h;
|
|
463 |
if SDL_UpperBlit(SourceSurface, r, DestSurface, @rr) < 0 then
|
|
464 |
begin
|
|
465 |
Writeln('Blit: ', SDL_GetError);
|
|
466 |
exit
|
|
467 |
end;
|
|
468 |
end;
|
|
469 |
|
|
470 |
procedure DrawGear(Stuff: TStuff; X, Y: integer; Surface: PSDL_Surface);
|
|
471 |
begin
|
|
472 |
DrawFromRect(X, Y, @StuffPoz[Stuff], StoreSurface, Surface)
|
|
473 |
end;
|
|
474 |
|
|
475 |
procedure DrawSpriteFromRect(r: TSDL_Rect; X, Y, Height, Position: integer; Surface: PSDL_Surface);
|
|
476 |
begin
|
|
477 |
r.y:= r.y + Height * Position;
|
|
478 |
r.h:= Height;
|
|
479 |
DrawFromRect(X, Y, @r, StoreSurface, Surface)
|
|
480 |
end;
|
|
481 |
|
|
482 |
procedure DrawSprite(Sprite: TSprite; X, Y, Position: integer; Surface: PSDL_Surface);
|
|
483 |
var r: TSDL_Rect;
|
|
484 |
begin
|
|
485 |
r.x:= 0;
|
|
486 |
r.w:= SpritesData[Sprite].Width;
|
|
487 |
r.y:= Position * SpritesData[Sprite].Height;
|
|
488 |
r.h:= SpritesData[Sprite].Height;
|
|
489 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Surface, Surface)
|
|
490 |
end;
|
|
491 |
|
|
492 |
procedure DXOutText(X, Y: Integer; Font: THWFont; s: string; Surface: PSDL_Surface);
|
|
493 |
var clr: TSDL_Color;
|
|
494 |
tmpsurf: PSDL_Surface;
|
|
495 |
r: TSDL_Rect;
|
|
496 |
begin
|
|
497 |
r.x:= X;
|
|
498 |
r.y:= Y;
|
|
499 |
SDL_GetRGB(cWhiteColor, PixelFormat, @clr.r, @clr.g, @clr.b);
|
|
500 |
tmpsurf:= TTF_RenderText_Solid(Fontz[Font].Handle, PChar(s), clr);
|
|
501 |
SDL_UpperBlit(tmpsurf, nil, Surface, @r);
|
|
502 |
SDL_FreeSurface(tmpsurf)
|
|
503 |
end;
|
|
504 |
|
|
505 |
procedure DrawLand(X, Y: integer; Surface: PSDL_Surface);
|
|
506 |
const r: TSDL_Rect = (x: 0; y: 0; w: 2048; h: 1024);
|
|
507 |
begin
|
|
508 |
DrawFromRect(X, Y, @r, LandSurface, Surface)
|
|
509 |
end;
|
|
510 |
|
|
511 |
procedure DrawCaption(X, Y: integer; Rect: TSDL_Rect; Surface: PSDL_Surface; const fromTempSurf: boolean = false);
|
|
512 |
begin
|
|
513 |
if fromTempSurf then DrawFromRect(X - (Rect.w) div 2, Y, @Rect, TempSurface, Surface)
|
|
514 |
else DrawFromRect(X - (Rect.w) div 2, Y, @Rect, StoreSurface, Surface)
|
|
515 |
end;
|
|
516 |
|
|
517 |
procedure DrawHedgehog(X, Y: integer; Dir: integer; Pos, Step: LongWord; Surface: PSDL_Surface);
|
|
518 |
var r: TSDL_Rect;
|
|
519 |
begin
|
|
520 |
r.x:= Step * 32;
|
|
521 |
r.y:= Pos * 32;
|
|
522 |
if Dir = -1 then r.x:= cHHSurfaceWidth - 32 - r.x;
|
|
523 |
r.w:= 32;
|
|
524 |
r.h:= 32;
|
|
525 |
DrawFromRect(X, Y, @r, HHSurface, Surface)
|
|
526 |
end;
|
|
527 |
|
|
528 |
procedure StoreRelease;
|
|
529 |
var ii: TSprite;
|
|
530 |
begin
|
|
531 |
for ii:= Low(TSprite) to High(TSprite) do
|
|
532 |
SDL_FreeSurface(SpritesData[ii].Surface);
|
|
533 |
SDL_FreeSurface( HHSurface );
|
|
534 |
SDL_FreeSurface(TempSurface );
|
|
535 |
SDL_FreeSurface(LandSurface );
|
|
536 |
SDL_FreeSurface(StoreSurface )
|
|
537 |
end;
|
|
538 |
|
|
539 |
procedure RenderHealth(var Hedgehog: THedgehog);
|
30
|
540 |
var s: string[15];
|
4
|
541 |
begin
|
|
542 |
str(Hedgehog.Gear.Health, s);
|
|
543 |
Hedgehog.HealthRect:= WriteInRoundRect(TempSurface, Hedgehog.HealthRect.x, Hedgehog.HealthRect.y, Hedgehog.Team.Color, fnt16, s);
|
|
544 |
if Hedgehog.Gear.Damage > 0 then
|
|
545 |
begin
|
|
546 |
str(Hedgehog.Gear.Damage, s);
|
|
547 |
Hedgehog.HealthTagRect:= WriteInRoundRect(TempSurface, Hedgehog.HealthRect.x + Hedgehog.HealthRect.w, Hedgehog.HealthRect.y, Hedgehog.Team.Color, fnt16, s)
|
|
548 |
end;
|
|
549 |
end;
|
|
550 |
|
|
551 |
function RenderString(var s: shortstring; Color, Pos: integer): TSDL_Rect;
|
|
552 |
begin
|
|
553 |
Result:= WriteInRoundRect(TempSurface, 64, Pos * Fontz[fntBig].Height, Color, fntBig, s);
|
|
554 |
end;
|
|
555 |
|
|
556 |
procedure AddProgress;
|
|
557 |
const Step: Longword = 0;
|
|
558 |
ProgrSurf: PSDL_Surface = nil;
|
|
559 |
MaxCalls = 10; // MaxCalls should be the count of calls to AddProgress to prevent memory leakage
|
|
560 |
var r: TSDL_Rect;
|
|
561 |
begin
|
|
562 |
if Step = 0 then
|
|
563 |
begin
|
|
564 |
WriteToConsole(msgLoading + 'progress sprite... ');
|
|
565 |
ProgrSurf:= IMG_Load(PChar(string('Data/Graphics/BigDigits.png')));
|
|
566 |
SDLTry(ProgrSurf <> nil, true);
|
|
567 |
WriteLnToConsole(msgOK)
|
|
568 |
end;
|
|
569 |
SDL_FillRect(SDLPrimSurface, nil, 0);
|
|
570 |
r.x:= 0;
|
|
571 |
r.w:= 32;
|
|
572 |
r.h:= 32;
|
|
573 |
r.y:= Step * 32;
|
|
574 |
DrawFromRect(cScreenWidth div 2 - 16, cScreenHeight div 2 - 16, @r, ProgrSurf, SDLPrimSurface);
|
|
575 |
SDL_Flip(SDLPrimSurface);
|
|
576 |
inc(Step);
|
|
577 |
if Step = MaxCalls then
|
|
578 |
begin
|
|
579 |
WriteLnToConsole('Freeing progress surface... ');
|
|
580 |
SDL_FreeSurface(ProgrSurf)
|
|
581 |
end;
|
|
582 |
end;
|
|
583 |
|
35
|
584 |
function LoadImage(filename: string; hasAlpha: boolean): PSDL_Surface;
|
30
|
585 |
var tmpsurf: PSDL_Surface;
|
4
|
586 |
begin
|
|
587 |
WriteToConsole(msgLoading + filename + '... ');
|
30
|
588 |
tmpsurf:= IMG_Load(PChar(filename));
|
|
589 |
TryDo(tmpsurf <> nil, msgFailed, true);
|
35
|
590 |
TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true);
|
30
|
591 |
if cFullScreen then
|
|
592 |
begin
|
35
|
593 |
if hasAlpha then Result:= SDL_DisplayFormatAlpha(tmpsurf)
|
|
594 |
else Result:= SDL_DisplayFormat(tmpsurf);
|
30
|
595 |
SDL_FreeSurface(tmpsurf);
|
|
596 |
end else Result:= tmpsurf;
|
4
|
597 |
WriteLnToConsole(msgOK)
|
|
598 |
end;
|
|
599 |
|
|
600 |
end.
|