some optimizations to drawing and fetching data of new ammomenu
authorkoda
Sun, 10 Oct 2010 02:06:42 +0200
changeset 3940 cc29628976cc
parent 3939 e6fddcb04dc2
child 3941 017b2b31e1c6
some optimizations to drawing and fetching data of new ammomenu
QTfrontend/hwform.cpp
hedgewars/PascalExports.pas
hedgewars/uMobile.pas
hedgewars/uStore.pas
project_files/HedgewarsMobile/Classes/AmmoMenuViewController.h
project_files/HedgewarsMobile/Classes/AmmoMenuViewController.m
project_files/HedgewarsMobile/Classes/CommodityFunctions.m
project_files/HedgewarsMobile/Classes/OverlayViewController.m
project_files/HedgewarsMobile/Classes/PascalImports.h
project_files/HedgewarsMobile/Classes/SDL_uikitappdelegate.m
--- a/QTfrontend/hwform.cpp	Sat Oct 09 18:01:47 2010 +0200
+++ b/QTfrontend/hwform.cpp	Sun Oct 10 02:06:42 2010 +0200
@@ -276,7 +276,7 @@
 
 void HWForm::keyReleaseEvent(QKeyEvent *event)
 {
-  if (event->key() == Qt::Key_Escape || event->key() == Qt::Key_Backspace)
+  if (event->key() == Qt::Key_Escape /*|| event->key() == Qt::Key_Backspace*/ )
     this->GoBack();
 }
 
--- a/hedgewars/PascalExports.pas	Sat Oct 09 18:01:47 2010 +0200
+++ b/hedgewars/PascalExports.pas	Sun Oct 10 02:06:42 2010 +0200
@@ -17,6 +17,7 @@
      uGears, uSound, hwengine, uAmmos, uLocale; // don't change the order!
 
 {$INCLUDE "config.inc"}
+type PPByte = ^PByte;
 
 implementation
 {$IFDEF HWLIBRARY}
@@ -276,33 +277,24 @@
         SetWeapon(TAmmoType(whichone+1));
 end;
 
-function HW_getAmmoCounts: PByte; cdecl; export;
-var counts : PByte;
-    a : PHHAmmo;
+function HW_getAmmoCounts(counts: PLongInt): LongInt; cdecl; export;
+var a : PHHAmmo;
     slot, index: LongInt;
 begin
     if (CurrentTeam^.ExtDriven) or (CurrentTeam^.Hedgehogs[0].BotLevel <> 0) then
-        exit(nil);
+        exit(-1);
     a:= CurrentHedgehog^.Ammo;
-    GetMem(counts,ord(High(TAmmoType)));
-    FillChar(counts^,ord(High(TAmmoType)),0);
     for slot:= 0 to cMaxSlotIndex do
         for index:= 0 to cMaxSlotAmmoIndex do
-            counts[ord(a^[slot,index].AmmoType)-1]:= byte(a^[slot,index].Count);
-    exit(counts);
-    // leak?
+            counts[ord(a^[slot,index].AmmoType)-1]:= a^[slot,index].Count;
+    exit(0);
 end;
 
-function HW_getAmmoDelays: PByte; cdecl; export;
-var skipTurns : PByte;
-    a : TAmmoType;
+procedure HW_getAmmoDelays (skipTurns: PByte); cdecl; export;
+var a : TAmmoType;
 begin
-    GetMem(skipTurns,ord(High(TAmmoType)));
-    FillChar(skipTurns^,ord(High(TAmmoType)),0);
     for a:= Low(TAmmoType) to High(TAmmoType) do
         skipTurns[ord(a)-1]:= byte(Ammoz[a].SkipTurns);
-    exit(skipTurns);
-    // leak?
 end;
 
 function HW_getTurnsForCurrentTeam:LongInt; cdecl; export;
--- a/hedgewars/uMobile.pas	Sat Oct 09 18:01:47 2010 +0200
+++ b/hedgewars/uMobile.pas	Sun Oct 10 02:06:42 2010 +0200
@@ -41,6 +41,7 @@
 procedure perfExt_SaveFinishedSynching;
 
 implementation
+uses uTeams;
 
 function isPhone: Boolean;
 begin
