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