cocoaTouch/MapConfigViewController.m
changeset 3365 37ac593e9027
child 3366 f0e5ff24fb72
equal deleted inserted replaced
3364:e5403e2bf02c 3365:37ac593e9027
       
     1 //
       
     2 //  MapConfigViewController.m
       
     3 //  HedgewarsMobile
       
     4 //
       
     5 //  Created by Vittorio on 22/04/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "MapConfigViewController.h"
       
    10 #import "PascalImports.h"
       
    11 #import "CommodityFunctions.h"
       
    12 #import "UIImageExtra.h"
       
    13 #import "SDL_net.h"
       
    14 #import <pthread.h>
       
    15 
       
    16 @implementation MapConfigViewController
       
    17 @synthesize previewButton, maxHogs, seedCommand;
       
    18 
       
    19 
       
    20 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    21     return rotationManager(interfaceOrientation);
       
    22 }
       
    23 
       
    24 #pragma mark -
       
    25 #pragma mark Preview Handling
       
    26 -(int) sendToEngine: (NSString *)string {
       
    27 	unsigned char length = [string length];
       
    28 	
       
    29 	SDLNet_TCP_Send(csd, &length , 1);
       
    30 	return SDLNet_TCP_Send(csd, [string UTF8String], length);
       
    31 }
       
    32 
       
    33 -(void) engineProtocol:(NSInteger) port {
       
    34 	IPaddress ip;
       
    35 	BOOL clientQuit, serverQuit;
       
    36 
       
    37     serverQuit = NO;
       
    38     clientQuit =NO;
       
    39 	if (SDLNet_Init() < 0) {
       
    40 		NSLog(@"SDLNet_Init: %s", SDLNet_GetError());
       
    41         serverQuit = YES;
       
    42 	}
       
    43 	
       
    44 	/* Resolving the host using NULL make network interface to listen */
       
    45 	if (SDLNet_ResolveHost(&ip, NULL, port) < 0) {
       
    46 		NSLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError());
       
    47         serverQuit = YES;
       
    48 	}
       
    49 	
       
    50 	/* Open a connection with the IP provided (listen on the host's port) */
       
    51 	if (!(sd = SDLNet_TCP_Open(&ip))) {
       
    52 		NSLog(@"SDLNet_TCP_Open: %s %\n", SDLNet_GetError(), port);
       
    53         serverQuit = YES;
       
    54 	}
       
    55 	
       
    56 	NSLog(@"engineProtocol - Waiting for a client on port %d", port);
       
    57 	while (!serverQuit) {
       
    58 		/* This check the sd if there is a pending connection.
       
    59 		 * If there is one, accept that, and open a new socket for communicating */
       
    60 		csd = SDLNet_TCP_Accept(sd);
       
    61 		if (NULL != csd) {			
       
    62 			NSLog(@"engineProtocol - Client found");
       
    63             
       
    64             [self sendToEngine:self.seedCommand];
       
    65             [self sendToEngine:@"e$template_filter 1"];
       
    66             [self sendToEngine:@"e$mapgen 0"];
       
    67             [self sendToEngine:@"e$maze_size 1"];
       
    68             [self sendToEngine:@"!"];
       
    69                 
       
    70             memset(map, 0, 128*32);
       
    71             SDLNet_TCP_Recv(csd, map, 128*32);
       
    72             SDLNet_TCP_Recv(csd, &maxHogs, sizeof(Uint8));
       
    73 
       
    74 			SDLNet_TCP_Close(csd);
       
    75 			serverQuit = YES;
       
    76 		}
       
    77 	}
       
    78 	
       
    79 	SDLNet_TCP_Close(sd);
       
    80 	SDLNet_Quit();
       
    81 }
       
    82 
       
    83 
       
    84 -(void) updatePreview {
       
    85     pthread_t thread_id;
       
    86     
       
    87     // generate a seed
       
    88     char randomStr[36];
       
    89     for (int i = 0; i<36; i++) {
       
    90          randomStr[i] = random()%255;
       
    91     }
       
    92     NSString *seedCmd = [[NSString alloc] initWithFormat:@"eseed {%s}", randomStr];
       
    93     self.seedCommand = seedCmd;
       
    94     [seedCmd release];
       
    95     
       
    96     // select the port for IPC
       
    97     int port = randomPort();
       
    98     pthread_create(&thread_id, NULL, (void *)GenLandPreview, (void *)port);
       
    99     [self engineProtocol:port];
       
   100 
       
   101     // draw the buffer (1 pixel per component, 0= transparent 1= color)
       
   102     int xc = 0;
       
   103     int yc = 0;
       
   104     UIGraphicsBeginImageContext(CGSizeMake(256,128));      
       
   105     CGContextRef context = UIGraphicsGetCurrentContext();       
       
   106     UIGraphicsPushContext(context);  
       
   107     for (int x = 0; x < 32*128; x++) {
       
   108         unsigned char byte = map[x];
       
   109         for (int z = 0; z < 8; z++) {
       
   110             // select the color
       
   111             if ((byte & 0x00000001) != 0)
       
   112                 CGContextSetRGBFillColor(context, 0.5, 0.5, 0.7, 1.0);
       
   113             else
       
   114                 CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.0);
       
   115             
       
   116             // draw pixel
       
   117             CGContextFillRect(context,CGRectMake(xc,yc,1,1));
       
   118             // move coordinates
       
   119             xc = (xc+1)%256;
       
   120             if (xc == 0) yc++;
       
   121             
       
   122             // shift to next bit
       
   123             byte = byte >> 1;
       
   124         }
       
   125     }
       
   126     UIGraphicsPopContext();
       
   127     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
       
   128     UIGraphicsEndImageContext();
       
   129 
       
   130     /*
       
   131     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
       
   132     CGContextRef bitmapImage = CGBitmapContextCreate(mapExp, 128, 32, 8, 128, colorspace, kCGImageAlphaNone);
       
   133     CGColorSpaceRelease(colorspace);
       
   134     
       
   135     CGImageRef previewCGImage = CGBitmapContextCreateImage(bitmapImage);
       
   136     UIImage *previewImage = [[UIImage alloc] initWithCGImage:previewCGImage];
       
   137     CGImageRelease(previewCGImage);
       
   138     */
       
   139     
       
   140     // set the image in the button
       
   141     [self.previewButton setImage:image forState:UIControlStateNormal];
       
   142 }
       
   143 
       
   144 #pragma mark -
       
   145 #pragma mark view management
       
   146 -(void) viewDidLoad {
       
   147     srandom(time(NULL));
       
   148     [super viewDidLoad];
       
   149 
       
   150     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
       
   151     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
       
   152     
       
   153     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       
   154     button.frame = CGRectMake(32, 32, 256, 128);
       
   155     [button addTarget:self action:@selector(updatePreview) forControlEvents:UIControlEventTouchUpInside];
       
   156     self.previewButton = button;
       
   157     [button release];
       
   158     [self.view addSubview:self.previewButton];
       
   159 }
       
   160 
       
   161 -(void) viewWillAppear:(BOOL)animated {
       
   162     [super viewWillAppear:animated];
       
   163     [self updatePreview];
       
   164 }
       
   165 
       
   166 -(void) didReceiveMemoryWarning {
       
   167     // Releases the view if it doesn't have a superview.
       
   168     [super didReceiveMemoryWarning];
       
   169     // Release any cached data, images, etc that aren't in use.
       
   170 }
       
   171 
       
   172 #pragma mark -
       
   173 #pragma mark memory
       
   174 -(void) viewDidUnload {
       
   175     self.previewButton = nil;
       
   176     self.seedCommand = nil;
       
   177     [super viewDidUnload];
       
   178     // Release any retained subviews of the main view.
       
   179     // e.g. self.myOutlet = nil;
       
   180 }
       
   181 
       
   182 -(void) dealloc {
       
   183     [previewButton release];
       
   184     [seedCommand release];
       
   185     [super dealloc];
       
   186 }
       
   187 
       
   188 
       
   189 @end