author | unc0rr |
Sun, 22 Mar 2009 17:41:07 +0000 | |
changeset 1909 | 30fa1608b54f |
parent 1908 | 5be17e24751a |
child 1912 | c3d31fb59f0e |
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; |
4 | 319 |
begin |
759 | 320 |
if SourceTexture^.h = 0 then exit; |
4 | 321 |
rr.x:= X; |
322 |
rr.y:= Y; |
|
351 | 323 |
rr.w:= r^.w; |
324 |
rr.h:= r^.h; |
|
755 | 325 |
|
1896 | 326 |
_l:= r^.x / SourceTexture^.w * SourceTexture^.rx; |
327 |
_r:= (r^.x + r^.w) / SourceTexture^.w * SourceTexture^.rx; |
|
328 |
_t:= r^.y / SourceTexture^.h * SourceTexture^.ry; |
|
329 |
_b:= (r^.y + r^.h) / SourceTexture^.h * SourceTexture^.ry; |
|
755 | 330 |
|
331 |
glBindTexture(GL_TEXTURE_2D, SourceTexture^.id); |
|
332 |
||
333 |
glBegin(GL_QUADS); |
|
334 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
335 |
glTexCoord2f(_l, _t); |
755 | 336 |
glVertex2i(X, Y); |
337 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
338 |
glTexCoord2f(_r, _t); |
755 | 339 |
glVertex2i(rr.w + X, Y); |
340 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
341 |
glTexCoord2f(_r, _b); |
755 | 342 |
glVertex2i(rr.w + X, rr.h + Y); |
343 |
||
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
344 |
glTexCoord2f(_l, _b); |
755 | 345 |
glVertex2i(X, rr.h + Y); |
346 |
||
762 | 347 |
glEnd() |
348 |
end; |
|
349 |
||
350 |
procedure DrawTexture(X, Y: LongInt; Texture: PTexture); |
|
1908
5be17e24751a
Use GL_TRIANGLE_FAN instead of GL_TRIANGLES (more efficient)
unc0rr
parents:
1906
diff
changeset
|
351 |
var VertexBuffer, TextureBuffer: array [0..3] of TVertex2f; |
762 | 352 |
begin |
1904 | 353 |
glPushMatrix; |
354 |
glTranslatef(X, Y, 0); |
|
355 |
||
762 | 356 |
glBindTexture(GL_TEXTURE_2D, Texture^.id); |
357 |
||
1904 | 358 |
VertexBuffer[0].X:= 0; |
359 |
VertexBuffer[0].Y:= 0; |
|
360 |
VertexBuffer[1].X:= Texture^.w; |
|
361 |
VertexBuffer[1].Y:= 0; |
|
362 |
VertexBuffer[2].X:= Texture^.w; |
|
363 |
VertexBuffer[2].Y:= Texture^.h; |
|
364 |
VertexBuffer[3].X:= 0; |
|
365 |
VertexBuffer[3].Y:= Texture^.h; |
|
762 | 366 |
|
1904 | 367 |
TextureBuffer[0].X:= 0; |
368 |
TextureBuffer[0].Y:= 0; |
|
369 |
TextureBuffer[1].X:= Texture^.rx; |
|
370 |
TextureBuffer[1].Y:= 0; |
|
371 |
TextureBuffer[2].X:= Texture^.rx; |
|
372 |
TextureBuffer[2].Y:= Texture^.ry; |
|
373 |
TextureBuffer[3].X:= 0; |
|
374 |
TextureBuffer[3].Y:= Texture^.ry; |
|
762 | 375 |
|
1904 | 376 |
glEnableClientState(GL_VERTEX_ARRAY); |
377 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
|
762 | 378 |
|
1904 | 379 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
380 |
glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]); |
|
1908
5be17e24751a
Use GL_TRIANGLE_FAN instead of GL_TRIANGLES (more efficient)
unc0rr
parents:
1906
diff
changeset
|
381 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
1904 | 382 |
|
383 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
|
384 |
glDisableClientState(GL_VERTEX_ARRAY); |
|
385 |
||
386 |
glPopMatrix |
|
4 | 387 |
end; |
388 |
||
1251 | 389 |
procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, Frames: LongInt); |
1242 | 390 |
var ft, fb: GLfloat; |
391 |
hw: LongInt; |
|
392 |
begin |
|
393 |
glPushMatrix; |
|
394 |
glTranslatef(X, Y, 0); |
|
1251 | 395 |
glScalef(Scale, Scale, 1.0); |
1242 | 396 |
|
397 |
if Dir < 0 then |
|
398 |
hw:= - 16 |
|
399 |
else |
|
400 |
hw:= 16; |
|
401 |
||
1896 | 402 |
ft:= Frame / Frames * Texture^.ry; |
403 |
fb:= (Frame + 1) / Frames * Texture^.ry; |
|
1242 | 404 |
|
405 |
glBindTexture(GL_TEXTURE_2D, Texture^.id); |
|
406 |
||
407 |
glBegin(GL_QUADS); |
|
408 |
||
409 |
glTexCoord2f(0, ft); |
|
410 |
glVertex2i(-hw, -16); |
|
411 |
||
1896 | 412 |
glTexCoord2f(Texture^.rx, ft); |
1242 | 413 |
glVertex2i(hw, -16); |
414 |
||
1896 | 415 |
glTexCoord2f(Texture^.rx, fb); |
1242 | 416 |
glVertex2i(hw, 16); |
417 |
||
418 |
glTexCoord2f(0, fb); |
|
419 |
glVertex2i(-hw, 16); |
|
420 |
||
421 |
glEnd(); |
|
422 |
||
423 |
glPopMatrix |
|
424 |
end; |
|
425 |
||
822 | 426 |
procedure DrawRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real); |
775 | 427 |
begin |
777 | 428 |
DrawRotatedTex(SpritesData[Sprite].Texture, |
822 | 429 |
SpritesData[Sprite].Width, |
430 |
SpritesData[Sprite].Height, |
|
431 |
X, Y, Dir, Angle) |
|
777 | 432 |
end; |
433 |
||
853 | 434 |
procedure DrawRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real); |
806 | 435 |
begin |
436 |
glPushMatrix; |
|
809 | 437 |
glTranslatef(X, Y, 0); |
806 | 438 |
glRotatef(Angle, 0, 0, 1); |
439 |
||
853 | 440 |
if Dir < 0 then glScalef(-1.0, 1.0, 1.0); |
441 |
||
442 |
DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame); |
|
806 | 443 |
|
444 |
glPopMatrix |
|
445 |
end; |
|
446 |
||
822 | 447 |
procedure DrawRotatedTex(Tex: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real); |
777 | 448 |
begin |
775 | 449 |
glPushMatrix; |
450 |
glTranslatef(X, Y, 0); |
|
822 | 451 |
|
452 |
if Dir < 0 then |
|
453 |
begin |
|
454 |
hw:= - hw; |
|
455 |
glRotatef(Angle, 0, 0, -1); |
|
456 |
end else |
|
457 |
glRotatef(Angle, 0, 0, 1); |
|
458 |
||
775 | 459 |
|
777 | 460 |
glBindTexture(GL_TEXTURE_2D, Tex^.id); |
775 | 461 |
|
462 |
glBegin(GL_QUADS); |
|
463 |
||
464 |
glTexCoord2f(0, 0); |
|
465 |
glVertex2i(-hw, -hh); |
|
466 |
||
1896 | 467 |
glTexCoord2f(Tex^.rx, 0); |
775 | 468 |
glVertex2i(hw, -hh); |
469 |
||
1896 | 470 |
glTexCoord2f(Tex^.rx, Tex^.ry); |
775 | 471 |
glVertex2i(hw, hh); |
472 |
||
1896 | 473 |
glTexCoord2f(0, Tex^.ry); |
775 | 474 |
glVertex2i(-hw, hh); |
475 |
||
476 |
glEnd(); |
|
477 |
||
478 |
glPopMatrix |
|
479 |
end; |
|
480 |
||
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
481 |
procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt); |
4 | 482 |
begin |
483 |
r.y:= r.y + Height * Position; |
|
484 |
r.h:= Height; |
|
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
485 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture) |
4 | 486 |
end; |
487 |
||
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
488 |
procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt); |
755 | 489 |
var r: TSDL_Rect; |
4 | 490 |
begin |
755 | 491 |
r.x:= 0; |
492 |
r.w:= SpritesData[Sprite].Width; |
|
493 |
r.y:= Frame * SpritesData[Sprite].Height; |
|
494 |
r.h:= SpritesData[Sprite].Height; |
|
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
495 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture) |
4 | 496 |
end; |
497 |
||
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
498 |
procedure DrawSprite2(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt); |
43 | 499 |
var r: TSDL_Rect; |
500 |
begin |
|
501 |
r.x:= FrameX * SpritesData[Sprite].Width; |
|
502 |
r.w:= SpritesData[Sprite].Width; |
|
503 |
r.y:= FrameY * SpritesData[Sprite].Height; |
|
504 |
r.h:= SpritesData[Sprite].Height; |
|
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
505 |
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture) |
43 | 506 |
end; |
507 |
||
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
508 |
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
|
509 |
var r: TSDL_Rect; |
198 | 510 |
begin |
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
511 |
r.x:= 0; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
512 |
r.w:= Source^.w; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
513 |
r.y:= Frame * Height; |
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
514 |
r.h:= Height; |
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
515 |
DrawFromRect(X, Y, @r, Source) |
198 | 516 |
end; |
517 |
||
762 | 518 |
procedure DrawCentered(X, Top: LongInt; Source: PTexture); |
95 | 519 |
begin |
762 | 520 |
DrawTexture(X - Source^.w div 2, Top, Source) |
4 | 521 |
end; |
522 |
||
1865
ebc6dfca60d4
- nemo's patch: some animations, zero probability for infinite weapons
unc0rr
parents:
1854
diff
changeset
|
523 |
procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real); |
761 | 524 |
var l, r, t, b: real; |
4 | 525 |
begin |
761 | 526 |
|
527 |
t:= Pos * 32 / HHTexture^.h; |
|
528 |
b:= (Pos + 1) * 32 / HHTexture^.h; |
|
529 |
||
530 |
if Dir = -1 then |
|
531 |
begin |
|
532 |
l:= (Step + 1) * 32 / HHTexture^.w; |
|
533 |
r:= Step * 32 / HHTexture^.w |
|
534 |
end else |
|
535 |
begin |
|
536 |
l:= Step * 32 / HHTexture^.w; |
|
537 |
r:= (Step + 1) * 32 / HHTexture^.w |
|
538 |
end; |
|
539 |
||
821
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
540 |
|
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
541 |
glPushMatrix(); |
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
542 |
glTranslatef(X, Y, 0); |
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
543 |
glRotatef(Angle, 0, 0, 1); |
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
544 |
|
761 | 545 |
glBindTexture(GL_TEXTURE_2D, HHTexture^.id); |
546 |
||
547 |
glBegin(GL_QUADS); |
|
548 |
||
549 |
glTexCoord2f(l, t); |
|
821
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
550 |
glVertex2i(-16, -16); |
761 | 551 |
|
552 |
glTexCoord2f(r, t); |
|
821
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
553 |
glVertex2i(16, -16); |
761 | 554 |
|
555 |
glTexCoord2f(r, b); |
|
821
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
556 |
glVertex2i(16, 16); |
761 | 557 |
|
558 |
glTexCoord2f(l, b); |
|
821
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
559 |
glVertex2i(-16, 16); |
761 | 560 |
|
1854 | 561 |
|
821
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
562 |
glEnd(); |
1854 | 563 |
glColor4f(1,1,1,1); |
821
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
564 |
|
e6c0408b54ed
Use 'regular standing' and 'rope swing' hedgehog sprites
unc0rr
parents:
809
diff
changeset
|
565 |
glPopMatrix |
4 | 566 |
end; |
567 |
||
1431 | 568 |
procedure DrawFillRect(r: TSDL_Rect); |
569 |
begin |
|
570 |
glDisable(GL_TEXTURE_2D); |
|
571 |
||
572 |
glColor4ub(0, 0, 0, 127); |
|
573 |
||
574 |
glBegin(GL_QUADS); |
|
575 |
||
576 |
glVertex2i(r.x , r.y ); |
|
577 |
glVertex2i(r.x + r.w , r.y ); |
|
578 |
glVertex2i(r.x + r.w , r.y + r.h); |
|
579 |
glVertex2i(r.x , r.y + r.h); |
|
580 |
||
581 |
glEnd(); |
|
582 |
||
583 |
glColor4f(1, 1, 1, 1); |
|
584 |
glEnable(GL_TEXTURE_2D) |
|
585 |
end; |
|
586 |
||
4 | 587 |
procedure StoreRelease; |
588 |
var ii: TSprite; |
|
589 |
begin |
|
590 |
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
|
591 |
begin |
759 | 592 |
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
|
593 |
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
|
594 |
end; |
761 | 595 |
|
1806 | 596 |
FreeTexture(HHTexture) |
4 | 597 |
end; |
598 |
||
762 | 599 |
function RenderStringTex(s: string; Color: Longword; font: THWFont): PTexture; |
432 | 600 |
var w, h: LongInt; |
351 | 601 |
Result: PSDL_Surface; |
95 | 602 |
begin |
355 | 603 |
TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), w, h); |
785 | 604 |
|
762 | 605 |
Result:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + FontBorder * 2 + 4, h + FontBorder * 2, |
606 |
32, RMask, GMask, BMask, AMask); |
|
785 | 607 |
|
107 | 608 |
TryDo(Result <> nil, 'RenderString: fail to create surface', true); |
785 | 609 |
|
95 | 610 |
WriteInRoundRect(Result, 0, 0, Color, font, s); |
785 | 611 |
|
762 | 612 |
TryDo(SDL_SetColorKey(Result, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
785 | 613 |
|
762 | 614 |
RenderStringTex:= Surface2Tex(Result); |
785 | 615 |
|
762 | 616 |
SDL_FreeSurface(Result) |
95 | 617 |
end; |
618 |
||
4 | 619 |
procedure RenderHealth(var Hedgehog: THedgehog); |
95 | 620 |
var s: shortstring; |
4 | 621 |
begin |
351 | 622 |
str(Hedgehog.Gear^.Health, s); |
762 | 623 |
if Hedgehog.HealthTagTex <> nil then FreeTexture(Hedgehog.HealthTagTex); |
624 |
Hedgehog.HealthTagTex:= RenderStringTex(s, Hedgehog.Team^.Clan^.Color, fnt16) |
|
4 | 625 |
end; |
626 |
||
518 | 627 |
function LoadImage(const filename: string; hasAlpha: boolean; critical, setTransparent: boolean): PSDL_Surface; |
30 | 628 |
var tmpsurf: PSDL_Surface; |
753 | 629 |
//Result: PSDL_Surface; |
355 | 630 |
s: shortstring; |
4 | 631 |
begin |
632 |
WriteToConsole(msgLoading + filename + '... '); |
|
355 | 633 |
s:= filename + '.' + cBitsStr + '.png'; |
634 |
tmpsurf:= IMG_Load(Str2PChar(s)); |
|
351 | 635 |
|
74 | 636 |
if tmpsurf = nil then |
351 | 637 |
begin |
355 | 638 |
s:= filename + '.png'; |
639 |
tmpsurf:= IMG_Load(Str2PChar(s)); |
|
351 | 640 |
end; |
80 | 641 |
|
642 |
if tmpsurf = nil then |
|
643 |
if critical then OutError(msgFailed, true) |
|
644 |
else begin |
|
645 |
WriteLnToConsole(msgFailed); |
|
351 | 646 |
exit(nil) |
80 | 647 |
end; |
351 | 648 |
|
764
7513452b1d51
Now game looks almost like it did before switching to OpenGL
unc0rr
parents:
762
diff
changeset
|
649 |
if setTransparent then TryDo(SDL_SetColorKey(tmpsurf, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
753 | 650 |
//if hasAlpha then Result:= SDL_DisplayFormatAlpha(tmpsurf) |
651 |
// else Result:= SDL_DisplayFormat(tmpsurf); |
|
620 | 652 |
{$IFDEF DEBUGFILE}WriteLnToConsole('(' + inttostr(tmpsurf^.w) + ',' + inttostr(tmpsurf^.h) + ') ');{$ENDIF} |
351 | 653 |
WriteLnToConsole(msgOK); |
753 | 654 |
LoadImage:= tmpsurf//Result |
655 |
end; |
|
656 |
||
657 |
procedure SetupOpenGL; |
|
658 |
begin |
|
756 | 659 |
glLoadIdentity; |
753 | 660 |
glViewport(0, 0, cScreenWidth, cScreenHeight); |
754 | 661 |
glScalef(2.0 / cScreenWidth, -2.0 / cScreenHeight, 1.0); |
662 |
glTranslatef(-cScreenWidth / 2, -cScreenHeight / 2, 0); |
|
756 | 663 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
753 | 664 |
glMatrixMode(GL_MODELVIEW) |
4 | 665 |
end; |
666 |
||
510 | 667 |
//////////////////////////////////////////////////////////////////////////////// |
766 | 668 |
var ProgrTex: PTexture = nil; |
534 | 669 |
Step: integer = 0; |
510 | 670 |
|
671 |
procedure AddProgress; |
|
672 |
var r: TSDL_Rect; |
|
766 | 673 |
texsurf: PSDL_Surface; |
510 | 674 |
begin |
675 |
if Step = 0 then |
|
676 |
begin |
|
677 |
WriteToConsole(msgLoading + 'progress sprite: '); |
|
766 | 678 |
texsurf:= LoadImage(Pathz[ptGraphics] + '/Progress', false, true, true); |
679 |
ProgrTex:= Surface2Tex(texsurf); |
|
680 |
SDL_FreeSurface(texsurf) |
|
510 | 681 |
end; |
1045 | 682 |
|
766 | 683 |
glClear(GL_COLOR_BUFFER_BIT); |
775 | 684 |
glEnable(GL_TEXTURE_2D); |
510 | 685 |
r.x:= 0; |
766 | 686 |
r.w:= ProgrTex^.w; |
687 |
r.h:= ProgrTex^.w; |
|
688 |
r.y:= (Step mod (ProgrTex^.h div ProgrTex^.w)) * ProgrTex^.w; |
|
689 |
DrawFromRect((cScreenWidth - ProgrTex^.w) div 2, |
|
841
0700e3d3474d
Get rid if deprecated Surface parameter of Draw* calls
unc0rr
parents:
840
diff
changeset
|
690 |
(cScreenHeight - ProgrTex^.w) div 2, @r, ProgrTex); |
775 | 691 |
glDisable(GL_TEXTURE_2D); |
766 | 692 |
SDL_GL_SwapBuffers(); |
510 | 693 |
inc(Step); |
694 |
end; |
|
695 |
||
696 |
procedure FinishProgress; |
|
697 |
begin |
|
698 |
WriteLnToConsole('Freeing progress surface... '); |
|
766 | 699 |
FreeTexture(ProgrTex) |
510 | 700 |
end; |
701 |
||
4 | 702 |
end. |