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