move all extra categories in a single source, add caching for HWUtils
authorkoda
Tue, 25 Oct 2011 22:51:10 +0200
changeset 6208 b831679e9467
parent 6207 cbc80c22691c
child 6209 074ab6ebcb3e
move all extra categories in a single source, add caching for HWUtils
project_files/HedgewarsMobile/Classes/AudioManagerController.h
project_files/HedgewarsMobile/Classes/AudioManagerController.m
project_files/HedgewarsMobile/Classes/HWUtils.h
project_files/HedgewarsMobile/Classes/HWUtils.m
project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m
project_files/HedgewarsMobile/ExtraCategories.h
project_files/HedgewarsMobile/ExtraCategories.m
project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj
project_files/HedgewarsMobile/Hedgewars_Prefix.pch
project_files/HedgewarsMobile/SDL-50560ca58f80.patch
--- a/project_files/HedgewarsMobile/Classes/AudioManagerController.h	Tue Oct 25 22:14:55 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/AudioManagerController.h	Tue Oct 25 22:51:10 2011 +0200
@@ -34,6 +34,6 @@
 +(void) playBackSound;
 +(void) playSelectSound;
 
-+(void) cleanupMemory;
++(void) releaseCache;
 
 @end
--- a/project_files/HedgewarsMobile/Classes/AudioManagerController.m	Tue Oct 25 22:14:55 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/AudioManagerController.m	Tue Oct 25 22:51:10 2011 +0200
@@ -108,7 +108,7 @@
 
 #pragma mark -
 #pragma mark memory management