@@ -72,6 +73,8 @@
 procedure perfExt_AmmoUpdate;
 begin
 {$IFDEF IPHONEOS}
+    if (CurrentTeam^.ExtDriven) or (CurrentTeam^.Hedgehogs[0].BotLevel <> 0) then
+        exit(); // the other way around throws a compiler error
     updateVisualsNewTurn();
 {$ENDIF}
 end;
--- a/hedgewars/uStore.pas	Sat Oct 09 18:01:47 2010 +0200
+++ b/hedgewars/uStore.pas	Sun Oct 10 02:06:42 2010 +0200
@@ -1211,7 +1211,7 @@
 {$ENDIF}
 
     // set view port to whole window
-    if rotationQt = 0 then
+    if (rotationQt = 0) or (rotationQt = 180) then
         glViewport(0, 0, cScreenWidth, cScreenHeight)
     else
         glViewport(0, 0, cScreenHeight, cScreenWidth);
--- a/project_files/HedgewarsMobile/Classes/AmmoMenuViewController.h	Sat Oct 09 18:01:47 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/AmmoMenuViewController.h	Sun Oct 10 02:06:42 2010 +0200
@@ -26,7 +26,7 @@
     UIImage *weaponsImage;
     NSArray *buttonsArray;
     
-    unsigned char *delay;
+    uint8_t *delay;
     CGPoint startingPoint;
     BOOL isVisible;
 }
--- a/project_files/HedgewarsMobile/Classes/AmmoMenuViewController.m	Sat Oct 09 18:01:47 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/AmmoMenuViewController.m	Sun Oct 10 02:06:42 2010 +0200
@@ -28,10 +28,13 @@
 @implementation AmmoMenuViewController
 @synthesize weaponsImage, buttonsArray, isVisible;
 
+-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
+    return rotationManager(interfaceOrientation);
+}
 
--(void) viewDidLoad {
-    [super viewDidLoad];
-    
+#pragma mark -
+#pragma mark view handling
+-(void) viewDidLoad {    
     [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(updateAmmoVisuals)
                                                  name:@"updateAmmoVisuals"
@@ -45,21 +48,14 @@
     [self.view.layer setMasksToBounds:YES];
 
     self.isVisible = NO;
-    delay = HW_getAmmoDelays();
+    delay = (uint8_t *) calloc(HW_getNumberOfWeapons(), sizeof(uint8_t));
+    HW_getAmmoDelays(delay);
 
-    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
-    spinner.hidesWhenStopped = YES;
-    spinner.center = self.view.center;
-    [spinner startAnimating];
-    [self.view addSubview:spinner];
-    if (self.buttonsArray == nil)
-        [NSThread detachNewThreadSelector:@selector(loadAmmoStuff:) toTarget:self withObject:spinner];
-    [spinner release];
+    [super viewDidLoad];
 }
 
 -(void) viewWillAppear:(BOOL)animated {
-    if (self.buttonsArray != nil)
-        [self updateAmmoVisuals];
+    [self updateAmmoVisuals];
     [super viewWillAppear:animated];
 }
 
@@ -68,32 +64,31 @@
     [container addSubview:self.view];
     self.view.center = CGPointMake(container.center.y, container.center.x);
     self.isVisible = YES;
-    [self viewDidAppear:YES];                 
 }
 
 -(void) disappear {
-
-    [self.view removeFromSuperview];
+    if (self.isVisible)
+        [self.view removeFromSuperview];
     self.isVisible = NO;
 }
 
+#pragma mark -
+#pragma mark drawing
 -(void) loadAmmoStuff:(id) object {
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)object;
 
     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 *array = [[NSMutableArray alloc] initWithCapacity:HW_getNumberOfWeapons()];
     for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
-        int x_src = ((i*32)/(int)ammoStoreImage.size.height)*32;
-        int y_src = (i*32)%(int)ammoStoreImage.size.height;
         int x_dst = 10+(i%10)*44;
         int y_dst = 10+(i/10)*44;
         
         if (i / 10 % 2 != 0)
             x_dst += 20;
-        UIImage *img = [ammoStoreImage cutAt:CGRectMake(x_src, y_src, 32, 32)];
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake(x_dst, y_dst, 40, 40);
         button.tag = i;
@@ -101,7 +96,6 @@
         button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
         [button.layer setCornerRadius:6];
         [button.layer setMasksToBounds:YES];
-        [button setBackgroundImage:img forState:UIControlStateNormal];
         [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
         [button setTitleColor:UICOLOR_HW_YELLOW_TEXT forState:UIControlStateNormal];
         button.titleLabel.backgroundColor = [UIColor blackColor];
@@ -113,28 +107,17 @@
         [self.view addSubview:button];
         [array addObject:button];
     }
-    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:array,@"array",ammoStoreImage,@"image",spinner,@"spinner",nil];
+    [self performSelectorOnMainThread:@selector(setButtonsArray:) withObject:array waitUntilDone:NO];
     [array release];
