hedgewars/uMisc.pas
changeset 2947 803b277e4894
parent 2915 a02f276035e8
child 2948 3f21a9dc93d0
equal deleted inserted replaced
2946:1d9e0a541c62 2947:803b277e4894
     1 (*
     1 (*
     2  * Hedgewars, a free turn based strategy game
     2 * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2004-2008 Andrey Korotaev <unC0Rr@gmail.com>
     3 * Copyright (c) 2004-2008 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     4 *
     5  * This program is free software; you can redistribute it and/or modify
     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
     6 * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7 * the Free Software Foundation; version 2 of the License
     8  *
     8 *
     9  * This program is distributed in the hope that it will be useful,
     9 * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    12 * GNU General Public License for more details.
    13  *
    13 *
    14  * You should have received a copy of the GNU General Public License
    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
    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
    16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  *)
    17 *)
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit uMisc;
    21 unit uMisc;
    22 interface
    22 interface
    23 
    23 
    24 uses	SDLh, uConsts, uFloat,
    24 uses	SDLh, uConsts, uFloat,
    25 {$IFDEF GLES11}
    25 {$IFDEF GLES11}
    26 	gles11;
    26     gles11;
    27 {$ELSE}
    27 {$ELSE}
    28 	GL;
    28     GL;
    29 {$ENDIF}
    29 {$ENDIF}
    30 
    30 
    31 var
    31 var
    32 	isCursorVisible : boolean;
    32     isCursorVisible : boolean;
    33 	isTerminated    : boolean;
    33     isTerminated    : boolean;
    34 	isInLag         : boolean;
    34     isInLag         : boolean;
    35 	isPaused        : boolean;
    35     isPaused        : boolean;
    36 	isSoundEnabled  : boolean;
    36     isSoundEnabled  : boolean;
    37 	isMusicEnabled  : boolean;
    37     isMusicEnabled  : boolean;
    38 	isSEBackup      : boolean;
    38     isSEBackup      : boolean;
    39 	isInMultiShoot  : boolean;
    39     isInMultiShoot  : boolean;
    40 	isSpeed         : boolean;
    40     isSpeed         : boolean;
    41 
    41 
    42 	fastUntilLag    : boolean;
    42     fastUntilLag    : boolean;
    43 
    43 
    44 	GameState	: TGameState;
    44     GameState   : TGameState;
    45 	GameType	: TGameType;
    45     GameType    : TGameType;
    46 	GameFlags	: Longword;
    46     GameFlags   : Longword;
    47 	TrainingFlags	: Longword;
    47     TrainingFlags   : Longword;
    48 	TurnTimeLeft	: Longword;
    48     TurnTimeLeft    : Longword;
    49 	cSuddenDTurns	: LongInt;
    49     cSuddenDTurns   : LongInt;
    50 	cDamagePercent	: LongInt;
    50     cDamagePercent  : LongInt;
    51 	cMineDudPercent	: LongInt;
    51     cMineDudPercent : LongWord;
    52 	cTemplateFilter	: LongInt;
    52     cTemplateFilter : LongInt;
    53 
    53 
    54 	cHedgehogTurnTime: Longword;
    54     cHedgehogTurnTime: Longword;
    55 	cMinesTime       : LongInt;
    55     cMinesTime       : LongInt;
    56 	cMaxAIThinkTime  : Longword;
    56     cMaxAIThinkTime  : Longword;
    57 
    57 
    58 	cCloudsNumber    : LongInt;
    58     cCloudsNumber    : LongInt;
    59 	cScreenWidth     : LongInt;
    59     cScreenWidth     : LongInt;
    60 	cScreenHeight    : LongInt;
    60     cScreenHeight    : LongInt;
    61 	cInitWidth       : LongInt;
    61     cInitWidth       : LongInt;
    62 	cInitHeight      : LongInt;
    62     cInitHeight      : LongInt;
    63 	cVSyncInUse	 : boolean;	
    63     cVSyncInUse      : boolean;
    64 	cBits            : LongInt;
    64     cBits            : LongInt;
    65 	cBitsStr         : string[2];
    65     cBitsStr         : string[2];
    66 	cTagsMaskIndex   : byte;
    66     cTagsMask        : byte;
    67 	zoom             : GLfloat;
    67     zoom             : GLfloat;
    68 	ZoomValue        : GLfloat;
    68     ZoomValue        : GLfloat;
    69 
    69 
    70 	cWaterLine       : LongInt;
    70     cWaterLine       : LongInt;
    71 	cGearScrEdgesDist: LongInt;
    71     cGearScrEdgesDist: LongInt;
    72 	cAltDamage       : boolean;
    72     cAltDamage       : boolean;
    73 
    73 
    74 	GameTicks	: LongWord;
    74     GameTicks	: LongWord;
    75 	TrainingTimeInc	: Longword;
    75     TrainingTimeInc	: Longword;
    76 	TrainingTimeInD	: Longword;
    76     TrainingTimeInD	: Longword;
    77 	TrainingTimeInM	: Longword;
    77     TrainingTimeInM	: Longword;
    78 	TrainingTimeMax	: Longword;
    78     TrainingTimeMax	: Longword;
    79 
    79 
    80 	TimeTrialStartTime: Longword;
    80     TimeTrialStartTime: Longword;
    81 	TimeTrialStopTime : Longword;
    81     TimeTrialStopTime : Longword;
    82 	
    82     
    83 	recordFileName	: shortstring;
    83     recordFileName	: shortstring;
    84 	cShowFPS	: boolean;
    84     cShowFPS	: boolean;
    85 	cCaseFactor	: Longword;
    85     cCaseFactor	: Longword;
    86 	cLandAdditions	: Longword;
    86     cLandAdditions	: Longword;
    87 	cExplosives	: Longword;
    87     cExplosives	: Longword;
    88 	cFullScreen	: boolean;
    88     cFullScreen	: boolean;
    89 	cReducedQuality	: boolean;
    89     cReducedQuality	: boolean;
    90 	cLocaleFName	: shortstring;
    90     cLocaleFName	: shortstring;
    91 	cSeed		: shortstring;
    91     cSeed		: shortstring;
    92 	cInitVolume	: LongInt;
    92     cInitVolume	: LongInt;
    93 	cVolumeDelta	: LongInt;
    93     cVolumeDelta	: LongInt;
    94 	cTimerInterval	: Longword;
    94     cTimerInterval	: Longword;
    95 	cHasFocus	: boolean;
    95     cHasFocus	: boolean;
    96 	cInactDelay	: Longword;
    96     cInactDelay	: Longword;
    97 
    97 
    98 	bBetweenTurns	: boolean;
    98     bBetweenTurns	: boolean;
    99 	cHealthDecrease	: LongWord;
    99     cHealthDecrease	: LongWord;
   100 	bWaterRising	: Boolean;
   100     bWaterRising	: Boolean;
   101 
   101 
   102 	ShowCrosshair	: boolean;
   102     ShowCrosshair	: boolean;
   103 	CursorMovementX : Integer;
   103     CursorMovementX : Integer;
   104 	CursorMovementY : Integer;
   104     CursorMovementY : Integer;
   105 	cDrownSpeed	: hwFloat;
   105     cDrownSpeed	: hwFloat;
   106 	cMaxWindSpeed	: hwFloat;
   106     cMaxWindSpeed	: hwFloat;
   107 	cWindSpeed	: hwFloat;
   107     cWindSpeed	: hwFloat;
   108 	cGravity	: hwFloat;
   108     cGravity	: hwFloat;
   109 	cDamageModifier	: hwFloat;
   109     cDamageModifier	: hwFloat;
   110 	cLaserSighting	: boolean;
   110     cLaserSighting	: boolean;
   111 	cVampiric	: boolean;
   111     cVampiric	: boolean;
   112 	cArtillery	: boolean;
   112     cArtillery	: boolean;
   113 	WeaponTooltipTex : PTexture;
   113     WeaponTooltipTex : PTexture;
   114 	cWeaponTooltips: boolean;
   114     cWeaponTooltips: boolean;
   115 
   115 
   116 	flagMakeCapture	: boolean;
   116     flagMakeCapture	: boolean;
   117 
   117 
   118 	InitStepsFlags	: Longword;
   118     InitStepsFlags	: Longword;
   119 	RealTicks	: Longword;
   119     RealTicks	: Longword;
   120 	AttackBar	: LongInt;
   120     AttackBar	: LongInt;
   121 
   121 
   122 	WaterColorArray	: array[0..3] of HwColor4f;
   122     WaterColorArray	: array[0..3] of HwColor4f;
   123 
   123 
   124 	CursorPoint	: TPoint;
   124     CursorPoint	: TPoint;
   125 	TargetPoint	: TPoint;
   125     TargetPoint	: TPoint;
   126 
   126 
   127     TextureList : PTexture;
   127     TextureList : PTexture;
   128 
   128 
   129 
   129 
   130 procedure init_uMisc;
   130 procedure init_uMisc;
   176 procedure SplitBySpace(var a, b: shortstring);
   176 procedure SplitBySpace(var a, b: shortstring);
   177 var i, t: LongInt;
   177 var i, t: LongInt;
   178 begin
   178 begin
   179 i:= Pos(' ', a);
   179 i:= Pos(' ', a);
   180 if i > 0 then
   180 if i > 0 then
   181 	begin
   181     begin
   182 	for t:= 1 to Pred(i) do
   182     for t:= 1 to Pred(i) do
   183 		if (a[t] >= 'A')and(a[t] <= 'Z') then Inc(a[t], 32);
   183         if (a[t] >= 'A')and(a[t] <= 'Z') then Inc(a[t], 32);
   184 	b:= copy(a, i + 1, Length(a) - i);
   184     b:= copy(a, i + 1, Length(a) - i);
   185 	byte(a[0]):= Pred(i)
   185     byte(a[0]):= Pred(i)
   186 	end else b:= '';
   186     end else b:= '';
   187 end;
   187 end;
   188 
   188 
   189 procedure SplitByChar(var a, b: ansistring; c: char);
   189 procedure SplitByChar(var a, b: ansistring; c: char);
   190 var i: LongInt;
   190 var i: LongInt;
   191 begin
   191 begin
   192 i:= Pos(c, a);
   192 i:= Pos(c, a);
   193 if i > 0 then
   193 if i > 0 then
   194 	begin
   194     begin
   195 	b:= copy(a, i + 1, Length(a) - i);
   195     b:= copy(a, i + 1, Length(a) - i);
   196 	setlength(a, Pred(i));
   196     setlength(a, Pred(i));
   197 	end else b:= '';
   197     end else b:= '';
   198 end;
   198 end;
   199 
   199 
   200 procedure movecursor(dx, dy: Integer);
   200 procedure movecursor(dx, dy: Integer);
   201 var x, y: LongInt;
   201 var x, y: LongInt;
   202 begin
   202 begin
   227 procedure OutError(Msg: shortstring; isFatalError: boolean);
   227 procedure OutError(Msg: shortstring; isFatalError: boolean);
   228 begin
   228 begin
   229 {$IFDEF DEBUGFILE}AddFileLog(Msg);{$ENDIF}
   229 {$IFDEF DEBUGFILE}AddFileLog(Msg);{$ENDIF}
   230 WriteLnToConsole(Msg);
   230 WriteLnToConsole(Msg);
   231 if isFatalError then
   231 if isFatalError then
   232    begin
   232 begin
   233    SendIPC('E' + GetLastConsoleLine);
   233 SendIPC('E' + GetLastConsoleLine);
   234    SDL_Quit;
   234 SDL_Quit;
   235    halt(1)
   235 halt(1)
   236    end
   236 end
   237 end;
   237 end;
   238 
   238 
   239 procedure TryDo(Assert: boolean; Msg: shortstring; isFatal: boolean);
   239 procedure TryDo(Assert: boolean; Msg: shortstring; isFatal: boolean);
   240 begin
   240 begin
   241 if not Assert then OutError(Msg, isFatal)
   241 if not Assert then OutError(Msg, isFatal)
   311 
   311 
   312 procedure SendKB;
   312 procedure SendKB;
   313 var s: shortstring;
   313 var s: shortstring;
   314 begin
   314 begin
   315 if KBnum <> 0 then
   315 if KBnum <> 0 then
   316    begin
   316 begin
   317    s:= 'K' + inttostr(KBnum);
   317 s:= 'K' + inttostr(KBnum);
   318    SendIPCRaw(@s, Length(s) + 1)
   318 SendIPCRaw(@s, Length(s) + 1)
   319    end
   319 end
   320 end;
   320 end;
   321 
   321 
   322 procedure SetLittle(var r: hwFloat);
   322 procedure SetLittle(var r: hwFloat);
   323 begin
   323 begin
   324 r:= SignAs(cLittle, r)
   324 r:= SignAs(cLittle, r)
   354 end;
   354 end;
   355 
   355 
   356 procedure ResetVertexArrays(texture: PTexture);
   356 procedure ResetVertexArrays(texture: PTexture);
   357 begin
   357 begin
   358 with texture^ do
   358 with texture^ do
   359 	begin
   359     begin
   360 	vb[0].X:= 0;
   360     vb[0].X:= 0;
   361 	vb[0].Y:= 0;
   361     vb[0].Y:= 0;
   362 	vb[1].X:= w;
   362     vb[1].X:= w;
   363 	vb[1].Y:= 0;
   363     vb[1].Y:= 0;
   364 	vb[2].X:= w;
   364     vb[2].X:= w;
   365 	vb[2].Y:= h;
   365     vb[2].Y:= h;
   366 	vb[3].X:= 0;
   366     vb[3].X:= 0;
   367 	vb[3].Y:= h;
   367     vb[3].Y:= h;
   368 
   368 
   369 	tb[0].X:= 0;
   369     tb[0].X:= 0;
   370 	tb[0].Y:= 0;
   370     tb[0].Y:= 0;
   371 	tb[1].X:= rx;
   371     tb[1].X:= rx;
   372 	tb[1].Y:= 0;
   372     tb[1].Y:= 0;
   373 	tb[2].X:= rx;
   373     tb[2].X:= rx;
   374 	tb[2].Y:= ry;
   374     tb[2].Y:= ry;
   375 	tb[3].X:= 0;
   375     tb[3].X:= 0;
   376 	tb[3].Y:= ry
   376     tb[3].Y:= ry
   377 	end;
   377     end;
   378 end;
   378 end;
   379 
   379 
   380 function NewTexture(width, height: Longword; buf: Pointer): PTexture;
   380 function NewTexture(width, height: Longword; buf: Pointer): PTexture;
   381 begin
   381 begin
   382 new(NewTexture);
   382 new(NewTexture);
   422 Surface2Tex^.w:= surf^.w;
   422 Surface2Tex^.w:= surf^.w;
   423 Surface2Tex^.h:= surf^.h;
   423 Surface2Tex^.h:= surf^.h;
   424 
   424 
   425 if (surf^.format^.BytesPerPixel <> 4) then
   425 if (surf^.format^.BytesPerPixel <> 4) then
   426 begin
   426 begin
   427 	TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true);
   427     TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true);
   428 	Surface2Tex^.id:= 0;
   428     Surface2Tex^.id:= 0;
   429 	exit
   429     exit
   430 end;
   430 end;
   431 
   431 
   432 
   432 
   433 glGenTextures(1, @Surface2Tex^.id);
   433 glGenTextures(1, @Surface2Tex^.id);
   434 
   434 
   435 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id);
   435 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id);
   436 
   436 
   437 if SDL_MustLock(surf) then
   437 if SDL_MustLock(surf) then
   438 	SDLTry(SDL_LockSurface(surf) >= 0, true);
   438     SDLTry(SDL_LockSurface(surf) >= 0, true);
   439 
   439 
   440 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
   440 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
   441 begin
   441 begin
   442 	tw:= toPowerOf2(Surf^.w);
   442     tw:= toPowerOf2(Surf^.w);
   443 	th:= toPowerOf2(Surf^.h);
   443     th:= toPowerOf2(Surf^.h);
   444 
   444 
   445 	Surface2Tex^.rx:= Surf^.w / tw;
   445     Surface2Tex^.rx:= Surf^.w / tw;
   446 	Surface2Tex^.ry:= Surf^.h / th;
   446     Surface2Tex^.ry:= Surf^.h / th;
   447 
   447 
   448 	GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel);
   448     GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel);
   449 
   449 
   450 		fromP4:= Surf^.pixels;
   450         fromP4:= Surf^.pixels;
   451 		toP4:= tmpp;
   451         toP4:= tmpp;
   452 
   452 
   453 		for y:= 0 to Pred(Surf^.h) do
   453         for y:= 0 to Pred(Surf^.h) do
   454 		begin
   454         begin
   455 			for x:= 0 to Pred(Surf^.w) do toP4^[x]:= fromP4^[x];
   455             for x:= 0 to Pred(Surf^.w) do toP4^[x]:= fromP4^[x];
   456 			for x:= Surf^.w to Pred(tw) do toP4^[x]:= 0;
   456             for x:= Surf^.w to Pred(tw) do toP4^[x]:= 0;
   457 			toP4:= @(toP4^[tw]);
   457             toP4:= @(toP4^[tw]);
   458 			fromP4:= @(fromP4^[Surf^.pitch div 4]);
   458             fromP4:= @(fromP4^[Surf^.pitch div 4]);
   459 		end;
   459         end;
   460 
   460 
   461 		for y:= Surf^.h to Pred(th) do
   461         for y:= Surf^.h to Pred(th) do
   462 		begin
   462         begin
   463 			for x:= 0 to Pred(tw) do toP4^[x]:= 0;
   463             for x:= 0 to Pred(tw) do toP4^[x]:= 0;
   464 			toP4:= @(toP4^[tw]);
   464             toP4:= @(toP4^[tw]);
   465 		end;
   465         end;
   466 
   466 
   467 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp);
   467     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp);
   468 
   468 
   469 	FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
   469     FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
   470 end
   470 end
   471 else
   471 else
   472 begin
   472 begin
   473 	Surface2Tex^.rx:= 1.0;
   473     Surface2Tex^.rx:= 1.0;
   474 	Surface2Tex^.ry:= 1.0;
   474     Surface2Tex^.ry:= 1.0;
   475 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels);
   475     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels);
   476 end;
   476 end;
   477 
   477 
   478 ResetVertexArrays(Surface2Tex);
   478 ResetVertexArrays(Surface2Tex);
   479 
   479 
   480 if SDL_MustLock(surf) then
   480 if SDL_MustLock(surf) then
   481 	SDL_UnlockSurface(surf);
   481     SDL_UnlockSurface(surf);
   482 
   482 
   483 SetTextureParameters(enableClamp);
   483 SetTextureParameters(enableClamp);
   484 end;
   484 end;
   485 
   485 
   486 procedure FreeTexture(tex: PTexture);
   486 procedure FreeTexture(tex: PTexture);
   487 begin
   487 begin
   488 if tex <> nil then
   488 if tex <> nil then
   489 	begin
   489     begin
   490     if tex^.NextTexture <> nil then tex^.NextTexture^.PrevTexture:= tex^.PrevTexture;
   490     if tex^.NextTexture <> nil then tex^.NextTexture^.PrevTexture:= tex^.PrevTexture;
   491     if tex^.PrevTexture <> nil then tex^.PrevTexture^.NextTexture:= tex^.NextTexture
   491     if tex^.PrevTexture <> nil then tex^.PrevTexture^.NextTexture:= tex^.NextTexture
   492        else TextureList:= tex^.NextTexture;
   492     else TextureList:= tex^.NextTexture;
   493 	glDeleteTextures(1, @tex^.id);
   493     glDeleteTextures(1, @tex^.id);
   494 	Dispose(tex)
   494     Dispose(tex)
   495 	end
   495     end
   496 end;
   496 end;
   497 
   497 
   498 function DecodeBase64(s: shortstring): shortstring;
   498 function DecodeBase64(s: shortstring): shortstring;
   499 const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
   499 const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
   500 var i, t, c: Longword;
   500 var i, t, c: Longword;
   501 begin
   501 begin
   502 c:= 0;
   502 c:= 0;
   503 for i:= 1 to Length(s) do
   503 for i:= 1 to Length(s) do
   504 	begin
   504     begin
   505 	t:= Pos(s[i], table);
   505     t:= Pos(s[i], table);
   506 	if s[i] = '=' then inc(c);
   506     if s[i] = '=' then inc(c);
   507 	if t > 0 then byte(s[i]):= t - 1 else byte(s[i]):= 0
   507     if t > 0 then byte(s[i]):= t - 1 else byte(s[i]):= 0
   508 	end;
   508     end;
   509 
   509 
   510 i:= 1;
   510 i:= 1;
   511 t:= 1;
   511 t:= 1;
   512 while i <= length(s) do
   512 while i <= length(s) do
   513 	begin
   513     begin
   514 	DecodeBase64[t    ]:= char((byte(s[i    ]) shl 2) or (byte(s[i + 1]) shr 4));
   514     DecodeBase64[t    ]:= char((byte(s[i    ]) shl 2) or (byte(s[i + 1]) shr 4));
   515 	DecodeBase64[t + 1]:= char((byte(s[i + 1]) shl 4) or (byte(s[i + 2]) shr 2));
   515     DecodeBase64[t + 1]:= char((byte(s[i + 1]) shl 4) or (byte(s[i + 2]) shr 2));
   516 	DecodeBase64[t + 2]:= char((byte(s[i + 2]) shl 6) or (byte(s[i + 3])      ));
   516     DecodeBase64[t + 2]:= char((byte(s[i + 2]) shl 6) or (byte(s[i + 3])      ));
   517 	inc(t, 3);
   517     inc(t, 3);
   518 	inc(i, 4)
   518     inc(i, 4)
   519 	end;
   519     end;
   520 
   520 
   521 if c < 3 then t:= t - c;
   521 if c < 3 then t:= t - c;
   522 
   522 
   523 byte(DecodeBase64[0]):= t - 1
   523 byte(DecodeBase64[0]):= t - 1
   524 end;
   524 end;
   525 
   525 
   526 {$IFNDEF IPHONEOS}
   526 {$IFNDEF IPHONEOS}
   527 procedure MakeScreenshot(filename: shortstring);
   527 procedure MakeScreenshot(filename: shortstring);
   528 var p: Pointer;
   528 var p: Pointer;
   529 	size: Longword;
   529     size: Longword;
   530 	f: file;
   530     f: file;
   531 {$IFNDEF WIN32}
   531 {$IFNDEF WIN32}
   532 	// TGA Header
   532     // TGA Header
   533 	head: array[0..8] of Word = (0, 2, 0, 0, 0, 0, 0, 0, 24);
   533     head: array[0..8] of Word = (0, 2, 0, 0, 0, 0, 0, 0, 24);
   534 {$ELSE}
   534 {$ELSE}
   535 	// Windows Bitmap Header
   535     // Windows Bitmap Header
   536 	head: array[0..53] of Byte = (
   536     head: array[0..53] of Byte = (
   537 	$42, $4D, // identifier ("BM")
   537     $42, $4D, // identifier ("BM")
   538 	0, 0, 0, 0, // file size
   538     0, 0, 0, 0, // file size
   539 	0, 0, 0, 0, // reserved
   539     0, 0, 0, 0, // reserved
   540 	54, 0, 0, 0, // starting offset
   540     54, 0, 0, 0, // starting offset
   541 	40, 0, 0, 0, // header size
   541     40, 0, 0, 0, // header size
   542 	0, 0, 0, 0, // width
   542     0, 0, 0, 0, // width
   543 	0, 0, 0, 0, // height
   543     0, 0, 0, 0, // height
   544 	1, 0, // color planes
   544     1, 0, // color planes
   545 	24, 0, // bit depth
   545     24, 0, // bit depth
   546 	0, 0, 0, 0, // compression method (uncompressed)
   546     0, 0, 0, 0, // compression method (uncompressed)
   547 	0, 0, 0, 0, // image size
   547     0, 0, 0, 0, // image size
   548 	96, 0, 0, 0, // horizontal resolution
   548     96, 0, 0, 0, // horizontal resolution
   549 	96, 0, 0, 0, // vertical resolution
   549     96, 0, 0, 0, // vertical resolution
   550 	0, 0, 0, 0, // number of colors (all)
   550     0, 0, 0, 0, // number of colors (all)
   551 	0, 0, 0, 0 // number of important colors
   551     0, 0, 0, 0 // number of important colors
   552 	);
   552     );
   553 {$ENDIF}
   553 {$ENDIF}
   554 begin
   554 begin
   555 playSound(sndShutter);
   555 playSound(sndShutter);
   556 
   556 
   557 size:= cScreenWidth * cScreenHeight * 3;
   557 size:= cScreenWidth * cScreenHeight * 3;
   590 
   590 
   591 {$I-}
   591 {$I-}
   592 Assign(f, filename);
   592 Assign(f, filename);
   593 Rewrite(f, 1);
   593 Rewrite(f, 1);
   594 if IOResult = 0 then
   594 if IOResult = 0 then
   595 	begin
   595     begin
   596 	BlockWrite(f, head, sizeof(head));
   596     BlockWrite(f, head, sizeof(head));
   597 	BlockWrite(f, p^, size);
   597     BlockWrite(f, p^, size);
   598 	Close(f);
   598     Close(f);
   599 	end;
   599     end;
   600 {$I+}
   600 {$I+}
   601 
   601 
   602 FreeMem(p)
   602 FreeMem(p)
   603 end;
   603 end;
   604 {$ENDIF}
   604 {$ENDIF}
   618 
   618 
   619 function doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface;
   619 function doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface;
   620 {* for more information http://www.idevgames.com/forum/showpost.php?p=85864&postcount=7 *}
   620 {* for more information http://www.idevgames.com/forum/showpost.php?p=85864&postcount=7 *}
   621 var convertedSurf: PSDL_Surface = nil;
   621 var convertedSurf: PSDL_Surface = nil;
   622 begin
   622 begin
   623 	if (tmpsurf^.format^.bitsperpixel = 24) or ((tmpsurf^.format^.bitsperpixel = 32) and (tmpsurf^.format^.rshift > tmpsurf^.format^.bshift)) then
   623     if (tmpsurf^.format^.bitsperpixel = 24) or ((tmpsurf^.format^.bitsperpixel = 32) and (tmpsurf^.format^.rshift > tmpsurf^.format^.bshift)) then
   624 	begin
   624     begin
   625 		convertedSurf:= SDL_ConvertSurface(tmpsurf, @conversionFormat, SDL_SWSURFACE);
   625         convertedSurf:= SDL_ConvertSurface(tmpsurf, @conversionFormat, SDL_SWSURFACE);
   626 		SDL_FreeSurface(tmpsurf);
   626         SDL_FreeSurface(tmpsurf);
   627 		exit(convertedSurf);
   627         exit(convertedSurf);
   628 	end;
   628     end;
   629 
   629 
   630 	exit(tmpsurf);
   630     exit(tmpsurf);
   631 end;
   631 end;
   632 
   632 
   633 function endian(independent: LongWord): LongWord;
   633 function endian(independent: LongWord): LongWord;
   634 begin
   634 begin
   635 {$IFDEF ENDIAN_LITTLE}
   635 {$IFDEF ENDIAN_LITTLE}
   636 endian:= independent;
   636 endian:= independent;
   637 {$ELSE}
   637 {$ELSE}
   638 endian:= (((independent and $FF000000) shr 24) or
   638 endian:= (((independent and $FF000000) shr 24) or
   639 	  ((independent and $00FF0000) shr 8) or
   639     ((independent and $00FF0000) shr 8) or
   640 	  ((independent and $0000FF00) shl 8) or
   640     ((independent and $0000FF00) shl 8) or
   641 	  ((independent and $000000FF) shl 24))
   641     ((independent and $000000FF) shl 24))
   642 {$ENDIF}
   642 {$ENDIF}
   643 end;
   643 end;
   644 
   644 
   645 
   645 
   646 procedure init_uMisc;
   646 procedure init_uMisc;
   647 {$IFNDEF IPHONEOS}var i: LongInt;{$ENDIF}
   647 {$IFNDEF IPHONEOS}var i: LongInt;{$ENDIF}
   648 begin
   648 begin
   649 	cDrownSpeed.QWordValue	:= 257698038;		// 0.06
   649     cDrownSpeed.QWordValue	:= 257698038;		// 0.06
   650 	cMaxWindSpeed.QWordValue:= 2147484;		// 0.0005
   650     cMaxWindSpeed.QWordValue:= 2147484;		// 0.0005
   651 	cWindSpeed.QWordValue	:= 429496;		// 0.0001
   651     cWindSpeed.QWordValue	:= 429496;		// 0.0001
   652 	cGravity                := cMaxWindSpeed;
   652     cGravity                := cMaxWindSpeed;
   653 	cDamageModifier         := _1;
   653     cDamageModifier         := _1;
   654 	TargetPoint             := cTargetPointRef;
   654     TargetPoint             := cTargetPointRef;
   655 	TextureList             := nil;
   655     TextureList             := nil;
   656 	
   656     
   657 	// int, longint longword and byte
   657     // int, longint longword and byte
   658 	CursorMovementX		:= 0;
   658     CursorMovementX		:= 0;
   659 	CursorMovementY		:= 0;
   659     CursorMovementY		:= 0;
   660 	GameTicks		:= 0;
   660     GameTicks		:= 0;
   661 	TrainingTimeInc		:= 10000;
   661     TrainingTimeInc		:= 10000;
   662 	TrainingTimeInD		:= 500;
   662     TrainingTimeInD		:= 500;
   663 	TrainingTimeInM		:= 5000;
   663     TrainingTimeInM		:= 5000;
   664 	TrainingTimeMax		:= 60000;
   664     TrainingTimeMax		:= 60000;
   665 	TimeTrialStartTime	:= 0;
   665     TimeTrialStartTime	:= 0;
   666 	TimeTrialStopTime	:= 0;
   666     TimeTrialStopTime	:= 0;
   667 	cWaterLine		:= LAND_HEIGHT;
   667     cWaterLine		:= LAND_HEIGHT;
   668 	cGearScrEdgesDist	:= 240;
   668     cGearScrEdgesDist	:= 240;
   669 	cHealthDecrease		:= 0;
   669     cHealthDecrease		:= 0;
   670 
   670 
   671 	GameFlags		:= 0;
   671     GameFlags		:= 0;
   672 	TrainingFlags		:= 0;
   672     TrainingFlags		:= 0;
   673 	TurnTimeLeft		:= 0;
   673     TurnTimeLeft		:= 0;
   674 	cSuddenDTurns		:= 15;
   674     cSuddenDTurns		:= 15;
   675 	cDamagePercent		:= 100;
   675     cDamagePercent		:= 100;
   676 	cMineDudPercent		:= 0;
   676     cMineDudPercent		:= 0;
   677 	cTemplateFilter		:= 0;
   677     cTemplateFilter		:= 0;
   678 
   678 
   679 	cHedgehogTurnTime	:= 45000;
   679     cHedgehogTurnTime	:= 45000;
   680 	cMinesTime		:= 3000;
   680     cMinesTime		:= 3000;
   681 	cMaxAIThinkTime		:= 9000;
   681     cMaxAIThinkTime		:= 9000;
   682 
   682 
   683 	cCloudsNumber		:= 9;
   683     cCloudsNumber		:= 9;
   684 	cScreenWidth		:= 1024;
   684     cScreenWidth		:= 1024;
   685 	cScreenHeight		:= 768;
   685     cScreenHeight		:= 768;
   686 	cInitWidth		:= cScreenWidth;
   686     cInitWidth		:= cScreenWidth;
   687 	cInitHeight		:= cScreenHeight;
   687     cInitHeight		:= cScreenHeight;
   688 	cBits			:= 32;
   688     cBits			:= 32;
   689 	cTagsMaskIndex		:= Low(cTagsMasks);
   689     cTagsMask		:= 0;
   690 	KBnum			:= 0;
   690     KBnum			:= 0;
   691 	InitStepsFlags		:= 0;
   691     InitStepsFlags		:= 0;
   692 	RealTicks		:= 0;
   692     RealTicks		:= 0;
   693 	AttackBar		:= 0; // 0 - none, 1 - just bar at the right-down corner, 2 - like in WWP
   693     AttackBar		:= 0; // 0 - none, 1 - just bar at the right-down corner, 2 - like in WWP
   694 	
   694     
   695 	// tgametype and glfloat and string
   695     // tgametype and glfloat and string
   696 	GameState		:= Low(TGameState);
   696     GameState		:= Low(TGameState);
   697 	GameType		:= gmtLocal;
   697     GameType		:= gmtLocal;
   698 	zoom			:= 2.0;
   698     zoom			:= 2.0;
   699 	ZoomValue		:= 2.0;
   699     ZoomValue		:= 2.0;
   700 	cBitsStr		:= '32';
   700     cBitsStr		:= '32';
   701 	WeaponTooltipTex	:= nil;
   701     WeaponTooltipTex	:= nil;
   702 
   702 
   703 	// booleans
   703     // booleans
   704 	cLaserSighting		:= false;
   704     cLaserSighting		:= false;
   705 	cVampiric		:= false;
   705     cVampiric		:= false;
   706 	cArtillery		:= false;
   706     cArtillery		:= false;
   707 	flagMakeCapture		:= false;
   707     flagMakeCapture		:= false;
   708 	bBetweenTurns		:= false;
   708     bBetweenTurns		:= false;
   709 	bWaterRising		:= false;
   709     bWaterRising		:= false;
   710 	isCursorVisible		:= false;
   710     isCursorVisible		:= false;
   711 	isTerminated		:= false;
   711     isTerminated		:= false;
   712 	isInLag			:= false;
   712     isInLag			:= false;
   713 	isPaused		:= false;
   713     isPaused		:= false;
   714 	isMusicEnabled		:= false;
   714     isMusicEnabled		:= false;
   715 	isInMultiShoot		:= false;
   715     isInMultiShoot		:= false;
   716 	isSpeed			:= false;
   716     isSpeed			:= false;
   717 	fastUntilLag		:= false;
   717     fastUntilLag		:= false;
   718 	cVSyncInUse		:= true;	
   718     cVSyncInUse		:= true;	
   719 	isSoundEnabled		:= true;
   719     isSoundEnabled		:= true;
   720 	isSEBackup		:= true;
   720     isSEBackup		:= true;
   721 	
   721     
   722 	// init flags
   722     // init flags
   723 	recordFileName		:= '';
   723     recordFileName		:= '';
   724 	cShowFPS		:= false;
   724     cShowFPS		:= false;
   725 	cCaseFactor		:= 5;  {0..9}
   725     cCaseFactor		:= 5;  {0..9}
   726 	cLandAdditions		:= 4;
   726     cLandAdditions		:= 4;
   727 	cExplosives		:= 2;
   727     cExplosives		:= 2;
   728 	cFullScreen		:= false;
   728     cFullScreen		:= false;
   729 	cReducedQuality		:= false;
   729     cReducedQuality		:= false;
   730 	cLocaleFName		:= 'en.txt';
   730     cLocaleFName		:= 'en.txt';
   731 	cSeed			:= '';
   731     cSeed			:= '';
   732 	cInitVolume		:= 50;
   732     cInitVolume		:= 50;
   733 	cVolumeDelta		:= 0;
   733     cVolumeDelta		:= 0;
   734 	cTimerInterval		:= 8;
   734     cTimerInterval		:= 8;
   735 	cHasFocus		:= true;
   735     cHasFocus		:= true;
   736 	cInactDelay		:= 1250;
   736     cInactDelay		:= 1250;
   737 	cAltDamage		:= true;
   737     cAltDamage		:= true;
   738 {$IFDEF DEBUGFILE}
   738 {$IFDEF DEBUGFILE}
   739 {$I-}
   739 {$I-}
   740 {$IFDEF IPHONEOS}
   740 {$IFDEF IPHONEOS}
   741 	f:= stderr;
   741     f:= stderr;
   742 {$ELSE}
   742 {$ELSE}
   743 	if ParamStr(1) <> '' then
   743     if ParamStr(1) <> '' then
   744 		begin
   744         begin
   745 		for i:= 0 to 7 do
   745         for i:= 0 to 7 do
   746 		begin
   746         begin
   747 			assign(f, ParamStr(1) + '/debug' + inttostr(i) + '.txt');
   747             assign(f, ParamStr(1) + '/debug' + inttostr(i) + '.txt');
   748 			rewrite(f);
   748             rewrite(f);
   749 			if IOResult = 0 then break;
   749             if IOResult = 0 then break;
   750 		end;
   750         end;
   751 		if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr
   751         if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr
   752 		end
   752         end
   753 	else
   753     else
   754 		f:= stderr;
   754         f:= stderr;
   755 {$ENDIF}
   755 {$ENDIF}
   756 {$I+}
   756 {$I+}
   757 {$ENDIF}
   757 {$ENDIF}
   758 
   758 
   759 end;
   759 end;
   760 
   760 
   761 procedure free_uMisc;
   761 procedure free_uMisc;
   762 begin
   762 begin
   763 	//uRandom.DumpBuffer;
   763     //uRandom.DumpBuffer;
   764 	while TextureList <> nil do FreeTexture(TextureList);
   764     while TextureList <> nil do FreeTexture(TextureList);
   765 
   765 
   766 {$IFDEF DEBUGFILE}
   766 {$IFDEF DEBUGFILE}
   767 	writeln(f, 'halt at ', GameTicks, ' ticks. TurnTimeLeft = ', TurnTimeLeft);
   767     writeln(f, 'halt at ', GameTicks, ' ticks. TurnTimeLeft = ', TurnTimeLeft);
   768 	flush(f);
   768     flush(f);
   769 	close(f);
   769     close(f);
   770 {$ENDIF}
   770 {$ENDIF}
   771 end;
   771 end;
   772 
   772 
   773 end.
   773 end.