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