author | unc0rr |
Mon, 28 Jan 2008 17:11:19 +0000 | |
changeset 771 | 86fbe8753a7c |
parent 769 | 788efc1d649f |
child 775 | 23c253aae336 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
393 | 3 |
* Copyright (c) 2004-2007 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 |
|
755 | 21 |
uses uConsts, uTeams, SDLh, uFloat, GL; |
4 | 22 |
{$INCLUDE options.inc} |
23 |
||
24 |
procedure StoreInit; |
|
25 |
procedure StoreLoad; |
|
26 |
procedure StoreRelease; |
|
689 | 27 |
procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt; Surface: PSDL_Surface); |
371 | 28 |
procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt; Surface: PSDL_Surface); |
29 |
procedure DrawSprite2(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt; Surface: PSDL_Surface); |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
30 |
procedure DrawSurfSprite(X, Y, Height, Frame: LongInt; Source: PTexture; Surface: PSDL_Surface); |
762 | 31 |
procedure DrawLand (X, Y: LongInt); |
32 |
procedure DrawTexture(X, Y: LongInt; Texture: PTexture); |
|
371 | 33 |
procedure DXOutText(X, Y: LongInt; Font: THWFont; s: string; Surface: PSDL_Surface); |
762 | 34 |
procedure DrawCentered(X, Top: LongInt; Source: PTexture); |
755 | 35 |
procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture; DestSurface: PSDL_Surface); |
371 | 36 |
procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Surface: PSDL_Surface); |
762 | 37 |
function RenderStringTex(s: string; Color: Longword; font: THWFont): PTexture; |
4 | 38 |
procedure RenderHealth(var Hedgehog: THedgehog); |
39 |
procedure AddProgress; |
|
510 | 40 |
procedure FinishProgress; |
518 | 41 |
function LoadImage(const filename: string; hasAlpha, critical, setTransparent: boolean): PSDL_Surface; |
753 | 42 |
procedure SetupOpenGL; |
4 | 43 |
|
44 |
var PixelFormat: PSDL_PixelFormat; |
|
45 |
SDLPrimSurface: PSDL_Surface; |
|
762 | 46 |
PauseTexture: PTexture; |
4 | 47 |
|
48 |
implementation |
|
755 | 49 |
uses uMisc, uConsole, uLand, uLocale, GLU; |
4 | 50 |
|
690 | 51 |
var |
761 | 52 |
HHTexture: PTexture; |
4 | 53 |
|
54 |
procedure StoreInit; |
|
55 |
begin |
|
56 |
||
57 |
end; |
|
58 |
||
351 | 59 |
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean); |
47 | 60 |
var r: TSDL_Rect; |
61 |
begin |
|
62 |
r:= rect^; |
|
83 | 63 |
if Clear then SDL_FillRect(Surface, @r, 0); |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
64 |
|
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
65 |
BorderColor:= SDL_MapRGB(Surface^.format, BorderColor shr 16, BorderColor shr 8, BorderColor and $FF); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
66 |
FillColor:= SDL_MapRGB(Surface^.format, FillColor shr 16, FillColor shr 8, FillColor and $FF); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
67 |
|
351 | 68 |
r.y:= rect^.y + 1; |
69 |
r.h:= rect^.h - 2; |
|
47 | 70 |
SDL_FillRect(Surface, @r, BorderColor); |
351 | 71 |
r.x:= rect^.x + 1; |
72 |
r.w:= rect^.w - 2; |
|
73 |
r.y:= rect^.y; |
|
74 |
r.h:= rect^.h; |
|
47 | 75 |
SDL_FillRect(Surface, @r, BorderColor); |
351 | 76 |
r.x:= rect^.x + 2; |
77 |
r.y:= rect^.y + 1; |
|
78 |
r.w:= rect^.w - 4; |
|
79 |
r.h:= rect^.h - 2; |
|
47 | 80 |
SDL_FillRect(Surface, @r, FillColor); |
351 | 81 |
r.x:= rect^.x + 1; |
82 |
r.y:= rect^.y + 2; |
|
83 |
r.w:= rect^.w - 2; |
|
84 |
r.h:= rect^.h - 4; |
|
47 | 85 |
SDL_FillRect(Surface, @r, FillColor) |
86 |
end; |
|
87 |
||
371 | 88 |
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: string): TSDL_Rect; |
351 | 89 |
var w, h: LongInt; |
4 | 90 |
tmpsurf: PSDL_Surface; |
91 |
clr: TSDL_Color; |
|
351 | 92 |
Result: TSDL_Rect; |
4 | 93 |
begin |
355 | 94 |
TTF_SizeUTF8(Fontz[Font].Handle, Str2PChar(s), w, h); |
4 | 95 |
Result.x:= X; |
96 |
Result.y:= Y; |
|
202 | 97 |
Result.w:= w + FontBorder * 2 + 4; |
98 |
Result.h:= h + FontBorder * 2; |
|
351 | 99 |
DrawRoundRect(@Result, cWhiteColor, cColorNearBlack, Surface, true); |
189 | 100 |
clr.r:= Color shr 16; |
101 |
clr.g:= (Color shr 8) and $FF; |
|
102 |
clr.b:= Color and $FF; |
|
355 | 103 |
tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, Str2PChar(s), clr.value); |
202 | 104 |
Result.x:= X + FontBorder + 2; |
105 |
Result.y:= Y + FontBorder; |
|
199 | 106 |
SDLTry(tmpsurf <> nil, true); |
4 | 107 |
SDL_UpperBlit(tmpsurf, nil, Surface, @Result); |
108 |
SDL_FreeSurface(tmpsurf); |
|
109 |
Result.x:= X; |
|
110 |
Result.y:= Y; |
|
202 | 111 |
Result.w:= w + FontBorder * 2 + 4; |
351 | 112 |
Result.h:= h + FontBorder * 2; |
113 |
WriteInRoundRect:= Result |
|
4 | 114 |
end; |
115 |
||
116 |
procedure StoreLoad; |
|
689 | 117 |
var ii: TSprite; |
4 | 118 |
fi: THWFont; |
119 |
s: string; |
|
120 |
tmpsurf: PSDL_Surface; |
|
121 |
||
122 |
procedure WriteNames(Font: THWFont); |
|
547 | 123 |
var t: LongInt; |
371 | 124 |
i: LongInt; |
47 | 125 |
r, rr: TSDL_Rect; |
371 | 126 |
drY: LongInt; |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
127 |
texsurf: PSDL_Surface; |
4 | 128 |
begin |
129 |
r.x:= 0; |
|
690 | 130 |
r.y:= 0; |
70 | 131 |
drY:= cScreenHeight - 4; |
547 | 132 |
for t:= 0 to Pred(TeamsCount) do |
133 |
with TeamsArray[t]^ do |
|
4 | 134 |
begin |
762 | 135 |
NameTagTex:= RenderStringTex(TeamName, Clan^.Color, Font); |
690 | 136 |
|
47 | 137 |
r.w:= cTeamHealthWidth + 5; |
762 | 138 |
r.h:= NameTagTex^.h; |
690 | 139 |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
140 |
texsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 32, RMask, GMask, BMask, AMask); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
141 |
TryDo(texsurf <> nil, errmsgCreateSurface, true); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
142 |
TryDo(SDL_SetColorKey(texsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
690 | 143 |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
144 |
DrawRoundRect(@r, cWhiteColor, cColorNearBlack, texsurf, true); |
47 | 145 |
rr:= r; |
146 |
inc(rr.x, 2); dec(rr.w, 4); inc(rr.y, 2); dec(rr.h, 4); |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
147 |
DrawRoundRect(@rr, Clan^.Color, Clan^.Color, texsurf, false); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
148 |
HealthTex:= Surface2Tex(texsurf); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
149 |
SDL_FreeSurface(texsurf); |
690 | 150 |
|
49 | 151 |
dec(drY, r.h + 2); |
547 | 152 |
DrawHealthY:= drY; |
4 | 153 |
for i:= 0 to 7 do |
547 | 154 |
with Hedgehogs[i] do |
95 | 155 |
if Gear <> nil then |
762 | 156 |
NameTagTex:= RenderStringTex(Name, Clan^.Color, fnt16); |
4 | 157 |
end; |
158 |
end; |
|
159 |
||
160 |
procedure MakeCrossHairs; |
|
547 | 161 |
var t: LongInt; |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
162 |
tmpsurf, texsurf: PSDL_Surface; |
4 | 163 |
s: string; |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
164 |
Color: Longword; |
4 | 165 |
begin |
53 | 166 |
s:= Pathz[ptGraphics] + '/' + cCHFileName; |
351 | 167 |
tmpsurf:= LoadImage(s, true, true, false); |
4 | 168 |
|
547 | 169 |
for t:= 0 to Pred(TeamsCount) do |
170 |
with TeamsArray[t]^ do |
|
4 | 171 |
begin |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
172 |
texsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, tmpsurf^.w, tmpsurf^.h, 32, RMask, GMask, BMask, AMask); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
173 |
TryDo(texsurf <> nil, errmsgCreateSurface, true); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
174 |
|
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
175 |
Color:= Clan^.Color; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
176 |
Color:= SDL_MapRGB(texsurf^.format, Color shr 16, Color shr 8, Color and $FF); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
177 |
SDL_FillRect(texsurf, nil, Color); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
178 |
|
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
179 |
SDL_UpperBlit(tmpsurf, nil, texsurf, nil); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
180 |
TryDo(SDL_SetColorKey(texsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
181 |
|
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
182 |
CrosshairTex:= Surface2Tex(texsurf); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
183 |
SDL_FreeSurface(texsurf) |
4 | 184 |
end; |
351 | 185 |
|
4 | 186 |
SDL_FreeSurface(tmpsurf) |
187 |
end; |
|
188 |
||
189 |
procedure InitHealth; |
|
547 | 190 |
var i, t: LongInt; |
4 | 191 |
begin |
547 | 192 |
for t:= 0 to Pred(TeamsCount) do |
193 |
if TeamsArray[t] <> nil then |
|
194 |
with TeamsArray[t]^ do |
|
4 | 195 |
begin |
196 |
for i:= 0 to cMaxHHIndex do |
|
547 | 197 |
if Hedgehogs[i].Gear <> nil then |
198 |
RenderHealth(Hedgehogs[i]); |
|
4 | 199 |
end |
200 |
end; |
|
201 |
||
202 |
procedure LoadGraves; |
|
689 | 203 |
var t: LongInt; |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
204 |
texsurf: PSDL_Surface; |
4 | 205 |
begin |
547 | 206 |
for t:= 0 to Pred(TeamsCount) do |
207 |
if TeamsArray[t] <> nil then |
|
208 |
with TeamsArray[t]^ do |
|
4 | 209 |
begin |
547 | 210 |
if GraveName = '' then GraveName:= 'Simple'; |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
211 |
texsurf:= LoadImage(Pathz[ptGraves] + '/' + GraveName, false, true, true); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
212 |
GraveTex:= Surface2Tex(texsurf); |
765
a45111da760e
Fix a bug introduced in previous revision (accidental delete of land surface)
unc0rr
parents:
764
diff
changeset
|
213 |
SDL_FreeSurface(texsurf) |
4 | 214 |
end |
215 |
end; |
|
216 |
||
217 |
procedure GetSkyColor; |
|
755 | 218 |
// var p: PByteArray; |
4 | 219 |
begin |
755 | 220 |
(* if SDL_MustLock(SpritesData[sprSky].Surface) then |
80 | 221 |
SDLTry(SDL_LockSurface(SpritesData[sprSky].Surface) >= 0, true); |
351 | 222 |
p:= SpritesData[sprSky].Surface^.pixels; |
223 |
case SpritesData[sprSky].Surface^.format^.BytesPerPixel of |
|
4 | 224 |
1: cSkyColor:= PByte(p)^; |
225 |
2: cSkyColor:= PWord(p)^; |
|
107 | 226 |
3: cSkyColor:= (p^[0]) or (p^[1] shl 8) or (p^[2] shl 16); |
4 | 227 |
4: cSkyColor:= PLongword(p)^; |
755 | 228 |
end;*) |
229 |
cSkyColor:= $3030A0; |
|
753 | 230 |
glClearColor((cSkyColor shr 16) / 255, ((cSkyColor shr 8) and $FF) / 255, (cSkyColor and $FF) / 255, 0); |
231 |
||
755 | 232 |
// if SDL_MustLock(SpritesData[sprSky].Surface) then |
233 |
// SDL_UnlockSurface(SpritesData[sprSky].Surface) |
|
4 | 234 |
end; |
235 |
||
236 |
procedure GetExplosionBorderColor; |
|
237 |
var f: textfile; |
|
371 | 238 |
c: LongInt; |
4 | 239 |
begin |
80 | 240 |
s:= Pathz[ptCurrTheme] + '/' + cThemeCFGFilename; |
4 | 241 |
WriteToConsole(msgLoading + s + ' '); |
351 | 242 |
Assign(f, s); |
4 | 243 |
{$I-} |
244 |
Reset(f); |
|
245 |
Readln(f, s); |
|
351 | 246 |
Close(f); |
4 | 247 |
{$I+} |
248 |
TryDo(IOResult = 0, msgFailed, true); |
|
249 |
WriteLnToConsole(msgOK); |
|
250 |
val(s, cExplosionBorderColor, c); |
|
495 | 251 |
TryDo(c = 0, 'Theme data corrupted', true); |
768 | 252 |
cExplosionBorderColor:= (cExplosionBorderColor shr 16) or |
253 |
(cExplosionBorderColor and $FF) or |
|
254 |
(cExplosionBorderColor shl 16) or |
|
255 |
$FF000000 |
|
4 | 256 |
end; |
257 |
||
258 |
begin |
|
259 |
for fi:= Low(THWFont) to High(THWFont) do |
|
260 |
with Fontz[fi] do |
|
261 |
begin |
|
53 | 262 |
s:= Pathz[ptFonts] + '/' + Name; |
200 | 263 |
WriteToConsole(msgLoading + s + '... '); |
355 | 264 |
Handle:= TTF_OpenFont(Str2PChar(s), Height); |
200 | 265 |
SDLTry(Handle <> nil, true); |
202 | 266 |
TTF_SetFontStyle(Handle, style); |
4 | 267 |
WriteLnToConsole(msgOK) |
268 |
end; |
|
269 |
AddProgress; |
|
53 | 270 |
|
4 | 271 |
GetExplosionBorderColor; |
272 |
||
273 |
AddProgress; |
|
274 |
WriteNames(fnt16); |
|
70 | 275 |
MakeCrossHairs; |
4 | 276 |
LoadGraves; |
277 |
||
278 |
AddProgress; |
|
279 |
for ii:= Low(TSprite) to High(TSprite) do |
|
280 |
with SpritesData[ii] do |
|
80 | 281 |
begin |
282 |
if AltPath = ptNone then |
|
769
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
283 |
tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, true, true, true) |
80 | 284 |
else begin |
769
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
285 |
tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, true, false, true); |
755 | 286 |
if tmpsurf = nil then |
769
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
287 |
tmpsurf:= LoadImage(Pathz[AltPath] + '/' + FileName, true, true, true) |
80 | 288 |
end; |
755 | 289 |
if Width = 0 then Width:= tmpsurf^.w; |
290 |
if Height = 0 then Height:= tmpsurf^.h; |
|
761 | 291 |
Texture:= Surface2Tex(tmpsurf); |
769
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
292 |
if saveSurf then Surface:= tmpsurf else SDL_FreeSurface(tmpsurf) |
80 | 293 |
end; |
294 |
||
295 |
GetSkyColor; |
|
4 | 296 |
|
297 |
AddProgress; |
|
567 | 298 |
|
761 | 299 |
tmpsurf:= LoadImage(Pathz[ptGraphics] + '/' + cHHFileName, true, true, true); |
300 |
HHTexture:= Surface2Tex(tmpsurf); |
|
301 |
SDL_FreeSurface(tmpsurf); |
|
4 | 302 |
|
303 |
InitHealth; |
|
304 |
||
762 | 305 |
PauseTexture:= RenderStringTex(trmsg[sidPaused], $FFFF00, fntBig); |
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
208
diff
changeset
|
306 |
|
4 | 307 |
{$IFDEF DUMP} |
308 |
SDL_SaveBMP_RW(LandSurface, SDL_RWFromFile('LandSurface.bmp', 'wb'), 1); |
|
309 |
SDL_SaveBMP_RW(StoreSurface, SDL_RWFromFile('StoreSurface.bmp', 'wb'), 1); |
|
310 |
{$ENDIF} |
|
311 |
end; |
|
312 |
||
755 | 313 |
procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture; DestSurface: PSDL_Surface); |
4 | 314 |
var rr: TSDL_Rect; |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
315 |
_l, _r, _t, _b: real; |
4 | 316 |
begin |
759 | 317 |
if SourceTexture^.h = 0 then exit; |
4 | 318 |
rr.x:= X; |
319 |
rr.y:= Y; |
|
351 | 320 |
rr.w:= r^.w; |
321 |
rr.h:= r^.h; |
|
755 | 322 |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
323 |
_l:= r^.x / SourceTexture^.w; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
324 |
_r:= (r^.x + r^.w) / SourceTexture^.w; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
325 |
_t:= r^.y / SourceTexture^.h; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
326 |
_b:= (r^.y + r^.h) / SourceTexture^.h; |
755 | 327 |
|
328 |
glBindTexture(GL_TEXTURE_2D, SourceTexture^.id); |
|
329 |
glEnable(GL_TEXTURE_2D); |
|
330 |
||
331 |
glBegin(GL_QUADS); |
|
332 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
333 |
glTexCoord2f(_l, _t); |
755 | 334 |
glVertex2i(X, Y); |
335 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
336 |
glTexCoord2f(_r, _t); |
755 | 337 |
glVertex2i(rr.w + X, Y); |
338 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
339 |
glTexCoord2f(_r, _b); |
755 | 340 |
glVertex2i(rr.w + X, rr.h + Y); |
341 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
342 |
glTexCoord2f(_l, _b); |
755 | 343 |
glVertex2i(X, rr.h + Y); |
344 |
||
762 | 345 |
glEnd() |
346 |
end; |
|
347 |
||
348 |
procedure DrawTexture(X, Y: LongInt; Texture: PTexture); |
|
349 |
begin |
|
350 |
glBindTexture(GL_TEXTURE_2D, Texture^.id); |
|
351 |
glEnable(GL_TEXTURE_2D); |
|
352 |
||
353 |
glBegin(GL_QUADS); |
|
354 |
||
355 |
glTexCoord2f(0, 0); |
|
356 |
glVertex2i(X, Y); |
|
357 |
||
358 |
glTexCoord2f(1, 0); |
|
359 |
glVertex2i(Texture^.w + X, Y); |
|
360 |
||
361 |
glTexCoord2f(1, 1); |
|
362 |
glVertex2i(Texture^.w + X, Texture^.h + Y); |
|
363 |
||
364 |
glTexCoord2f(0, 1); |
|
365 |
glVertex2i(X, Texture^.h + Y); |
|
366 |
||
367 |
glEnd() |
|
4 | 368 |
end; |
369 |
||
689 | 370 |
procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt; Surface: PSDL_Surface); |
4 | 371 |
begin |
372 |
r.y:= r.y + Height * Position; |
|
373 |
r.h:= Height; |
|
755 | 374 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture, Surface) |
4 | 375 |
end; |
376 |
||
371 | 377 |
procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt; Surface: PSDL_Surface); |
755 | 378 |
var r: TSDL_Rect; |
4 | 379 |
begin |
755 | 380 |
r.x:= 0; |
381 |
r.w:= SpritesData[Sprite].Width; |
|
382 |
r.y:= Frame * SpritesData[Sprite].Height; |
|
383 |
r.h:= SpritesData[Sprite].Height; |
|
384 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture, Surface) |
|
4 | 385 |
end; |
386 |
||
371 | 387 |
procedure DrawSprite2(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt; Surface: PSDL_Surface); |
43 | 388 |
var r: TSDL_Rect; |
389 |
begin |
|
390 |
r.x:= FrameX * SpritesData[Sprite].Width; |
|
391 |
r.w:= SpritesData[Sprite].Width; |
|
392 |
r.y:= FrameY * SpritesData[Sprite].Height; |
|
393 |
r.h:= SpritesData[Sprite].Height; |
|
755 | 394 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture, Surface) |
43 | 395 |
end; |
396 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
397 |
procedure DrawSurfSprite(X, Y, Height, Frame: LongInt; Source: PTexture; Surface: PSDL_Surface); |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
398 |
var r: TSDL_Rect; |
198 | 399 |
begin |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
400 |
r.x:= 0; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
401 |
r.w:= Source^.w; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
402 |
r.y:= Frame * Height; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
403 |
r.h:= Height; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
404 |
DrawFromRect(X, Y, @r, Source, Surface) |
198 | 405 |
end; |
406 |
||
371 | 407 |
procedure DXOutText(X, Y: LongInt; Font: THWFont; s: string; Surface: PSDL_Surface); |
4 | 408 |
var clr: TSDL_Color; |
409 |
tmpsurf: PSDL_Surface; |
|
410 |
r: TSDL_Rect; |
|
411 |
begin |
|
412 |
r.x:= X; |
|
413 |
r.y:= Y; |
|
189 | 414 |
clr.r:= $FF; |
415 |
clr.g:= $FF; |
|
416 |
clr.b:= $FF; |
|
355 | 417 |
tmpsurf:= TTF_RenderUTF8_Solid(Fontz[Font].Handle, Str2PChar(s), clr.value); |
208 | 418 |
if tmpsurf = nil then |
419 |
begin |
|
420 |
SetKB(1); |
|
421 |
exit |
|
422 |
end; |
|
4 | 423 |
SDL_UpperBlit(tmpsurf, nil, Surface, @r); |
424 |
SDL_FreeSurface(tmpsurf) |
|
425 |
end; |
|
426 |
||
762 | 427 |
procedure DrawLand(X, Y: LongInt); |
4 | 428 |
begin |
762 | 429 |
DrawTexture(X, Y, LandTexture) |
4 | 430 |
end; |
431 |
||
762 | 432 |
procedure DrawCentered(X, Top: LongInt; Source: PTexture); |
95 | 433 |
begin |
762 | 434 |
DrawTexture(X - Source^.w div 2, Top, Source) |
4 | 435 |
end; |
436 |
||
371 | 437 |
procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Surface: PSDL_Surface); |
761 | 438 |
var l, r, t, b: real; |
4 | 439 |
begin |
761 | 440 |
|
441 |
t:= Pos * 32 / HHTexture^.h; |
|
442 |
b:= (Pos + 1) * 32 / HHTexture^.h; |
|
443 |
||
444 |
if Dir = -1 then |
|
445 |
begin |
|
446 |
l:= (Step + 1) * 32 / HHTexture^.w; |
|
447 |
r:= Step * 32 / HHTexture^.w |
|
448 |
end else |
|
449 |
begin |
|
450 |
l:= Step * 32 / HHTexture^.w; |
|
451 |
r:= (Step + 1) * 32 / HHTexture^.w |
|
452 |
end; |
|
453 |
||
454 |
glBindTexture(GL_TEXTURE_2D, HHTexture^.id); |
|
455 |
glEnable(GL_TEXTURE_2D); |
|
456 |
||
457 |
glBegin(GL_QUADS); |
|
458 |
||
459 |
glTexCoord2f(l, t); |
|
460 |
glVertex2i(X, Y); |
|
461 |
||
462 |
glTexCoord2f(r, t); |
|
463 |
glVertex2i(32 + X, Y); |
|
464 |
||
465 |
glTexCoord2f(r, b); |
|
466 |
glVertex2i(32 + X, 32 + Y); |
|
467 |
||
468 |
glTexCoord2f(l, b); |
|
469 |
glVertex2i(X, 32 + Y); |
|
470 |
||
471 |
glEnd(); |
|
472 |
||
4 | 473 |
end; |
474 |
||
475 |
procedure StoreRelease; |
|
476 |
var ii: TSprite; |
|
477 |
begin |
|
478 |
for ii:= Low(TSprite) to High(TSprite) do |
|
769
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
479 |
begin |
759 | 480 |
FreeTexture(SpritesData[ii].Texture); |
769
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
481 |
if SpritesData[ii].Surface <> nil then SDL_FreeSurface(SpritesData[ii].Surface) |
788efc1d649f
- Save 8 MB of memory by freeing LandSurface and not using it anymore after game initialization
unc0rr
parents:
768
diff
changeset
|
482 |
end; |
761 | 483 |
|
484 |
FreeTexture(HHTexture); |
|
485 |
FreeTexture(LandTexture); |
|
486 |
||
487 |
SDL_FreeSurface(LandSurface) |
|
4 | 488 |
end; |
489 |
||
762 | 490 |
function RenderStringTex(s: string; Color: Longword; font: THWFont): PTexture; |
432 | 491 |
var w, h: LongInt; |
351 | 492 |
Result: PSDL_Surface; |
95 | 493 |
begin |
355 | 494 |
TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), w, h); |
762 | 495 |
Result:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + FontBorder * 2 + 4, h + FontBorder * 2, |
496 |
32, RMask, GMask, BMask, AMask); |
|
107 | 497 |
TryDo(Result <> nil, 'RenderString: fail to create surface', true); |
95 | 498 |
WriteInRoundRect(Result, 0, 0, Color, font, s); |
762 | 499 |
TryDo(SDL_SetColorKey(Result, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
500 |
RenderStringTex:= Surface2Tex(Result); |
|
501 |
SDL_FreeSurface(Result) |
|
95 | 502 |
end; |
503 |
||
4 | 504 |
procedure RenderHealth(var Hedgehog: THedgehog); |
95 | 505 |
var s: shortstring; |
4 | 506 |
begin |
351 | 507 |
str(Hedgehog.Gear^.Health, s); |
762 | 508 |
if Hedgehog.HealthTagTex <> nil then FreeTexture(Hedgehog.HealthTagTex); |
509 |
Hedgehog.HealthTagTex:= RenderStringTex(s, Hedgehog.Team^.Clan^.Color, fnt16) |
|
4 | 510 |
end; |
511 |
||
518 | 512 |
function LoadImage(const filename: string; hasAlpha: boolean; critical, setTransparent: boolean): PSDL_Surface; |
30 | 513 |
var tmpsurf: PSDL_Surface; |
753 | 514 |
//Result: PSDL_Surface; |
355 | 515 |
s: shortstring; |
4 | 516 |
begin |
517 |
WriteToConsole(msgLoading + filename + '... '); |
|
355 | 518 |
s:= filename + '.' + cBitsStr + '.png'; |
519 |
tmpsurf:= IMG_Load(Str2PChar(s)); |
|
351 | 520 |
|
74 | 521 |
if tmpsurf = nil then |
351 | 522 |
begin |
355 | 523 |
s:= filename + '.png'; |
524 |
tmpsurf:= IMG_Load(Str2PChar(s)); |
|
351 | 525 |
end; |
80 | 526 |
|
527 |
if tmpsurf = nil then |
|
528 |
if critical then OutError(msgFailed, true) |
|
529 |
else begin |
|
530 |
WriteLnToConsole(msgFailed); |
|
351 | 531 |
exit(nil) |
80 | 532 |
end; |
351 | 533 |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
534 |
if setTransparent then TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
753 | 535 |
//if hasAlpha then Result:= SDL_DisplayFormatAlpha(tmpsurf) |
536 |
// else Result:= SDL_DisplayFormat(tmpsurf); |
|
620 | 537 |
{$IFDEF DEBUGFILE}WriteLnToConsole('(' + inttostr(tmpsurf^.w) + ',' + inttostr(tmpsurf^.h) + ') ');{$ENDIF} |
351 | 538 |
WriteLnToConsole(msgOK); |
753 | 539 |
LoadImage:= tmpsurf//Result |
540 |
end; |
|
541 |
||
542 |
procedure SetupOpenGL; |
|
543 |
begin |
|
756 | 544 |
glLoadIdentity; |
753 | 545 |
glViewport(0, 0, cScreenWidth, cScreenHeight); |
754 | 546 |
glScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0); |
547 |
glTranslatef(-cScreenWidth / 2, -cScreenHeight / 2, 0); |
|
756 | 548 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
753 | 549 |
glMatrixMode(GL_MODELVIEW) |
4 | 550 |
end; |
551 |
||
510 | 552 |
//////////////////////////////////////////////////////////////////////////////// |
766 | 553 |
var ProgrTex: PTexture = nil; |
534 | 554 |
Step: integer = 0; |
510 | 555 |
|
556 |
procedure AddProgress; |
|
557 |
var r: TSDL_Rect; |
|
766 | 558 |
texsurf: PSDL_Surface; |
510 | 559 |
begin |
560 |
if Step = 0 then |
|
561 |
begin |
|
562 |
WriteToConsole(msgLoading + 'progress sprite: '); |
|
766 | 563 |
texsurf:= LoadImage(Pathz[ptGraphics] + '/Progress', false, true, true); |
564 |
ProgrTex:= Surface2Tex(texsurf); |
|
565 |
SDL_FreeSurface(texsurf) |
|
510 | 566 |
end; |
766 | 567 |
glClear(GL_COLOR_BUFFER_BIT); |
510 | 568 |
r.x:= 0; |
766 | 569 |
r.w:= ProgrTex^.w; |
570 |
r.h:= ProgrTex^.w; |
|
571 |
r.y:= (Step mod (ProgrTex^.h div ProgrTex^.w)) * ProgrTex^.w; |
|
572 |
DrawFromRect((cScreenWidth - ProgrTex^.w) div 2, |
|
573 |
(cScreenHeight - ProgrTex^.w) div 2, @r, ProgrTex, SDLPrimSurface); |
|
574 |
SDL_GL_SwapBuffers(); |
|
510 | 575 |
inc(Step); |
576 |
end; |
|
577 |
||
578 |
procedure FinishProgress; |
|
579 |
begin |
|
580 |
WriteLnToConsole('Freeing progress surface... '); |
|
766 | 581 |
FreeTexture(ProgrTex) |
510 | 582 |
end; |
583 |
||
4 | 584 |
end. |