author  unc0rr 
Tue, 28 Jul 2009 12:32:33 +0000  
changeset 2279  ef19b949ad30 
parent 2272  c59656d7b1de 
child 2290  bf87ca44782e 
permissions  rwrr 
4  1 
(* 
1066  2 
* Hedgewars, a free turn based strategy game 
883  3 
* Copyright (c) 20042008 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 021111307, USA 

4  17 
*) 
18 

19 
unit uMisc; 

20 
interface 

1906  21 
uses uConsts, SDLh, 
2152  22 
{$IFDEF GLES11} 
1906  23 
gles11, 
24 
{$ELSE} 

2250  25 
GL, 
1906  26 
{$ENDIF} 
27 
uFloat; 

4  28 
{$INCLUDE options.inc} 
1054  29 
var 
30 
isCursorVisible : boolean = false; 

31 
isTerminated : boolean = false; 

32 
isInLag : boolean = false; 

33 
isPaused : boolean = false; 

34 
isSoundEnabled : boolean = true; 

1128  35 
isMusicEnabled : boolean = false; 
1054  36 
isSEBackup : boolean = true; 
37 
isInMultiShoot : boolean = false; 

38 
isSpeed : boolean = false; 

4  39 

1560
e140bc57ff68
Quick replay round to spectators until current move
unc0rr
parents:
1530
diff
changeset

40 
fastUntilLag : boolean = false; 
e140bc57ff68
Quick replay round to spectators until current move
unc0rr
parents:
1530
diff
changeset

41 

1054  42 
GameState : TGameState = Low(TGameState); 
43 
GameType : TGameType = gmtLocal; 

44 
GameFlags : Longword = 0; 

45 
TurnTimeLeft : Longword = 0; 

1784  46 
cSuddenDTurns : LongInt = 15; 
1895  47 
cDamagePercent : LongInt = 100; 
1797  48 
cTemplateFilter : LongInt = 0; 
1888
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1861
diff
changeset

49 

1054  50 
cHedgehogTurnTime: Longword = 45000; 
2031
b6f3e56fb100
david_ac's game scheme options for mine delay/quantity, tweaks to sniper rifle
nemo
parents:
2023
diff
changeset

51 
cMinesTime : LongInt = 3000; 
1054  52 
cMaxAIThinkTime : Longword = 9000; 
4  53 

1054  54 
cCloudsNumber : LongInt = 9; 
55 
cScreenWidth : LongInt = 1024; 

56 
cScreenHeight : LongInt = 768; 

1121
d595dc56b4f3
Remember initial resolution settings to use when switching to fullscreen mode
unc0rr
parents:
1080
diff
changeset

57 
cInitWidth : LongInt = 1024; 
d595dc56b4f3
Remember initial resolution settings to use when switching to fullscreen mode
unc0rr
parents:
1080
diff
changeset

58 
cInitHeight : LongInt = 768; 
2008  59 
cBits : LongInt = 32; 
60 
cBitsStr : string[2] = '32'; 

1054  61 
cTagsMask : byte = 7; 
2162  62 
zoom : GLfloat = 2.0; 
74  63 

1760  64 
cWaterLine : LongInt = LAND_HEIGHT; 
1054  65 
cVisibleWater : LongInt = 128; 
66 
cGearScrEdgesDist: LongInt = 240; 

1530  67 
cCursorEdgesDist : LongInt = 100; 
1054  68 
cTeamHealthWidth : LongInt = 128; 
69 
cAltDamage : boolean = true; 

4  70 

1054  71 
GameTicks : LongWord = 0; 
72 

73 
cSkyColor : Longword = 0; 

74 
cWhiteColor : Longword = $FFFFFFFF; 

75 
cColorNearBlack : Longword = $FF000010; 

76 
cExplosionBorderColor : LongWord = $808080; 

4  77 

2008  78 
cShowFPS : boolean = false; 
1298  79 
cCaseFactor : Longword = 5; {0..9} 
1054  80 
cLandAdditions: Longword = 4; 
1888
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1861
diff
changeset

