author | unc0rr |
Thu, 02 Apr 2009 18:50:08 +0000 | |
changeset 1931 | ffe420e9e61a |
parent 1916 | 9c3d0e3df6bb |
child 1939 | 4b8e4cd9e7c0 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2004-2008 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 |
|
1906 | 21 |
uses uConsts, uTeams, SDLh, |
22 |
{$IFDEF IPHONE} |
|
23 |
gles11, |
|
24 |
{$ELSE} |
|
25 |
GL, |
|
26 |
{$ENDIF} |
|
27 |
uFloat; |
|
4 | 28 |
{$INCLUDE options.inc} |
29 |
||
30 |
procedure StoreInit; |
|
31 |
procedure StoreLoad; |
|
32 |
procedure StoreRelease; |
|
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
33 |
procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt); |
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
34 |
procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt); |
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
35 |
procedure DrawSprite2(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt); |
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
36 |
procedure DrawSurfSprite(X, Y, Height, Frame: LongInt; Source: PTexture); |
762 | 37 |
procedure DrawTexture(X, Y: LongInt; Texture: PTexture); |
1251 | 38 |
procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, Frames: LongInt); |
822 | 39 |
procedure DrawRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real); |
853 | 40 |
procedure DrawRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real); |
822 | 41 |
procedure DrawRotatedTex(Tex: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real); |
762 | 42 |
procedure DrawCentered(X, Top: LongInt; Source: PTexture); |
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
43 |
procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
1865
ebc6dfca60d4
- nemo's patch: some animations, zero probability for infinite weapons
unc0rr
parents:
1854
diff
changeset
|
44 |
procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real); |
1431 | 45 |
procedure DrawFillRect(r: TSDL_Rect); |
762 | 46 |
function RenderStringTex(s: string; Color: Longword; font: THWFont): PTexture; |
4 | 47 |
procedure RenderHealth(var Hedgehog: THedgehog); |
48 |
procedure AddProgress; |
|
510 | 49 |
procedure FinishProgress; |
518 | 50 |
function LoadImage(const filename: string; hasAlpha, critical, setTransparent: boolean): PSDL_Surface; |
753 | 51 |
procedure SetupOpenGL; |
4 | 52 |
|
1525 | 53 |
var PixelFormat: PSDL_PixelFormat = nil; |
54 |
SDLPrimSurface: PSDL_Surface = nil; |
|
1023 | 55 |
PauseTexture, |
56 |
ConfirmTexture: PTexture; |
|
4 | 57 |
|
58 |
implementation |
|
1906 | 59 |
uses uMisc, uConsole, uLand, uLocale; |
4 | 60 |
|
690 | 61 |
var |
761 | 62 |
HHTexture: PTexture; |
4 | 63 |
|
64 |
procedure StoreInit; |
|
65 |
begin |
|
66 |
||
67 |
end; |
|
68 |
||
351 | 69 |
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean); |
47 | 70 |
var r: TSDL_Rect; |
71 |
begin |
|
72 |
r:= rect^; |
|
83 | 73 |
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
|
74 |
|
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
75 |
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
|
76 |
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
|
77 |
|
351 | 78 |
r.y:= rect^.y + 1; |
79 |
r.h:= rect^.h - 2; |
|
47 | 80 |
SDL_FillRect(Surface, @r, BorderColor); |
351 | 81 |
r.x:= rect^.x + 1; |
82 |
r.w:= rect^.w - 2; |
|
83 |
r.y:= rect^.y; |
|
84 |
r.h:= rect^.h; |
|
47 | 85 |
SDL_FillRect(Surface, @r, BorderColor); |
351 | 86 |
r.x:= rect^.x + 2; |
87 |
r.y:= rect^.y + 1; |
|
88 |
r.w:= rect^.w - 4; |
|
89 |
r.h:= rect^.h - 2; |
|
47 | 90 |
SDL_FillRect(Surface, @r, FillColor); |
351 | 91 |
r.x:= rect^.x + 1; |
92 |
r.y:= rect^.y + 2; |
|
93 |
r.w:= rect^.w - 2; |
|
94 |
r.h:= rect^.h - 4; |
|
47 | 95 |
SDL_FillRect(Surface, @r, FillColor) |
96 |
end; |
|
97 |
||
371 | 98 |
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: string): TSDL_Rect; |
351 | 99 |
var w, h: LongInt; |
4 | 100 |
tmpsurf: PSDL_Surface; |
101 |
clr: TSDL_Color; |
|
351 | 102 |
Result: TSDL_Rect; |
4 | 103 |
begin |
355 | 104 |
TTF_SizeUTF8(Fontz[Font].Handle, Str2PChar(s), w, h); |
4 | 105 |
Result.x:= X; |
106 |
Result.y:= Y; |
|
202 | 107 |
Result.w:= w + FontBorder * 2 + 4; |
108 |
Result.h:= h + FontBorder * 2; |
|
351 | 109 |
DrawRoundRect(@Result, cWhiteColor, cColorNearBlack, Surface, true); |
189 | 110 |
clr.r:= Color shr 16; |
111 |
clr.g:= (Color shr 8) and $FF; |
|
112 |
clr.b:= Color and $FF; |
|
355 | 113 |
tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, Str2PChar(s), clr.value); |
202 | 114 |
Result.x:= X + FontBorder + 2; |
115 |
Result.y:= Y + FontBorder; |
|
199 | 116 |
SDLTry(tmpsurf <> nil, true); |
4 | 117 |
SDL_UpperBlit(tmpsurf, nil, Surface, @Result); |
118 |
SDL_FreeSurface(tmpsurf); |
|
119 |
Result.x:= X; |
|
120 |
Result.y:= Y; |
|
202 | 121 |
Result.w:= w + FontBorder * 2 + 4; |
351 | 122 |
Result.h:= h + FontBorder * 2; |
123 |
WriteInRoundRect:= Result |
|
4 | 124 |
end; |
125 |
||
126 |
procedure StoreLoad; |
|
844 | 127 |
var s: string; |
4 | 128 |
|
1185 | 129 |
procedure WriteNames(Font: THWFont); |
130 |
var t: LongInt; |
|
131 |
i: LongInt; |
|
132 |
r, rr: TSDL_Rect; |
|
133 |
drY: LongInt; |
|
134 |
texsurf: PSDL_Surface; |
|
135 |
begin |
|
136 |
r.x:= 0; |
|
137 |
r.y:= 0; |
|
138 |
drY:= - 4; |
|
139 |
for t:= 0 to Pred(TeamsCount) do |
|
140 |
with TeamsArray[t]^ do |
|
141 |
begin |
|
142 |
NameTagTex:= RenderStringTex(TeamName, Clan^.Color, Font); |
|
690 | 143 |
|
1185 | 144 |
r.w:= cTeamHealthWidth + 5; |
145 |
r.h:= NameTagTex^.h; |
|
690 | 146 |
|
1185 | 147 |
texsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 32, RMask, GMask, BMask, AMask); |
148 |
TryDo(texsurf <> nil, errmsgCreateSurface, true); |
|
149 |
TryDo(SDL_SetColorKey(texsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
|
690 | 150 |
|
1185 | 151 |
DrawRoundRect(@r, cWhiteColor, cColorNearBlack, texsurf, true); |
152 |
rr:= r; |
|
153 |
inc(rr.x, 2); dec(rr.w, 4); inc(rr.y, 2); dec(rr.h, 4); |
|
154 |
DrawRoundRect(@rr, Clan^.Color, Clan^.Color, texsurf, false); |
|
155 |
HealthTex:= Surface2Tex(texsurf); |
|
156 |
SDL_FreeSurface(texsurf); |
|
690 | 157 |
|
1185 | 158 |
dec(drY, r.h + 2); |
159 |
DrawHealthY:= drY; |
|
160 |
for i:= 0 to 7 do |
|
161 |
with Hedgehogs[i] do |
|
162 |
if Gear <> nil then |
|
1242 | 163 |
begin |
1185 | 164 |
NameTagTex:= RenderStringTex(Name, Clan^.Color, fnt16); |
1294
50198e5c7f02
- Hedgehog doesn't take off hat when shooting from shotgun
unc0rr
parents:
1251
diff
changeset
|
165 |
if Hat <> 'NoHat' then |
50198e5c7f02
- Hedgehog doesn't take off hat when shooting from shotgun
unc0rr
parents:
1251
diff
changeset
|
166 |
begin |
1698 | 167 |
texsurf:= LoadImage(Pathz[ptHats] + '/' + Hat, false, false, false); |
168 |
if texsurf <> nil then |
|
169 |
begin |
|
170 |
HatTex:= Surface2Tex(texsurf); |
|
171 |
SDL_FreeSurface(texsurf) |
|
172 |
end |
|
1294
50198e5c7f02
- Hedgehog doesn't take off hat when shooting from shotgun
unc0rr
parents:
1251
diff
changeset
|
173 |
end |
1242 | 174 |
end; |
1185 | 175 |
end; |
176 |
end; |
|
4 | 177 |
|
1185 | 178 |
procedure MakeCrossHairs; |
179 |
var t: LongInt; |
|
180 |
tmpsurf, texsurf: PSDL_Surface; |
|
181 |
Color, i: Longword; |
|
182 |
begin |
|
183 |
s:= Pathz[ptGraphics] + '/' + cCHFileName; |
|
184 |
tmpsurf:= LoadImage(s, true, true, false); |
|
4 | 185 |
|
1185 | 186 |
for t:= 0 to Pred(TeamsCount) do |
187 |
with TeamsArray[t]^ do |
|
188 |
begin |
|
189 |
texsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, tmpsurf^.w, tmpsurf^.h, 32, RMask, GMask, BMask, AMask); |
|
190 |
TryDo(texsurf <> nil, errmsgCreateSurface, true); |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
191 |
|
1185 | 192 |
Color:= Clan^.Color; |
193 |
Color:= SDL_MapRGB(texsurf^.format, Color shr 16, Color shr 8, Color and $FF); |
|
194 |
SDL_FillRect(texsurf, nil, Color); |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
195 |
|
1185 | 196 |
SDL_UpperBlit(tmpsurf, nil, texsurf, nil); |
777 | 197 |
|
1185 | 198 |
TryDo(tmpsurf^.format^.BytesPerPixel = 4, 'Ooops', true); |
777 | 199 |
|
1185 | 200 |
if SDL_MustLock(texsurf) then |
201 |
SDLTry(SDL_LockSurface(texsurf) >= 0, true); |
|
777 | 202 |
|
1185 | 203 |
// make black pixel be alpha-transparent |
204 |
for i:= 0 to texsurf^.w * texsurf^.h - 1 do |
|
205 |
if PLongwordArray(texsurf^.pixels)^[i] = $FF000000 then PLongwordArray(texsurf^.pixels)^[i]:= 0; |
|
777 | 206 |
|
1185 | 207 |
if SDL_MustLock(texsurf) then |
208 |
SDL_UnlockSurface(texsurf); |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
209 |
|
1185 | 210 |
CrosshairTex:= Surface2Tex(texsurf); |
211 |
SDL_FreeSurface(texsurf) |
|
212 |
end; |
|
351 | 213 |
|
1185 | 214 |
SDL_FreeSurface(tmpsurf) |
215 |
end; |
|
4 | 216 |
|
1185 | 217 |
procedure InitHealth; |
218 |
var i, t: LongInt; |
|
219 |
begin |
|
220 |
for t:= 0 to Pred(TeamsCount) do |
|
221 |
if TeamsArray[t] <> nil then |
|
222 |
with TeamsArray[t]^ do |
|
223 |
begin |
|
224 |
for i:= 0 to cMaxHHIndex do |
|
225 |
if Hedgehogs[i].Gear <> nil then |
|
226 |
RenderHealth(Hedgehogs[i]); |
|
227 |
end |
|
228 |
end; |
|
4 | 229 |
|
1185 | 230 |
procedure LoadGraves; |
231 |
var t: LongInt; |
|
232 |
texsurf: PSDL_Surface; |
|
233 |
begin |
|
234 |
for t:= 0 to Pred(TeamsCount) do |
|
235 |
if TeamsArray[t] <> nil then |
|
236 |
with TeamsArray[t]^ do |
|
237 |
begin |
|
238 |
if GraveName = '' then GraveName:= 'Simple'; |
|
239 |
texsurf:= LoadImage(Pathz[ptGraves] + '/' + GraveName, false, true, true); |
|
240 |
GraveTex:= Surface2Tex(texsurf); |
|
241 |
SDL_FreeSurface(texsurf) |
|
242 |
end |
|
243 |
end; |
|
4 | 244 |
|
844 | 245 |
var ii: TSprite; |
246 |
fi: THWFont; |
|
247 |
ai: TAmmoType; |
|
248 |
tmpsurf: PSDL_Surface; |
|
249 |
i: LongInt; |
|
4 | 250 |
begin |
251 |
for fi:= Low(THWFont) to High(THWFont) do |
|
1185 | 252 |
with Fontz[fi] do |
253 |
begin |
|
254 |
s:= Pathz[ptFonts] + '/' + Name; |
|
255 |
WriteToConsole(msgLoading + s + '... '); |
|
256 |
Handle:= TTF_OpenFont(Str2PChar(s), Height); |
|
257 |
SDLTry(Handle <> nil, true); |
|
258 |
TTF_SetFontStyle(Handle, style); |
|
259 |
WriteLnToConsole(msgOK) |
|
260 |
end; |
|
4 | 261 |
AddProgress; |
53 | 262 |
|
4 | 263 |
WriteNames(fnt16); |
70 | 264 |
MakeCrossHairs; |
4 | 265 |
LoadGraves; |
266 |
||
267 |
AddProgress; |
|
268 |
for ii:= Low(TSprite) to High(TSprite) do |
|
1185 | 269 |
with SpritesData[ii] do |
270 |
begin |
|
271 |
if AltPath = ptNone then |
|
272 |
tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, true, true, true) |
|
273 |
else begin |
|
274 |
tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, true, false, true); |
|
275 |
if tmpsurf = nil then |
|
276 |
tmpsurf:= LoadImage(Pathz[AltPath] + '/' + FileName, true, true, true) |
|
277 |
end; |
|
278 |
if Width = 0 then Width:= tmpsurf^.w; |
|
279 |
if Height = 0 then Height:= tmpsurf^.h; |
|
280 |
Texture:= Surface2Tex(tmpsurf); |
|
281 |
if saveSurf then Surface:= tmpsurf else SDL_FreeSurface(tmpsurf) |
|
282 |
end; |
|
80 | 283 |
|
4 | 284 |
AddProgress; |
567 | 285 |
|
761 | 286 |
tmpsurf:= LoadImage(Pathz[ptGraphics] + '/' + cHHFileName, true, true, true); |
287 |
HHTexture:= Surface2Tex(tmpsurf); |
|
288 |
SDL_FreeSurface(tmpsurf); |
|
4 | 289 |
|
290 |
InitHealth; |
|
291 |
||
762 | 292 |
PauseTexture:= RenderStringTex(trmsg[sidPaused], $FFFF00, fntBig); |
1023 | 293 |
ConfirmTexture:= RenderStringTex(trmsg[sidConfirm], $FFFF00, fntBig); |
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
208
diff
changeset
|
294 |
|
843 | 295 |
for ai:= Low(TAmmoType) to High(TAmmoType) do |
296 |
with Ammoz[ai] do |
|
297 |
begin |
|
298 |
tmpsurf:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(trAmmo[NameId]), $FFFFFF); |
|
299 |
NameTex:= Surface2Tex(tmpsurf); |
|
300 |
SDL_FreeSurface(tmpsurf) |
|
301 |
end; |
|
302 |
||
844 | 303 |
for i:= Low(CountTexz) to High(CountTexz) do |
304 |
begin |
|
305 |
tmpsurf:= TTF_RenderUTF8_Blended(Fontz[fnt16].Handle, Str2PChar(IntToStr(i) + 'x'), $FFFFFF); |
|
306 |
CountTexz[i]:= Surface2Tex(tmpsurf); |
|
307 |
SDL_FreeSurface(tmpsurf) |
|
308 |
end; |
|
309 |
||
4 | 310 |
{$IFDEF DUMP} |
311 |
SDL_SaveBMP_RW(LandSurface, SDL_RWFromFile('LandSurface.bmp', 'wb'), 1); |
|
312 |
SDL_SaveBMP_RW(StoreSurface, SDL_RWFromFile('StoreSurface.bmp', 'wb'), 1); |
|
313 |
{$ENDIF} |
|
314 |
end; |
|
315 |
||
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
316 |
procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
4 | 317 |
var rr: TSDL_Rect; |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
318 |
_l, _r, _t, _b: real; |
1916 | 319 |
VertexBuffer, TextureBuffer: array [0..3] of TVertex2f; |
4 | 320 |
begin |
759 | 321 |
if SourceTexture^.h = 0 then exit; |
4 | 322 |
rr.x:= X; |
323 |
rr.y:= Y; |
|
351 | 324 |
rr.w:= r^.w; |
325 |
rr.h:= r^.h; |
|
755 | 326 |
|
1896 | 327 |
_l:= r^.x / SourceTexture^.w * SourceTexture^.rx; |
328 |
_r:= (r^.x + r^.w) / SourceTexture^.w * SourceTexture^.rx; |
|
329 |
_t:= r^.y / SourceTexture^.h * SourceTexture^.ry; |
|
330 |
_b:= (r^.y + r^.h) / SourceTexture^.h * SourceTexture^.ry; |
|
755 | 331 |
|
332 |
glBindTexture(GL_TEXTURE_2D, SourceTexture^.id); |
|
333 |
||
1916 | 334 |
VertexBuffer[0].X:= X; |
335 |
VertexBuffer[0].Y:= Y; |
|
336 |
VertexBuffer[1].X:= rr.w + X; |
|
337 |
VertexBuffer[1].Y:= Y; |
|
338 |
VertexBuffer[2].X:= rr.w + X; |
|
339 |
VertexBuffer[2].Y:= rr.h + Y; |
|
340 |
VertexBuffer[3].X:= X; |
|
341 |
VertexBuffer[3].Y:= rr.h + Y; |
|
755 | 342 |
|
1916 | 343 |
TextureBuffer[0].X:= _l; |
344 |
TextureBuffer[0].Y:= _t; |
|
345 |
TextureBuffer[1].X:= _r; |
|
346 |
TextureBuffer[1].Y:= _t; |
|
347 |
TextureBuffer[2].X:= _r; |
|
348 |
TextureBuffer[2].Y:= _b; |
|
349 |
TextureBuffer[3].X:= _l; |
|
350 |
TextureBuffer[3].Y:= _b; |
|
755 | 351 |
|
1916 | 352 |
glEnableClientState(GL_VERTEX_ARRAY); |
353 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|
755 | 354 |
|
1916 | 355 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
356 |
glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]); |
|
357 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
|
358 |
||
359 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
|
360 |
glDisableClientState(GL_VERTEX_ARRAY) |
|
762 | 361 |
end; |
362 |
||
363 |
procedure DrawTexture(X, Y: LongInt; Texture: PTexture); |
|
364 |
begin |
|
1904 | 365 |
glPushMatrix; |
366 |
glTranslatef(X, Y, 0); |
|
367 |
||
762 | 368 |
glBindTexture(GL_TEXTURE_2D, Texture^.id); |
369 |
||
1904 | 370 |
glEnableClientState(GL_VERTEX_ARRAY); |
371 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|
762 | 372 |
|
1912
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1908
diff
changeset
|
373 |
glVertexPointer(2, GL_FLOAT, 0, @Texture^.vb); |
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1908
diff
changeset
|
374 |
glTexCoordPointer(2, GL_FLOAT, 0, @Texture^.tb); |
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1908
diff
changeset
|
375 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(Texture^.vb)); |
1904 | 376 |
|
377 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
|
378 |
glDisableClientState(GL_VERTEX_ARRAY); |
|
379 |
||
380 |
glPopMatrix |
|
4 | 381 |
end; |
382 |
||
1251 | 383 |
procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, Frames: LongInt); |
1242 | 384 |
var ft, fb: GLfloat; |
385 |
hw: LongInt; |
|
1916 | 386 |
VertexBuffer, TextureBuffer: array [0..3] of TVertex2f; |
1242 | 387 |
begin |
388 |
glPushMatrix; |
|
389 |
glTranslatef(X, Y, 0); |
|
1251 | 390 |
glScalef(Scale, Scale, 1.0); |
1242 | 391 |
|
392 |
if Dir < 0 then |
|
393 |
hw:= - 16 |
|
394 |
else |
|
395 |
hw:= 16; |
|
396 |
||
1896 | 397 |
ft:= Frame / Frames * Texture^.ry; |
398 |
fb:= (Frame + 1) / Frames * Texture^.ry; |
|
1242 | 399 |
|
400 |
glBindTexture(GL_TEXTURE_2D, Texture^.id); |
|
401 |
||
1916 | 402 |
VertexBuffer[0].X:= -hw; |
403 |
VertexBuffer[0].Y:= -16; |
|
404 |
VertexBuffer[1].X:= hw; |
|
405 |
VertexBuffer[1].Y:= -16; |
|
406 |
VertexBuffer[2].X:= hw; |
|
407 |
VertexBuffer[2].Y:= 16; |
|
408 |
VertexBuffer[3].X:= -hw; |
|
409 |
VertexBuffer[3].Y:= 16; |
|
1242 | 410 |
|
1916 | 411 |
TextureBuffer[0].X:= 0; |
412 |
TextureBuffer[0].Y:= ft; |
|
413 |
TextureBuffer[1].X:= Texture^.rx; |
|
414 |
TextureBuffer[1].Y:= ft; |
|
415 |
TextureBuffer[2].X:= Texture^.rx; |
|
416 |
TextureBuffer[2].Y:= fb; |
|
417 |
TextureBuffer[3].X:= 0; |
|
418 |
TextureBuffer[3].Y:= fb; |
|
1242 | 419 |
|
1916 | 420 |
glEnableClientState(GL_VERTEX_ARRAY); |
421 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|
1242 | 422 |
|
1916 | 423 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
424 |
glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]); |
|
425 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
|
426 |
||
427 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
|
428 |
glDisableClientState(GL_VERTEX_ARRAY); |
|
429 |
||
1242 | 430 |
|
431 |
glPopMatrix |
|
432 |
end; |
|
433 |
||
822 | 434 |
procedure DrawRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real); |
775 | 435 |
begin |
777 | 436 |
DrawRotatedTex(SpritesData[Sprite].Texture, |
822 | 437 |
SpritesData[Sprite].Width, |
438 |
SpritesData[Sprite].Height, |
|
439 |
X, Y, Dir, Angle) |
|
777 | 440 |
end; |
441 |
||
853 | 442 |
procedure DrawRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real); |
806 | 443 |
begin |
444 |
glPushMatrix; |
|
809 | 445 |
glTranslatef(X, Y, 0); |
806 | 446 |
glRotatef(Angle, 0, 0, 1); |
447 |
||
853 | 448 |
if Dir < 0 then glScalef(-1.0, 1.0, 1.0); |
449 |
||
450 |
DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame); |
|
806 | 451 |
|
452 |
glPopMatrix |
|
453 |
end; |
|
454 |
||
822 | 455 |
procedure DrawRotatedTex(Tex: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real); |
1916 | 456 |
var VertexBuffer: array [0..3] of TVertex2f; |
777 | 457 |
begin |
775 | 458 |
glPushMatrix; |
459 |
glTranslatef(X, Y, 0); |
|
822 | 460 |
|
461 |
if Dir < 0 then |
|
462 |
begin |
|
463 |
hw:= - hw; |
|
464 |
glRotatef(Angle, 0, 0, -1); |
|
465 |
end else |
|
466 |
glRotatef(Angle, 0, 0, 1); |
|
467 |
||
775 | 468 |
|
777 | 469 |
glBindTexture(GL_TEXTURE_2D, Tex^.id); |
775 | 470 |
|
1916 | 471 |
VertexBuffer[0].X:= -hw; |
472 |
VertexBuffer[0].Y:= -hh; |
|
473 |
VertexBuffer[1].X:= hw; |
|
474 |
VertexBuffer[1].Y:= -hh; |
|
475 |
VertexBuffer[2].X:= hw; |
|
476 |
VertexBuffer[2].Y:= hh; |
|
477 |
VertexBuffer[3].X:= -hw; |
|
478 |
VertexBuffer[3].Y:= hh; |
|
775 | 479 |
|
1916 | 480 |
glEnableClientState(GL_VERTEX_ARRAY); |
481 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|
775 | 482 |
|
1916 | 483 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
484 |
glTexCoordPointer(2, GL_FLOAT, 0, @Tex^.tb); |
|
485 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
|
775 | 486 |
|
1916 | 487 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
488 |
glDisableClientState(GL_VERTEX_ARRAY); |
|
775 | 489 |
|
490 |
glPopMatrix |
|
491 |
end; |
|
492 |
||
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
493 |
procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt); |
4 | 494 |
begin |
495 |
r.y:= r.y + Height * Position; |
|
496 |
r.h:= Height; |
|
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
497 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture) |
4 | 498 |
end; |
499 |
||
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
500 |
procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt); |
755 | 501 |
var r: TSDL_Rect; |
4 | 502 |
begin |
755 | 503 |
r.x:= 0; |
504 |
r.w:= SpritesData[Sprite].Width; |
|
505 |
r.y:= Frame * SpritesData[Sprite].Height; |
|
506 |
r.h:= SpritesData[Sprite].Height; |
|
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
507 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture) |
4 | 508 |
end; |
509 |
||
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
510 |
procedure DrawSprite2(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt); |
43 | 511 |
var r: TSDL_Rect; |
512 |
begin |
|
513 |
r.x:= FrameX * SpritesData[Sprite].Width; |
|
514 |
r.w:= SpritesData[Sprite].Width; |
|
515 |
r.y:= FrameY * SpritesData[Sprite].Height; |
|
516 |
r.h:= SpritesData[Sprite].Height; |
|
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
517 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture) |
43 | 518 |
end; |
519 |
||
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
520 |
procedure DrawSurfSprite(X, Y, Height, Frame: LongInt; Source: PTexture); |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
521 |
var r: TSDL_Rect; |
198 | 522 |
begin |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
523 |
r.x:= 0; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
524 |
r.w:= Source^.w; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
525 |
r.y:= Frame * Height; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
526 |
r.h:= Height; |
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
527 |
DrawFromRect(X, Y, @r, Source) |
198 | 528 |
end; |
529 |
||
762 | 530 |
procedure DrawCentered(X, Top: LongInt; Source: PTexture); |
95 | 531 |
begin |
762 | 532 |
DrawTexture(X - Source^.w div 2, Top, Source) |
4 | 533 |
end; |
534 |
||
1865
ebc6dfca60d4
- nemo's patch: some animations, zero probability for infinite weapons
unc0rr
parents:
1854
diff
changeset
|
535 |
procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real); |
1916 | 536 |
const VertexBuffer: array [0..3] of TVertex2f = ( |
537 |
(x: -16; y: -16), |
|
538 |
(x: 16; y: -16), |
|
539 |
(x: 16; y: 16), |
|
540 |
(x: -16; y: 16)); |
|
761 | 541 |
var l, r, t, b: real; |
1916 | 542 |
TextureBuffer: array [0..3] of TVertex2f; |
4 | 543 |
begin |
761 | 544 |
|
545 |
t:= Pos * 32 / HHTexture^.h; |
|
546 |
b:= (Pos + 1) * 32 / HHTexture^.h; |
|
547 |
||
548 |
if Dir = -1 then |
|
549 |
begin |
|
550 |
l:= (Step + 1) * 32 / HHTexture^.w; |
|
551 |
r:= Step * 32 / HHTexture^.w |
|
552 |
end else |
|
553 |
begin |
|
554 |
l:= Step * 32 / HHTexture^.w; |
|
555 |
r:= (Step + 1) * 32 / HHTexture^.w |
|
556 |
end; |
|
557 |
||
821
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
558 |
|
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
559 |
glPushMatrix(); |
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
560 |
glTranslatef(X, Y, 0); |
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
561 |
glRotatef(Angle, 0, 0, 1); |
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
562 |
|
761 | 563 |
glBindTexture(GL_TEXTURE_2D, HHTexture^.id); |
564 |
||
1916 | 565 |
TextureBuffer[0].X:= l; |
566 |
TextureBuffer[0].Y:= t; |
|
567 |
TextureBuffer[1].X:= r; |
|
568 |
TextureBuffer[1].Y:= t; |
|
569 |
TextureBuffer[2].X:= r; |
|
570 |
TextureBuffer[2].Y:= b; |
|
571 |
TextureBuffer[3].X:= l; |
|
572 |
TextureBuffer[3].Y:= b; |
|
761 | 573 |
|
1916 | 574 |
glEnableClientState(GL_VERTEX_ARRAY); |
575 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|
761 | 576 |
|
1916 | 577 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
578 |
glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]); |
|
579 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
|
761 | 580 |
|
1916 | 581 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
582 |
glDisableClientState(GL_VERTEX_ARRAY); |
|
761 | 583 |
|
1854 | 584 |
|
585 |
glColor4f(1,1,1,1); |
|
821
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
586 |
|
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
587 |
glPopMatrix |
4 | 588 |
end; |
589 |
||
1431 | 590 |
procedure DrawFillRect(r: TSDL_Rect); |
1916 | 591 |
var VertexBuffer: array [0..3] of TVertex2f; |
1431 | 592 |
begin |
593 |
glDisable(GL_TEXTURE_2D); |
|
594 |
||
595 |
glColor4ub(0, 0, 0, 127); |
|
596 |
||
1916 | 597 |
VertexBuffer[0].X:= r.x; |
598 |
VertexBuffer[0].Y:= r.y; |
|
599 |
VertexBuffer[1].X:= r.x + r.w; |
|
600 |
VertexBuffer[1].Y:= r.y; |
|
601 |
VertexBuffer[2].X:= r.x + r.w; |
|
602 |
VertexBuffer[2].Y:= r.y + r.h; |
|
603 |
VertexBuffer[3].X:= r.x; |
|
604 |
VertexBuffer[3].Y:= r.y + r.h; |
|
1431 | 605 |
|
1916 | 606 |
glEnableClientState(GL_VERTEX_ARRAY); |
607 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
|
608 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
|
609 |
glDisableClientState(GL_VERTEX_ARRAY); |
|
1431 | 610 |
|
611 |
glColor4f(1, 1, 1, 1); |
|
612 |
glEnable(GL_TEXTURE_2D) |
|
613 |
end; |
|
614 |
||
4 | 615 |
procedure StoreRelease; |
616 |
var ii: TSprite; |
|
617 |
begin |
|
618 |
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
|
619 |
begin |
759 | 620 |
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
|
621 |
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
|
622 |
end; |
761 | 623 |
|
1806 | 624 |
FreeTexture(HHTexture) |
4 | 625 |
end; |
626 |
||
762 | 627 |
function RenderStringTex(s: string; Color: Longword; font: THWFont): PTexture; |
432 | 628 |
var w, h: LongInt; |
351 | 629 |
Result: PSDL_Surface; |
95 | 630 |
begin |
355 | 631 |
TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), w, h); |
785 | 632 |
|
762 | 633 |
Result:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + FontBorder * 2 + 4, h + FontBorder * 2, |
634 |
32, RMask, GMask, BMask, AMask); |
|
785 | 635 |
|
107 | 636 |
TryDo(Result <> nil, 'RenderString: fail to create surface', true); |
785 | 637 |
|
95 | 638 |
WriteInRoundRect(Result, 0, 0, Color, font, s); |
785 | 639 |
|
762 | 640 |
TryDo(SDL_SetColorKey(Result, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
785 | 641 |
|
762 | 642 |
RenderStringTex:= Surface2Tex(Result); |
785 | 643 |
|
762 | 644 |
SDL_FreeSurface(Result) |
95 | 645 |
end; |
646 |
||
4 | 647 |
procedure RenderHealth(var Hedgehog: THedgehog); |
95 | 648 |
var s: shortstring; |
4 | 649 |
begin |
351 | 650 |
str(Hedgehog.Gear^.Health, s); |
762 | 651 |
if Hedgehog.HealthTagTex <> nil then FreeTexture(Hedgehog.HealthTagTex); |
652 |
Hedgehog.HealthTagTex:= RenderStringTex(s, Hedgehog.Team^.Clan^.Color, fnt16) |
|
4 | 653 |
end; |
654 |
||
518 | 655 |
function LoadImage(const filename: string; hasAlpha: boolean; critical, setTransparent: boolean): PSDL_Surface; |
30 | 656 |
var tmpsurf: PSDL_Surface; |
753 | 657 |
//Result: PSDL_Surface; |
355 | 658 |
s: shortstring; |
4 | 659 |
begin |
660 |
WriteToConsole(msgLoading + filename + '... '); |
|
355 | 661 |
s:= filename + '.' + cBitsStr + '.png'; |
662 |
tmpsurf:= IMG_Load(Str2PChar(s)); |
|
351 | 663 |
|
74 | 664 |
if tmpsurf = nil then |
351 | 665 |
begin |
355 | 666 |
s:= filename + '.png'; |
667 |
tmpsurf:= IMG_Load(Str2PChar(s)); |
|
351 | 668 |
end; |
80 | 669 |
|
670 |
if tmpsurf = nil then |
|
671 |
if critical then OutError(msgFailed, true) |
|
672 |
else begin |
|
673 |
WriteLnToConsole(msgFailed); |
|
351 | 674 |
exit(nil) |
80 | 675 |
end; |
351 | 676 |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
677 |
if setTransparent then TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
753 | 678 |
//if hasAlpha then Result:= SDL_DisplayFormatAlpha(tmpsurf) |
679 |
// else Result:= SDL_DisplayFormat(tmpsurf); |
|
620 | 680 |
{$IFDEF DEBUGFILE}WriteLnToConsole('(' + inttostr(tmpsurf^.w) + ',' + inttostr(tmpsurf^.h) + ') ');{$ENDIF} |
351 | 681 |
WriteLnToConsole(msgOK); |
753 | 682 |
LoadImage:= tmpsurf//Result |
683 |
end; |
|
684 |
||
685 |
procedure SetupOpenGL; |
|
686 |
begin |
|
756 | 687 |
glLoadIdentity; |
753 | 688 |
glViewport(0, 0, cScreenWidth, cScreenHeight); |
754 | 689 |
glScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0); |
690 |
glTranslatef(-cScreenWidth / 2, -cScreenHeight / 2, 0); |
|
756 | 691 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
753 | 692 |
glMatrixMode(GL_MODELVIEW) |
4 | 693 |
end; |
694 |
||
510 | 695 |
//////////////////////////////////////////////////////////////////////////////// |
766 | 696 |
var ProgrTex: PTexture = nil; |
534 | 697 |
Step: integer = 0; |
510 | 698 |
|
699 |
procedure AddProgress; |
|
700 |
var r: TSDL_Rect; |
|
766 | 701 |
texsurf: PSDL_Surface; |
510 | 702 |
begin |
703 |
if Step = 0 then |
|
704 |
begin |
|
705 |
WriteToConsole(msgLoading + 'progress sprite: '); |
|
766 | 706 |
texsurf:= LoadImage(Pathz[ptGraphics] + '/Progress', false, true, true); |
707 |
ProgrTex:= Surface2Tex(texsurf); |
|
708 |
SDL_FreeSurface(texsurf) |
|
510 | 709 |
end; |
1045 | 710 |
|
766 | 711 |
glClear(GL_COLOR_BUFFER_BIT); |
775 | 712 |
glEnable(GL_TEXTURE_2D); |
510 | 713 |
r.x:= 0; |
766 | 714 |
r.w:= ProgrTex^.w; |
715 |
r.h:= ProgrTex^.w; |
|
716 |
r.y:= (Step mod (ProgrTex^.h div ProgrTex^.w)) * ProgrTex^.w; |
|
717 |
DrawFromRect((cScreenWidth - ProgrTex^.w) div 2, |
|
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
718 |
(cScreenHeight - ProgrTex^.w) div 2, @r, ProgrTex); |
775 | 719 |
glDisable(GL_TEXTURE_2D); |
766 | 720 |
SDL_GL_SwapBuffers(); |
510 | 721 |
inc(Step); |
722 |
end; |
|
723 |
||
724 |
procedure FinishProgress; |
|
725 |
begin |
|
726 |
WriteLnToConsole('Freeing progress surface... '); |
|
766 | 727 |
FreeTexture(ProgrTex) |
510 | 728 |
end; |
729 |
||
4 | 730 |
end. |