author | unc0rr |
Mon, 15 Jan 2007 23:25:44 +0000 | |
changeset 344 | a98ade8c99b6 |
parent 294 | 92a7ccd67bb9 |
child 351 | 29bc9c36ad5f |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
47 | 3 |
* Copyright (c) 2004, 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 4 |
* |
183 | 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 |
|
4 | 8 |
* |
183 | 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. |
|
4 | 13 |
* |
183 | 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 |
|
4 | 17 |
*) |
18 |
||
19 |
unit uStore; |
|
20 |
interface |
|
21 |
uses uConsts, uTeams, SDLh; |
|
22 |
{$INCLUDE options.inc} |
|
23 |
||
24 |
procedure StoreInit; |
|
25 |
procedure StoreLoad; |
|
26 |
procedure StoreRelease; |
|
27 |
procedure DrawGear(Stuff : TStuff; X, Y: integer; Surface: PSDL_Surface); |
|
28 |
procedure DrawSpriteFromRect(r: TSDL_Rect; X, Y, Height, Position: integer; Surface: PSDL_Surface); |
|
161 | 29 |
procedure DrawSprite (Sprite: TSprite; X, Y, Frame: integer; Surface: PSDL_Surface); |
43 | 30 |
procedure DrawSprite2(Sprite: TSprite; X, Y, FrameX, FrameY: integer; Surface: PSDL_Surface); |
198 | 31 |
procedure DrawSurfSprite(X, Y, Height, Frame: integer; Source, Surface: PSDL_Surface); |
4 | 32 |
procedure DrawLand (X, Y: integer; Surface: PSDL_Surface); |
33 |
procedure DXOutText(X, Y: Integer; Font: THWFont; s: string; Surface: PSDL_Surface); |
|
95 | 34 |
procedure DrawCaption(X, Y: integer; Rect: TSDL_Rect; Surface: PSDL_Surface); |
35 |
procedure DrawCentered(X, Top: integer; Source, Surface: PSDL_Surface); |
|
47 | 36 |
procedure DrawFromStoreRect(X, Y: integer; Rect: PSDL_Rect; Surface: PSDL_Surface); |
4 | 37 |
procedure DrawHedgehog(X, Y: integer; Dir: integer; Pos, Step: LongWord; Surface: PSDL_Surface); |
108 | 38 |
function RenderString(s: string; Color: integer; font: THWFont): PSDL_Surface; |
4 | 39 |
procedure RenderHealth(var Hedgehog: THedgehog); |
40 |
procedure AddProgress; |
|
198 | 41 |
function LoadImage(filename: string; hasAlpha: boolean; const critical: boolean = true; const setTransparent: boolean = true): PSDL_Surface; |
4 | 42 |
|
43 |
var PixelFormat: PSDL_PixelFormat; |
|
44 |
SDLPrimSurface: PSDL_Surface; |
|
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
208
diff
changeset
|
45 |
PauseSurface: PSDL_Surface; |
4 | 46 |
|
47 |
implementation |
|
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
208
diff
changeset
|
48 |
uses uMisc, uConsole, uLand, uLocale; |
4 | 49 |
|
50 |
var StoreSurface, |
|
51 |
HHSurface: PSDL_Surface; |
|
52 |
||
53 |
procedure StoreInit; |
|
54 |
begin |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
190
diff
changeset
|
55 |
StoreSurface:= SDL_CreateRGBSurface(SDL_HWSURFACE, 576, 1024, cBits, PixelFormat.RMask, PixelFormat.GMask, PixelFormat.BMask, PixelFormat.AMask); |
4 | 56 |
TryDo( StoreSurface <> nil, errmsgCreateSurface + ': store' , true); |
37 | 57 |
SDL_FillRect(StoreSurface, nil, 0); |
4 | 58 |
|
59 |
TryDo(SDL_SetColorKey( StoreSurface, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true); |
|
60 |
end; |
|
61 |
||
62 |
procedure LoadToSurface(Filename: String; Surface: PSDL_Surface; X, Y: integer); |
|
63 |
var tmpsurf: PSDL_Surface; |
|
64 |
rr: TSDL_Rect; |
|
65 |
begin |
|
35 | 66 |
tmpsurf:= LoadImage(Filename, false); |
4 | 67 |
rr.x:= X; |
68 |
rr.y:= Y; |
|
69 |
SDL_UpperBlit(tmpsurf, nil, Surface, @rr); |
|
70 |
SDL_FreeSurface(tmpsurf); |
|
71 |
end; |
|
72 |
||
83 | 73 |
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; const Clear: boolean = true); |
47 | 74 |
var r: TSDL_Rect; |
75 |
begin |
|
76 |
r:= rect^; |
|
83 | 77 |
if Clear then SDL_FillRect(Surface, @r, 0); |
47 | 78 |
r.y:= rect.y + 1; |
79 |
r.h:= rect.h - 2; |
|
80 |
SDL_FillRect(Surface, @r, BorderColor); |
|
81 |
r.x:= rect.x + 1; |
|
82 |
r.w:= rect.w - 2; |
|
83 |
r.y:= rect.y; |
|
84 |
r.h:= rect.h; |
|
85 |
SDL_FillRect(Surface, @r, BorderColor); |
|
86 |
r.x:= rect.x + 2; |
|
87 |
r.y:= rect.y + 1; |
|
88 |
r.w:= rect.w - 4; |
|
89 |
r.h:= rect.h - 2; |
|
90 |
SDL_FillRect(Surface, @r, FillColor); |
|
91 |
r.x:= rect.x + 1; |
|
92 |
r.y:= rect.y + 2; |
|
93 |
r.w:= rect.w - 2; |
|
94 |
r.h:= rect.h - 4; |
|
95 |
SDL_FillRect(Surface, @r, FillColor) |
|
96 |
end; |
|
97 |
||
4 | 98 |
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: integer; Color: LongWord; Font: THWFont; s: string): TSDL_Rect; |
99 |
var w, h: integer; |
|
100 |
tmpsurf: PSDL_Surface; |
|
101 |
clr: TSDL_Color; |
|
102 |
begin |
|
74 | 103 |
TTF_SizeUTF8(Fontz[Font].Handle, PChar(s), w, h); |
4 | 104 |
Result.x:= X; |
105 |
Result.y:= Y; |
|
202 | 106 |
Result.w:= w + FontBorder * 2 + 4; |
107 |
Result.h:= h + FontBorder * 2; |
|
47 | 108 |
DrawRoundRect(@Result, cWhiteColor, cColorNearBlack, Surface); |
189 | 109 |
clr.r:= Color shr 16; |
110 |
clr.g:= (Color shr 8) and $FF; |
|
111 |
clr.b:= Color and $FF; |
|
206
32fa6282efe2
Add workaround over freepascal bug (http://www.freepascal.org/mantis/view.php?id=7613)
unc0rr
parents:
203
diff
changeset
|
112 |
tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, PChar(s), clr.value); |
202 | 113 |
Result.x:= X + FontBorder + 2; |
114 |
Result.y:= Y + FontBorder; |
|
199 | 115 |
SDLTry(tmpsurf <> nil, true); |
4 | 116 |
SDL_UpperBlit(tmpsurf, nil, Surface, @Result); |
117 |
SDL_FreeSurface(tmpsurf); |
|
118 |
Result.x:= X; |
|
119 |
Result.y:= Y; |
|
202 | 120 |
Result.w:= w + FontBorder * 2 + 4; |
121 |
Result.h:= h + FontBorder * 2 |
|
4 | 122 |
end; |
123 |
||
124 |
procedure StoreLoad; |
|
125 |
var i: TStuff; |
|
126 |
ii: TSprite; |
|
127 |
fi: THWFont; |
|
128 |
s: string; |
|
129 |
tmpsurf: PSDL_Surface; |
|
130 |
||
131 |
procedure WriteNames(Font: THWFont); |
|
132 |
var Team: PTeam; |
|
133 |
i: integer; |
|
47 | 134 |
r, rr: TSDL_Rect; |
49 | 135 |
drY: integer; |
4 | 136 |
begin |
137 |
r.x:= 0; |
|
138 |
r.y:= 272; |
|
70 | 139 |
drY:= cScreenHeight - 4; |
4 | 140 |
Team:= TeamsList; |
141 |
while Team<>nil do |
|
142 |
begin |
|
47 | 143 |
r.w:= 104; |
95 | 144 |
Team.NameTag:= RenderString(Team.TeamName, Team.Color, Font); |
47 | 145 |
r.w:= cTeamHealthWidth + 5; |
95 | 146 |
r.h:= Team.NameTag.h; |
47 | 147 |
DrawRoundRect(@r, cWhiteColor, cColorNearBlack, StoreSurface); |
148 |
Team.HealthRect:= r; |
|
149 |
rr:= r; |
|
150 |
inc(rr.x, 2); dec(rr.w, 4); inc(rr.y, 2); dec(rr.h, 4); |
|
189 | 151 |
DrawRoundRect(@rr, Team.AdjColor, Team.AdjColor, StoreSurface, false); |
47 | 152 |
inc(r.y, r.h); |
49 | 153 |
dec(drY, r.h + 2); |
154 |
Team.DrawHealthY:= drY; |
|
4 | 155 |
for i:= 0 to 7 do |
95 | 156 |
with Team.Hedgehogs[i] do |
157 |
if Gear <> nil then |
|
158 |
NameTag:= RenderString(Name, Team.Color, fnt16); |
|
4 | 159 |
Team:= Team.Next |
160 |
end; |
|
161 |
end; |
|
162 |
||
163 |
procedure MakeCrossHairs; |
|
164 |
var Team: PTeam; |
|
165 |
tmpsurf: PSDL_Surface; |
|
166 |
s: string; |
|
167 |
begin |
|
53 | 168 |
s:= Pathz[ptGraphics] + '/' + cCHFileName; |
198 | 169 |
tmpsurf:= LoadImage(PChar(s), true, true, false); |
4 | 170 |
|
171 |
Team:= TeamsList; |
|
172 |
while Team<>nil do |
|
173 |
begin |
|
203 | 174 |
Team.CrosshairSurf:= SDL_CreateRGBSurface(SDL_HWSURFACE, tmpsurf.w, tmpsurf.h, cBits, PixelFormat.RMask, PixelFormat.GMask, PixelFormat.BMask, PixelFormat.AMask); |
198 | 175 |
TryDo(Team.CrosshairSurf <> nil, errmsgCreateSurface, true); |
176 |
SDL_FillRect(Team.CrosshairSurf, nil, Team.AdjColor); |
|
177 |
SDL_UpperBlit(tmpsurf, nil, Team.CrosshairSurf, nil); |
|
178 |
TryDo(SDL_SetColorKey(Team.CrosshairSurf, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true); |
|
4 | 179 |
Team:= Team.Next |
180 |
end; |
|
181 |
||
182 |
SDL_FreeSurface(tmpsurf) |
|
183 |
end; |
|
184 |
||
185 |
procedure InitHealth; |
|
186 |
var p: PTeam; |
|
95 | 187 |
i: integer; |
4 | 188 |
begin |
189 |
p:= TeamsList; |
|
190 |
while p <> nil do |
|
191 |
begin |
|
192 |
for i:= 0 to cMaxHHIndex do |
|
193 |
if p.Hedgehogs[i].Gear <> nil then |
|
194 |
RenderHealth(p.Hedgehogs[i]); |
|
195 |
p:= p.Next |
|
196 |
end |
|
197 |
end; |
|
198 |
||
199 |
procedure LoadGraves; |
|
200 |
var p: PTeam; |
|
201 |
l: integer; |
|
202 |
begin |
|
203 |
p:= TeamsList; |
|
204 |
l:= 512; |
|
205 |
while p <> nil do |
|
206 |
begin |
|
207 |
dec(l, 32); |
|
208 |
if p.GraveName = '' then p.GraveName:= 'Simple'; |
|
74 | 209 |
LoadToSurface(Pathz[ptGraves] + '/' + p.GraveName, StoreSurface, l, 512); |
4 | 210 |
p.GraveRect.x:= l; |
211 |
p.GraveRect.y:= 512; |
|
212 |
p.GraveRect.w:= 32; |
|
213 |
p.GraveRect.h:= 256; |
|
214 |
p:= p.Next |
|
215 |
end |
|
216 |
end; |
|
217 |
||
218 |
procedure GetSkyColor; |
|
107 | 219 |
var p: PByteArray; |
4 | 220 |
begin |
80 | 221 |
if SDL_MustLock(SpritesData[sprSky].Surface) then |
222 |
SDLTry(SDL_LockSurface(SpritesData[sprSky].Surface) >= 0, true); |
|
107 | 223 |
p:= SpritesData[sprSky].Surface.pixels; |
80 | 224 |
case SpritesData[sprSky].Surface.format.BytesPerPixel of |
4 | 225 |
1: cSkyColor:= PByte(p)^; |
226 |
2: cSkyColor:= PWord(p)^; |
|
107 | 227 |
3: cSkyColor:= (p^[0]) or (p^[1] shl 8) or (p^[2] shl 16); |
4 | 228 |
4: cSkyColor:= PLongword(p)^; |
229 |
end; |
|
80 | 230 |
if SDL_MustLock(SpritesData[sprSky].Surface) then |
231 |
SDL_UnlockSurface(SpritesData[sprSky].Surface) |
|
4 | 232 |
end; |
233 |
||
234 |
procedure GetExplosionBorderColor; |
|
235 |
var f: textfile; |
|
236 |
c: integer; |
|
237 |
begin |
|
80 | 238 |
s:= Pathz[ptCurrTheme] + '/' + cThemeCFGFilename; |
4 | 239 |
WriteToConsole(msgLoading + s + ' '); |
240 |
AssignFile(f, s); |
|
241 |
{$I-} |
|
242 |
Reset(f); |
|
243 |
Readln(f, s); |
|
244 |
Closefile(f); |
|
245 |
{$I+} |
|
246 |
TryDo(IOResult = 0, msgFailed, true); |
|
247 |
WriteLnToConsole(msgOK); |
|
248 |
val(s, cExplosionBorderColor, c); |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
190
diff
changeset
|
249 |
AdjustColor(cExplosionBorderColor); |
4 | 250 |
end; |
251 |
||
252 |
begin |
|
253 |
for fi:= Low(THWFont) to High(THWFont) do |
|
254 |
with Fontz[fi] do |
|
255 |
begin |
|
53 | 256 |
s:= Pathz[ptFonts] + '/' + Name; |
200 | 257 |
WriteToConsole(msgLoading + s + '... '); |
4 | 258 |
Handle:= TTF_OpenFont(PChar(s), Height); |
200 | 259 |
SDLTry(Handle <> nil, true); |
202 | 260 |
TTF_SetFontStyle(Handle, style); |
4 | 261 |
WriteLnToConsole(msgOK) |
262 |
end; |
|
263 |
AddProgress; |
|
53 | 264 |
|
105 | 265 |
WriteToConsole('LandSurface tuning... '); |
4 | 266 |
tmpsurf:= LandSurface; |
267 |
TryDo(tmpsurf <> nil, msgFailed, true); |
|
268 |
if cFullScreen then |
|
269 |
begin |
|
270 |
LandSurface:= SDL_DisplayFormat(tmpsurf); |
|
271 |
SDL_FreeSurface(tmpsurf); |
|
272 |
end else LandSurface:= tmpsurf; |
|
35 | 273 |
TryDo(SDL_SetColorKey(LandSurface, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
4 | 274 |
WriteLnToConsole(msgOK); |
275 |
||
276 |
GetExplosionBorderColor; |
|
277 |
||
278 |
AddProgress; |
|
279 |
for i:= Low(TStuff) to High(TStuff) do |
|
53 | 280 |
LoadToSurface(Pathz[StuffLoadData[i].Path] + '/' + StuffLoadData[i].FileName, StoreSurface, StuffPoz[i].x, StuffPoz[i].y); |
4 | 281 |
|
282 |
AddProgress; |
|
283 |
WriteNames(fnt16); |
|
70 | 284 |
MakeCrossHairs; |
4 | 285 |
LoadGraves; |
286 |
||
287 |
AddProgress; |
|
288 |
for ii:= Low(TSprite) to High(TSprite) do |
|
289 |
with SpritesData[ii] do |
|
80 | 290 |
begin |
291 |
if AltPath = ptNone then |
|
292 |
Surface:= LoadImage(Pathz[Path] + '/' + FileName, hasAlpha) |
|
293 |
else begin |
|
294 |
Surface:= LoadImage(Pathz[Path] + '/' + FileName, hasAlpha, false); |
|
295 |
if Surface = nil then |
|
296 |
Surface:= LoadImage(Pathz[AltPath] + '/' + FileName, hasAlpha) |
|
297 |
end; |
|
298 |
if Width = 0 then Width:= Surface.w; |
|
299 |
if Height = 0 then Height:= Surface.h |
|
300 |
end; |
|
301 |
||
302 |
GetSkyColor; |
|
4 | 303 |
|
304 |
AddProgress; |
|
53 | 305 |
tmpsurf:= LoadImage(Pathz[ptGraphics] + '/' + cHHFileName, false); |
35 | 306 |
TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true); |
4 | 307 |
HHSurface:= SDL_DisplayFormat(tmpsurf); |
308 |
SDL_FreeSurface(tmpsurf); |
|
309 |
||
310 |
InitHealth; |
|
311 |
||
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
208
diff
changeset
|
312 |
PauseSurface:= RenderString(trmsg[sidPaused], $FFFF00, fntBig); |
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
208
diff
changeset
|
313 |
|
4 | 314 |
{$IFDEF DUMP} |
315 |
SDL_SaveBMP_RW(LandSurface, SDL_RWFromFile('LandSurface.bmp', 'wb'), 1); |
|
316 |
SDL_SaveBMP_RW(StoreSurface, SDL_RWFromFile('StoreSurface.bmp', 'wb'), 1); |
|
317 |
{$ENDIF} |
|
318 |
end; |
|
319 |
||
320 |
procedure DrawFromRect(X, Y: integer; r: PSDL_Rect; SourceSurface, DestSurface: PSDL_Surface); |
|
321 |
var rr: TSDL_Rect; |
|
322 |
begin |
|
323 |
rr.x:= X; |
|
324 |
rr.y:= Y; |
|
325 |
rr.w:= r.w; |
|
326 |
rr.h:= r.h; |
|
327 |
if SDL_UpperBlit(SourceSurface, r, DestSurface, @rr) < 0 then |
|
328 |
begin |
|
43 | 329 |
OutError('Blit: ' + SDL_GetError, true); |
4 | 330 |
exit |
331 |
end; |
|
332 |
end; |
|
333 |
||
334 |
procedure DrawGear(Stuff: TStuff; X, Y: integer; Surface: PSDL_Surface); |
|
335 |
begin |
|
336 |
DrawFromRect(X, Y, @StuffPoz[Stuff], StoreSurface, Surface) |
|
337 |
end; |
|
338 |
||
339 |
procedure DrawSpriteFromRect(r: TSDL_Rect; X, Y, Height, Position: integer; Surface: PSDL_Surface); |
|
340 |
begin |
|
341 |
r.y:= r.y + Height * Position; |
|
342 |
r.h:= Height; |
|
343 |
DrawFromRect(X, Y, @r, StoreSurface, Surface) |
|
344 |
end; |
|
345 |
||
161 | 346 |
procedure DrawSprite (Sprite: TSprite; X, Y, Frame: integer; Surface: PSDL_Surface); |
4 | 347 |
begin |
198 | 348 |
DrawSurfSprite(X, Y, SpritesData[Sprite].Height, Frame, SpritesData[Sprite].Surface, Surface) |
4 | 349 |
end; |
350 |
||
43 | 351 |
procedure DrawSprite2(Sprite: TSprite; X, Y, FrameX, FrameY: integer; Surface: PSDL_Surface); |
352 |
var r: TSDL_Rect; |
|
353 |
begin |
|
354 |
r.x:= FrameX * SpritesData[Sprite].Width; |
|
355 |
r.w:= SpritesData[Sprite].Width; |
|
356 |
r.y:= FrameY * SpritesData[Sprite].Height; |
|
357 |
r.h:= SpritesData[Sprite].Height; |
|
358 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Surface, Surface) |
|
359 |
end; |
|
360 |
||
198 | 361 |
procedure DrawSurfSprite(X, Y, Height, Frame: integer; Source, Surface: PSDL_Surface); |
362 |
var r: TSDL_Rect; |
|
363 |
begin |
|
364 |
r.x:= 0; |
|
365 |
r.w:= Source.w; |
|
366 |
r.y:= Frame * Height; |
|
367 |
r.h:= Height; |
|
368 |
DrawFromRect(X, Y, @r, Source, Surface) |
|
369 |
end; |
|
370 |
||
4 | 371 |
procedure DXOutText(X, Y: Integer; Font: THWFont; s: string; Surface: PSDL_Surface); |
372 |
var clr: TSDL_Color; |
|
373 |
tmpsurf: PSDL_Surface; |
|
374 |
r: TSDL_Rect; |
|
375 |
begin |
|
376 |
r.x:= X; |
|
377 |
r.y:= Y; |
|
189 | 378 |
clr.r:= $FF; |
379 |
clr.g:= $FF; |
|
380 |
clr.b:= $FF; |
|
206
32fa6282efe2
Add workaround over freepascal bug (http://www.freepascal.org/mantis/view.php?id=7613)
unc0rr
parents:
203
diff
changeset
|
381 |
tmpsurf:= TTF_RenderUTF8_Solid(Fontz[Font].Handle, PChar(s), clr.value); |
208 | 382 |
if tmpsurf = nil then |
383 |
begin |
|
384 |
SetKB(1); |
|
385 |
exit |
|
386 |
end; |
|
4 | 387 |
SDL_UpperBlit(tmpsurf, nil, Surface, @r); |
388 |
SDL_FreeSurface(tmpsurf) |
|
389 |
end; |
|
390 |
||
391 |
procedure DrawLand(X, Y: integer; Surface: PSDL_Surface); |
|
392 |
const r: TSDL_Rect = (x: 0; y: 0; w: 2048; h: 1024); |
|
393 |
begin |
|
394 |
DrawFromRect(X, Y, @r, LandSurface, Surface) |
|
395 |
end; |
|
396 |
||
47 | 397 |
procedure DrawFromStoreRect(X, Y: integer; Rect: PSDL_Rect; Surface: PSDL_Surface); |
398 |
begin |
|
399 |
DrawFromRect(X, Y, Rect, StoreSurface, Surface) |
|
400 |
end; |
|
401 |
||
95 | 402 |
procedure DrawCaption(X, Y: integer; Rect: TSDL_Rect; Surface: PSDL_Surface); |
4 | 403 |
begin |
95 | 404 |
DrawFromRect(X - (Rect.w) div 2, Y, @Rect, StoreSurface, Surface) |
405 |
end; |
|
406 |
||
407 |
procedure DrawCentered(X, Top: integer; Source, Surface: PSDL_Surface); |
|
408 |
var r: TSDL_Rect; |
|
409 |
begin |
|
410 |
r.x:= X - Source.w div 2; |
|
411 |
r.y:= Top; |
|
412 |
r.w:= Source.w; |
|
413 |
r.h:= Source.h; |
|
414 |
SDL_UpperBlit(Source, nil, Surface, @r) |
|
4 | 415 |
end; |
416 |
||
417 |
procedure DrawHedgehog(X, Y: integer; Dir: integer; Pos, Step: LongWord; Surface: PSDL_Surface); |
|
418 |
var r: TSDL_Rect; |
|
419 |
begin |
|
420 |
r.x:= Step * 32; |
|
421 |
r.y:= Pos * 32; |
|
294 | 422 |
if Dir = -1 then r.x:= HHSurface.w - 32 - r.x; |
4 | 423 |
r.w:= 32; |
424 |
r.h:= 32; |
|
425 |
DrawFromRect(X, Y, @r, HHSurface, Surface) |
|
426 |
end; |
|
427 |
||
428 |
procedure StoreRelease; |
|
429 |
var ii: TSprite; |
|
430 |
begin |
|
431 |
for ii:= Low(TSprite) to High(TSprite) do |
|
432 |
SDL_FreeSurface(SpritesData[ii].Surface); |
|
433 |
SDL_FreeSurface( HHSurface ); |
|
434 |
SDL_FreeSurface(LandSurface ); |
|
435 |
SDL_FreeSurface(StoreSurface ) |
|
436 |
end; |
|
437 |
||
108 | 438 |
function RenderString(s: string; Color: integer; font: THWFont): PSDL_Surface; |
95 | 439 |
var w, h: integer; |
440 |
begin |
|
108 | 441 |
TTF_SizeUTF8(Fontz[font].Handle, PChar(s), w, h); |
202 | 442 |
Result:= SDL_CreateRGBSurface(SDL_HWSURFACE, w + FontBorder * 2 + 4, h + FontBorder * 2, |
443 |
cBits, PixelFormat.RMask, PixelFormat.GMask, PixelFormat.BMask, PixelFormat.AMask); |
|
107 | 444 |
TryDo(Result <> nil, 'RenderString: fail to create surface', true); |
95 | 445 |
WriteInRoundRect(Result, 0, 0, Color, font, s); |
446 |
TryDo(SDL_SetColorKey(Result, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true) |
|
447 |
end; |
|
448 |
||
4 | 449 |
procedure RenderHealth(var Hedgehog: THedgehog); |
95 | 450 |
var s: shortstring; |
4 | 451 |
begin |
452 |
str(Hedgehog.Gear.Health, s); |
|
95 | 453 |
if Hedgehog.HealthTag <> nil then SDL_FreeSurface(Hedgehog.HealthTag); |
454 |
Hedgehog.HealthTag:= RenderString(s, Hedgehog.Team^.Color, fnt16) |
|
4 | 455 |
end; |
456 |
||
457 |
procedure AddProgress; |
|
458 |
const Step: Longword = 0; |
|
459 |
ProgrSurf: PSDL_Surface = nil; |
|
71 | 460 |
MaxCalls = 11; // MaxCalls should be the count of calls to AddProgress to prevent memory leakage |
4 | 461 |
var r: TSDL_Rect; |
462 |
begin |
|
463 |
if Step = 0 then |
|
464 |
begin |
|
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
95
diff
changeset
|
465 |
WriteToConsole(msgLoading + 'progress sprite: '); |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
95
diff
changeset
|
466 |
ProgrSurf:= LoadImage(Pathz[ptGraphics] + '/BigDigits', false); |
4 | 467 |
end; |
468 |
SDL_FillRect(SDLPrimSurface, nil, 0); |
|
469 |
r.x:= 0; |
|
470 |
r.w:= 32; |
|
471 |
r.h:= 32; |
|
71 | 472 |
r.y:= (Step mod 10) * 32; |
4 | 473 |
DrawFromRect(cScreenWidth div 2 - 16, cScreenHeight div 2 - 16, @r, ProgrSurf, SDLPrimSurface); |
474 |
SDL_Flip(SDLPrimSurface); |
|
475 |
inc(Step); |
|
476 |
if Step = MaxCalls then |
|
477 |
begin |
|
478 |
WriteLnToConsole('Freeing progress surface... '); |
|
479 |
SDL_FreeSurface(ProgrSurf) |
|
480 |
end; |
|
481 |
end; |
|
482 |
||
198 | 483 |
function LoadImage(filename: string; hasAlpha: boolean; const critical: boolean = true; const setTransparent: boolean = true): PSDL_Surface; |
30 | 484 |
var tmpsurf: PSDL_Surface; |
4 | 485 |
begin |
486 |
WriteToConsole(msgLoading + filename + '... '); |
|
74 | 487 |
tmpsurf:= IMG_Load(PChar(filename + '.' + cBitsStr + '.png')); |
488 |
if tmpsurf = nil then |
|
489 |
tmpsurf:= IMG_Load(PChar(filename + '.png')); |
|
80 | 490 |
|
491 |
if tmpsurf = nil then |
|
492 |
if critical then OutError(msgFailed, true) |
|
493 |
else begin |
|
494 |
WriteLnToConsole(msgFailed); |
|
495 |
Result:= nil; |
|
496 |
exit |
|
497 |
end; |
|
498 |
||
198 | 499 |
if setTransparent then TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY or SDL_RLEACCEL, 0) = 0, errmsgTransparentSet, true); |
80 | 500 |
if hasAlpha then Result:= SDL_DisplayFormatAlpha(tmpsurf) |
501 |
else Result:= SDL_DisplayFormat(tmpsurf); |
|
4 | 502 |
WriteLnToConsole(msgOK) |
503 |
end; |
|
504 |
||
505 |
end. |