81 
cFullScreen : boolean = false; 
1812  82 
cReducedQuality : boolean = false; 
1054  83 
cLocaleFName : shortstring = 'en.txt'; 
84 
cSeed : shortstring = ''; 

2210  85 
cInitVolume : LongInt = 50; 
1054  86 
cVolumeDelta : LongInt = 0; 
2008  87 
cTimerInterval : Longword = 8; 
1054  88 
cHasFocus : boolean = true; 
1057  89 
cInactDelay : Longword = 1250; 
4  90 

1054  91 
bBetweenTurns: boolean = false; 
1055
9af540b23409
Water rises after 25 mins of round, health is decreased after 20 mins
unc0rr
parents:
1054
diff
changeset

92 
cHealthDecrease: LongWord = 0; 
9af540b23409
Water rises after 25 mins of round, health is decreased after 20 mins
unc0rr
parents:
1054
diff
changeset

93 
bWaterRising : Boolean = false; 
4  94 

614  95 
{$WARNINGS OFF} 
1124  96 
cAirPlaneSpeed: hwFloat = (isNegative: false; QWordValue: 3006477107); // 1.4 
1054  97 
cBombsSpeed : hwFloat = (isNegative: false; QWordValue: 429496729); 
614  98 
{$WARNINGS ON} 
543
465e2ec8f05f
 Better randomness of placing hedgehogs on the land
unc0rr
parents:
539
diff
changeset

99 

4  100 
var 
1054  101 
cSendEmptyPacketTime : LongWord = 2000; 
102 
cSendCursorPosTime : LongWord = 50; 

103 
ShowCrosshair : boolean; 

104 
cDrownSpeed, 

105 
cMaxWindSpeed, 

106 
cWindSpeed, 

107 
cGravity: hwFloat; 

1849  108 
cDamageModifier: hwFloat; 
1854  109 
cLaserSighting: boolean; 
2017  110 
cVampiric: boolean; 
2023
41d3afaa20c7
Artillery mode, sniper rifle, speech bubble tweaks, fix of rope bug introduced by enabling hats in jump
nemo
parents:
2017
diff
changeset

111 
cArtillery: boolean; 
4  112 

1054  113 
flagMakeCapture: boolean = false; 
4  114 

1054  115 
InitStepsFlags: Longword = 0; 
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
53
diff
changeset

116 

1054  117 
RealTicks: Longword = 0; 
836  118 

1054  119 
AttackBar: LongInt = 0; // 0  none, 1  just bar at the rightdown corner, 2  like in WWP 
4  120 

2171
8208946331ba
Smaxx refactor of LoadImage to use flags, iphone changes by koda (mostly use of rgba instead of rgb)
nemo
parents:
2163
diff
changeset

121 
type HwColor4f = record 
8208946331ba
Smaxx refactor of LoadImage to use flags, iphone changes by koda (mostly use of rgba instead of rgb)
nemo
parents:
2163
diff
changeset

122 
r, g, b, a: byte 
1911  123 
end; 
124 

2272
c59656d7b1de
Add water opacity to theme  defaulting to 50% opaque on all themes but Eyes (80%) and Underwater (100%)
nemo
parents:
2271
diff
changeset

125 
var cWaterOpacity: byte = $80; 
c59656d7b1de
Add water opacity to theme  defaulting to 50% opaque on all themes but Eyes (80%) and Underwater (100%)
nemo
parents:
2271
diff
changeset

126 

2171
8208946331ba
Smaxx refactor of LoadImage to use flags, iphone changes by koda (mostly use of rgba instead of rgb)
nemo
parents:
2163
diff
changeset

127 
var WaterColorArray: array[0..3] of HwColor4f; 
1911  128 

371  129 
function hwSign(r: hwFloat): LongInt; 
802
ed5450a89b96
Start implementing 'visual gears'  gears, that don't need to be synchronized (clouds and flakes)
unc0rr
parents:
788
diff
changeset

130 
function Min(a, b: LongInt): LongInt; 
371  131 
function Max(a, b: LongInt): LongInt; 
351  132 
procedure OutError(Msg: String; isFatalError: boolean); 
4  133 
procedure TryDo(Assert: boolean; Msg: string; isFatal: boolean); 
134 
procedure SDLTry(Assert: boolean; isFatal: boolean); 

