project_files/HedgewarsMobile/Classes/MapPreviewButtonView.m
branchios-revival
changeset 11133 fa3d227fb308
parent 10108 c68cf030eded
child 11137 14f50dde3e8c
equal deleted inserted replaced
11132:ccf095d96ef4 11133:fa3d227fb308
    81     if (!(sd = SDLNet_TCP_Open(&ip))) {
    81     if (!(sd = SDLNet_TCP_Open(&ip))) {
    82         DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), port);
    82         DLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), port);
    83         serverQuit = YES;
    83         serverQuit = YES;
    84     }
    84     }
    85 
    85 
    86     // launch the preview here so that we're sure the tcp channel is open
    86     // launch the preview in background here so that we're sure the tcp channel is open
    87     pthread_t thread_id;
    87     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
    88     pthread_create(&thread_id, NULL, (void *(*)(void *))GenLandPreview, (void *)port);
    88         NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", port];
    89     pthread_detach(thread_id);
    89         NSString *documentsDirectory = DOCUMENTS_FOLDER();
    90 
    90         
       
    91         NSMutableArray *gameParameters = [[NSMutableArray alloc] initWithObjects:
       
    92                                           @"--internal",
       
    93                                           @"--port", ipcString,
       
    94                                           @"--user-prefix", documentsDirectory,
       
    95                                           @"--landpreview",
       
    96                                           nil];
       
    97         [ipcString release];
       
    98         
       
    99         int argc = [gameParameters count];
       
   100         const char **argv = (const char **)malloc(sizeof(const char*)*argc);
       
   101         for (int i = 0; i < argc; i++)
       
   102             argv[i] = strdup([[gameParameters objectAtIndex:i] UTF8String]);
       
   103         [gameParameters release];
       
   104         
       
   105         RunEngine(argc, argv);
       
   106         
       
   107         // cleanup
       
   108         for (int i = 0; i < argc; i++)
       
   109             free((void *)argv[i]);
       
   110         free(argv);
       
   111     });
       
   112     
    91     DLog(@"Waiting for a client on port %d", port);
   113     DLog(@"Waiting for a client on port %d", port);
    92     while (!serverQuit) {
   114     while (!serverQuit) {
    93         /* This check the sd if there is a pending connection.
   115         /* This check the sd if there is a pending connection.
    94          * If there is one, accept that, and open a new socket for communicating */
   116          * If there is one, accept that, and open a new socket for communicating */
    95         csd = SDLNet_TCP_Accept(sd);
   117         csd = SDLNet_TCP_Accept(sd);
   133     uint8_t unpackedMap[128*32*8];
   155     uint8_t unpackedMap[128*32*8];
   134     [self engineProtocol:unpackedMap];
   156     [self engineProtocol:unpackedMap];
   135 
   157 
   136     // http://developer.apple.com/mac/library/qa/qa2001/qa1037.html
   158     // http://developer.apple.com/mac/library/qa/qa2001/qa1037.html
   137     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
   159     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
   138     CGContextRef bitmapImage = CGBitmapContextCreate(unpackedMap, 256, 128, 8, 256, colorspace, kCGImageAlphaNone);
   160     CGContextRef bitmapImage = CGBitmapContextCreate(unpackedMap, 256, 128, 8, 256, colorspace, (CGBitmapInfo)kCGImageAlphaNone);
   139     CGColorSpaceRelease(colorspace);
   161     CGColorSpaceRelease(colorspace);
   140 
   162 
   141     CGImageRef previewCGImage = CGBitmapContextCreateImage(bitmapImage);
   163     CGImageRef previewCGImage = CGBitmapContextCreateImage(bitmapImage);
   142     CGContextRelease(bitmapImage);
   164     CGContextRelease(bitmapImage);
   143     UIImage *previewImage = [[UIImage alloc] initWithCGImage:previewCGImage];
   165     UIImage *previewImage = [[UIImage alloc] initWithCGImage:previewCGImage];