fix a nasty 196 bytes memory leak in engine, plus other stuff for iphone frontend
authorkoda
Sun, 24 Jan 2010 13:37:03 +0000
changeset 2705 2b5625c4ec16
parent 2704 51cda17b7c3b
child 2706 935b7d618cf0
fix a nasty 196 bytes memory leak in engine, plus other stuff for iphone frontend
cocoaTouch/GameSetup.m
cocoaTouch/SDLOverrides/SDL_uikitappdelegate.h
cocoaTouch/SDLOverrides/SDL_uikitview.m
hedgewars/hwengine.pas
hedgewars/uLand.pas
hedgewars/uLandObjects.pas
hedgewars/uMisc.pas
hedgewars/uStore.pas
--- a/cocoaTouch/GameSetup.m	Sun Jan 24 07:37:00 2010 +0000
+++ b/cocoaTouch/GameSetup.m	Sun Jan 24 13:37:03 2010 +0000
@@ -48,7 +48,7 @@
 -(void) engineProtocol {
 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 	IPaddress ip;
-	int idx, eProto;
+	int eProto;
 	BOOL clientQuit, serverQuit;
 	char buffer[BUFFER_SIZE], string[BUFFER_SIZE];
 	Uint8 msgSize;
@@ -181,7 +181,6 @@
 			while (!clientQuit){
 				/* Now we can communicate with the client using csd socket
 				 * sd will remain opened waiting other connections */
-				idx = 0;
 				msgSize = 0;
 				memset(buffer, 0, BUFFER_SIZE);
 				memset(string, 0, BUFFER_SIZE);
--- a/cocoaTouch/SDLOverrides/SDL_uikitappdelegate.h	Sun Jan 24 07:37:00 2010 +0000
+++ b/cocoaTouch/SDLOverrides/SDL_uikitappdelegate.h	Sun Jan 24 13:37:03 2010 +0000
@@ -24,7 +24,7 @@
 #import "SDL_video.h"
 
 @interface SDLUIKitDelegate:NSObject<UIApplicationDelegate> {
-    UIWindow *window;
+	UIWindow *window;
 	SDL_WindowID windowID;
 	UITabBarController *controller;
 }
--- a/cocoaTouch/SDLOverrides/SDL_uikitview.m	Sun Jan 24 07:37:00 2010 +0000
+++ b/cocoaTouch/SDLOverrides/SDL_uikitview.m	Sun Jan 24 13:37:03 2010 +0000
@@ -67,6 +67,7 @@
 
 	attackButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 480, 260,50)];
 	[attackButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal];
+	[attackButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateHighlighted];
 	[attackButton addTarget:[self superclass] action:@selector(attackButtonPressed) forControlEvents:UIControlEventTouchDown];
 	[attackButton addTarget:[self superclass] action:@selector(attackButtonReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
 	[self addSubview:attackButton];
--- a/hedgewars/hwengine.pas	Sun Jan 24 07:37:00 2010 +0000
+++ b/hedgewars/hwengine.pas	Sun Jan 24 13:37:03 2010 +0000
@@ -255,7 +255,7 @@
 		if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p];
 		
 	WriteToConsole('Init SDL... ');
-	SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true);
+	SDLTry(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, true);
 	WriteLnToConsole(msgOK);
 
 	SDL_EnableUNICODE(1);
--- a/hedgewars/uLand.pas	Sun Jan 24 07:37:00 2010 +0000
+++ b/hedgewars/uLand.pas	Sun Jan 24 13:37:03 2010 +0000
@@ -314,8 +314,7 @@
 function LandBackPixel(x, y: LongInt): LongWord;
 var p: PLongWordArray;
 begin
-	if LandBackSurface = nil then
-		LandBackPixel:= 0
+	if LandBackSurface = nil then LandBackPixel:= 0
 	else
 	begin
 		p:= LandBackSurface^.pixels;
@@ -328,61 +327,63 @@
     r, rr: TSDL_Rect;
     x, yd, yu: LongInt;
 begin
-tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/LandTex', ifCritical or ifIgnoreCaps);
-r.y:= 0;
-while r.y < LAND_HEIGHT do
+	tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/LandTex', ifCritical or ifIgnoreCaps);
+	r.y:= 0;
+	while r.y < LAND_HEIGHT do
 	begin
-	r.x:= 0;
-	while r.x < LAND_WIDTH do
+		r.x:= 0;
+		while r.x < LAND_WIDTH do
 		begin
-		SDL_UpperBlit(tmpsurf, nil, Surface, @r);
-		inc(r.x, tmpsurf^.w)
+			SDL_UpperBlit(tmpsurf, nil, Surface, @r);
+			inc(r.x, tmpsurf^.w)
 		end;
-	inc(r.y, tmpsurf^.h)
+		inc(r.y, tmpsurf^.h)
 	end;
-SDL_FreeSurface(tmpsurf);
+	SDL_FreeSurface(tmpsurf);
 
-LandBackSurface:= LoadImage(Pathz[ptCurrTheme] + '/LandBackTex', ifIgnoreCaps or ifTransparent);
+	// freed in free_uLand() below
+	LandBackSurface:= LoadImage(Pathz[ptCurrTheme] + '/LandBackTex', ifIgnoreCaps or ifTransparent);
 
-tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/Border', ifCritical or ifIgnoreCaps or ifTransparent);
-for x:= 0 to LAND_WIDTH - 1 do
+	tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/Border', ifCritical or ifIgnoreCaps or ifTransparent);
+	for x:= 0 to LAND_WIDTH - 1 do
 	begin
-	yd:= LAND_HEIGHT - 1;
-	repeat
-		while (yd > 0) and (Land[yd, x] =  0) do dec(yd);
+		yd:= LAND_HEIGHT - 1;
+		repeat
+			while (yd > 0) and (Land[yd, x] =  0) do dec(yd);
 
-		if (yd < 0) then yd:= 0;
+			if (yd < 0) then yd:= 0;
 
-		while (yd < LAND_HEIGHT) and (Land[yd, x] <> 0) do inc(yd);
-		dec(yd);
-		yu:= yd;
+			while (yd < LAND_HEIGHT) and (Land[yd, x] <> 0) do inc(yd);
+			dec(yd);
+			yu:= yd;
 
-		while (yu > 0  ) and (Land[yu, x] <> 0) do dec(yu);
-		while (yu < yd ) and (Land[yu, x] =  0) do inc(yu);
+			while (yu > 0  ) and (Land[yu, x] <> 0) do dec(yu);
+			while (yu < yd ) and (Land[yu, x] =  0) do inc(yu);
 
-		if (yd < LAND_HEIGHT - 1) and ((yd - yu) >= 16) then
+			if (yd < LAND_HEIGHT - 1) and ((yd - yu) >= 16) then
 			begin
-			rr.x:= x;
-			rr.y:= yd - 15;
-			r.x:= x mod tmpsurf^.w;
-			r.y:= 16;
-			r.w:= 1;
-			r.h:= 16;
-			SDL_UpperBlit(tmpsurf, @r, Surface, @rr);
+				rr.x:= x;
+				rr.y:= yd - 15;
+				r.x:= x mod tmpsurf^.w;
+				r.y:= 16;
+				r.w:= 1;
+				r.h:= 16;
+				SDL_UpperBlit(tmpsurf, @r, Surface, @rr);
 			end;
-		if (yu > 0) then
+			if (yu > 0) then
 			begin
-			rr.x:= x;
-			rr.y:= yu;
-			r.x:= x mod tmpsurf^.w;
-			r.y:= 0;
-			r.w:= 1;
-			r.h:= min(16, yd - yu + 1);
-			SDL_UpperBlit(tmpsurf, @r, Surface, @rr);
+				rr.x:= x;
+				rr.y:= yu;
+				r.x:= x mod tmpsurf^.w;
+				r.y:= 0;
+				r.w:= 1;
+				r.h:= min(16, yd - yu + 1);
+				SDL_UpperBlit(tmpsurf, @r, Surface, @rr);
 			end;