-    [ammoStoreImage release];
-
-    [self performSelectorOnMainThread:@selector(ready:) withObject:dict waitUntilDone:NO];
     
+    [self performSelectorOnMainThread:@selector(updateAmmoVisuals) withObject:nil waitUntilDone:NO];
+    UIActivityIndicatorView *spinner = (UIActivityIndicatorView *)object;
+    [spinner stopAnimating];
     [pool drain];
 }
 
--(void) ready:(id) object {
-    NSDictionary *dict = (NSDictionary *)object;
-    [[dict objectForKey:@"spinner"] stopAnimating];
-    self.weaponsImage = [dict objectForKey:@"image"];
-    self.buttonsArray = [dict objectForKey:@"array"];
-    [self updateAmmoVisuals];
-}
-
 -(void) updateAmmoVisuals {
-    unsigned char *loadout = HW_getAmmoCounts();
-    int turns = HW_getTurnsForCurrentTeam();
-
-    if (self.buttonsArray == nil) {
+    if (self.buttonsArray == nil || self.weaponsImage == nil) {
         UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
         spinner.hidesWhenStopped = YES;
         spinner.center = self.view.center;
@@ -142,54 +125,61 @@
         [self.view addSubview:spinner];
         [NSThread detachNewThreadSelector:@selector(loadAmmoStuff:) toTarget:self withObject:spinner];
         [spinner release];
+        return;
     }
     
-    if (loadout == NULL) {
-        self.view.userInteractionEnabled = NO;
-        return;
-    } else
-        self.view.userInteractionEnabled = YES;
+    [NSThread detachNewThreadSelector:@selector(drawingThread) toTarget:self withObject:nil];
+}
 
-    for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
-        UIButton *button = [self.buttonsArray objectAtIndex:i];
-        if (loadout[i] > 0) {
-            if (button.enabled == NO) {
-                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)];
-                [button setBackgroundImage:img forState:UIControlStateNormal];
+-(void) drawingThread {
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    int *loadout = (int *)calloc(HW_getNumberOfWeapons(), sizeof(int));
+    int res = HW_getAmmoCounts(loadout);
+    int turns = HW_getTurnsForCurrentTeam();
+    
+    if (res == 0) {
+        self.view.userInteractionEnabled = YES;
+        
+        for (int i = 0; i < HW_getNumberOfWeapons(); i++) {
+            UIButton *button = [self.buttonsArray objectAtIndex:i];
+            if (loadout[i] > 0) {
+                if (delay[i]-turns >= 0) {
+                    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)];
+                        [button setBackgroundImage:[img convertToGrayScale] forState:UIControlStateNormal];
+                        button.imageView.tag = 10000;
+                    }
+                } else {
+                    button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
+                    [button setTitle:@"" 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)];
+                        [button setBackgroundImage:img forState:UIControlStateNormal];
+                        button.imageView.tag = 0;
+                    }
+                }
+                button.enabled = YES;
+            } else {
+                if (button.enabled == YES)
+                    [button setBackgroundImage:nil forState:UIControlStateNormal];
+                button.layer.borderColor = [[UIColor darkGrayColor] CGColor];
+                button.enabled = NO;
             }
-            button.enabled = YES;
-            button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
-        } else {
-            if (button.enabled == YES)
-                [button setBackgroundImage:nil forState:UIControlStateNormal];
-            button.enabled = NO;
-            button.layer.borderColor = [[UIColor darkGrayColor] CGColor];
+            
         }
