GCI2012: Convert uMobile into a Callback Record
authorRowan D
Mon, 03 Dec 2012 17:51:57 +0100
changeset 8204 9a6030d96273
parent 8203 2c98ec41697a
child 8205 921e63507bea
GCI2012: Convert uMobile into a Callback Record
hedgewars/ArgParsers.inc
hedgewars/CMakeLists.txt
hedgewars/GSHandlers.inc
hedgewars/VGSHandlers.inc
hedgewars/uConsts.pas
hedgewars/uGame.pas
hedgewars/uGears.pas
hedgewars/uMobile.pas
hedgewars/uStore.pas
hedgewars/uTouch.pas
hedgewars/uTypes.pas
hedgewars/uUtils.pas
hedgewars/uVariables.pas
hedgewars/uVisualGears.pas
hedgewars/uWorld.pas
--- a/hedgewars/ArgParsers.inc	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/ArgParsers.inc	Mon Dec 03 17:51:57 2012 +0100
@@ -202,8 +202,6 @@
             or (cmdArray[index] = '--lowquality');
         if (not isBool) or ((ParamStr(paramIndex)='1') and (cmdArray[index]<>'--nomusic') and (cmdArray[index]<>'--nosound')) then
             parseParameter(cmdArray[index], ParamStr(paramIndex), tmpInt);
-        //if isBool then
-        //  paramIndex:= paramIndex+1;
         index:= index+1;
         end;
 end;
--- a/hedgewars/CMakeLists.txt	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/CMakeLists.txt	Mon Dec 03 17:51:57 2012 +0100
@@ -59,7 +59,6 @@
     uLandTexture.pas
     uLocale.pas
     uMisc.pas
-    uMobile.pas
     uPhysFSLayer.pas
     uRandom.pas
     uRender.pas
--- a/hedgewars/GSHandlers.inc	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/GSHandlers.inc	Mon Dec 03 17:51:57 2012 +0100
@@ -2223,7 +2223,9 @@
         begin
         doMakeExplosion(hwRound(Gear^.X), hwRound(Gear^.Y), 30, Gear^.Hedgehog, EXPLAutoSound);
         DeleteGear(Gear);
-        performRumble();
+        with mobileRecord do
+            if (performRumble <> nil) and (not fastUntilLag) then
+                performRumble(kSystemSoundID_Vibrate);
         exit
         end;
     if (GameTicks and $3F) = 0 then
@@ -4308,7 +4310,9 @@
     Gear^.dY.isNegative := not Gear^.dY.isNegative;
 
     Gear^.doStep := @doStepSineGunShotWork;
-    performRumble();
+    with mobileRecord do
+        if (performRumble <> nil) and (not fastUntilLag) then
+            performRumble(kSystemSoundID_Vibrate);
 end;
 
 ////////////////////////////////////////////////////////////////////////////////
--- a/hedgewars/VGSHandlers.inc	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/VGSHandlers.inc	Mon Dec 03 17:51:57 2012 +0100
@@ -697,7 +697,9 @@
 Gear^.doStep:= @doStepBigExplosionWork;
 if Steps > 1 then
     Gear^.doStep(Gear, Steps-1);
-performRumble();
+with mobileRecord do
+    if (performRumble <> nil) and (not fastUntilLag) then
+        performRumble(kSystemSoundID_Vibrate);
 end;
 
 procedure doStepChunk(Gear: PVisualGear; Steps: Longword);
--- a/hedgewars/uConsts.pas	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/uConsts.pas	Mon Dec 03 17:51:57 2012 +0100
@@ -275,6 +275,8 @@
     NoPointX = Low(LongInt);
     cTargetPointRef : TPoint = (X: NoPointX; Y: 0);
 
+    kSystemSoundID_Vibrate = $00000FFF;
+
 implementation
 
 end.
--- a/hedgewars/uGame.pas	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/uGame.pas	Mon Dec 03 17:51:57 2012 +0100
@@ -26,7 +26,7 @@
 ////////////////////
     implementation
 ////////////////////
-uses uInputHandler, uTeams, uIO, uAI, uGears, uSound, uMobile, 
+uses uInputHandler, uTeams, uIO, uAI, uGears, uSound, 
     uVisualGears, uTypes, uVariables, uCommands, uConsts
     {$IFDEF USE_TOUCH_INTERFACE}, uTouch{$ENDIF};
 
