cocoaTouch/MapConfigViewController.m
changeset 3492 07256e1ad559
parent 3490 016b3172b645
child 3495 a6b4f351d400
equal deleted inserted replaced
3491:4619b1ae99b5 3492:07256e1ad559
    31 	
    31 	
    32 	SDLNet_TCP_Send(csd, &length , 1);
    32 	SDLNet_TCP_Send(csd, &length , 1);
    33 	return SDLNet_TCP_Send(csd, [string UTF8String], length);
    33 	return SDLNet_TCP_Send(csd, [string UTF8String], length);
    34 }
    34 }
    35 
    35 
    36 -(void) engineProtocol:(NSInteger) port {
    36 -(uint8_t *)engineProtocol:(NSInteger) port {
    37 	IPaddress ip;
    37 	IPaddress ip;
    38 	BOOL serverQuit = NO;
    38 	BOOL serverQuit = NO;
       
    39     uint8_t map[128*32];
    39     
    40     
    40 	if (SDLNet_Init() < 0) {
    41 	if (SDLNet_Init() < 0) {
    41 		NSLog(@"SDLNet_Init: %s", SDLNet_GetError());
    42 		NSLog(@"SDLNet_Init: %s", SDLNet_GetError());
    42         serverQuit = YES;
    43         serverQuit = YES;
    43 	}
    44 	}
    52 	if (!(sd = SDLNet_TCP_Open(&ip))) {
    53 	if (!(sd = SDLNet_TCP_Open(&ip))) {
    53 		NSLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), port);
    54 		NSLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), port);
    54         serverQuit = YES;
    55         serverQuit = YES;
    55 	}
    56 	}
    56 	
    57 	
       
    58     // launch the preview here so that we're sure the tcp channel is open
       
    59     pthread_t thread_id;
       
    60     pthread_create(&thread_id, NULL, (void *)GenLandPreview, (void *)port);
       
    61     pthread_detach(thread_id);
       
    62     
    57 	DLog(@"Waiting for a client on port %d", port);
    63 	DLog(@"Waiting for a client on port %d", port);
    58 	while (!serverQuit) {
    64 	while (!serverQuit) {
    59 		/* This check the sd if there is a pending connection.
    65 		/* This check the sd if there is a pending connection.
    60 		 * If there is one, accept that, and open a new socket for communicating */
    66 		 * If there is one, accept that, and open a new socket for communicating */
    61 		csd = SDLNet_TCP_Accept(sd);
    67 		csd = SDLNet_TCP_Accept(sd);
    77 		}
    83 		}
    78 	}
    84 	}
    79 	
    85 	
    80 	SDLNet_TCP_Close(sd);
    86 	SDLNet_TCP_Close(sd);
    81 	SDLNet_Quit();
    87 	SDLNet_Quit();
       
    88     return map;
    82 }
    89 }
    83 
    90 
    84 -(void) drawingThread {
    91 -(void) drawingThread {
    85     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    92     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    86     
    93     
    87     // select the port for IPC and launch the preview generation
    94     // select the port for IPC and launch the preview generation through engineProtocol:
    88     int port = randomPort();
    95     int port = randomPort();
    89     pthread_t thread_id;
    96     uint8_t *map = [self engineProtocol:port];
    90     pthread_create(&thread_id, NULL, (void *)GenLandPreview, (void *)port);
    97     uint8_t mapExp[128*32*8];
    91     pthread_detach(thread_id);
       
    92     [self engineProtocol:port];
       
    93 
    98 
    94     // draw the buffer (1 pixel per component, 0= transparent 1= color)
    99     // draw the buffer (1 pixel per component, 0= transparent 1= color)
    95     int xc = 0;
   100     int k = 0;
    96     int yc = -1;
       
    97     UIGraphicsBeginImageContext(CGSizeMake(256,128));      
       
    98     CGContextRef context = UIGraphicsGetCurrentContext();       
       
    99     UIGraphicsPushContext(context);  
       
   100     for (int i = 0; i < 32*128; i++) {
   101     for (int i = 0; i < 32*128; i++) {
   101         unsigned char byte = map[i];
   102         unsigned char byte = map[i];
   102         for (int j = 0; j < 8; j++) {
   103         for (int j = 0; j < 8; j++) {
   103             // select the color based on the leftmost bit
   104             // select the color based on the leftmost bit
   104             if ((byte & 0x80) != 0)
   105             if ((byte & 0x80) != 0)
   105                 CGContextSetRGBFillColor(context, 0.5, 0.5, 0.7, 1.0);
   106                 mapExp[k] = 100;
   106             else
   107             else
   107                 CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.0);
   108                 mapExp[k] = 255;
   108             // draw pixel
       
   109             CGContextFillRect(context,CGRectMake(xc,yc,1,1));
       
   110             // move coordinates
       
   111             xc = (xc + 1) % 256;
       
   112             if (xc == 0) yc++;
       
   113             // shift to next bit
   109             // shift to next bit
   114             byte <<= 1;
   110             byte <<= 1;
       
   111             k++;
   115         }
   112         }
   116     }
   113     }
       
   114     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
       
   115     CGContextRef bitmapImage = CGBitmapContextCreate(mapExp, 256, 128, 8, 256, colorspace, kCGImageAlphaNone);
       
   116     CGColorSpaceRelease(colorspace);
       
   117     
       
   118     CGImageRef previewCGImage = CGBitmapContextCreateImage(bitmapImage);
       
   119     UIImage *previewImage = [[UIImage alloc] initWithCGImage:previewCGImage];
       
   120     CGImageRelease(previewCGImage);
       
   121 
       
   122     // set the preview image (autoreleased) in the button and the maxhog label on the main thread to prevent a leak
       
   123     [self performSelectorOnMainThread:@selector(setButtonImage:) withObject:[previewImage makeRoundCornersOfSize:CGSizeMake(12, 12)] waitUntilDone:NO];
       
   124     [self performSelectorOnMainThread:@selector(setLabelText:) withObject:[NSString stringWithFormat:@"%d", maxHogs] waitUntilDone:NO];
       
   125     
       
   126     // restore functionality of button and remove the spinning wheel on the main thread to prevent a leak
       
   127     [self performSelectorOnMainThread:@selector(turnOnWidgets) withObject:nil waitUntilDone:NO];
       
   128     
       
   129     [pool release];
       
   130     //Invoking this method should be avoided as it does not give your thread a chance to clean up any resources it allocated during its execution.
       
   131     //[NSThread exit];
       
   132 
       
   133     /*
       
   134     // http://developer.apple.com/mac/library/qa/qa2001/qa1037.html
       
   135     UIGraphicsBeginImageContext(CGSizeMake(256,128));      
       
   136     CGContextRef context = UIGraphicsGetCurrentContext();       
       
   137     UIGraphicsPushContext(context);  
       
   138 
       
   139     CGContextSetRGBFillColor(context, 0.5, 0.5, 0.7, 1.0);
       
   140     CGContextFillRect(context,CGRectMake(xc,yc,1,1));
       
   141     
   117     UIGraphicsPopContext();
   142     UIGraphicsPopContext();
   118     UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
   143     UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
   119     UIGraphicsEndImageContext();
   144     UIGraphicsEndImageContext();
   120 
       
   121     // set the preview image (autoreleased) in the button and the maxhog label
       
   122     [self.previewButton setBackgroundImage:previewImage forState:UIControlStateNormal];
       
   123     self.maxLabel.text = [NSString stringWithFormat:@"%d", maxHogs];
       
   124     
       
   125     // restore functionality of button and remove the spinning wheel
       
   126     [self turnOnWidgets];
       
   127     UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[self.previewButton viewWithTag:INDICATOR_TAG];
       
   128     [indicator stopAnimating];
       
   129     [indicator removeFromSuperview];
       
   130     
       
   131     [pool release];
       
   132     //Invoking this method should be avoided as it does not give your thread a chance to clean up any resources it allocated during its execution.
       
   133     //[NSThread exit];
       
   134 
       
   135     /*
       
   136     // http://developer.apple.com/mac/library/qa/qa2001/qa1037.html
       
   137     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
       
   138     CGContextRef bitmapImage = CGBitmapContextCreate(mapExp, 128, 32, 8, 128, colorspace, kCGImageAlphaNone);
       
   139     CGColorSpaceRelease(colorspace);
       
   140     
       
   141     CGImageRef previewCGImage = CGBitmapContextCreateImage(bitmapImage);
       
   142     UIImage *previewImage = [[UIImage alloc] initWithCGImage:previewCGImage];
       
   143     CGImageRelease(previewCGImage);
       
   144     */
   145     */
   145 }
   146 }
   146 
   147 
   147 -(IBAction) updatePreview {
   148 -(IBAction) updatePreview {
   148     // don't generate a new preview while it's already generating one
   149     // don't generate a new preview while it's already generating one
   190 -(void) updatePreviewWithMap:(NSInteger) index {
   191 -(void) updatePreviewWithMap:(NSInteger) index {
   191     // change the preview button
   192     // change the preview button
   192     NSString *fileImage = [[NSString alloc] initWithFormat:@"%@/%@/preview.png", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
   193     NSString *fileImage = [[NSString alloc] initWithFormat:@"%@/%@/preview.png", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
   193     UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileImage];
   194     UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileImage];
   194     [fileImage release];
   195     [fileImage release];
   195     [self.previewButton setBackgroundImage:image forState:UIControlStateNormal];
   196     [self.previewButton setImage:[image makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal];
   196     [image release];
   197 
   197     
       
   198     // update label
   198     // update label
   199     maxHogs = 18;
   199     maxHogs = 18;
   200     NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
   200     NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
   201     NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL];
   201     NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL];
   202     [fileCfg release];
   202     [fileCfg release];
   225     self.previewButton.alpha = 1.0f;
   225     self.previewButton.alpha = 1.0f;
   226     self.previewButton.enabled = YES;
   226     self.previewButton.enabled = YES;
   227     self.segmentedControl.enabled = YES;
   227     self.segmentedControl.enabled = YES;
   228     self.slider.enabled = YES;
   228     self.slider.enabled = YES;
   229     busy = NO;
   229     busy = NO;
   230 }
   230     
   231     
   231     UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[self.previewButton viewWithTag:INDICATOR_TAG];
       
   232     if (indicator) {
       
   233         [indicator stopAnimating];
       
   234         [indicator removeFromSuperview];
       
   235     }
       
   236 }
       
   237  
       
   238 -(void) setLabelText:(NSString *)str {
       
   239     self.maxLabel.text = str;
       
   240 }
       
   241 
       
   242 -(void) setButtonImage:(UIImage *)img {
       
   243     [self.previewButton setBackgroundImage:img forState:UIControlStateNormal];
       
   244 }
       
   245 
       
   246 -(void) restoreBackgroundImage {
       
   247     // white rounded rectangle as background image for previewButton
       
   248     UIGraphicsBeginImageContext(CGSizeMake(256,128));      
       
   249     CGContextRef context = UIGraphicsGetCurrentContext();       
       
   250     UIGraphicsPushContext(context);  
       
   251 
       
   252     CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
       
   253     CGContextFillRect(context,CGRectMake(0,0,256,128));
       
   254     
       
   255     UIGraphicsPopContext();
       
   256     UIImage *bkgImg = UIGraphicsGetImageFromCurrentImageContext();
       
   257     UIGraphicsEndImageContext();
       
   258     [self.previewButton setBackgroundImage:[[bkgImg retain] makeRoundCornersOfSize:CGSizeMake(12, 12)] forState:UIControlStateNormal];
       
   259 }
       
   260 
   232 #pragma mark -
   261 #pragma mark -
   233 #pragma mark Table view data source
   262 #pragma mark Table view data source
   234 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   263 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   235     return 1;
   264     return 1;
   236 }
   265 }
   396             
   425             
   397         case 1: // Map
   426         case 1: // Map
   398             mapgen = @"e$mapgen 0";
   427             mapgen = @"e$mapgen 0";
   399             self.slider.enabled = NO;
   428             self.slider.enabled = NO;
   400             self.sizeLabel.text = @".";
   429             self.sizeLabel.text = @".";
       
   430             [self restoreBackgroundImage];
   401             break;
   431             break;
   402             
   432             
   403         case 2: // Maze
   433         case 2: // Maze
   404             mapgen = @"e$mapgen 1";
   434             mapgen = @"e$mapgen 1";
   405             [self sliderChanged:nil];
   435             [self sliderChanged:nil];
   456     [array release];
   486     [array release];
   457     self.mapArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL];
   487     self.mapArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL];
   458 
   488 
   459     self.tableView.rowHeight = 42;
   489     self.tableView.rowHeight = 42;
   460     busy = NO;
   490     busy = NO;
       
   491     
       
   492     // draw a white background
       
   493     [self restoreBackgroundImage];
       
   494     
   461     // initialize some "default" values
   495     // initialize some "default" values
   462     self.sizeLabel.text = NSLocalizedString(@"All",@"");
   496     self.sizeLabel.text = NSLocalizedString(@"All",@"");
   463     self.slider.value = 0.05f;
   497     self.slider.value = 0.05f;
   464     self.segmentedControl.selectedSegmentIndex = 0;
   498     self.segmentedControl.selectedSegmentIndex = 0;
   465 
   499