316  135 
function IntToStr(n: LongInt): shortstring; 
351  136 
function FloatToStr(n: hwFloat): shortstring; 
775  137 
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat; 
371  138 
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; 
139 
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; 

4  140 
procedure AdjustColor(var Color: Longword); 
141 
{$IFDEF DEBUGFILE} 

142 
procedure AddFileLog(s: shortstring); 

24  143 
function RectToStr(Rect: TSDL_Rect): shortstring; 
4  144 
{$ENDIF} 
208  145 
procedure SetKB(n: Longword); 
146 
procedure SendKB; 

351  147 
procedure SetLittle(var r: hwFloat); 
306  148 
procedure SendStat(sit: TStatInfoType; s: shortstring); 
753  149 
function Str2PChar(const s: shortstring): PChar; 
1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

150 
function NewTexture(width, height: Longword; buf: Pointer): PTexture; 
755  151 
function Surface2Tex(surf: PSDL_Surface): PTexture; 
759  152 
procedure FreeTexture(tex: PTexture); 
945
4ead9cde4e14
 Start chat implementation: chat strings are on the screen
unc0rr
parents:
916
diff
changeset

153 
function toPowerOf2(i: Longword): Longword; 
949  154 
function DecodeBase64(s: shortstring): shortstring; 
2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

155 
{$IFNDEF IPHONEOS} 
1080  156 
procedure MakeScreenshot(s: shortstring); 
2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

157 
{$ENDIF} 
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

158 

1861  159 
function modifyDamage(dmg: Longword): Longword; 
4  160 

161 
var CursorPoint: TPoint; 

162 
TargetPoint: TPoint = (X: NoPointX; Y: 0); 

163 

164 
implementation 

1896  165 
uses uConsole, uStore, uIO, Math, uRandom; 
208  166 
var KBnum: Longword = 0; 
4  167 
{$IFDEF DEBUGFILE} 
168 
var f: textfile; 

169 
{$ENDIF} 

170 

371  171 
function hwSign(r: hwFloat): LongInt; 
4  172 
begin 
2279  173 
if r.isNegative and (r.QWordValue > 0) then hwSign:= 1 else hwSign:= 1 
4  174 
end; 
175 

371  176 
function Min(a, b: LongInt): LongInt; 
4  177 
begin 
351  178 
if a < b then Min:= a else Min:= b 
4  179 
end; 
180 

371  181 
function Max(a, b: LongInt): LongInt; 
4  182 
begin 
351  183 
if a > b then Max:= a else Max:= b 
4  184 
end; 
185 

351  186 
procedure OutError(Msg: String; isFatalError: boolean); 
4  187 
begin 
188 
{$IFDEF DEBUGFILE}AddFileLog(Msg);{$ENDIF} 

53  189 
WriteLnToConsole(Msg); 
4  190 
if isFatalError then 
191 
begin 

53  192 
SendIPC('E' + GetLastConsoleLine); 
4  193 
SDL_Quit; 
194 
halt(1) 

53  195 
end 
4  196 
end; 
197 

198 
procedure TryDo(Assert: boolean; Msg: string; isFatal: boolean); 

199 
begin 

70  200 
if not Assert then OutError(Msg, isFatal) 
4  201 
end; 
202 

203 
procedure SDLTry(Assert: boolean; isFatal: boolean); 

204 
begin 

205 
if not Assert then OutError(SDL_GetError, isFatal) 

206 
end; 

207 

188  208 
procedure AdjustColor(var Color: Longword); 
4  209 
begin 
210 
Color:= SDL_MapRGB(PixelFormat, (Color shr 16) and $FF, (Color shr 8) and $FF, Color and $FF) 

211 
end; 

212 

316  213 
function IntToStr(n: LongInt): shortstring; 
4  214 
begin 
351  215 
str(n, IntToStr) 
4  216 
end; 
217 