-        
-        if (button.enabled == YES) {
-            if (delay[i]-turns >= 0) {
-                button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
-                [button setTitle:[NSString stringWithFormat:@" %d ",delay[i]-turns+1] forState:UIControlStateNormal];
-                if (button.enabled == YES) {
-                    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)];
-                    [button setBackgroundImage:[img convertToGrayScale] forState:UIControlStateNormal];
-                }
-            } else {
-                button.layer.borderColor = [UICOLOR_HW_YELLOW_TEXT CGColor];
-                [button setTitle:@"" forState:UIControlStateNormal];
-                if (button.enabled == YES) {
-                    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)];
-                    [button setBackgroundImage:img forState:UIControlStateNormal];
-                }
-            }
-        }
+    } else {
+        self.view.userInteractionEnabled = NO;
     }
+
+    free(loadout);
+    loadout = NULL;
+    [pool drain];
 }
 
 #pragma mark -
@@ -249,6 +239,7 @@
     [[NSNotificationCenter defaultCenter] removeObserver:self];
     self.weaponsImage = nil;
     self.buttonsArray = nil;
+    free(delay);
     delay = NULL;
     MSG_DIDUNLOAD();
     [super viewDidUnload];
--- a/project_files/HedgewarsMobile/Classes/CommodityFunctions.m	Sat Oct 09 18:01:47 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/CommodityFunctions.m	Sun Oct 10 02:06:42 2010 +0200
@@ -183,12 +183,12 @@
     [theScheme release];
 }
 
-BOOL rotationManager (UIInterfaceOrientation interfaceOrientation) {
+BOOL inline rotationManager (UIInterfaceOrientation interfaceOrientation) {
     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
            (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
 }
 
-NSInteger randomPort () {
+NSInteger inline randomPort () {
     srandom(time(NULL));
     return (random() % 64511) + 1024;
 }
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Sat Oct 09 18:01:47 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Sun Oct 10 02:06:42 2010 +0200
@@ -597,16 +597,16 @@
                 }
             } else
                 initialDistanceForPinching = currentDistanceOfPinching;
-
             break;
         default:
+            DLog(@"Nope");
             break;
     }
 }
 
 #pragma mark -
 #pragma mark Functions called by pascal
-void setGameRunning(BOOL value) {
+void inline setGameRunning(BOOL value) {
     isGameRunning = value;
 }
 
@@ -653,8 +653,10 @@
     theSegment.alpha = 0;
     [UIView commitAnimations];
 
-    [theWindow performSelector:@selector(removeFromSuperview) withObject:theButton afterDelay:ANIMATION_DURATION];
-    [theWindow performSelector:@selector(removeFromSuperview) withObject:theSegment afterDelay:ANIMATION_DURATION];
+    if (theButton)
+        [theWindow performSelector:@selector(removeFromSuperview) withObject:theButton afterDelay:ANIMATION_DURATION];
+    if (theSegment)
+        [theWindow performSelector:@selector(removeFromSuperview) withObject:theSegment afterDelay:ANIMATION_DURATION];
 
     cachedGrenadeTime = 2;
 }
--- a/project_files/HedgewarsMobile/Classes/PascalImports.h	Sat Oct 09 18:01:47 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/PascalImports.h	Sun Oct 10 02:06:42 2010 +0200
@@ -82,8 +82,8 @@
     void HW_setPianoSound(int snd);
     
     void HW_setWeapon(int whichone);
-    unsigned char *HW_getAmmoDelays(void);
-    unsigned char *HW_getAmmoCounts(void);
+    void HW_getAmmoDelays(unsigned char *);
+    int  HW_getAmmoCounts(int *);
     int  HW_getTurnsForCurrentTeam(void);
     int  HW_getNumberOfWeapons(void);
     char *HW_getWeaponNameByIndex(int whichone);
--- a/project_files/HedgewarsMobile/Classes/SDL_uikitappdelegate.m	Sat Oct 09 18:01:47 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/SDL_uikitappdelegate.m	Sun Oct 10 02:06:42 2010 +0200
@@ -158,7 +158,7 @@
     else
         gameWindow = [[UIApplication sharedApplication] keyWindow];
     [gameWindow addSubview:overlayController.view];
-    [overlayController release];
+    //[overlayController release];
 }
 
 // override the direct execution of SDL_main to allow us to implement the frontend (or even using a nib)