optimizations to new ammomenu and overlay
authorkoda
Fri, 15 Oct 2010 01:10:59 +0200
changeset 3977 9df7b4812da9
parent 3976 abaf741a4e21
child 3978 9660600e43cb
optimizations to new ammomenu and overlay
project_files/HedgewarsMobile/Classes/AmmoMenuViewController.h
project_files/HedgewarsMobile/Classes/AmmoMenuViewController.m
project_files/HedgewarsMobile/Classes/OverlayViewController.m
--- a/project_files/HedgewarsMobile/Classes/AmmoMenuViewController.h	Thu Oct 14 22:01:33 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/AmmoMenuViewController.h	Fri Oct 15 01:10:59 2010 +0200
@@ -23,16 +23,16 @@
 
 
 @interface AmmoMenuViewController : UIViewController {
-    UIImage *weaponsImage;
     NSArray *buttonsArray;
+    NSArray *imagesArray;
     
     uint8_t *delay;
     CGPoint startingPoint;
     BOOL isVisible;
 }
 
-@property (nonatomic,retain) UIImage *weaponsImage;
-@property (nonatomic,retain) NSArray *buttonsArray;
+@property (retain) NSArray *buttonsArray;
+@property (retain) NSArray *imagesArray;
 @property (assign) BOOL isVisible;
 
 -(void) buttonPressed:(id)sender;
--- a/project_files/HedgewarsMobile/Classes/AmmoMenuViewController.m	Thu Oct 14 22:01:33 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/AmmoMenuViewController.m	Fri Oct 15 01:10:59 2010 +0200
@@ -28,7 +28,7 @@
 #define BTNS_PER_ROW 9
 
 @implementation AmmoMenuViewController
-@synthesize weaponsImage, buttonsArray, isVisible;
+@synthesize imagesArray, buttonsArray, isVisible;
 
 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
     return rotationManager(interfaceOrientation);
@@ -87,12 +87,10 @@
 #pragma mark drawing
 -(void) loadAmmoStuff:(id) object {
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
     NSString *str = [NSString stringWithFormat:@"%@/AmmoMenu/Ammos.png",GRAPHICS_DIRECTORY()];
     UIImage *ammoStoreImage = [[UIImage alloc] initWithContentsOfFile:str];
-    [self performSelectorOnMainThread:@selector(setWeaponsImage:) withObject:ammoStoreImage waitUntilDone:NO];
-    [ammoStoreImage release];
 
+    NSMutableArray *imgs = [[NSMutableArray alloc] initWithCapacity:HW_getNumberOfWeapons()];
     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:HW_getNumberOfWeapons()];
     int i, j, e;
     for (i = 0, j = 0, e = 0; i < HW_getNumberOfWeapons(); i++) {
@@ -133,19 +131,27 @@
         button.titleLabel.layer.borderWidth = 1;
         [self.view addSubview:button];
         [array addObject:button];
-        
+
+        int x_src = ((i*32)/(int)ammoStoreImage.size.height)*32;
+        int y_src = (i*32)%(int)ammoStoreImage.size.height;
+        UIImage *img = [ammoStoreImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
+        [imgs addObject:img];
     }
     [self performSelectorOnMainThread:@selector(setButtonsArray:) withObject:array waitUntilDone:NO];
     [array release];
-    
-    [self performSelectorOnMainThread:@selector(updateAmmoVisuals) withObject:nil waitUntilDone:NO];
+
+    [self performSelectorOnMainThread:@selector(setImagesArray:) withObject:imgs waitUntilDone:NO];
+    [imgs release];
+    [ammoStoreImage release];
+
+    [self performSelectorOnMainThread:@selector(updateAmmoVisuals) withObject:nil waitUntilDone:YES];
     UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)object;
     [spinner stopAnimating];
     [pool drain];
 }
 
 -(void) updateAmmoVisuals {
-    if (self.buttonsArray == nil || self.weaponsImage == nil) {
+    if (self.buttonsArray == nil || self.imagesArray == nil) {
         UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
         spinner.hidesWhenStopped = YES;
         spinner.center = self.view.center;
@@ -156,11 +162,6 @@
         return;
     }
     
-    [NSThread detachNewThreadSelector:@selector(drawingThread) toTarget:self withObject:nil];
-}
-
--(void) drawingThread {
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     int *loadout = (int *)calloc(HW_getNumberOfWeapons(), sizeof(int));
     int res = HW_getAmmoCounts(loadout);
     int turns = HW_getTurnsForCurrentTeam();
@@ -175,9 +176,7 @@
                     button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
                     [button setTitle:[NSString stringWithFormat:@" %d ",delay[i]-turns+1] forState:UIControlStateNormal];
                     if (button.currentBackgroundImage == nil) {
-                        int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
-                        int y_src = (i*32)%(int)self.weaponsImage.size.height;
-                        UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
+                        UIImage *img = [self.imagesArray objectAtIndex:i];
                         [button setBackgroundImage:[img convertToGrayScale] forState:UIControlStateNormal];
                         button.imageView.tag = 10000;
                     }
@@ -185,9 +184,7 @@
                     button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
                     [button setTitle:nil forState:UIControlStateNormal];
                     if (button.currentBackgroundImage == nil || button.imageView.tag == 10000) {
-                        int x_src = ((i*32)/(int)self.weaponsImage.size.height)*32;
-                        int y_src = (i*32)%(int)self.weaponsImage.size.height;
-                        UIImage *img = [self.weaponsImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
+                        UIImage *img = [self.imagesArray objectAtIndex:i];
                         [button setBackgroundImage:img forState:UIControlStateNormal];
                         button.imageView.tag = 0;
                     }
@@ -199,7 +196,6 @@
                 button.layer.borderColor = [[UIColor darkGrayColor] CGColor];
                 button.enabled = NO;
             }
-            
         }
     } else {
         self.view.userInteractionEnabled = NO;
@@ -207,7 +203,6 @@
 
     free(loadout);
     loadout = NULL;
-    [pool drain];
 }
 
 #pragma mark -