351  218 
function FloatToStr(n: hwFloat): shortstring; 
4  219 
begin 
1346  220 
FloatToStr:= cstr(n) + '_' + inttostr(Lo(n.QWordValue)) 
4  221 
end; 
222 

775  223 
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat; 
224 
var dY, dX: Extended; 

225 
begin 

226 
dY:= _dY.QWordValue / $100000000; 

227 
if _dY.isNegative then dY:=  dY; 

228 
dX:= _dX.QWordValue / $100000000; 

229 
if _dX.isNegative then dX:=  dX; 

230 
DxDy2Angle:= arctan2(dY, dX) * 180 / pi 

231 
end; 

232 

371  233 
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; 
100  234 
const _16divPI: Extended = 16/pi; 
370
c75410fe3133
 Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset

235 
var dY, dX: Extended; 
100  236 
begin 
370
c75410fe3133
 Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset

237 
dY:= _dY.QWordValue / $100000000; 
c75410fe3133
 Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset

238 
if _dY.isNegative then dY:=  dY; 
c75410fe3133
 Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset

239 
dX:= _dX.QWordValue / $100000000; 
c75410fe3133
 Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset

240 
if _dX.isNegative then dX:=  dX; 
c75410fe3133
 Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset

241 
DxDy2Angle32:= trunc(arctan2(dY, dX) * _16divPI) and $1f 
4  242 
end; 
243 

371  244 
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; 
100  245 
const MaxAngleDivPI: Extended = cMaxAngle/pi; 
370
c75410fe3133
 Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset

246 
var dY, dX: Extended; 
100  247 
begin 
370
c75410fe3133
 Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset

248 
dY:= _dY.QWordValue / $100000000; 
c75410fe3133
 Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset

249 
if _dY.isNegative then dY:=  dY; 
c75410fe3133
 Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset

250 
dX:= _dX.QWordValue / $100000000; 
c75410fe3133
 Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset

251 
if _dX.isNegative then dX:=  dX; 
438  252 
DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI) 
100  253 
end; 
4  254 

208  255 
procedure SetKB(n: Longword); 
256 
begin 

257 
KBnum:= n 

258 
end; 

259 

260 
procedure SendKB; 

261 
var s: shortstring; 

262 
begin 

263 
if KBnum <> 0 then 

264 
begin 

265 
s:= 'K' + inttostr(KBnum); 

266 
SendIPCRaw(@s, Length(s) + 1) 

267 
end 

268 
end; 

269 

351  270 
procedure SetLittle(var r: hwFloat); 
300  271 
begin 
553
5478386d935f
 Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
543
diff
changeset

272 
r:= SignAs(cLittle, r) 
300  273 
end; 
274 

337  275 
procedure SendStat(sit: TStatInfoType; s: shortstring); 
1625  276 
const stc: array [TStatInfoType] of char = 'rDkKH'; 
337  277 
begin 
278 
SendIPC('i' + stc[sit] + s) 

279 
end; 

280 

534  281 
function Str2PChar(const s: shortstring): PChar; 
351  282 
const CharArray: array[byte] of Char = ''; 
283 
begin 

284 
CharArray:= s; 

285 
CharArray[Length(s)]:= #0; 

286 
Str2PChar:= @CharArray 

287 
end; 

288 

771  289 
function isPowerOf2(i: Longword): boolean; 
290 
begin 

291 
if i = 0 then exit(true); 

292 
while (i and 1) = 0 do i:= i shr 1; 

293 
isPowerOf2:= (i = 1) 

294 
end; 

295 

296 
function toPowerOf2(i: Longword): Longword; 

297 
begin 

298 
toPowerOf2:= 1; 

299 
while (toPowerOf2 < i) do toPowerOf2:= toPowerOf2 shl 1 

300 
end; 

301 

1912
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

302 
procedure ResetVertexArrays(texture: PTexture); 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

303 
begin 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

304 
with texture^ do 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

305 
begin 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

306 
vb[0].X:= 0; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

307 
vb[0].Y:= 0; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

308 
vb[1].X:= w; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

309 
vb[1].Y:= 0; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

310 
vb[2].X:= w; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

311 
vb[2].Y:= h; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