@@ -96,7 +96,9 @@
                         AddVisualGear(0, 0, vgtTeamHealthSorter);
                         AddVisualGear(0, 0, vgtSmoothWindBar);
                         {$IFDEF IPHONEOS}InitIPC;{$ENDIF}
-                        uMobile.SaveLoadingEnded();
+                        with mobileRecord do
+                            if SaveLoadingEnded <> nil then
+                                SaveLoadingEnded();
                         end;
                 end
         else ProcessGears
--- a/hedgewars/uGears.pas	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/uGears.pas	Mon Dec 03 17:51:57 2012 +0100
@@ -57,7 +57,7 @@
 
 implementation
 uses uStore, uSound, uTeams, uRandom, uCollisions, uIO, uLandGraphics,
-    uLocale, uAI, uAmmos, uStats, uVisualGears, uScript, GLunit, uMobile, uVariables,
+    uLocale, uAI, uAmmos, uStats, uVisualGears, uScript, GLunit, uVariables,
     uCommands, uUtils, uTextures, uRenderUtils, uGearsRender, uCaptions, uDebug, uLandTexture,
     uGearsHedgehog, uGearsUtils, uGearsList, uGearsHandlers, uGearsHandlersRope;
 
--- a/hedgewars/uMobile.pas	Mon Dec 03 17:49:02 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,110 +0,0 @@
-(*
- * Hedgewars, a free turn based strategy game
- * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
- *)
-
-{$INCLUDE "options.inc"}
-
-(*
- * This unit contains a lot of useful functions when hw is running on mobile
- * Unlike HwLibrary when you declare functions that you will call from your code,
- * here you need to provide functions that Pascall code will call.
- *)
-
-unit uMobile;
-interface
-
-function  isPhone: Boolean; inline;
-function  getScreenDPI: Double; inline;
-procedure performRumble; inline;
-
-procedure GameLoading; inline;
-procedure GameLoaded; inline;
-procedure SaveLoadingEnded; inline;
-
-implementation
-uses uVariables, uConsole, SDLh;
-
-// add here any external call that you need
-{$IFDEF IPHONEOS}
-(*  iOS calls written in ObjcExports.m  *)
-procedure startLoadingIndicator; cdecl; external;
-procedure stopLoadingIndicator; cdecl; external;
-procedure saveFinishedSynching; cdecl; external;
-function  isApplePhone: Boolean; cdecl; external;
-procedure AudioServicesPlaySystemSound(num: LongInt); cdecl; external;
-{$ENDIF}
-
-// this function is just to determine whether we are running on a limited screen device
-function isPhone: Boolean; inline;
-begin
-    isPhone:= false;
-{$IFDEF IPHONEOS}
-    isPhone:= isApplePhone();
-{$ENDIF}
-{$IFDEF ANDROID}
-    //nasty nasty hack. TODO: implement callback to java to have a unified way of determining if it is a tablet
-    if (cScreenWidth < 1000) and (cScreenHeight < 500) then
-        isPhone:= true;
-{$ENDIF}
-end;
-
-function getScreenDPI: Double; inline;
-begin
-{$IFDEF ANDROID}
-//    getScreenDPI:= Android_JNI_getDensity();
-    getScreenDPI:= 1;
-{$ELSE}
-    getScreenDPI:= 1;
-{$ENDIF}
-end;
-
-// this function should make the device vibrate in some way
-procedure PerformRumble; inline;
-{$IFDEF IPHONEOS}const kSystemSoundID_Vibrate = $00000FFF;{$ENDIF}
-begin
-    // do not vibrate while synchronising a demo/save
-    if not fastUntilLag then
-    begin
-{$IFDEF IPHONEOS}
-        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
-{$ENDIF}
-    end;
-end;
-
-procedure GameLoading; inline;
-begin
-{$IFDEF IPHONEOS}
-    startLoadingIndicator();
-{$ENDIF}
-end;
-
-procedure GameLoaded; inline;
-begin
-{$IFDEF IPHONEOS}
-    stopLoadingIndicator();
-{$ENDIF}
-end;
-
-procedure SaveLoadingEnded; inline;
-begin
-{$IFDEF IPHONEOS}
-    saveFinishedSynching();
-{$ENDIF}
-end;
-
-
-end.
--- a/hedgewars/uStore.pas	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/uStore.pas	Mon Dec 03 17:51:57 2012 +0100
@@ -56,7 +56,7 @@
 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
 
 implementation