-+(void) cleanupMemory {
++(void) releaseCache {
     [backgroundMusic stop];
     [backgroundMusic release], backgroundMusic = nil;
     AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1;
--- a/project_files/HedgewarsMobile/Classes/HWUtils.h	Tue Oct 25 22:14:55 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/HWUtils.h	Tue Oct 25 22:51:10 2011 +0200
@@ -27,41 +27,7 @@
 
 +(NSString *)modelType;
 +(NSArray *)teamColors;
-
-@end
-
-
-@interface UITableView (extra)
-
--(void) setBackgroundColorForAnyTable:(UIColor *)color;
++(void) releaseCache;
 
 @end
 
-
-@interface UIColor (extra)
-
-+(UIColor *)darkYellowColor;
-+(UIColor *)lightYellowColor;
-+(UIColor *)darkBlueColor;
-+(UIColor *)darkBlueColorTransparent;
-+(UIColor *)blackColorTransparent;
-
-@end
-
-
-@interface UILabel (extra)
-
--(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title;
--(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth;
--(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth
-          withBorderColor:(UIColor *)borderColor withBackgroundColor:(UIColor *)backColor;
-
-@end
-
-
-@interface NSString (extra)
-
--(NSString *)MD5hash;
-
-@end
-
--- a/project_files/HedgewarsMobile/Classes/HWUtils.m	Tue Oct 25 22:14:55 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/HWUtils.m	Tue Oct 25 22:51:10 2011 +0200
@@ -22,134 +22,48 @@
 #import "HWUtils.h"
 #import <sys/types.h>
 #import <sys/sysctl.h>
-#import <QuartzCore/QuartzCore.h>
-#import <CommonCrypto/CommonDigest.h>
-#import "PascalImports.h"
 #import "hwconsts.h"
 
+static NSString *cachedModel = nil;
+static NSArray *cachedColors = nil;
+
 @implementation HWUtils
 
 +(NSString *)modelType {
-    size_t size;
-    // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
-    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
-    char *name = (char *)malloc(sizeof(char) * size);
-    // get the platform name
-    sysctlbyname("hw.machine", name, &size, NULL, 0);
-    NSString *modelId = [NSString stringWithUTF8String:name];
-    free(name);
+    if (cachedModel == nil) {
+        size_t size;
+        // set 'oldp' parameter to NULL to get the size of the data returned so we can allocate appropriate amount of space
+        sysctlbyname("hw.machine", NULL, &size, NULL, 0);
+        char *name = (char *)malloc(sizeof(char) * size);
+        // get the platform name
+        sysctlbyname("hw.machine", name, &size, NULL, 0);
 
-    return modelId;
+        cachedModel = [[NSString stringWithUTF8String:name] retain];
+        free(name);
+        DLog(@"Cache now contains: %@",cachedModel);
+    }
+    return cachedModel;
 }
 
 +(NSArray *)teamColors {
-    // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing
-    unsigned int colors[] = HW_TEAMCOLOR_ARRAY;
-    NSMutableArray *array = [[NSMutableArray alloc] init];
+    if (cachedColors == nil) {
+        // by default colors are ARGB but we do computation over RGB, hence we have to "& 0x00FFFFFF" before processing
+        unsigned int colors[] = HW_TEAMCOLOR_ARRAY;
+        NSMutableArray *array = [[NSMutableArray alloc] init];
 
-    int i = 0;
-    while(colors[i] != 0)
-        [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]];
+        int i = 0;
+        while(colors[i] != 0)
+            [array addObject:[NSNumber numberWithUnsignedInt:(colors[i++] & 0x00FFFFFF)]];
 
-    NSArray *final = [NSArray arrayWithArray:array];
-    [array release];
-    return final;
+        cachedColors = [[NSArray arrayWithArray:array] retain];
+        [array release];
+    }
+    return cachedColors;
 }
 
-@end
-
-
-@implementation UITableView (extra)
-
--(void) setBackgroundColorForAnyTable:(UIColor *) color {
-    UIView *backView = [[UIView alloc] initWithFrame:self.frame];
-    backView.backgroundColor = color;
-    self.backgroundView = backView;
-    [backView release];
-    self.backgroundColor = [UIColor clearColor];
++(void) releaseCache {
+    releaseAndNil(cachedModel);
+    releaseAndNil(cachedColors);
 }
 
 @end
-
-
-
-@implementation UIColor (extra)
-
-+(UIColor *)darkYellowColor {
-    return [UIColor colorWithRed:(CGFloat)0xFE/255 green:(CGFloat)0xC0/255 blue:0 alpha:1];
-}
-
-+(UIColor *)lightYellowColor {
-    return [UIColor colorWithRed:(CGFloat)0xF0/255 green:(CGFloat)0xD0/255 blue:0 alpha:1];
-}
-
-+(UIColor *)darkBlueColor {
-    return [UIColor colorWithRed:(CGFloat)0x0F/255 green:0 blue:(CGFloat)0x42/255 alpha:1];
-}
-
-+(UIColor *)darkBlueColorTransparent {
-    return [UIColor colorWithRed:(CGFloat)0x0F/255 green:0 blue:(CGFloat)0x55/255 alpha:0.6f];
-}
-
-+(UIColor *)blackColorTransparent {
-    return [UIColor colorWithRed:0 green:0 blue:0 alpha:0.65f];
-}
-
-@end
-
-
-@implementation UILabel (extra)
-
--(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title {
-    return [self initWithFrame:frame
-                      andTitle:title
-               withBorderWidth:1.5f
-               withBorderColor:[UIColor darkYellowColor]
-           withBackgroundColor:[UIColor darkBlueColor]];
-}
-
--(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth {
-    return [self initWithFrame:frame
-                      andTitle:title
-               withBorderWidth:borderWidth
-               withBorderColor:[UIColor darkYellowColor]
-           withBackgroundColor:[UIColor darkBlueColorTransparent]];
-}
-
--(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth
-          withBorderColor:(UIColor *)borderColor withBackgroundColor:(UIColor *)backColor{
-    UILabel *theLabel = [self initWithFrame:frame];
-    theLabel.backgroundColor = backColor;
-
-    if (title != nil) {
-        theLabel.text = title;
-        theLabel.textColor = [UIColor lightYellowColor];
-        theLabel.textAlignment = UITextAlignmentCenter;
-        theLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]*80/100];
-    }
-
-    [theLabel.layer setBorderWidth:borderWidth];
-    [theLabel.layer setBorderColor:borderColor.CGColor];
-    [theLabel.layer setCornerRadius:8.0f];
-    [theLabel.layer setMasksToBounds:YES];
-
-    return theLabel;
-}
-
-@end
-
-
-@implementation NSString (extra)
-
--(NSString *)MD5hash {
-    const char *cStr = [self UTF8String];
-    unsigned char result[16];
-    CC_MD5( cStr, strlen(cStr), result );
-    return [NSString stringWithFormat:
-            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
-            result[0], result[1], result[2], result[3], result[4], result[5],
-            result[6], result[7], result[8], result[9], result[10], result[11],
-            result[12], result[13], result[14], result[15]];
-}
-
-@end
--- a/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m	Tue Oct 25 22:14:55 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m	Tue Oct 25 22:51:10 2011 +0200
@@ -90,11 +90,12 @@
 }
 
 -(void) applicationDidReceiveMemoryWarning:(UIApplication *)application {
-    // don't stop music when it is playing
+    [HWUtils releaseCache];
+    // don't stop music if it is playing
     if (self.isInGame) {
-        [AudioManagerController cleanupMemory];
-        MSG_MEMCLEAN();
+        [AudioManagerController releaseCache];
     }
+    MSG_MEMCLEAN();
     // don't clean mainMenuViewController here!!!
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/ExtraCategories.h	Tue Oct 25 22:51:10 2011 +0200
@@ -0,0 +1,57 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@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.
+ *
+ * File created on 25/10/2011.
+ */
+
+
+#import <Foundation/Foundation.h>
+
+
+@interface UITableView (backgroundColor)
+
+-(void) setBackgroundColorForAnyTable:(UIColor *)color;
+
+@end
+
+
+@interface UIColor (HWColors)
+
++(UIColor *)darkYellowColor;
++(UIColor *)lightYellowColor;
++(UIColor *)darkBlueColor;
++(UIColor *)darkBlueColorTransparent;
++(UIColor *)blackColorTransparent;
+
+@end
+
+
+@interface UILabel (quickStyle)
+
+-(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title;
+-(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth;
+-(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth
+          withBorderColor:(UIColor *)borderColor withBackgroundColor:(UIColor *)backColor;
+
+@end
+
+
+@interface NSString (MD5)
+
+-(NSString *)MD5hash;
+
+@end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/ExtraCategories.m	Tue Oct 25 22:51:10 2011 +0200
@@ -0,0 +1,127 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@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.
+ *
+ * File created on 25/10/2011.
+ */
+
+
+#import "ExtraCategories.h"
+#import <QuartzCore/QuartzCore.h>
+#import <CommonCrypto/CommonDigest.h>
+
+@implementation UITableView (backgroundColor)
+
+-(void) setBackgroundColorForAnyTable:(UIColor *) color {
+    if ([self respondsToSelector:@selector(backgroundView)]) {
+        UIView *backView = [[UIView alloc] initWithFrame:self.frame];
+        backView.backgroundColor = color;
+        self.backgroundView = backView;
+        [backView release];
+    }
+    self.backgroundColor = [UIColor clearColor];
+}
+
+@end
+
+
+@implementation UIColor (HWColors)
+
++(UIColor *)darkYellowColor {
+    return [UIColor colorWithRed:(CGFloat)0xFE/255 green:(CGFloat)0xC0/255 blue:0 alpha:1];
+}
+
++(UIColor *)lightYellowColor {
+    return [UIColor colorWithRed:(CGFloat)0xF0/255 green:(CGFloat)0xD0/255 blue:0 alpha:1];
+}
+
++(UIColor *)darkBlueColor {
+    return [UIColor colorWithRed:(CGFloat)0x0F/255 green:0 blue:(CGFloat)0x42/255 alpha:1];
+}
+
+// older devices don't get any transparency for performance reasons
++(UIColor *)darkBlueColorTransparent {
+    return [UIColor colorWithRed:(CGFloat)0x0F/255
+                           green:0
+                            blue:(CGFloat)0x55/255
+                           alpha:IS_NOT_POWERFUL([HWUtils modelType]) ? 1 : 0.6f];
+}
+
++(UIColor *)blackColorTransparent {
+    return [UIColor colorWithRed:0
+                           green:0
+                            blue:0
+                           alpha:IS_NOT_POWERFUL([HWUtils modelType]) ? 1 : 0.65f];
+}
+
+@end
+
+
+@implementation UILabel (quickStyle)
+
+-(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title {
+    return [self initWithFrame:frame
+                      andTitle:title
+               withBorderWidth:1.5f
+               withBorderColor:[UIColor darkYellowColor]
+           withBackgroundColor:[UIColor darkBlueColor]];
+}
+
+-(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth {
+    return [self initWithFrame:frame
+                      andTitle:title
+               withBorderWidth:borderWidth
+               withBorderColor:[UIColor darkYellowColor]
+           withBackgroundColor:[UIColor darkBlueColorTransparent]];
+}
+
+-(UILabel *)initWithFrame:(CGRect)frame andTitle:(NSString *)title  withBorderWidth:(CGFloat) borderWidth
+          withBorderColor:(UIColor *)borderColor withBackgroundColor:(UIColor *)backColor{
+    UILabel *theLabel = [self initWithFrame:frame];
+    theLabel.backgroundColor = backColor;
+
+    if (title != nil) {
+        theLabel.text = title;
+        theLabel.textColor = [UIColor lightYellowColor];
+        theLabel.textAlignment = UITextAlignmentCenter;
+        theLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]*80/100];
+    }
+
+    [theLabel.layer setBorderWidth:borderWidth];
+    [theLabel.layer setBorderColor:borderColor.CGColor];
+    [theLabel.layer setCornerRadius:8.0f];
+    [theLabel.layer setMasksToBounds:YES];
+
+    return theLabel;
+}
+
+@end
+
+
+@implementation NSString (MD5)
+
+-(NSString *)MD5hash {
+    const char *cStr = [self UTF8String];
+    unsigned char result[16];
+    CC_MD5( cStr, strlen(cStr), result );
+    return [NSString stringWithFormat:
+            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
+            result[0], result[1], result[2], result[3], result[4], result[5],
+            result[6], result[7], result[8], result[9], result[10], result[11],
+            result[12], result[13], result[14], result[15]];
+}
+
+@end
--- a/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Tue Oct 25 22:14:55 2011 +0200
+++ b/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Tue Oct 25 22:51:10 2011 +0200
@@ -223,6 +223,7 @@
 		61C079E411F35A300072BF46 /* EditableCellView.m in Sources */ = {isa = PBXBuildFile; fileRef = 61C079E311F35A300072BF46 /* EditableCellView.m */; };
 		61C28D3F142D380400DA16C2 /* AudioManagerController.m in Sources */ = {isa = PBXBuildFile; fileRef = 61C28D3E142D380400DA16C2 /* AudioManagerController.m */; };
 		61CADE331402EE290030C3EB /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 61CADE321402EE290030C3EB /* ImageIO.framework */; };
+		61D0BDF91457508C0011A899 /* ExtraCategories.m in Sources */ = {isa = PBXBuildFile; fileRef = 61D0BDF81457508C0011A899 /* ExtraCategories.m */; };
 		61D205A1127CDD1100ABD83E /* ObjcExports.m in Sources */ = {isa = PBXBuildFile; fileRef = 61D205A0127CDD1100ABD83E /* ObjcExports.m */; };
 		61D3D2A51290E03A003CE7C3 /* irc.png in Resources */ = {isa = PBXBuildFile; fileRef = 61D3D2A41290E03A003CE7C3 /* irc.png */; };
 		61DE8F221257EB1100B80214 /* AmmoMenuViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 61DE8F211257EB1100B80214 /* AmmoMenuViewController.m */; };
@@ -592,6 +593,8 @@
 		61C28D3D142D380400DA16C2 /* AudioManagerController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AudioManagerController.h; path = Classes/AudioManagerController.h; sourceTree = "<group>"; };
 		61C28D3E142D380400DA16C2 /* AudioManagerController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AudioManagerController.m; path = Classes/AudioManagerController.m; sourceTree = "<group>"; };
 		61CADE321402EE290030C3EB /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; };
+		61D0BDF71457508C0011A899 /* ExtraCategories.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExtraCategories.h; sourceTree = "<group>"; };
+		61D0BDF81457508C0011A899 /* ExtraCategories.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExtraCategories.m; sourceTree = "<group>"; };
 		61D2059F127CDD1100ABD83E /* ObjcExports.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ObjcExports.h; path = Classes/ObjcExports.h; sourceTree = "<group>"; };
 		61D205A0127CDD1100ABD83E /* ObjcExports.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ObjcExports.m; path = Classes/ObjcExports.m; sourceTree = "<group>"; };
 		61D3D2A41290E03A003CE7C3 /* irc.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = irc.png; path = Resources/Icons/irc.png; sourceTree = "<group>"; };
@@ -735,6 +738,8 @@
 				6165922311CA9BD500D6E256 /* CGPointUtils.c */,
 				6165922C11CA9BD500D6E256 /* UIImageExtra.h */,
 				6165922D11CA9BD500D6E256 /* UIImageExtra.m */,
+				61D0BDF71457508C0011A899 /* ExtraCategories.h */,
+				61D0BDF81457508C0011A899 /* ExtraCategories.m */,
 			);
 			name = "Other Sources";
 			sourceTree = "<group>";
@@ -1635,6 +1640,7 @@
 				6167A6761391514600AA6D07 /* RestoreViewController.m in Sources */,
 				61C28D3F142D380400DA16C2 /* AudioManagerController.m in Sources */,
 				61915D5B143A4E2C00299991 /* MissionTrainingViewController.m in Sources */,
+				61D0BDF91457508C0011A899 /* ExtraCategories.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
--- a/project_files/HedgewarsMobile/Hedgewars_Prefix.pch	Tue Oct 25 22:14:55 2011 +0200
+++ b/project_files/HedgewarsMobile/Hedgewars_Prefix.pch	Tue Oct 25 22:51:10 2011 +0200
@@ -29,5 +29,6 @@
 #import "HedgewarsAppDelegate.h"
 #import "AudioManagerController.h"
 #import "HWUtils.h"
+#import "ExtraCategories.h"
 #endif