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 {$INLINE ON} |
|
21 |
20 |
22 unit uMisc; |
21 unit uMisc; |
23 interface |
22 interface |
24 |
23 |
25 uses SDLh, uConsts, uFloat, GLunit, Math; |
24 uses SDLh, uConsts, uFloat, GLunit, Math; |
26 |
25 |
27 var |
26 var |
|
27 /////// init flags /////// |
|
28 cScreenWidth : LongInt = 1024; |
|
29 cScreenHeight : LongInt = 768; |
|
30 cBits : LongInt = 32; |
|
31 //ipcPort is in uIO |
|
32 cFullScreen : boolean = false; |
|
33 isSoundEnabled : boolean = true; |
|
34 isMusicEnabled : boolean = false; |
|
35 cLocaleFName : shortstring = 'en.txt'; |
|
36 cInitVolume : LongInt = 100; |
|
37 cTimerInterval : LongInt = 8; |
|
38 PathPrefix : shortstring = './'; |
|
39 cShowFPS : boolean = false; |
|
40 cAltDamage : boolean = true; |
|
41 cReducedQuality : LongInt = rqNone; |
|
42 //userNick is in uChat |
|
43 recordFileName : shortstring = ''; |
|
44 cReadyDelay : Longword = 0; |
|
45 cLogfileBase : shortstring = 'debug'; |
|
46 ////////////////////////// |
|
47 |
28 isCursorVisible : boolean; |
48 isCursorVisible : boolean; |
29 isTerminated : boolean; |
49 isTerminated : boolean; |
30 isInLag : boolean; |
50 isInLag : boolean; |
31 isPaused : boolean; |
51 isPaused : boolean; |
32 |
52 |
33 isSEBackup : boolean; |
53 isSEBackup : boolean; |
34 isInMultiShoot : boolean; |
54 isInMultiShoot : boolean; |
35 isSpeed : boolean; |
55 isSpeed : boolean; |
36 isFirstFrame : boolean; |
56 isFirstFrame : boolean; |
37 |
57 |
38 //isStereoEnabled : boolean; |
|
39 cStereoMode : TStereoMode; |
58 cStereoMode : TStereoMode; |
40 |
|
41 fastUntilLag : boolean; |
59 fastUntilLag : boolean; |
42 |
60 |
43 GameState : TGameState; |
61 GameState : TGameState; |
44 GameType : TGameType; |
62 GameType : TGameType; |
45 GameFlags : Longword; |
63 GameFlags : Longword; |
46 TrainingFlags : Longword; |
64 TrainingFlags : Longword; |
47 TurnTimeLeft : Longword; |
65 TurnTimeLeft : Longword; |
|
66 ReadyTimeLeft : Longword; |
48 cSuddenDTurns : LongInt; |
67 cSuddenDTurns : LongInt; |
49 cDamagePercent : LongInt; |
68 cDamagePercent : LongInt; |
50 cMineDudPercent : LongWord; |
69 cMineDudPercent : LongWord; |
51 cTemplateFilter : LongInt; |
70 cTemplateFilter : LongInt; |
52 cMapGen : LongInt; |
71 cMapGen : LongInt; |
71 TrainingTimeInM : Longword; |
90 TrainingTimeInM : Longword; |
72 TrainingTimeMax : Longword; |
91 TrainingTimeMax : Longword; |
73 |
92 |
74 TimeTrialStartTime: Longword; |
93 TimeTrialStartTime: Longword; |
75 TimeTrialStopTime : Longword; |
94 TimeTrialStopTime : Longword; |
76 |
95 |
77 // init flags |
96 // originally from uConsts |
78 cScreenWidth : LongInt = 1024; |
97 Pathz: array[TPathType] of shortstring; |
79 cScreenHeight : LongInt = 768; |
98 CountTexz: array[1..Pred(AMMO_INFINITE)] of PTexture; |
80 cBits : LongInt = 32; |
99 LAND_WIDTH : LongInt; |
81 cBitsStr : string[2] = '32'; |
100 LAND_HEIGHT : LongInt; |
82 //ipcPort is in uIO |
101 LAND_WIDTH_MASK : LongWord; |
83 cFullScreen : boolean = false; |
102 LAND_HEIGHT_MASK : LongWord; |
84 isSoundEnabled : boolean = true; |
103 cMaxCaptions : LongInt; |
85 isMusicEnabled : boolean = false; |
104 |
86 cLocaleFName : shortstring = 'en.txt'; |
105 cLeftScreenBorder : LongInt; |
87 cInitVolume : LongInt = 50; |
106 cRightScreenBorder : LongInt; |
88 cTimerInterval : LongInt = 8; |
107 cScreenSpace : LongInt; |
89 //pathPrefix is in uConsts |
108 |
90 cShowFPS : boolean = false; |
|
91 cAltDamage : boolean = true; |
|
92 cReducedQuality : LongInt = rqNone; |
|
93 |
|
94 recordFileName : shortstring = ''; |
|
95 |
|
96 cCaseFactor : Longword; |
109 cCaseFactor : Longword; |
97 cLandAdditions : Longword; |
110 cLandAdditions : Longword; |
98 cExplosives : Longword; |
111 cExplosives : Longword; |
99 |
112 |
100 cSeed : shortstring; |
113 cSeed : shortstring; |
148 procedure SplitBySpace(var a, b: shortstring); |
161 procedure SplitBySpace(var a, b: shortstring); |
149 procedure SplitByChar(var a, b: ansistring; c: char); |
162 procedure SplitByChar(var a, b: ansistring; c: char); |
150 function EnumToStr(const en : TGearType) : shortstring; overload; |
163 function EnumToStr(const en : TGearType) : shortstring; overload; |
151 function EnumToStr(const en : TSound) : shortstring; overload; |
164 function EnumToStr(const en : TSound) : shortstring; overload; |
152 function EnumToStr(const en : TAmmoType) : shortstring; overload; |
165 function EnumToStr(const en : TAmmoType) : shortstring; overload; |
|
166 function EnumToStr(const en : THogEffect) : shortstring; overload; |
153 procedure movecursor(dx, dy: LongInt); |
167 procedure movecursor(dx, dy: LongInt); |
154 function hwSign(r: hwFloat): LongInt; |
168 function hwSign(r: hwFloat): LongInt; inline; |
155 function Min(a, b: LongInt): LongInt; |
169 function Min(a, b: LongInt): LongInt; inline; |
156 function Max(a, b: LongInt): LongInt; |
170 function Max(a, b: LongInt): LongInt; inline; |
157 procedure OutError(Msg: shortstring; isFatalError: boolean); |
171 procedure OutError(Msg: shortstring; isFatalError: boolean); |
158 procedure TryDo(Assert: boolean; Msg: shortstring; isFatal: boolean); inline; |
172 procedure TryDo(Assert: boolean; Msg: shortstring; isFatal: boolean); inline; |
159 procedure SDLTry(Assert: boolean; isFatal: boolean); |
173 procedure SDLTry(Assert: boolean; isFatal: boolean); |
160 function IntToStr(n: LongInt): shortstring; |
174 function IntToStr(n: LongInt): shortstring; |
161 function FloatToStr(n: hwFloat): shortstring; |
175 function FloatToStr(n: hwFloat): shortstring; |
162 function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat; |
176 function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat; |
163 function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; |
177 function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; |
164 function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; |
178 function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; |
|
179 (* |
165 procedure AdjustColor(var Color: Longword); |
180 procedure AdjustColor(var Color: Longword); |
166 procedure SetKB(n: Longword); |
181 procedure SetKB(n: Longword); |
|
182 *) |
167 procedure SendKB; |
183 procedure SendKB; |
168 procedure SetLittle(var r: hwFloat); |
184 procedure SetLittle(var r: hwFloat); |
169 procedure SendStat(sit: TStatInfoType; s: shortstring); |
185 procedure SendStat(sit: TStatInfoType; s: shortstring); |
170 function Str2PChar(const s: shortstring): PChar; |
186 function Str2PChar(const s: shortstring): PChar; |
171 function NewTexture(width, height: Longword; buf: Pointer): PTexture; |
187 function NewTexture(width, height: Longword; buf: Pointer): PTexture; |
172 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
188 function Surface2Tex(surf: PSDL_Surface; enableClamp: boolean): PTexture; |
173 procedure FreeTexture(tex: PTexture); |
189 procedure FreeTexture(tex: PTexture); |
174 function toPowerOf2(i: Longword): Longword; inline; |
190 function toPowerOf2(i: Longword): Longword; inline; |
175 function DecodeBase64(s: shortstring): shortstring; |
191 function DecodeBase64(s: shortstring): shortstring; |
176 function doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface; |
192 function doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface; |
177 function endian(independent: LongWord): LongWord; |
193 function endian(independent: LongWord): LongWord; inline; |
178 {$IFDEF DEBUGFILE} |
194 {$IFDEF DEBUGFILE} |
179 procedure AddFileLog(s: shortstring); |
195 procedure AddFileLog(s: shortstring); |
180 function RectToStr(Rect: TSDL_Rect): shortstring; |
196 (* function RectToStr(Rect: TSDL_Rect): shortstring; *) |
181 {$ENDIF} |
197 {$ENDIF} |
182 procedure MakeScreenshot(filename: shortstring); |
198 procedure MakeScreenshot(filename: shortstring); |
183 |
199 |
184 implementation |
200 implementation |
185 uses uConsole, uStore, uIO, uSound, typinfo, sysutils; |
201 uses uConsole, uStore, uIO, uSound, typinfo, sysutils, uMobile; |
186 |
202 |
187 var KBnum: Longword; |
203 var KBnum: Longword; |
188 {$IFDEF DEBUGFILE} |
204 {$IFDEF DEBUGFILE} |
189 f: textfile; |
205 f: textfile; |
190 {$ENDIF} |
206 {$ENDIF} |
455 |
479 |
456 Surface2Tex^.w:= surf^.w; |
480 Surface2Tex^.w:= surf^.w; |
457 Surface2Tex^.h:= surf^.h; |
481 Surface2Tex^.h:= surf^.h; |
458 |
482 |
459 if (surf^.format^.BytesPerPixel <> 4) then |
483 if (surf^.format^.BytesPerPixel <> 4) then |
460 begin |
484 begin |
461 TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true); |
485 TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true); |
462 Surface2Tex^.id:= 0; |
486 Surface2Tex^.id:= 0; |
463 exit |
487 exit |
464 end; |
488 end; |
465 |
489 |
466 |
490 |
467 glGenTextures(1, @Surface2Tex^.id); |
491 glGenTextures(1, @Surface2Tex^.id); |
468 |
492 |
469 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
493 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
470 |
494 |
471 if SDL_MustLock(surf) then |
495 if SDL_MustLock(surf) then |
472 SDLTry(SDL_LockSurface(surf) >= 0, true); |
496 SDLTry(SDL_LockSurface(surf) >= 0, true); |
473 |
497 |
474 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then |
498 if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then |
475 begin |
499 begin |
476 tw:= toPowerOf2(Surf^.w); |
500 tw:= toPowerOf2(Surf^.w); |
477 th:= toPowerOf2(Surf^.h); |
501 th:= toPowerOf2(Surf^.h); |
478 |
502 |
479 Surface2Tex^.rx:= Surf^.w / tw; |
503 Surface2Tex^.rx:= Surf^.w / tw; |
480 Surface2Tex^.ry:= Surf^.h / th; |
504 Surface2Tex^.ry:= Surf^.h / th; |
481 |
505 |
482 GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel); |
506 GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel); |
483 |
507 |
484 fromP4:= Surf^.pixels; |
508 fromP4:= Surf^.pixels; |
485 toP4:= tmpp; |
509 toP4:= tmpp; |
486 |
510 |
487 for y:= 0 to Pred(Surf^.h) do |
511 for y:= 0 to Pred(Surf^.h) do |
488 begin |
512 begin |
489 for x:= 0 to Pred(Surf^.w) do toP4^[x]:= fromP4^[x]; |
513 for x:= 0 to Pred(Surf^.w) do toP4^[x]:= fromP4^[x]; |
490 for x:= Surf^.w to Pred(tw) do toP4^[x]:= 0; |
514 for x:= Surf^.w to Pred(tw) do toP4^[x]:= 0; |
491 toP4:= @(toP4^[tw]); |
515 toP4:= @(toP4^[tw]); |
492 fromP4:= @(fromP4^[Surf^.pitch div 4]); |
516 fromP4:= @(fromP4^[Surf^.pitch div 4]) |
493 end; |
517 end; |
494 |
518 |
495 for y:= Surf^.h to Pred(th) do |
519 for y:= Surf^.h to Pred(th) do |
496 begin |
520 begin |
497 for x:= 0 to Pred(tw) do toP4^[x]:= 0; |
521 for x:= 0 to Pred(tw) do toP4^[x]:= 0; |
498 toP4:= @(toP4^[tw]); |
522 toP4:= @(toP4^[tw]) |
499 end; |
523 end; |
500 |
524 |
501 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp); |
525 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp); |
502 |
526 |
503 FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
527 FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
504 end |
528 end |
505 else |
529 else |
506 begin |
530 begin |
507 Surface2Tex^.rx:= 1.0; |
531 Surface2Tex^.rx:= 1.0; |
508 Surface2Tex^.ry:= 1.0; |
532 Surface2Tex^.ry:= 1.0; |
509 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels); |
533 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels); |
510 end; |
534 end; |
511 |
535 |
512 ResetVertexArrays(Surface2Tex); |
536 ResetVertexArrays(Surface2Tex); |
513 |
537 |
514 if SDL_MustLock(surf) then |
538 if SDL_MustLock(surf) then |
515 SDL_UnlockSurface(surf); |
539 SDL_UnlockSurface(surf); |
636 procedure AddFileLog(s: shortstring); |
660 procedure AddFileLog(s: shortstring); |
637 begin |
661 begin |
638 writeln(f, GameTicks: 6, ': ', s); |
662 writeln(f, GameTicks: 6, ': ', s); |
639 flush(f) |
663 flush(f) |
640 end; |
664 end; |
641 |
665 (* |
642 function RectToStr(Rect: TSDL_Rect): shortstring; |
666 function RectToStr(Rect: TSDL_Rect): shortstring; |
643 begin |
667 begin |
644 RectToStr:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')' |
668 RectToStr:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')' |
645 end; |
669 end; |
|
670 *) |
646 {$ENDIF} |
671 {$ENDIF} |
647 |
672 |
648 function doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface; |
673 function doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface; |
649 {* for more information http://www.idevgames.com/forum/showpost.php?p=85864&postcount=7 *} |
674 {* for more information http://www.idevgames.com/forum/showpost.php?p=85864&postcount=7 *} |
650 var convertedSurf: PSDL_Surface = nil; |
675 var convertedSurf: PSDL_Surface = nil; |
673 |
698 |
674 |
699 |
675 procedure initModule; |
700 procedure initModule; |
676 {$IFDEF DEBUGFILE}{$IFNDEF IPHONEOS}var i: LongInt;{$ENDIF}{$ENDIF} |
701 {$IFDEF DEBUGFILE}{$IFNDEF IPHONEOS}var i: LongInt;{$ENDIF}{$ENDIF} |
677 begin |
702 begin |
|
703 Pathz:= cPathz; |
|
704 {* REFERENCE |
|
705 4096 -> $FFFFF000 |
|
706 2048 -> $FFFFF800 |
|
707 1024 -> $FFFFFC00 |
|
708 512 -> $FFFFFE00 *} |
|
709 if (cReducedQuality and rqLowRes) <> 0 then |
|
710 begin |
|
711 LAND_WIDTH:= 2048; |
|
712 LAND_HEIGHT:= 1024; |
|
713 LAND_WIDTH_MASK:= $FFFFF800; |
|
714 LAND_HEIGHT_MASK:= $FFFFFC00; |
|
715 end |
|
716 else |
|
717 begin |
|
718 LAND_WIDTH:= 4096; |
|
719 LAND_HEIGHT:= 2048; |
|
720 LAND_WIDTH_MASK:= $FFFFF000; |
|
721 LAND_HEIGHT_MASK:= $FFFFF800 |
|
722 end; |
|
723 |
678 cDrownSpeed.QWordValue := 257698038; // 0.06 |
724 cDrownSpeed.QWordValue := 257698038; // 0.06 |
679 cDrownSpeedf := 0.06; |
725 cDrownSpeedf := 0.06; |
680 cMaxWindSpeed.QWordValue:= 1073742; // 0.00025 |
726 cMaxWindSpeed.QWordValue:= 1073742; // 0.00025 |
681 cWindSpeed.QWordValue := 429496; // 0.0001 |
727 cWindSpeed.QWordValue := 429496; // 0.0001 |
682 cWindSpeedf := 0.0001; |
728 cWindSpeedf := 0.0001; |
683 cGravity := cMaxWindSpeed * 2; |
729 cGravity := cMaxWindSpeed * 2; |
684 cGravityf := 0.00025 * 2; |
730 cGravityf := 0.00025 * 2; |
685 cDamageModifier := _1; |
731 cDamageModifier := _1; |
686 TargetPoint := cTargetPointRef; |
732 TargetPoint := cTargetPointRef; |
687 TextureList := nil; |
733 TextureList := nil; |
688 |
734 |
689 // int, longint longword and byte |
735 // int, longint longword and byte |
690 CursorMovementX := 0; |
736 CursorMovementX := 0; |
691 CursorMovementY := 0; |
737 CursorMovementY := 0; |
692 GameTicks := 0; |
738 GameTicks := 0; |
693 TrainingTimeInc := 10000; |
739 TrainingTimeInc := 10000; |
745 isSEBackup := true; |
791 isSEBackup := true; |
746 cSeed := ''; |
792 cSeed := ''; |
747 cVolumeDelta := 0; |
793 cVolumeDelta := 0; |
748 cHasFocus := true; |
794 cHasFocus := true; |
749 cInactDelay := 1250; |
795 cInactDelay := 1250; |
|
796 ReadyTimeLeft := 0; |
750 |
797 |
751 ScreenFade := sfNone; |
798 ScreenFade := sfNone; |
752 |
799 |
753 {$IFDEF SDL13} |
800 {$IFDEF SDL13} |
754 SDLwindow := nil; |
801 SDLwindow := nil; |
755 {$ENDIF} |
802 {$ENDIF} |
|
803 |
|
804 // those values still aren't perfect |
|
805 cLeftScreenBorder:= round(-cMinZoomLevel * cScreenWidth); |
|
806 cRightScreenBorder:= round(cMinZoomLevel * cScreenWidth + LAND_WIDTH); |
|
807 cScreenSpace:= cRightScreenBorder - cLeftScreenBorder; |
|
808 |
|
809 if isPhone() then |
|
810 cMaxCaptions:= 3 |
|
811 else |
|
812 cMaxCaptions:= 4; |
|
813 |
756 {$IFDEF DEBUGFILE} |
814 {$IFDEF DEBUGFILE} |
757 {$I-} |
815 {$I-} |
758 {$IFDEF IPHONEOS} |
816 {$IFDEF IPHONEOS} |
759 Assign(f,'../Documents/debug.txt'); |
817 Assign(f,'../Documents/hw-' + cLogfileBase + '.log'); |
760 Rewrite(f); |
818 Rewrite(f); |
761 {$ELSE} |
819 {$ELSE} |
762 if (ParamStr(1) <> '') and (ParamStr(2) <> '') then |
820 if (ParamStr(1) <> '') and (ParamStr(2) <> '') then |
763 if (ParamCount <> 3) and (ParamCount <> 19) then |
821 if (ParamCount <> 3) and (ParamCount <> cDefaultParamNum) then |
764 begin |
822 begin |
765 for i:= 0 to 7 do |
823 for i:= 0 to 7 do |
766 begin |
824 begin |
767 assign(f, ExtractFileDir(ParamStr(2)) + '/debug' + inttostr(i) + '.txt'); |
825 assign(f, ExtractFileDir(ParamStr(2)) + '/' + cLogfileBase + inttostr(i) + '.log'); |
768 rewrite(f); |
826 rewrite(f); |
769 if IOResult = 0 then break; |
827 if IOResult = 0 then break; |
770 end; |
828 end; |
771 if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr |
829 if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr |
772 end |
830 end |
773 else |
831 else |
774 begin |
832 begin |
775 for i:= 0 to 7 do |
833 for i:= 0 to 7 do |
776 begin |
834 begin |
777 assign(f, ParamStr(1) + '/debug' + inttostr(i) + '.txt'); |
835 assign(f, ParamStr(1) + '/Logs/' + cLogfileBase + inttostr(i) + '.log'); |
778 rewrite(f); |
836 rewrite(f); |
779 if IOResult = 0 then break; |
837 if IOResult = 0 then break; |
780 end; |
838 end; |
781 if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr |
839 if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr |
782 end |
840 end |