312 
vb[3].X:= 0; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

313 
vb[3].Y:= h; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

314 

c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

315 
tb[0].X:= 0; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

316 
tb[0].Y:= 0; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

317 
tb[1].X:= rx; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

318 
tb[1].Y:= 0; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

319 
tb[2].X:= rx; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

320 
tb[2].Y:= ry; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

321 
tb[3].X:= 0; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

322 
tb[3].Y:= ry 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

323 
end; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

324 
end; 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

325 

1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

326 
function NewTexture(width, height: Longword; buf: Pointer): PTexture; 
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

327 
begin 
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

328 
new(NewTexture); 
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

329 
NewTexture^.w:= width; 
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

330 
NewTexture^.h:= height; 
1896  331 
NewTexture^.rx:= 1.0; 
332 
NewTexture^.ry:= 1.0; 

1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

333 

1912
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

334 
ResetVertexArrays(NewTexture); 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

335 

1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

336 
glGenTextures(1, @NewTexture^.id); 
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

337 

e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

338 
glBindTexture(GL_TEXTURE_2D, NewTexture^.id); 
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

339 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); 
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

340 

2271
ca0754f0f42b
flagging for "reduced quality". Clamping cuts frame rate in half in software rendering on my sucky laptop
nemo
parents:
2256
diff
changeset

341 
if not cReducedQuality then 
ca0754f0f42b
flagging for "reduced quality". Clamping cuts frame rate in half in software rendering on my sucky laptop
nemo
parents:
2256
diff
changeset

342 
begin 
ca0754f0f42b
flagging for "reduced quality". Clamping cuts frame rate in half in software rendering on my sucky laptop
nemo
parents:
2256
diff
changeset

343 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
ca0754f0f42b
flagging for "reduced quality". Clamping cuts frame rate in half in software rendering on my sucky laptop
nemo
parents:
2256
diff
changeset

344 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) 
ca0754f0f42b
flagging for "reduced quality". Clamping cuts frame rate in half in software rendering on my sucky laptop
nemo
parents:
2256
diff
changeset

345 
end; 
1180
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

346 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

347 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) 
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

348 
end; 
e56317fdf78d
Start implementing support for 32bit sprites concerned in map generation process.
unc0rr
parents:
1128
diff
changeset

349 

755  350 
function Surface2Tex(surf: PSDL_Surface): PTexture; 
2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

351 
var modeIntFormat: LongInt; 
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

352 
modeFormat: LongInt; 
1896  353 
tw, th, x, y: Longword; 
354 
tmpp: pointer; 

355 
fromP4, toP4: PLongWordArray; 

356 
fromP1, toP1: PByteArray; 

753  357 
begin 
755  358 
new(Surface2Tex); 
359 
Surface2Tex^.w:= surf^.w; 

360 
Surface2Tex^.h:= surf^.h; 

361 

2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

362 
if (surf^.format^.BytesPerPixel = 3) then 
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

363 
begin 
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

364 
modeIntFormat:= GL_RGB; 
2171
8208946331ba
Smaxx refactor of LoadImage to use flags, iphone changes by koda (mostly use of rgba instead of rgb)
nemo
parents:
2163
diff
changeset

365 
modeFormat:= modeIntFormat; 
2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

366 
end 
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

367 
else 
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

368 
if (surf^.format^.BytesPerPixel = 4) then 
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

369 
begin 
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

370 
modeIntFormat:= GL_RGBA; 
2171
8208946331ba
Smaxx refactor of LoadImage to use flags, iphone changes by koda (mostly use of rgba instead of rgb)
nemo
parents:
2163
diff
changeset

371 
modeFormat:= modeIntFormat; 
2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

372 
end 
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

373 
else 
753  374 
begin 
869  375 
TryDo(false, 'Surface2Tex: BytesPerPixel not in [3, 4]', true); 
755  376 
Surface2Tex^.id:= 0; 
753  377 
exit 
378 
end; 

379 

755  380 
glGenTextures(1, @Surface2Tex^.id); 
753  381 

755  382 
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); 
753  383 