@@ -267,7 +262,7 @@
 #pragma mark -
 #pragma mark memory
 -(void) didReceiveMemoryWarning {
-    self.weaponsImage = nil;
+    self.imagesArray = nil;
     self.buttonsArray = nil;
     MSG_MEMCLEAN();
     [super didReceiveMemoryWarning];
@@ -275,7 +270,7 @@
 
 -(void) viewDidUnload {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
-    self.weaponsImage = nil;
+    self.imagesArray = nil;
     self.buttonsArray = nil;
     free(delay);
     delay = NULL;
@@ -284,7 +279,7 @@
 }
 
 -(void) dealloc {
-    [weaponsImage release];
+    [imagesArray release];
     [buttonsArray release];
     [super dealloc];
 }
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Thu Oct 14 22:01:33 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Fri Oct 15 01:10:59 2010 +0200
@@ -48,10 +48,7 @@
 #pragma mark -
 #pragma mark rotation
 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
-    if (IS_DUALHEAD())
-        return YES;                         ////// TEST MEEEEE
-    else
-        return rotationManager(interfaceOrientation);
+    return rotationManager(interfaceOrientation);
 }
 
 // pause the game and remove objc menus so that animation is smoother
@@ -82,27 +79,24 @@
 -(void) didRotate:(NSNotification *)notification {
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
     UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:SDL_VIEW_TAG];
-
+    CGRect screenRect = [[UIScreen mainScreen] bounds];
+    
     [UIView beginAnimations:@"rotation" context:NULL];
     [UIView setAnimationDuration:0.7];
     switch (orientation) {
         case UIDeviceOrientationLandscapeLeft:
-            if (IS_DUALHEAD() == NO)
+            if (IS_DUALHEAD()) {
+                self.view.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
+                self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
+            } else
                 sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(a));
-            //self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
             break;
         case UIDeviceOrientationLandscapeRight:
-            if (IS_DUALHEAD() == NO)
+            if (IS_DUALHEAD()) {
+                self.view.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
+                self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
+            } else
                 sdlView.transform = CGAffineTransformMakeRotation(degreesToRadians(b));
-            //self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
-            break;
-        case UIDeviceOrientationPortrait:
-            if (IS_DUALHEAD())
-                self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
-            break;
-        case UIDeviceOrientationPortraitUpsideDown:
-            if (IS_DUALHEAD())
-                self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(180));
             break;
         default:
             // a debug log would spam too much
@@ -121,22 +115,20 @@
         isGameRunning = NO;
         isReplay = NO;
         cachedGrenadeTime = 2;
+
         isAttacking = NO;
         wasVisible = NO;
         isPopoverVisible = NO;    // it is called "popover" even on the iphone
+        self.view.alpha = 0;
     }
     return self;
 }
 
 -(void) viewDidLoad {
     CGRect screenRect = [[UIScreen mainScreen] bounds];
-    self.view.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
-    self.view.alpha = 0;
+    self.view.frame = CGRectMake(0, 0, screenRect.size.height, screenRect.size.width);
     self.view.center = CGPointMake(self.view.frame.size.height/2, self.view.frame.size.width/2);
 
-    // get the number of screens to know the previous state whan a display is connected or detached
-    initialScreenCount = [[UIScreen screens] count];
-    
     // detrmine the quanitiy and direction of the rotation
     if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
         a = 180;
@@ -146,6 +138,24 @@
         b = 180;
     }
 
+    // get the number of screens to know the previous state whan a display is connected or detached
+    initialScreenCount = [[UIScreen screens] count];
+
+    // set initial orientation of the controller orientation
+    if (IS_DUALHEAD()) {
+        switch (self.interfaceOrientation) {
+            case UIDeviceOrientationLandscapeLeft:
+                self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
+                break;
+            case UIDeviceOrientationLandscapeRight:
+                self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
+                break;
+            default:
+                DLog(@"Nope");
+                break;
+        }
+    }
+
     // the timer used to dim the overlay
     dimTimer = [[NSTimer alloc] initWithFireDate:(IS_DUALHEAD()) ? HIDING_TIME_NEVER : [NSDate dateWithTimeIntervalSinceNow:6]
                                         interval:1000