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