# HG changeset patch # User koda # Date 1305234026 -7200 # Node ID 4c9ae0f484dadef5031c45fa7f69c62e5434b274 # Parent db775bddf771e0966e462f6098af715598316cc6 some general tweaks (more intelligent use of macros and more caching) diff -r db775bddf771 -r 4c9ae0f484da project_files/HedgewarsMobile/Classes/CommodityFunctions.h --- a/project_files/HedgewarsMobile/Classes/CommodityFunctions.h Wed May 11 01:26:38 2011 +0200 +++ b/project_files/HedgewarsMobile/Classes/CommodityFunctions.h Thu May 12 23:00:26 2011 +0200 @@ -54,23 +54,24 @@ #define UICOLOR_HW_YELLOW_TEXT [UIColor colorWithRed:(CGFloat)0xF0/255 green:(CGFloat)0xD0/255 blue:0 alpha:1] #define UICOLOR_HW_DARKBLUE [UIColor colorWithRed:(CGFloat)0x0F/255 green:0 blue:(CGFloat)0x42/255 alpha:1] #define UICOLOR_HW_ALPHABLUE [UIColor colorWithRed:(CGFloat)0x0F/255 green:0 blue:(CGFloat)0x42/255 alpha:0.58f] -#define UICOLOR_HW_ALMOSTBLACK (IS_NOT_POWERFUL()) ? [UIColor blackColor] : [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6] +#define UICOLOR_HW_ALMOSTBLACK (IS_NOT_POWERFUL(getModelType())) ? [UIColor blackColor] : [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6] #define IS_DUALHEAD() ([[UIScreen class] respondsToSelector:@selector(screens)] && [[UIScreen screens] count] > 1) #define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) -#define IS_NOT_POWERFUL() ([modelType() hasPrefix:@"iPhone1"] || [modelType() hasPrefix:@"iPod1,1"] || [modelType() hasPrefix:@"iPod2,1"]) -#define IS_NOT_VERY_POWERFUL() ([modelType() hasPrefix:@"iPad1"] || [modelType() hasPrefix:@"iPhone2"] || [modelType() hasPrefix:@"iPod3"] || [modelType() hasPrefix:@"iPod4"] ) -#define IS_VERY_POWERFUL() (IS_NOT_POWERFUL() == NO && IS_NOT_VERY_POWERFUL() == NO) +#define IS_NOT_POWERFUL(x) ([x hasPrefix:@"iPhone1"] || [x hasPrefix:@"iPod1,1"] || [x hasPrefix:@"iPod2,1"]) +#define IS_NOT_VERY_POWERFUL(x) ([x hasPrefix:@"iPad1"] || [x hasPrefix:@"iPhone2"] || [x hasPrefix:@"iPod3"] || [x hasPrefix:@"iPod4"]) +#define IS_VERY_POWERFUL(x) (IS_NOT_POWERFUL(x) == NO && IS_NOT_VERY_POWERFUL(x) == NO) +#define rotationManager(x) (x == UIInterfaceOrientationLandscapeRight) || (x == UIInterfaceOrientationLandscapeLeft) void print_free_memory (void); void playSound (NSString *snd); -void popError (const char *title, const char *message); -BOOL rotationManager (UIInterfaceOrientation interfaceOrientation); BOOL isApplePhone (void); NSInteger randomPort (void); -NSString *modelType (void); + +NSString *getModelType (void); NSArray *getAvailableColors (void); + UILabel *createBlueLabel (NSString *title, CGRect frame); UILabel *createLabelWithParams (NSString *title, CGRect frame, CGFloat borderWidth, UIColor *borderColor, UIColor *backgroundColor); diff -r db775bddf771 -r 4c9ae0f484da project_files/HedgewarsMobile/Classes/CommodityFunctions.m --- a/project_files/HedgewarsMobile/Classes/CommodityFunctions.m Wed May 11 01:26:38 2011 +0200 +++ b/project_files/HedgewarsMobile/Classes/CommodityFunctions.m Thu May 12 23:00:26 2011 +0200 @@ -30,10 +30,6 @@ #import "PascalImports.h" #import "hwconsts.h" -BOOL inline rotationManager (UIInterfaceOrientation interfaceOrientation) { - return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || - (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); -} NSInteger inline randomPort () { srandom(time(NULL)); @@ -41,16 +37,6 @@ return (res == NETGAME_DEFAULT_PORT) ? randomPort() : res; } -void popError (const char *title, const char *message) { - UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithUTF8String:title] - message:[NSString stringWithUTF8String:message] - delegate:nil - cancelButtonTitle:@"Ok" - otherButtonTitles:nil]; - [alert show]; - [alert release]; -} - // by http://landonf.bikemonkey.org/code/iphone/Determining_Available_Memory.20081203.html void print_free_memory () { mach_port_t host_port; @@ -77,7 +63,7 @@ return (IS_IPAD() == NO); } -NSString *modelType () { +NSString *getModelType () { 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); @@ -91,10 +77,7 @@ } void playSound (NSString *snd) { - NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; - NSNumber *audio = [prefs objectForKey:@"sound"]; - - if (audio == nil || [audio boolValue] == YES) { + if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == YES) { // get the filename of the sound file: NSString *path = [NSString stringWithFormat:@"%@/%@.wav",[[NSBundle mainBundle] resourcePath],snd]; @@ -109,6 +92,7 @@ } NSArray *getAvailableColors (void) { + // 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]; diff -r db775bddf771 -r 4c9ae0f484da project_files/HedgewarsMobile/Classes/CreationChamber.m --- a/project_files/HedgewarsMobile/Classes/CreationChamber.m Wed May 11 01:26:38 2011 +0200 +++ b/project_files/HedgewarsMobile/Classes/CreationChamber.m Thu May 12 23:00:26 2011 +0200 @@ -30,8 +30,12 @@ [settings setObject:[NSNumber numberWithBool:NO] forKey:@"classic_menu"]; [settings setObject:[NSNumber numberWithBool:YES] forKey:@"enhanced"]; [settings setObject:[NSNumber numberWithBool:YES] forKey:@"multitasking"]; - [settings setObject:@"" forKey:@"username"]; - [settings setObject:@"" forKey:@"password"]; + + // don't overwrite these two strings when present + if ([settings objectForKey:@"username"] == nil) + [settings setObject:@"" forKey:@"username"]; + if ([settings objectForKey:@"password"] == nil) + [settings setObject:@"" forKey:@"password"]; [settings synchronize]; } diff -r db775bddf771 -r 4c9ae0f484da project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m --- a/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m Wed May 11 01:26:38 2011 +0200 +++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m Thu May 12 23:00:26 2011 +0200 @@ -86,7 +86,7 @@ NSString *rotation = [[NSString alloc] initWithFormat:@"%d", orientation]; BOOL enhanced = [[settings objectForKey:@"enhanced"] boolValue]; - NSString *modelId = modelType(); + NSString *modelId = getModelType(); NSInteger tmpQuality; if ([modelId hasPrefix:@"iPhone1"] || [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"]) // = iPhone and iPhone 3G or iPod Touch or iPod Touch 2G tmpQuality = 0x00000001 | 0x00000002 | 0x00000008 | 0x00000040; // rqLowRes | rqBlurryLand | rqSimpleRope | rqKillFlakes diff -r db775bddf771 -r 4c9ae0f484da project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.h --- a/project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.h Wed May 11 01:26:38 2011 +0200 +++ b/project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.h Thu May 12 23:00:26 2011 +0200 @@ -23,9 +23,7 @@ #import "EditableCellView.h" @interface GeneralSettingsViewController : UITableViewController { - NSUserDefaults *settings; + } -@property (nonatomic, retain) NSUserDefaults *settings; - @end diff -r db775bddf771 -r 4c9ae0f484da project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m --- a/project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m Wed May 11 01:26:38 2011 +0200 +++ b/project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m Thu May 12 23:00:26 2011 +0200 @@ -23,7 +23,6 @@ #import "CommodityFunctions.h" @implementation GeneralSettingsViewController -@synthesize settings; -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { @@ -38,50 +37,48 @@ -(void) viewWillAppear:(BOOL)animated { [self.tableView setContentOffset:CGPointMake(0,0) animated:NO]; - - self.settings = [NSUserDefaults standardUserDefaults]; - [super viewWillAppear:animated]; } -(void) viewWillDisappear:(BOOL)animated { + [[NSUserDefaults standardUserDefaults] synchronize]; [super viewWillDisappear:animated]; - [self.settings synchronize]; } #pragma mark - -(void) switchValueChanged:(id) sender { UISwitch *theSwitch = (UISwitch *)sender; UISwitch *theOtherSwitch = nil; + NSUserDefaults *settings = [NSUserDefaults standardUserDefaults]; switch (theSwitch.tag) { case 10: //soundSwitch // this turn off also the switch below - [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"sound"]; - [self.settings setObject:[NSNumber numberWithBool:NO] forKey:@"music"]; + [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"sound"]; + [settings setObject:[NSNumber numberWithBool:NO] forKey:@"music"]; theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView; [theOtherSwitch setOn:NO animated:YES]; break; case 20: //musicSwitch // if switch above is off, never turn on - if (NO == [[self.settings objectForKey:@"sound"] boolValue]) { - [self.settings setObject:[NSNumber numberWithBool:NO] forKey:@"music"]; + if (NO == [[settings objectForKey:@"sound"] boolValue]) { + [settings setObject:[NSNumber numberWithBool:NO] forKey:@"music"]; theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView; [theOtherSwitch setOn:NO animated:YES]; } else - [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"music"]; + [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"music"]; break; case 30: //alternateSwitch - [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"]; + [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"]; break; case 70: //enhanced graphics - [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"enhanced"]; + [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"enhanced"]; break; case 80: //nomultitasking - [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"multitasking"]; + [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"multitasking"]; break; case 60: //classic menu - [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"classic_menu"]; + [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"classic_menu"]; break; default: DLog(@"Wrong tag"); @@ -90,10 +87,12 @@ } -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue { + NSUserDefaults *settings = [NSUserDefaults standardUserDefaults]; + if (tagValue == 40) - [self.settings setObject:textString forKey:@"username"]; + [settings setObject:textString forKey:@"username"]; else - [self.settings setObject:[textString MD5hash] forKey:@"password"]; + [settings setObject:[textString MD5hash] forKey:@"password"]; } #pragma mark - @@ -148,6 +147,7 @@ static NSString *cellIdentifier2 = @"Cell2"; NSInteger row = [indexPath row]; NSInteger section = [indexPath section]; + NSUserDefaults *settings = [NSUserDefaults standardUserDefaults]; UITableViewCell *cell = nil; EditableCellView *editableCell = nil; @@ -166,14 +166,13 @@ if (row == 0) { editableCell.titleLabel.text = NSLocalizedString(@"Nickname","from the settings table"); editableCell.textField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@""); - editableCell.textField.text = [self.settings objectForKey:@"username"]; + editableCell.textField.text = [settings objectForKey:@"username"]; editableCell.textField.secureTextEntry = NO; editableCell.tag = 40; } else { - NSString *pwd = [self.settings objectForKey:@"password"]; editableCell.titleLabel.text = NSLocalizedString(@"Password","from the settings table"); editableCell.textField.placeholder = NSLocalizedString(@"Insert your password",@""); - editableCell.textField.text = ([pwd length] == 0) ? @"0123456789" : pwd; + editableCell.textField.text = [settings objectForKey:@"password"]; editableCell.textField.secureTextEntry = YES; editableCell.tag = 50; } @@ -194,11 +193,11 @@ switchContent = (UISwitch *)cell.accessoryView; if (row == 0) { cell.textLabel.text = NSLocalizedString(@"Sound", @""); - switchContent.on = [[self.settings objectForKey:@"sound"] boolValue]; + switchContent.on = [[settings objectForKey:@"sound"] boolValue]; switchContent.tag = 10; } else { cell.textLabel.text = NSLocalizedString(@"Music", @""); - switchContent.on = [[self.settings objectForKey:@"music"] boolValue]; + switchContent.on = [[settings objectForKey:@"music"] boolValue]; switchContent.tag = 20; } break; @@ -217,25 +216,25 @@ case 0: cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @""); cell.detailTextLabel.text = NSLocalizedString(@"Damage popups will notify you on every single hit", @""); - switchContent.on = [[self.settings objectForKey:@"alternate"] boolValue]; + switchContent.on = [[settings objectForKey:@"alternate"] boolValue]; switchContent.tag = 30; break; case 1: cell.textLabel.text = NSLocalizedString(@"Enanched Graphics Mode", @""); cell.detailTextLabel.text = NSLocalizedString(@"The game will use more memory so it could crash!", @""); - switchContent.on = [[self.settings objectForKey:@"enhanced"] boolValue]; + switchContent.on = [[settings objectForKey:@"enhanced"] boolValue]; switchContent.tag = 70; break; case 2: cell.textLabel.text = NSLocalizedString(@"Multitasking Enabled", @""); cell.detailTextLabel.text = NSLocalizedString(@"Disable it in case of issues when returing in game", @""); - switchContent.on = [[self.settings objectForKey:@"multitasking"] boolValue]; + switchContent.on = [[settings objectForKey:@"multitasking"] boolValue]; switchContent.tag = 80; break; case 3: cell.textLabel.text = NSLocalizedString(@"Classic Ammo Menu", @""); cell.detailTextLabel.text = NSLocalizedString(@"Select which style of ammo menu you prefer",@""); - switchContent.on = [[self.settings objectForKey:@"classic_menu"] boolValue]; + switchContent.on = [[settings objectForKey:@"classic_menu"] boolValue]; switchContent.tag = 60; break; default: @@ -271,13 +270,10 @@ } -(void) viewDidUnload { - self.settings = nil; - MSG_DIDUNLOAD(); [super viewDidUnload]; } -(void) dealloc { - [settings release]; [super dealloc]; } diff -r db775bddf771 -r 4c9ae0f484da project_files/HedgewarsMobile/Classes/MapConfigViewController.m --- a/project_files/HedgewarsMobile/Classes/MapConfigViewController.m Wed May 11 01:26:38 2011 +0200 +++ b/project_files/HedgewarsMobile/Classes/MapConfigViewController.m Thu May 12 23:00:26 2011 +0200 @@ -74,7 +74,7 @@ // perform as if user clicked on an entry [self tableView:self.tableView didSelectRowAtIndexPath:theIndex]; - if (IS_NOT_POWERFUL() == NO) + if (IS_NOT_POWERFUL(getModelType()) == NO) [self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; } @@ -378,6 +378,8 @@ #pragma mark - #pragma mark view management -(void) loadDataSourceArray { + NSString *model = getModelType(); + // themes.cfg contains all the user-selectable themes NSString *string = [[NSString alloc] initWithContentsOfFile:[THEMES_DIRECTORY() stringByAppendingString:@"/themes.cfg"] encoding:NSUTF8StringEncoding @@ -392,9 +394,9 @@ NSMutableArray *mapArray = [[NSMutableArray alloc] init]; for (NSString *str in mapArrayFull) { CGSize imgSize = PSPNGSizeFromMetaData([MAPS_DIRECTORY() stringByAppendingFormat:@"%@/map.png",str]); - if (IS_NOT_POWERFUL() && imgSize.height > 1024.0f) + if (IS_NOT_POWERFUL(model) && imgSize.height > 1024.0f) continue; - if (IS_NOT_VERY_POWERFUL() && imgSize.height > 1280.0f) + if (IS_NOT_VERY_POWERFUL(model) && imgSize.height > 1280.0f) continue; [mapArray addObject:str]; } @@ -403,9 +405,9 @@ NSMutableArray *missionArray = [[NSMutableArray alloc] init]; for (NSString *str in missionArrayFull) { CGSize imgSize = PSPNGSizeFromMetaData([MISSIONS_DIRECTORY() stringByAppendingFormat:@"%@/map.png",str]); - if (IS_NOT_POWERFUL() && imgSize.height > 1024.0f) + if (IS_NOT_POWERFUL(model) && imgSize.height > 1024.0f) continue; - if (IS_NOT_VERY_POWERFUL() && imgSize.height > 1280.0f) + if (IS_NOT_VERY_POWERFUL(model) && imgSize.height > 1280.0f) continue; [missionArray addObject:str]; } diff -r db775bddf771 -r 4c9ae0f484da project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m --- a/project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m Wed May 11 01:26:38 2011 +0200 +++ b/project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m Thu May 12 23:00:26 2011 +0200 @@ -197,7 +197,7 @@ [self setTitle:nil forState:UIControlStateNormal]; // don't display preview on slower device, too slow and memory hog - if (IS_NOT_POWERFUL()) { + if (IS_NOT_POWERFUL(getModelType())) { [self setTitle:NSLocalizedString(@"Preview not available",@"") forState:UIControlStateNormal]; [self turnOnWidgets]; } else { diff -r db775bddf771 -r 4c9ae0f484da project_files/HedgewarsMobile/Classes/OverlayViewController.m --- a/project_files/HedgewarsMobile/Classes/OverlayViewController.m Wed May 11 01:26:38 2011 +0200 +++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.m Thu May 12 23:00:26 2011 +0200 @@ -61,7 +61,8 @@ NSInteger angle_left = (self.initialOrientation == UIInterfaceOrientationLandscapeLeft) ? 180 : 0; NSInteger angle_right = (self.initialOrientation == UIInterfaceOrientationLandscapeLeft) ? 0 : 180; - if (IS_VERY_POWERFUL()) { + NSString *model = getModelType(); + if (IS_VERY_POWERFUL(model)) { [UIView beginAnimations:@"overlay rotation" context:NULL]; [UIView setAnimationDuration:0.7]; } @@ -82,7 +83,7 @@ // a debug log would spam too much break; } - if (IS_VERY_POWERFUL()) + if (IS_VERY_POWERFUL(model)) [UIView commitAnimations]; }