-		yd:= yu - 1;
-	until yd < 0;
+			yd:= yu - 1;
+		until yd < 0;
 	end;
+	SDL_FreeSurface(tmpsurf);
 end;
 
 procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr);
@@ -638,23 +639,19 @@
 procedure GenLandSurface;
 var tmpsurf: PSDL_Surface;
 begin
-WriteLnToConsole('Generating land...');
-
-GenBlank(EdgeTemplates[SelectTemplate]);
+	WriteLnToConsole('Generating land...');
+	GenBlank(EdgeTemplates[SelectTemplate]);
+	AddProgress();
 
-AddProgress;
-
-tmpsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, LAND_WIDTH, LAND_HEIGHT, 32, RMask, GMask, BMask, 0);
+	tmpsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, LAND_WIDTH, LAND_HEIGHT, 32, RMask, GMask, BMask, 0);
 
-TryDo(tmpsurf <> nil, 'Error creating pre-land surface', true);
-ColorizeLand(tmpsurf);
-AddOnLandObjects(tmpsurf);
+	TryDo(tmpsurf <> nil, 'Error creating pre-land surface', true);
+	ColorizeLand(tmpsurf);
+	AddOnLandObjects(tmpsurf);
 
-LandSurface2LandPixels(tmpsurf);
-SDL_FreeSurface(tmpsurf);
-
-AddProgress;
-
+	LandSurface2LandPixels(tmpsurf);
+	SDL_FreeSurface(tmpsurf);
+	AddProgress();
 end;
 
 procedure MakeFortsMap;
@@ -689,34 +686,34 @@
     p: PLongwordArray;
     x, y, cpX, cpY: Longword;
 begin
-tmpsurf:= LoadImage(Pathz[ptMapCurrent] + '/mask', ifAlpha or ifTransparent or ifIgnoreCaps);
-if (tmpsurf <> nil) and (tmpsurf^.w <= LAND_WIDTH) and (tmpsurf^.h <= LAND_HEIGHT) and (tmpsurf^.format^.BytesPerPixel = 4) then
-    begin
-	cpX:= (LAND_WIDTH - tmpsurf^.w) div 2;
-	cpY:= LAND_HEIGHT - tmpsurf^.h;
-    if SDL_MustLock(tmpsurf) then
-       SDLTry(SDL_LockSurface(tmpsurf) >= 0, true);
+	tmpsurf:= LoadImage(Pathz[ptMapCurrent] + '/mask', ifAlpha or ifTransparent or ifIgnoreCaps);
+	if (tmpsurf <> nil) and (tmpsurf^.w <= LAND_WIDTH) and (tmpsurf^.h <= LAND_HEIGHT) and (tmpsurf^.format^.BytesPerPixel = 4) then
+	begin
+		cpX:= (LAND_WIDTH - tmpsurf^.w) div 2;
+		cpY:= LAND_HEIGHT - tmpsurf^.h;
+		if SDL_MustLock(tmpsurf) then
+			SDLTry(SDL_LockSurface(tmpsurf) >= 0, true);
 
-    p:= tmpsurf^.pixels;
-    for y:= 0 to Pred(tmpsurf^.h) do
-        begin
-        for x:= 0 to Pred(tmpsurf^.w) do
-            begin
-            if ((AMask and p^[x]) = 0) then  // Tiy was having trouble generating transparent black
-                Land[cpY + y, cpX + x]:= 0
-            else if p^[x] = (AMask or RMask) then
-                Land[cpY + y, cpX + x]:= COLOR_INDESTRUCTIBLE
-            else if p^[x] = $FFFFFFFF then
-                Land[cpY + y, cpX + x]:= COLOR_LAND;
+			p:= tmpsurf^.pixels;
+			for y:= 0 to Pred(tmpsurf^.h) do
+			begin
+				for x:= 0 to Pred(tmpsurf^.w) do
+				begin
+					if ((AMask and p^[x]) = 0) then  // Tiy was having trouble generating transparent black
+						Land[cpY + y, cpX + x]:= 0
+					else if p^[x] = (AMask or RMask) then
+						Land[cpY + y, cpX + x]:= COLOR_INDESTRUCTIBLE
+					else if p^[x] = $FFFFFFFF then
+						Land[cpY + y, cpX + x]:= COLOR_LAND;
+				end;
+				p:= @(p^[tmpsurf^.pitch div 4]);
+			end;
 
