project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m
changeset 6841 3633928a3188
parent 6832 fae8fd118da9
child 7248 0ee403b190b5
--- 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 {