further code cleanup and less redundancy
authorkoda
Sat, 03 Jul 2010 04:09:27 +0200
changeset 3615 b78d7959540a
parent 3614 5806f8f614b7
child 3616 85d69ddb41b6
further code cleanup and less redundancy
hedgewars/hwengine.pas
hedgewars/uAI.pas
hedgewars/uGears.pas
hedgewars/uLandTexture.pas
hedgewars/uSound.pas
hedgewars/uVisualGears.pas
--- a/hedgewars/hwengine.pas	Fri Jul 02 21:20:38 2010 -0400
+++ b/hedgewars/hwengine.pas	Sat Jul 03 04:09:27 2010 +0200
@@ -29,52 +29,14 @@
 program hwengine;
 {$ENDIF}
 
-uses
-    SDLh in 'SDLh.pas',
-    uConsts in 'uConsts.pas',
-    uGame in 'uGame.pas',
-    uMisc in 'uMisc.pas',
-    uStore in 'uStore.pas',
-    uWorld in 'uWorld.pas',
-    uIO in 'uIO.pas',
-    uGears in 'uGears.pas',
-    uVisualGears in 'uVisualGears.pas',
-    uConsole in 'uConsole.pas',
-    uKeys in 'uKeys.pas',
-    uTeams in 'uTeams.pas',
-    uSound in 'uSound.pas',
-    uRandom in 'uRandom.pas',
-    uAI in 'uAI.pas',
-    uAIMisc in 'uAIMisc.pas',
-//  uAIAmmoTests in 'uAIAmmoTests.pas',
-//  uAIActions in 'uAIActions.pas',
-    uCollisions in 'uCollisions.pas',
-    uLand in 'uLand.pas',
-//  uLandTemplates in 'uLandTemplates.pas',
-//  uLandObjects in 'uLandObjects.pas',
-//  uLandGraphics in 'uLandGraphics.pas',
-    uLocale in 'uLocale.pas',
-    uAmmos in 'uAmmos.pas',
-//  uSHA in 'uSHA.pas',
-//  uFloat in 'uFloat.pas',
-    uStats in 'uStats.pas',
-    uChat in 'uChat.pas',
-    uLandTexture in 'uLandTexture.pas',
-    uScript in 'uScript.pas',
-    sysutils;
-
-// also: GSHandlers.inc
-//       GearDrawing.inc
-//       CCHandlers.inc
-//       HHHandlers.inc
-//       SinTable.inc
-//       proto.inc
-
+uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uKeys, uSound, 
+     uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uRandom, uLandTexture, uCollisions, sysutils;
+     
+type arrayofpchar = array[0..9] of PChar;
 var isTerminated: boolean = false;
     alsoShutdownFrontend: boolean = false;
+
 {$IFDEF HWLIBRARY}
-type arrayofpchar = array[0..9] of PChar;
-
 procedure initEverything(complete:boolean);
 procedure freeEverything(complete:boolean);
 
@@ -163,12 +125,7 @@
 procedure OnDestroy;
 begin
     WriteLnToConsole('Freeing resources...');
-    if isSoundEnabled then ReleaseSound();
-    FreeActionsList();
     StoreRelease();
-    FreeGearsList();
-    FreeVisualGears();
-    FreeLand();
     ControllerClose();
     SendKB();
     CloseIPC();
@@ -204,7 +161,6 @@
                         cHasFocus:= event.active.gain = 1;
 {$ENDIF}
 {$IFNDEF IPHONEOS}
-                //SDL_VIDEORESIZE: Resize(max(event.resize.w, 600), max(event.resize.h, 450));
                 SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then uKeys.wheelDown:= true;
                 SDL_MOUSEBUTTONUP: if event.button.button = SDL_BUTTON_WHEELUP then uKeys.wheelUp:= true;
 {$ENDIF}
@@ -319,12 +275,12 @@
 
     LoadLocale(Pathz[ptLocale] + '/en.txt');  // Do an initial load with english
     if cLocaleFName <> 'en.txt' then
-        begin
+    begin
         // Try two letter locale first before trying specific locale overrides
         if (Length(cLocaleFName) > 6) and (Copy(cLocaleFName,1,2)+'.txt' <> 'en.txt') then 
             LoadLocale(Pathz[ptLocale] + '/' + Copy(cLocaleFName,1,2)+'.txt');
         LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName);
-        end;
+    end;
 
     if recordFileName = '' then
         SendIPCAndWaitReply('C')        // ask for game config
@@ -349,7 +305,9 @@
     ParseCommand('rotmask', true);
 
     MainLoop();
+    // clean up SDL and GL context
     OnDestroy();
+    // clean up all the other memory allocated
     freeEverything(true);
     if alsoShutdownFrontend then halt;
 end;
@@ -384,7 +342,6 @@
         uLandTexture.initModule;
         //uLocale does not need initialization
         uRandom.initModule; 
-        //uSHA is initialized internally
         uScript.initModule;
         uSound.initModule;
         uStats.initModule;
@@ -400,13 +357,12 @@
     if complete then
     begin
         uWorld.freeModule;