-uses uMisc, uConsole, uMobile, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands
+uses uMisc, uConsole, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands
     , uPhysFSLayer
     , uDebug
     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
@@ -886,8 +886,10 @@
         squaresize:= texsurf^.w shr 1;
         numsquares:= texsurf^.h div squaresize;
         SDL_FreeSurface(texsurf);
-
-        uMobile.GameLoading();
+        with mobileRecord do
+            if GameLoading <> nil then
+                GameLoading();
+        
         end;
 
     TryDo(ProgrTex <> nil, 'Error - Progress Texure is nil!', true);
@@ -910,7 +912,9 @@
 
 procedure FinishProgress;
 begin
-    uMobile.GameLoaded();
+    with mobileRecord do
+        if GameLoaded <> nil then
+            GameLoaded();
     WriteLnToConsole('Freeing progress surface... ');
     FreeTexture(ProgrTex);
     ProgrTex:= nil;
--- a/hedgewars/uTouch.pas	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/uTouch.pas	Mon Dec 03 17:51:57 2012 +0100
@@ -22,7 +22,7 @@
 
 interface
 
-uses SysUtils, uConsole, uVariables, SDLh, uFloat, uConsts, uCommands, GLUnit, uTypes, uCaptions, uAmmos, uWorld, uMobile;
+uses SysUtils, uConsole, uVariables, SDLh, uFloat, uConsts, uCommands, GLUnit, uTypes, uCaptions, uAmmos, uWorld;
 
 
 procedure initModule;
@@ -558,7 +558,7 @@
     isOnCrosshair:= isOnRect((x-HalfRectSize), (y-HalfRectSize), RectSize, RectSize, finger);
     printFinger(finger);
     WriteLnToConsole(inttostr(finger.x) + '   ' + inttostr(x));
-    WriteLnToConsole(inttostr(x) + '  ' + inttostr(y) + '   ' + inttostr(round(uMobile.getScreenDPI * 10)));
+    WriteLnToConsole(inttostr(x) + '  ' + inttostr(y) + '   ' + inttostr(round(mobileRecord.getScreenDPI() * 10)));
 end;
 
 function isOnCurrentHog(finger: TTouch_Data): boolean;
@@ -640,7 +640,7 @@
     for index := 0 to High(fingers) do 
         fingers[index].id := nilFingerId;
 
-    rectSize:= round(baseRectSize * uMobile.getScreenDPI);
+    rectSize:= round(baseRectSize * mobileRecord.getScreenDPI());
     halfRectSize:= rectSize shl 1;
 end;
 
--- a/hedgewars/uTypes.pas	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/uTypes.pas	Mon Dec 03 17:51:57 2012 +0100
@@ -399,6 +399,18 @@
             Flawless: boolean;
             end;
 