-            end;
-        p:= @(p^[tmpsurf^.pitch div 4]);
-        end;
-
-    if SDL_MustLock(tmpsurf) then
-       SDL_UnlockSurface(tmpsurf);
-    SDL_FreeSurface(tmpsurf);
-    end;
+		if SDL_MustLock(tmpsurf) then
+			SDL_UnlockSurface(tmpsurf);
+	end;
+	if (tmpsurf <> nil) then 
+		SDL_FreeSurface(tmpsurf);
 end;
 
 procedure LoadMap;
@@ -737,7 +734,7 @@
 Readln(f);
 if not eof(f) then Readln(f, MaxHedgehogs);
 
-if(MaxHedgehogs = 0) then MaxHedgehogs:= 18;
+if (MaxHedgehogs = 0) then MaxHedgehogs:= 18;
 
 playHeight:= tmpsurf^.h;
 playWidth:= tmpsurf^.w;
--- a/hedgewars/uLandObjects.pas	Sun Jan 24 07:37:00 2010 +0000
+++ b/hedgewars/uLandObjects.pas	Sun Jan 24 13:37:03 2010 +0000
@@ -122,7 +122,7 @@
 
 procedure FreeRects;
 begin
-Dispose(rects)
+	Dispose(rects)
 end;
 
 function CheckIntersect(x1, y1, w1, h1: LongInt): boolean;
@@ -502,8 +502,8 @@
     until (i>rightX-int);
     end;
 AddThemeObjects(ThemeObjects, (8 * MaxHedgehogs) div 18); // MaxHedgehogs should roughly correspond to available surface area.  Was also thinking maybe using playHeight * playWidth div constant   :)
-AddProgress;
-FreeRects
+AddProgress();
+FreeRects();
 end;
 
 procedure AddOnLandObjects(Surface: PSDL_Surface);
--- a/hedgewars/uMisc.pas	Sun Jan 24 07:37:00 2010 +0000
+++ b/hedgewars/uMisc.pas	Sun Jan 24 13:37:03 2010 +0000
@@ -530,9 +530,10 @@
 	begin
 		convertedSurf:= SDL_ConvertSurface(tmpsurf, @conversionFormat, SDL_SWSURFACE);
 		SDL_FreeSurface(tmpsurf);
-		doSurfaceConversion:= convertedSurf
-	end
-	else doSurfaceConversion:= tmpsurf;
+		exit(convertedSurf);
+	end;
+
+	exit(tmpsurf);
 end;
 
 function endian(independent: LongWord): LongWord;
--- a/hedgewars/uStore.pas	Sun Jan 24 07:37:00 2010 +0000
+++ b/hedgewars/uStore.pas	Sun Jan 24 13:37:03 2010 +0000
@@ -1001,9 +1001,10 @@
 procedure RenderHealth(var Hedgehog: THedgehog);
 var s: shortstring;
 begin
-str(Hedgehog.Gear^.Health, s);
-if Hedgehog.HealthTagTex <> nil then FreeTexture(Hedgehog.HealthTagTex);
-Hedgehog.HealthTagTex:= RenderStringTex(s, Hedgehog.Team^.Clan^.Color, fnt16)
+	str(Hedgehog.Gear^.Health, s);
+	if Hedgehog.HealthTagTex <> nil then
+		FreeTexture(Hedgehog.HealthTagTex);
+	Hedgehog.HealthTagTex:= RenderStringTex(s, Hedgehog.Team^.Clan^.Color, fnt16)
 end;
 
 function  LoadImage(const filename: string; imageFlags: LongInt): PSDL_Surface;