771  384 
if SDL_MustLock(surf) then 
385 
SDLTry(SDL_LockSurface(surf) >= 0, true); 

386 

387 
if not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h)) then 

1896  388 
begin 
389 
tw:= toPowerOf2(Surf^.w); 

390 
th:= toPowerOf2(Surf^.h); 

391 

392 
Surface2Tex^.rx:= Surf^.w / tw; 

393 
Surface2Tex^.ry:= Surf^.h / th; 

394 

395 
GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel); 

396 

397 
if surf^.format^.BytesPerPixel = 4 then 

398 
begin 

399 
fromP4:= Surf^.pixels; 

400 
toP4:= tmpp; 

771  401 

1896  402 
for y:= 0 to Pred(Surf^.h) do 
403 
begin 

404 
for x:= 0 to Pred(Surf^.w) do 

405 
toP4^[x]:= fromP4^[x]; 

406 
for x:= Surf^.w to Pred(tw) do 

407 
toP4^[x]:= 0; 

408 
toP4:= @(toP4^[tw]); 

2017  409 
fromP4:= @(fromP4^[Surf^.pitch div 4]); 
1896  410 
end; 
411 

412 
for y:= Surf^.h to Pred(th) do 

413 
begin 

414 
for x:= 0 to Pred(tw) do 

415 
toP4^[x]:= 0; 

416 
toP4:= @(toP4^[tw]); 

417 
end; 

418 
end 

419 
else 

420 
begin 

421 
fromP1:= Surf^.pixels; 

422 
toP1:= tmpp; 

771  423 

1896  424 
for y:= 0 to Pred(Surf^.h) do 
425 
begin 

426 
for x:= 0 to Pred(Surf^.w) do 

427 
begin 

428 
toP1^[x * 3]:= fromP1^[x * 3]; 

429 
toP1^[x * 3 + 1]:= fromP1^[x * 3 + 1]; 

430 
toP1^[x * 3 + 2]:= fromP1^[x * 3 + 2]; 

431 
end; 

432 
for x:= Surf^.w to Pred(tw) do 

433 
begin 

434 
toP1^[x * 3]:= 0; 

435 
toP1^[x * 3 + 1]:= 0; 

436 
toP1^[x * 3 + 2]:= 0; 

437 
end; 

438 
toP1:= @(toP1^[tw * 3]); 

439 
fromP1:= @(fromP1^[Surf^.pitch]); 

440 
end; 

771  441 

1896  442 
for y:= Surf^.h to Pred(th) do 
443 
begin 

444 
for x:= 0 to Pred(tw) do 

445 
begin 

446 
toP1^[x * 3]:= 0; 

447 
toP1^[x * 3 + 1]:= 0; 

448 
toP1^[x * 3 + 2]:= 0; 

449 
end; 

450 
toP1:= @(toP1^[tw * 3]); 

451 
end; 

452 
end; 

771  453 

2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

454 
// legacy resizing function 
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

455 
// gluScaleImage(mode, Surf^.w, Surf^.h, GL_UNSIGNED_BYTE, Surf^.pixels, tw, th, GL_UNSIGNED_BYTE, tmpp); 
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

456 

12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

457 
glTexImage2D(GL_TEXTURE_2D, 0, modeIntFormat, tw, th, 0, modeFormat, GL_UNSIGNED_BYTE, tmpp); 
1896  458 

459 
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) 

460 
end else 

461 
begin 

462 
Surface2Tex^.rx:= 1.0; 

463 
Surface2Tex^.ry:= 1.0; 

2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

464 
glTexImage2D(GL_TEXTURE_2D, 0, modeIntFormat, surf^.w, surf^.h, 0, modeFormat, GL_UNSIGNED_BYTE, surf^.pixels); 
1896  465 
end; 
753  466 

1912
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

467 
ResetVertexArrays(Surface2Tex); 
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

468 

754  469 
if SDL_MustLock(surf) then 
1912
c3d31fb59f0e
Save much CPU time by initializing vertex arrays in texture creation function
unc0rr
parents:
1911
diff
changeset

470 
SDL_UnlockSurface(surf); 
754  471 