+     cdeclPtr = procedure; cdecl;
+     cdeclIntPtr = procedure(num: LongInt); cdecl;
+     functionDoublePtr = function: Double;
+
+     TMobileRecord = record
+                     getScreenDPI: functionDoublePtr;
+                     PerformRumble: cdeclIntPtr;
+                     GameLoading: cdeclPtr;
+                     GameLoaded: cdeclPtr;
+                     SaveLoadingEnded: cdeclPtr;
+                     end;
+
      TAmmoStrId = (sidGrenade, sidClusterBomb, sidBazooka, sidBee, sidShotgun,
             sidPickHammer, sidSkip, sidRope, sidMine, sidDEagle,
             sidDynamite, sidBaseballBat, sidFirePunch, sidSeconds,
--- a/hedgewars/uUtils.pas	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/uUtils.pas	Mon Dec 03 17:51:57 2012 +0100
@@ -73,6 +73,17 @@
 procedure WriteLn(var f: textfile; s: shortstring);
 {$ENDIF}
 
+function  isPhone: Boolean; inline;
+function  getScreenDPI: Double; inline; //cdecl; external;
+
+{$IFDEF IPHONEOS}
+procedure startLoadingIndicator; cdecl; external;
+procedure stopLoadingIndicator; cdecl; external;
+procedure saveFinishedSynching; cdecl; external;
+function  isApplePhone: Boolean; cdecl; external;
+procedure AudioServicesPlaySystemSound(num: LongInt); cdecl; external;
+{$ENDIF}
+
 procedure initModule(isNotPreview: boolean);
 procedure freeModule;
 
@@ -401,6 +412,31 @@
 end;
 {$ENDIF}
 
+// this function is just to determine whether we are running on a limited screen device
+function isPhone: Boolean; inline;
+begin
+    isPhone:= false;
+{$IFDEF IPHONEOS}
+    isPhone:= isApplePhone();
+{$ENDIF}
+{$IFDEF ANDROID}
+    //nasty nasty hack. TODO: implement callback to java to have a unified way of determining if it is a tablet
+    if (cScreenWidth < 1000) and (cScreenHeight < 500) then
+        isPhone:= true;
+{$ENDIF}
+end;
+
+//This dummy function should be reimplemented (externally).
+function getScreenDPI: Double; inline;
+begin
+{$IFDEF ANDROID}
+//    getScreenDPI:= Android_JNI_getDensity();
+    getScreenDPI:= 1;
+{$ELSE}
+    getScreenDPI:= 1;
+{$ENDIF}
+end;
+
 procedure initModule(isNotPreview: boolean);
 {$IFDEF DEBUGFILE}
 var logfileBase: shortstring;
--- a/hedgewars/uVariables.pas	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/uVariables.pas	Mon Dec 03 17:51:57 2012 +0100
@@ -21,7 +21,7 @@
 unit uVariables;
 interface
 
-uses SDLh, uTypes, uFloat, GLunit, uConsts, Math, uMobile, uUtils;
+uses SDLh, uTypes, uFloat, GLunit, uConsts, Math, uUtils;
 
 var
 /////// init flags ///////
@@ -197,6 +197,8 @@
 
     LastVoice : TVoice = ( snd: sndNone; voicepack: nil );
 
+    mobileRecord: TMobileRecord;
+
 /////////////////////////////////////
 //Buttons
 {$IFDEF USE_TOUCH_INTERFACE}
@@ -2519,6 +2521,19 @@
     cMapName:= '';
 
     LuaTemplateNumber:= 0;
+
+    mobileRecord.getScreenDPI:= @getScreenDPI; //TODO: define external function.
+    {$IFDEF IPHONEOS}
+    mobileRecord.PerformRumble:= @AudioServicesPlaySystemSound;
+    mobileRecord.GameLoading:= @startLoadingIndicator;
+    mobileRecord.GameLoaded:= @stopLoadingIndicator;
+    mobileRecord.SaveLoadingEnded:= @saveFinishedSynching;
+    {$ELSE}
+    mobileRecord.PerformRumble:= nil;
+    mobileRecord.GameLoading:= nil;
+    mobileRecord.GameLoaded:= nil;
+    mobileRecord.SaveLoadingEnded:= nil;
+    {$ENDIF}
 end;
 
 procedure freeModule;
--- a/hedgewars/uVisualGears.pas	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/uVisualGears.pas	Mon Dec 03 17:51:57 2012 +0100
@@ -53,7 +53,7 @@
 procedure KickFlakes(Radius, X, Y: LongInt);
 
 implementation
-uses uSound, uMobile, uVariables, uTextures, uRender, Math, uRenderUtils, uStore, uUtils;
+uses uSound, uVariables, uTextures, uRender, Math, uRenderUtils, uStore, uUtils;
 
 const 
     cExplFrameTicks = 110;
--- a/hedgewars/uWorld.pas	Mon Dec 03 17:49:02 2012 +0100
+++ b/hedgewars/uWorld.pas	Mon Dec 03 17:51:57 2012 +0100
@@ -60,7 +60,6 @@
     , uCaptions
     , uCursor
     , uCommands
-    , uMobile
 {$IFDEF USE_VIDEO_RECORDING}    
     , uVideoRec
 {$ENDIF}    
@@ -246,7 +245,7 @@
 {$IFDEF USE_TOUCH_INTERFACE}
 
 //positioning of the buttons
-buttonScale:= uMobile.getScreenDPI/cDefaultZoomLevel;
+buttonScale:= mobileRecord.getScreenDPI()/cDefaultZoomLevel;
 
 
 with JumpWidget do