let's outsmart the compiler for no particular benefit
authorkoda
Mon, 02 Apr 2012 00:32:17 +0200
changeset 6841 3633928a3188
parent 6838 b1a0e7a52c04
child 6843 59da15acb2f2
let's outsmart the compiler for no particular benefit
project_files/HedgewarsMobile/Classes/DefinesAndMacros.h
project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m
--- a/project_files/HedgewarsMobile/Classes/DefinesAndMacros.h	Fri Mar 30 23:58:08 2012 +0400
+++ b/project_files/HedgewarsMobile/Classes/DefinesAndMacros.h	Mon Apr 02 00:32:17 2012 +0200
@@ -38,8 +38,8 @@
 #define ZAssert(condition, ...) do { if (!(condition)) { ALog(__VA_ARGS__); }} while(0)
 #define rotationManager(x) (IS_IPAD() ? YES : (x == UIInterfaceOrientationLandscapeRight) || (x == UIInterfaceOrientationLandscapeLeft))
 
-#define START_TIMER NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
-#define END_TIMER(msg) 	NSTimeInterval stop = [NSDate timeIntervalSinceReferenceDate]; CMLog([NSString stringWithFormat:@"%@ Time = %f", msg, stop-start]);
+#define START_TIMER()   NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
+#define END_TIMER(msg) 	NSTimeInterval stop = [NSDate timeIntervalSinceReferenceDate]; DLog([NSString stringWithFormat:@"%@ Time = %f", msg, stop-start]);
 
 
 #define DOCUMENTS_FOLDER()      [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
--- a/project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m	Fri Mar 30 23:58:08 2012 +0400
+++ b/project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m	Mon Apr 02 00:32:17 2012 +0200
@@ -60,10 +60,10 @@
     return SDLNet_TCP_Send(csd, [string UTF8String], length);
 }
 
--(const uint8_t *)engineProtocol {
+-(void) engineProtocol:(uint8_t *)unpackedMap {
     IPaddress ip;
     BOOL serverQuit = NO;
-    static uint8_t map[128*32];
+    uint8_t packedMap[128*32];
     int port = [HWUtils randomPort];
 
     if (SDLNet_Init() < 0) {
@@ -103,50 +103,45 @@
             [self sendToEngine:[dictForEngine objectForKey:@"mazeSizeCommand"]];
             [self sendToEngine:@"!"];
 
-            memset(map, 0, 128*32);
-            SDLNet_TCP_Recv(csd, map, 128*32);
+            memset(packedMap, 0, 128*32);
+            SDLNet_TCP_Recv(csd, packedMap, 128*32);
             SDLNet_TCP_Recv(csd, &maxHogs, sizeof(uint8_t));
 
             SDLNet_TCP_Close(csd);
             serverQuit = YES;
         }
     }
-
     [HWUtils freePort:port];
     SDLNet_TCP_Close(sd);
     SDLNet_Quit();
-    return map;
+
+    // spread the packed bits in an array of bytes (one pixel per element, 0= transparent 1= color)
+    int k = 0;
+    memset(unpackedMap, 255, 128*32*8);     // 255 is white
+    for (int i = 0; i < 32*128; i++) {
+        for (int j = 7; j >= 0; j--) {
+            if (((packedMap[i] >> j) & 0x01) != 0)
+                unpackedMap[k] = 170;       // level of gray [0-255]
+            k++;
+        }
+    }
+    return;
 }
 
 -(void) drawingThread {
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    const uint8_t *map = [self engineProtocol];
-    uint8_t mapExp[128*32*8];
+    uint8_t unpackedMap[128*32*8];
+    [self engineProtocol:unpackedMap];
 
-    // draw the buffer (1 pixel per component, 0= transparent 1= color)
-    int k = 0;
-    for (int i = 0; i < 32*128; i++) {
-        unsigned char byte = map[i];
-        for (int j = 0; j < 8; j++) {
-            // select the color based on the leftmost bit
-            if ((byte & 0x80) != 0)
-                mapExp[k] = 100;
-            else
-                mapExp[k] = 255;
-            // shift to next bit
-            byte <<= 1;
-            k++;
-        }
-    }
+    // http://developer.apple.com/mac/library/qa/qa2001/qa1037.html
     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
-    CGContextRef bitmapImage = CGBitmapContextCreate(mapExp, 256, 128, 8, 256, colorspace, kCGImageAlphaNone);
+    CGContextRef bitmapImage = CGBitmapContextCreate(unpackedMap, 256, 128, 8, 256, colorspace, kCGImageAlphaNone);
     CGColorSpaceRelease(colorspace);
 
     CGImageRef previewCGImage = CGBitmapContextCreateImage(bitmapImage);
     CGContextRelease(bitmapImage);
     UIImage *previewImage = [[UIImage alloc] initWithCGImage:previewCGImage];
     CGImageRelease(previewCGImage);
-    previewCGImage = nil;
 
     // all these are performed on the main thread to prevent a leak
     [self performSelectorOnMainThread:@selector(setImageRounded:)
@@ -164,18 +159,6 @@
                         waitUntilDone:NO];
     
     [pool release];
-
-    /*
-    // http://developer.apple.com/mac/library/qa/qa2001/qa1037.html
-    UIGraphicsBeginImageContext(CGSizeMake(256,128));
-    CGContextRef context = UIGraphicsGetCurrentContext();
-    UIGraphicsPushContext(context);
-    CGContextSetRGBFillColor(context, 0.5, 0.5, 0.7, 1.0);
-    CGContextFillRect(context,CGRectMake(xc,yc,1,1));
-    UIGraphicsPopContext();
-    UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
-    UIGraphicsEndImageContext();
-    */
 }
 
 -(void) updatePreviewWithSeed:(NSString *)seed {