2256
30797f3a4321
sorry, these two don't help. tested, flakes still screwed up. anyway, the lines were only on terrain
nemo
parents:
2255
diff
changeset

472 
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
30797f3a4321
sorry, these two don't help. tested, flakes still screwed up. anyway, the lines were only on terrain
nemo
parents:
2255
diff
changeset

473 
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
788
00720357601f
 Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
785
diff
changeset

474 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
00720357601f
 Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
785
diff
changeset

475 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) 
753  476 
end; 
477 

759  478 
procedure FreeTexture(tex: PTexture); 
479 
begin 

2000
f9f47e681aad
Don't crash engine after round in reduced quality mode
unc0rr
parents:
1916
diff
changeset

480 
if tex <> nil then 
f9f47e681aad
Don't crash engine after round in reduced quality mode
unc0rr
parents:
1916
diff
changeset

481 
begin 
f9f47e681aad
Don't crash engine after round in reduced quality mode
unc0rr
parents:
1916
diff
changeset

482 
glDeleteTextures(1, @tex^.id); 
f9f47e681aad
Don't crash engine after round in reduced quality mode
unc0rr
parents:
1916
diff
changeset

483 
dispose(tex) 
f9f47e681aad
Don't crash engine after round in reduced quality mode
unc0rr
parents:
1916
diff
changeset

484 
end 
759  485 
end; 
337  486 

949  487 
function DecodeBase64(s: shortstring): shortstring; 
488 
const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; 

489 
var i, t, c: Longword; 

490 
begin 

491 
c:= 0; 

492 
for i:= 1 to Length(s) do 

493 
begin 

494 
t:= Pos(s[i], table); 

495 
if s[i] = '=' then inc(c); 

496 
if t > 0 then byte(s[i]):= t  1 else byte(s[i]):= 0 

497 
end; 

498 

499 
i:= 1; 

500 
t:= 1; 

501 
while i <= length(s) do 

502 
begin 

503 
DecodeBase64[t ]:= char((byte(s[i ]) shl 2) or (byte(s[i + 1]) shr 4)); 

504 
DecodeBase64[t + 1]:= char((byte(s[i + 1]) shl 4) or (byte(s[i + 2]) shr 2)); 

505 
DecodeBase64[t + 2]:= char((byte(s[i + 2]) shl 6) or (byte(s[i + 3]) )); 

506 
inc(t, 3); 

507 
inc(i, 4) 

508 
end; 

509 

510 
if c < 3 then t:= t  c; 

511 

512 
byte(DecodeBase64[0]):= t  1 

513 
end; 

514 

2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

515 
{$IFNDEF IPHONEOS} 
1080  516 
procedure MakeScreenshot(s: shortstring); 
517 
const head: array[0..8] of Word = (0, 2, 0, 0, 0, 0, 0, 0, 24); 

518 
var p: Pointer; 

519 
size: Longword; 

520 
f: file; 

521 
begin 

522 
head[6]:= cScreenWidth; 

523 
head[7]:= cScreenHeight; 

524 

525 
size:= cScreenWidth * cScreenHeight * 3; 

526 
p:= GetMem(size); 

527 

2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

528 

12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

529 
//remember that opengles operates on a single surface, so GL_FRONT *should* be implied 
1080  530 
glReadBuffer(GL_FRONT); 
531 
glReadPixels(0, 0, cScreenWidth, cScreenHeight, GL_BGR, GL_UNSIGNED_BYTE, p); 

532 

533 
{$I} 

534 
Assign(f, s); 

535 
Rewrite(f, 1); 

536 
if IOResult = 0 then 

537 
begin 

538 
BlockWrite(f, head, sizeof(head)); 

539 
BlockWrite(f, p^, size); 

540 
Close(f); 

541 
end; 

542 
{$I+} 

543 

544 
FreeMem(p) 

545 
end; 

2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset

546 
{$ENDIF} 
1080  547 

1861  548 
function modifyDamage(dmg: Longword): Longword; 
549 
begin 

1895  550 
ModifyDamage:= hwRound(_0_01 * cDamageModifier * dmg * cDamagePercent) 
1861  551 
end; 
552 

