author | unc0rr |
Sun, 27 Jan 2008 17:27:13 +0000 | |
changeset 766 | cdc8f75ab7bc |
parent 765 | a45111da760e |
child 768 | 2886dafa5bcf |
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); |
766 | 252 |
cExplosionBorderColor:= SDL_MapRGB(LandSurface^.format, |
253 |
cExplosionBorderColor shr 16, |
|
254 |
cExplosionBorderColor shr 8, |
|
255 |
cExplosionBorderColor and $FF); |
|
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 |
|
105 | 271 |
WriteToConsole('LandSurface tuning... '); |
4 | 272 |
tmpsurf:= LandSurface; |
273 |
TryDo(tmpsurf <> nil, msgFailed, true); |
|
274 |
if cFullScreen then |
|
275 |
begin |
|
276 |
LandSurface:= SDL_DisplayFormat(tmpsurf); |
|
277 |
SDL_FreeSurface(tmpsurf); |
|
278 |
end else LandSurface:= tmpsurf; |
|
35 | 279 |
TryDo(SDL_SetColorKey(LandSurface, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
4 | 280 |
WriteLnToConsole(msgOK); |
281 |
||
282 |
GetExplosionBorderColor; |
|
283 |
||
284 |
AddProgress; |
|
285 |
WriteNames(fnt16); |
|
70 | 286 |
MakeCrossHairs; |
4 | 287 |
LoadGraves; |
288 |
||
289 |
AddProgress; |
|
290 |
for ii:= Low(TSprite) to High(TSprite) do |
|
291 |
with SpritesData[ii] do |
|
80 | 292 |
begin |
293 |
if AltPath = ptNone then |
|
755 | 294 |
tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, hasAlpha, true, true) |
80 | 295 |
else begin |
755 | 296 |
tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, hasAlpha, false, true); |
297 |
if tmpsurf = nil then |
|
298 |
tmpsurf:= LoadImage(Pathz[AltPath] + '/' + FileName, hasAlpha, true, true) |
|
80 | 299 |
end; |
755 | 300 |
if Width = 0 then Width:= tmpsurf^.w; |
301 |
if Height = 0 then Height:= tmpsurf^.h; |
|
761 | 302 |
Texture:= Surface2Tex(tmpsurf); |
303 |
SDL_FreeSurface(tmpsurf) |
|
80 | 304 |
end; |
305 |
||
306 |
GetSkyColor; |
|
4 | 307 |
|
308 |
AddProgress; |
|
567 | 309 |
|
761 | 310 |
tmpsurf:= LoadImage(Pathz[ptGraphics] + '/' + cHHFileName, true, true, true); |
311 |
HHTexture:= Surface2Tex(tmpsurf); |
|
312 |
SDL_FreeSurface(tmpsurf); |
|
4 | 313 |
|
314 |
InitHealth; |
|
315 |
||
762 | 316 |
PauseTexture:= RenderStringTex(trmsg[sidPaused], $FFFF00, fntBig); |
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
208
diff
changeset
|
317 |
|
4 | 318 |
{$IFDEF DUMP} |
319 |
SDL_SaveBMP_RW(LandSurface, SDL_RWFromFile('LandSurface.bmp', 'wb'), 1); |
|
320 |
SDL_SaveBMP_RW(StoreSurface, SDL_RWFromFile('StoreSurface.bmp', 'wb'), 1); |
|
321 |
{$ENDIF} |
|
322 |
end; |
|
323 |
||
755 | 324 |
procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture; DestSurface: PSDL_Surface); |
4 | 325 |
var rr: TSDL_Rect; |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
326 |
_l, _r, _t, _b: real; |
4 | 327 |
begin |
759 | 328 |
if SourceTexture^.h = 0 then exit; |
4 | 329 |
rr.x:= X; |
330 |
rr.y:= Y; |
|
351 | 331 |
rr.w:= r^.w; |
332 |
rr.h:= r^.h; |
|
755 | 333 |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
334 |
_l:= r^.x / SourceTexture^.w; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
335 |
_r:= (r^.x + r^.w) / SourceTexture^.w; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
336 |
_t:= r^.y / SourceTexture^.h; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
337 |
_b:= (r^.y + r^.h) / SourceTexture^.h; |
755 | 338 |
|
339 |
glBindTexture(GL_TEXTURE_2D, SourceTexture^.id); |
|
340 |
glEnable(GL_TEXTURE_2D); |
|
341 |
||
342 |
glBegin(GL_QUADS); |
|
343 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
344 |
glTexCoord2f(_l, _t); |
755 | 345 |
glVertex2i(X, Y); |
346 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
347 |
glTexCoord2f(_r, _t); |
755 | 348 |
glVertex2i(rr.w + X, Y); |
349 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
350 |
glTexCoord2f(_r, _b); |
755 | 351 |
glVertex2i(rr.w + X, rr.h + Y); |
352 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
353 |
glTexCoord2f(_l, _b); |
755 | 354 |
glVertex2i(X, rr.h + Y); |
355 |
||
762 | 356 |
glEnd() |
357 |
end; |
|
358 |
||
359 |
procedure DrawTexture(X, Y: LongInt; Texture: PTexture); |
|
360 |
begin |
|
361 |
glBindTexture(GL_TEXTURE_2D, Texture^.id); |
|
362 |
glEnable(GL_TEXTURE_2D); |
|
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 |
||
689 | 381 |
procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt; Surface: PSDL_Surface); |
4 | 382 |
begin |
383 |
r.y:= r.y + Height * Position; |
|
384 |
r.h:= Height; |
|
755 | 385 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture, Surface) |
4 | 386 |
end; |
387 |
||
371 | 388 |
procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt; Surface: PSDL_Surface); |
755 | 389 |
var r: TSDL_Rect; |
4 | 390 |
begin |
755 | 391 |
r.x:= 0; |
392 |
r.w:= SpritesData[Sprite].Width; |
|
393 |
r.y:= Frame * SpritesData[Sprite].Height; |
|
394 |
r.h:= SpritesData[Sprite].Height; |
|
395 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture, Surface) |
|
4 | 396 |
end; |
397 |
||
371 | 398 |
procedure DrawSprite2(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt; Surface: PSDL_Surface); |
43 | 399 |
var r: TSDL_Rect; |
400 |
begin |
|
401 |
r.x:= FrameX * SpritesData[Sprite].Width; |
|
402 |
r.w:= SpritesData[Sprite].Width; |
|
403 |
r.y:= FrameY * SpritesData[Sprite].Height; |
|
404 |
r.h:= SpritesData[Sprite].Height; |
|
755 | 405 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture, Surface) |
43 | 406 |
end; |
407 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
408 |
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
|
409 |
var r: TSDL_Rect; |
198 | 410 |
begin |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
411 |
r.x:= 0; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
412 |
r.w:= Source^.w; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
413 |
r.y:= Frame * Height; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
414 |
r.h:= Height; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
415 |
DrawFromRect(X, Y, @r, Source, Surface) |
198 | 416 |
end; |
417 |
||
371 | 418 |
procedure DXOutText(X, Y: LongInt; Font: THWFont; s: string; Surface: PSDL_Surface); |
4 | 419 |
var clr: TSDL_Color; |
420 |
tmpsurf: PSDL_Surface; |
|
421 |
r: TSDL_Rect; |
|
422 |
begin |
|
423 |
r.x:= X; |
|
424 |
r.y:= Y; |
|
189 | 425 |
clr.r:= $FF; |
426 |
clr.g:= $FF; |
|
427 |
clr.b:= $FF; |
|
355 | 428 |
tmpsurf:= TTF_RenderUTF8_Solid(Fontz[Font].Handle, Str2PChar(s), clr.value); |
208 | 429 |
if tmpsurf = nil then |
430 |
begin |
|
431 |
SetKB(1); |
|
432 |
exit |
|
433 |
end; |
|
4 | 434 |
SDL_UpperBlit(tmpsurf, nil, Surface, @r); |
435 |
SDL_FreeSurface(tmpsurf) |
|
436 |
end; |
|
437 |
||
762 | 438 |
procedure DrawLand(X, Y: LongInt); |
4 | 439 |
begin |
762 | 440 |
DrawTexture(X, Y, LandTexture) |
4 | 441 |
end; |
442 |
||
762 | 443 |
procedure DrawCentered(X, Top: LongInt; Source: PTexture); |
95 | 444 |
begin |
762 | 445 |
DrawTexture(X - Source^.w div 2, Top, Source) |
4 | 446 |
end; |
447 |
||
371 | 448 |
procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Surface: PSDL_Surface); |
761 | 449 |
var l, r, t, b: real; |
4 | 450 |
begin |
761 | 451 |
|
452 |
t:= Pos * 32 / HHTexture^.h; |
|
453 |
b:= (Pos + 1) * 32 / HHTexture^.h; |
|
454 |
||
455 |
if Dir = -1 then |
|
456 |
begin |
|
457 |
l:= (Step + 1) * 32 / HHTexture^.w; |
|
458 |
r:= Step * 32 / HHTexture^.w |
|
459 |
end else |
|
460 |
begin |
|
461 |
l:= Step * 32 / HHTexture^.w; |
|
462 |
r:= (Step + 1) * 32 / HHTexture^.w |
|
463 |
end; |
|
464 |
||
465 |
glBindTexture(GL_TEXTURE_2D, HHTexture^.id); |
|
466 |
glEnable(GL_TEXTURE_2D); |
|
467 |
||
468 |
glBegin(GL_QUADS); |
|
469 |
||
470 |
glTexCoord2f(l, t); |
|
471 |
glVertex2i(X, Y); |
|
472 |
||
473 |
glTexCoord2f(r, t); |
|
474 |
glVertex2i(32 + X, Y); |
|
475 |
||
476 |
glTexCoord2f(r, b); |
|
477 |
glVertex2i(32 + X, 32 + Y); |
|
478 |
||
479 |
glTexCoord2f(l, b); |
|
480 |
glVertex2i(X, 32 + Y); |
|
481 |
||
482 |
glEnd(); |
|
483 |
||
4 | 484 |
end; |
485 |
||
486 |
procedure StoreRelease; |
|
487 |
var ii: TSprite; |
|
488 |
begin |
|
489 |
for ii:= Low(TSprite) to High(TSprite) do |
|
759 | 490 |
FreeTexture(SpritesData[ii].Texture); |
761 | 491 |
|
492 |
FreeTexture(HHTexture); |
|
493 |
FreeTexture(LandTexture); |
|
494 |
||
495 |
SDL_FreeSurface(LandSurface) |
|
4 | 496 |
end; |
497 |
||
762 | 498 |
function RenderStringTex(s: string; Color: Longword; font: THWFont): PTexture; |
432 | 499 |
var w, h: LongInt; |
351 | 500 |
Result: PSDL_Surface; |
95 | 501 |
begin |
355 | 502 |
TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), w, h); |
762 | 503 |
Result:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + FontBorder * 2 + 4, h + FontBorder * 2, |
504 |
32, RMask, GMask, BMask, AMask); |
|
107 | 505 |
TryDo(Result <> nil, 'RenderString: fail to create surface', true); |
95 | 506 |
WriteInRoundRect(Result, 0, 0, Color, font, s); |
762 | 507 |
TryDo(SDL_SetColorKey(Result, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
508 |
RenderStringTex:= Surface2Tex(Result); |
|
509 |
SDL_FreeSurface(Result) |
|
95 | 510 |
end; |
511 |
||
4 | 512 |
procedure RenderHealth(var Hedgehog: THedgehog); |
95 | 513 |
var s: shortstring; |
4 | 514 |
begin |
351 | 515 |
str(Hedgehog.Gear^.Health, s); |
762 | 516 |
if Hedgehog.HealthTagTex <> nil then FreeTexture(Hedgehog.HealthTagTex); |
517 |
Hedgehog.HealthTagTex:= RenderStringTex(s, Hedgehog.Team^.Clan^.Color, fnt16) |
|
4 | 518 |
end; |
519 |
||
518 | 520 |
function LoadImage(const filename: string; hasAlpha: boolean; critical, setTransparent: boolean): PSDL_Surface; |
30 | 521 |
var tmpsurf: PSDL_Surface; |
753 | 522 |
//Result: PSDL_Surface; |
355 | 523 |
s: shortstring; |
4 | 524 |
begin |
525 |
WriteToConsole(msgLoading + filename + '... '); |
|
355 | 526 |
s:= filename + '.' + cBitsStr + '.png'; |
527 |
tmpsurf:= IMG_Load(Str2PChar(s)); |
|
351 | 528 |
|
74 | 529 |
if tmpsurf = nil then |
351 | 530 |
begin |
355 | 531 |
s:= filename + '.png'; |
532 |
tmpsurf:= IMG_Load(Str2PChar(s)); |
|
351 | 533 |
end; |
80 | 534 |
|
535 |
if tmpsurf = nil then |
|
536 |
if critical then OutError(msgFailed, true) |
|
537 |
else begin |
|
538 |
WriteLnToConsole(msgFailed); |
|
351 | 539 |
exit(nil) |
80 | 540 |
end; |
351 | 541 |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
542 |
if setTransparent then TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
753 | 543 |
//if hasAlpha then Result:= SDL_DisplayFormatAlpha(tmpsurf) |
544 |
// else Result:= SDL_DisplayFormat(tmpsurf); |
|
620 | 545 |
{$IFDEF DEBUGFILE}WriteLnToConsole('(' + inttostr(tmpsurf^.w) + ',' + inttostr(tmpsurf^.h) + ') ');{$ENDIF} |
351 | 546 |
WriteLnToConsole(msgOK); |
753 | 547 |
LoadImage:= tmpsurf//Result |
548 |
end; |
|
549 |
||
550 |
procedure SetupOpenGL; |
|
551 |
begin |
|
756 | 552 |
glLoadIdentity; |
753 | 553 |
glViewport(0, 0, cScreenWidth, cScreenHeight); |
754 | 554 |
glScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0); |
555 |
glTranslatef(-cScreenWidth / 2, -cScreenHeight / 2, 0); |
|
756 | 556 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
753 | 557 |
glMatrixMode(GL_MODELVIEW) |
4 | 558 |
end; |
559 |
||
510 | 560 |
//////////////////////////////////////////////////////////////////////////////// |
766 | 561 |
var ProgrTex: PTexture = nil; |
534 | 562 |
Step: integer = 0; |
510 | 563 |
|
564 |
procedure AddProgress; |
|
565 |
var r: TSDL_Rect; |
|
766 | 566 |
texsurf: PSDL_Surface; |
510 | 567 |
begin |
568 |
if Step = 0 then |
|
569 |
begin |
|
570 |
WriteToConsole(msgLoading + 'progress sprite: '); |
|
766 | 571 |
texsurf:= LoadImage(Pathz[ptGraphics] + '/Progress', false, true, true); |
572 |
ProgrTex:= Surface2Tex(texsurf); |
|
573 |
SDL_FreeSurface(texsurf) |
|
510 | 574 |
end; |
766 | 575 |
glClear(GL_COLOR_BUFFER_BIT); |
510 | 576 |
r.x:= 0; |
766 | 577 |
r.w:= ProgrTex^.w; |
578 |
r.h:= ProgrTex^.w; |
|
579 |
r.y:= (Step mod (ProgrTex^.h div ProgrTex^.w)) * ProgrTex^.w; |
|
580 |
DrawFromRect((cScreenWidth - ProgrTex^.w) div 2, |
|
581 |
(cScreenHeight - ProgrTex^.w) div 2, @r, ProgrTex, SDLPrimSurface); |
|
582 |
SDL_GL_SwapBuffers(); |
|
510 | 583 |
inc(Step); |
584 |
end; |
|
585 |
||
586 |
procedure FinishProgress; |
|
587 |
begin |
|
588 |
WriteLnToConsole('Freeing progress surface... '); |
|
766 | 589 |
FreeTexture(ProgrTex) |
510 | 590 |
end; |
591 |
||
4 | 592 |
end. |