-        uVisualGears.freeModule;    //stub
+        uVisualGears.freeModule;
         uTeams.freeModule;
         uStore.freeModule;          //stub
         uStats.freeModule;          //stub
-        uSound.freeModule;          //stub
+        uSound.freeModule;
         uScript.freeModule;
-        //uSHA does not need to be freed
         uRandom.freeModule;         //stub
         //uLocale does not need to be freed
         //uLandTemplates does not need to be freed
@@ -423,7 +379,7 @@
         uAIMisc.freeModule;         //stub
         //uAIAmmoTests does not need to be freed
         //uAIActions does not need to be freed
-        uAI.freeModule;             //stub
+        uAI.freeModule;
     end;
     
     uIO.freeModule;             //stub
--- a/hedgewars/uAI.pas	Fri Jul 02 21:20:38 2010 -0400
+++ b/hedgewars/uAI.pas	Sat Jul 03 04:09:27 2010 +0200
@@ -358,7 +358,7 @@
 
 procedure freeModule;
 begin
-
+    FreeActionsList();
 end;
 
 end.
--- a/hedgewars/uGears.pas	Fri Jul 02 21:20:38 2010 -0400
+++ b/hedgewars/uGears.pas	Sat Jul 03 04:09:27 2010 +0200
@@ -1745,7 +1745,7 @@
 
 procedure freeModule;
 begin
-
+    FreeGearsList();
 end;
 
 end.
--- a/hedgewars/uLandTexture.pas	Fri Jul 02 21:20:38 2010 -0400
+++ b/hedgewars/uLandTexture.pas	Sat Jul 03 04:09:27 2010 +0200
@@ -26,7 +26,6 @@
 procedure freeModule;
 procedure UpdateLandTexture(X, Width, Y, Height: LongInt);
 procedure DrawLand(dX, dY: LongInt);
-procedure FreeLand;
 
 implementation
 uses uMisc, uLand, uStore, uConsts, GLunit;
@@ -38,10 +37,12 @@
             shouldUpdate: boolean;
             tex: PTexture;
             end;
+
 var LandTextures: array of array of TLandRecord;
     tmpPixels: array [0..TEXSIZE - 1, 0..TEXSIZE - 1] of LongWord;
-LANDTEXARW: LongWord;
+    LANDTEXARW: LongWord;
     LANDTEXARH: LongWord;
+    
 function Pixels(x, y: Longword): Pointer;
 var ty: Longword;
 begin
@@ -119,21 +120,6 @@
 
 end;
 
-procedure FreeLand;
-var x, y: LongInt;
-begin
-    for x:= 0 to LANDTEXARW -1 do
-        for y:= 0 to LANDTEXARH - 1 do
-            with LandTextures[x, y] do
-            begin
-                FreeTexture(tex);
-                tex:= nil;
-            end;
-    if LandBackSurface <> nil then
-        SDL_FreeSurface(LandBackSurface);
-    LandBackSurface:= nil;
-end;
-
 procedure initModule;
 begin
     if (cReducedQuality and rqBlurryLand) = 0 then
@@ -151,7 +137,18 @@
 end;
     
 procedure freeModule;
+var x, y: LongInt;
 begin
+    for x:= 0 to LANDTEXARW -1 do
+        for y:= 0 to LANDTEXARH - 1 do
+            with LandTextures[x, y] do
+            begin
+                FreeTexture(tex);
+                tex:= nil;
+            end;
+    if LandBackSurface <> nil then
+        SDL_FreeSurface(LandBackSurface);
+    LandBackSurface:= nil;
     LandTextures:= nil;
 end;
 end.
--- a/hedgewars/uSound.pas	Fri Jul 02 21:20:38 2010 -0400
+++ b/hedgewars/uSound.pas	Sat Jul 03 04:09:27 2010 +0200
@@ -280,7 +280,8 @@
 
 procedure freeModule;
 begin
-
+    if isSoundEnabled then
+        ReleaseSound();
 end;
 
 end.
--- a/hedgewars/uVisualGears.pas	Fri Jul 02 21:20:38 2010 -0400
+++ b/hedgewars/uVisualGears.pas	Sat Jul 03 04:09:27 2010 +0200
@@ -56,7 +56,6 @@
 procedure DeleteVisualGear(Gear: PVisualGear);
 procedure AddClouds;
 procedure AddDamageTag(X, Y, Damage, Color: LongWord);
-procedure FreeVisualGears;
 
 var VisualGearsList: PVisualGear;
     vobFrameTicks, vobFramesCount, vobCount: Longword;
@@ -475,11 +474,6 @@
     AddVisualGear( - cScreenWidth + i * ((cScreenWidth * 2 + (LAND_WIDTH+256)) div (cCloudsNumber + 1)), LAND_HEIGHT-1184, vgtCloud)
 end;
 
-procedure FreeVisualGears;
-begin
-    while VisualGearsList <> nil do DeleteVisualGear(VisualGearsList);
-end;
-
 procedure initModule;
 begin
     VisualGearsList:= nil;
@@ -487,6 +481,7 @@
 
 procedure freeModule;
 begin
+    while VisualGearsList <> nil do DeleteVisualGear(VisualGearsList);
 end;
 
 end.