949  553 
{$IFDEF DEBUGFILE} 
554 
procedure AddFileLog(s: shortstring); 

555 
begin 

556 
writeln(f, GameTicks: 6, ': ', s); 

557 
flush(f) 

558 
end; 

559 

560 
function RectToStr(Rect: TSDL_Rect): shortstring; 

561 
begin 

562 
RectToStr:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')' 

563 
end; 

564 

2242  565 
{$IFNDEF IPHONEOS} 
371  566 
var i: LongInt; 
488  567 
{$ENDIF} 
2242  568 
{$ENDIF} 
306  569 

4  570 
initialization 
351  571 
cDrownSpeed.QWordValue:= 257698038;// 0.06 
488  572 
cMaxWindSpeed.QWordValue:= 2147484;// 0.0005 
573 
cWindSpeed.QWordValue:= 429496;// 0.0001 

351  574 
cGravity:= cMaxWindSpeed; 
1849  575 
cDamageModifier:= _1; 
1854  576 
cLaserSighting:= false; 
2017  577 
cVampiric:= false; 
2023
41d3afaa20c7
Artillery mode, sniper rifle, speech bubble tweaks, fix of rope bug introduced by enabling hats in jump
nemo
parents:
2017
diff
changeset

578 
cArtillery:= false; 
351  579 

488  580 
{$IFDEF DEBUGFILE} 
337  581 
{$I} 
2156  582 
f:= stderr; 
2154
3d2917be12c3
Change default output to stderr since /tmp doesn't exist under windows and is useless under iphoneos, add a couple of extra parameters
nemo
parents:
2152
diff
changeset

583 
rewrite(f); 
3d2917be12c3
Change default output to stderr since /tmp doesn't exist under windows and is useless under iphoneos, add a couple of extra parameters
nemo
parents:
2152
diff
changeset

584 
{$IFNDEF IPHONEOS} 
3d2917be12c3
Change default output to stderr since /tmp doesn't exist under windows and is useless under iphoneos, add a couple of extra parameters
nemo
parents:
2152
diff
changeset

585 
if ParamCount <> 0 then 
3d2917be12c3
Change default output to stderr since /tmp doesn't exist under windows and is useless under iphoneos, add a couple of extra parameters
nemo
parents:
2152
diff
changeset

586 
for i:= 0 to 7 do 
2156  587 
begin 
588 
assign(f, ParamStr(1) + '/debug' + inttostr(i) + '.txt'); 

2154
3d2917be12c3
Change default output to stderr since /tmp doesn't exist under windows and is useless under iphoneos, add a couple of extra parameters
nemo
parents:
2152
diff
changeset

589 
rewrite(f); 
3d2917be12c3
Change default output to stderr since /tmp doesn't exist under windows and is useless under iphoneos, add a couple of extra parameters
nemo
parents:
2152
diff
changeset

590 
if IOResult = 0 then break 
337  591 
end; 
2154
3d2917be12c3
Change default output to stderr since /tmp doesn't exist under windows and is useless under iphoneos, add a couple of extra parameters
nemo
parents:
2152
diff
changeset

592 
{$ENDIF} 
337  593 
{$I+} 
17  594 

4  595 
finalization 
916  596 
//uRandom.DumpBuffer; 
2154
3d2917be12c3
Change default output to stderr since /tmp doesn't exist under windows and is useless under iphoneos, add a couple of extra parameters
nemo
parents:
2152
diff
changeset

597 

2242  598 
writeln(f, 'halt at ', GameTicks, ' ticks'); 
2154
3d2917be12c3
Change default output to stderr since /tmp doesn't exist under windows and is useless under iphoneos, add a couple of extra parameters
nemo
parents:
2152
diff
changeset

599 
flush(f); 
351  600 
close(f) 
2154
3d2917be12c3
Change default output to stderr since /tmp doesn't exist under windows and is useless under iphoneos, add a couple of extra parameters
nemo
parents:
2152
diff
changeset

601 

4  602 
{$ENDIF} 
603 

604 
end. 