画了几个小时时间,使用cygwin + toolchain做了一个iphone上的简单程序。 很简陋,并且未作过多的刷新等处理。效果如下。 Makefile为: CC=/usr/local/bin/arm-apple-darwin9-gcc CXX=/usr/local/bin/arm-apple-darwin9-g++ LD=$(CC) CFLAGS=-I/usr/local/lib/gcc/arm-apple-darwin9/4.2.1/include / -isysroot /usr/local/iphone-sysroot LDFLAGS=-framework CoreFoundation -framework CoreGraphics -framework Foundation -framework UIKit -framework GraphicsServices / -lobjc -bind_at_load -isysroot /usr/local/iphone-sysroot all: MyExample MyExample: MyExample.o $(LD) $(LDFLAGS) -o $@ $^ %.o: %.m $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ clean: rm -rf *.o HelloWorld HelloWorld.app 以下是头文件MyExample.h #import <CoreFoundation/CoreFoundation.h> #import <UIKit/UIKit.h> #import <UIKit/UITextView.h> @interface MainView : UIView { UIImage *images[12][9]; int *map[12][9]; CGPoint pos_ori; CGPoint pos_touch; CGPoint pos_old; CGPoint pos_new; CGPoint pos_mid_1; CGPoint pos_mid_2; int img_count; //1 choose one //0 choose no //2 bingo int status; } - (id)initWithFrame:(struct CGRect)windowRect; - (void)mouseDown: (struct _GSEvent *)event; - (void)mouseUp: (struct _GSEvent *)event; - (void)drawRect:(CGRect)rect; - (int)check_link : (int )p1_x p1_y:(int)p1_y p2_x:(int)p2_x p2_y:(int)p2_y; @end @interface MyApp : UIApplication { UIWindow *window; MainView *mainView; NSTimer *timer; } -(void)onTimer; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification; - (void)statusBarMouseDown:(struct _GSEvent *)event; @end 以下是源文件MyExample.m #import <Foundation/Foundation.h> #import <CoreFoundation/CoreFoundation.h> #import <GraphicsServices/GraphicsServices.h> #import "MyExample.h" #include <unistd.h> int main(int argc, char **argv) { NSAutoreleasePool *autoreleasePool = [ [ NSAutoreleasePool alloc ] init ]; UIApplicationUseLegacyEvents(YES); int returnCode = UIApplicationMain(argc, argv, @"MyApp", @"MyApp"); [ autoreleasePool release ]; return returnCode; } @implementation MyApp - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { window = [ [ UIWindow alloc ] initWithContentRect: [ UIHardware fullScreenApplicationContentRect ] ]; timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; CGRect rect = [ UIHardware fullScreenApplicationContentRect ]; rect.origin.x = rect.origin.y = 0.0f; mainView = [ [ MainView alloc ] initWithFrame: rect ]; [ window setContentView: mainView ]; [ window orderFront: self ]; [ window makeKey: self ]; [ window _setHidden: NO ]; } - (void)onTimer { [ mainView reset_status ]; [ mainView setNeedsDisplay ] ; } - (void)statusBarMouseDown:(struct _GSEvent *)event { [ mainView setNeedsDisplay ]; } @end @implementation MainView - (id)initWithFrame:(struct CGRect)windowRect { self = [ super initWithFrame: windowRect ]; if (nil != self) { pos_ori.x = 20; pos_ori.y = 20; srand( (unsigned)time( NULL ) ); img_count = 15; status = 0; int i ,j; for(i = 0; i<12;i++) { for(j = 0; j < 9; j ++) { map[i][j] = 0; } } int v = 0,value = 0; for(i = 1; i<11; i++) { for(j = 1;j < 8; j ++) { if(v % 2 == 0) { value = rand()% 15 + 1; map[i][j] = value; } else { map[i][j] = value; } v ++; } } for(i = 0; i < 200; i ++) { int p_x = rand() % 10 + 1; int p_y = rand() % 7 + 1; int q_x = rand() % 10 + 1; int q_y = rand() % 7 + 1; int temp = map[p_x][p_y]; map[p_x][p_y] = map[q_x][q_y]; map[q_x][q_y] = temp; } for(i = 1; i<11;i ++) { for(j = 1; j<8;j++) { if( map[i][j] != 0) { NSString *myString = [NSString localizedStringWithFormat:@"/Applications/project3_5.app/%d.png", map[i][j] ]; images[i][j] = [ UIImage imageAtPath: myString ]; } } } } return self; } - (void)reset_status{ if(status == 0) { } else if(status == 1) { } else if(status == 2) { status = 0; map[(int)pos_old.x][(int)pos_old.y] = 0; map[(int)pos_new.x][(int)pos_new.y] = 0; } } - (void)drawRect:(CGRect)rect { // float black[4] = { 0, 0, 0, 1 }; CGContextRef ctx = UICurrentContext( ); CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); CGContextSetLineWidth(ctx, 4.0); int i , j; int pos_cur_x, pos_cur_y ; for(i = 1;i<11;i++) { for(j = 1;j<8;j++) { if( map[i][j] != 0) { CGRect pos_img = CGRectMake((j - 1) * 40 + pos_ori.x , (i - 1) * 40 + pos_ori.y , 40, 40); [ images[i][j] draw1PartImageInRect: pos_img ]; } } } if(status == 1) { pos_cur_x = (int)pos_touch.y * 40 - 20; pos_cur_y = (int)pos_touch.x * 40 - 20; CGPoint strokeSegments[] = { CGPointMake(pos_cur_x, pos_cur_y), CGPointMake(pos_cur_x + 40, pos_cur_y), CGPointMake(pos_cur_x + 40, pos_cur_y + 40), CGPointMake(pos_cur_x, pos_cur_y + 40), CGPointMake(pos_cur_x, pos_cur_y), }; CGContextAddLines(ctx, strokeSegments, sizeof(strokeSegments)/sizeof(strokeSegments[0])); CGContextStrokePath(ctx); } if(status == 2) { pos_cur_x = (int)pos_old.y * 40; pos_cur_y = (int)pos_old.x * 40; CGPoint strokeSegments[] = { CGPointMake(pos_cur_x, pos_cur_y), CGPointMake(pos_mid_1.y * 40, pos_mid_1.x * 40), CGPointMake(pos_mid_2.y * 40, pos_mid_2.x * 40), CGPointMake(pos_new.y * 40, pos_new.x * 40), }; CGContextAddLines(ctx, strokeSegments, sizeof(strokeSegments)/sizeof(strokeSegments[0])); CGContextStrokePath(ctx); } } - (int)check_link : (int )p1_x p1_y:(int)p1_y p2_x:(int)p2_x p2_y:(int)p2_y{ int res = 0; int i,j,k,h,small,big; int flag = 0; if(p1_x == p2_x && p1_y == p2_y) { res = 0; } else if(map[p1_x][p1_y] == map[p2_x][p2_y]) { if(p1_x == p2_x) { flag = 0; if(p1_y > p2_y) { small = p2_y; big = p1_y; } else { small = p1_y; big = p2_y; } for(i = small + 1; i<big; i ++) { if(map[p1_x][i] != 0 ) { flag = 1; break; } } if(flag == 0) { pos_mid_1.x = p1_x; pos_mid_1.y = p1_y; pos_mid_2.x = p1_x; pos_mid_2.y = p1_y; res = 1; } if(res == 1) { return res; } } if(p1_y == p2_y) { flag = 0; if(p1_x > p2_x) { small = p2_x; big = p1_x; } else { small = p1_x; big = p2_x; } for(i = small + 1; i<big;i++) { if(map[i][p1_y] != 0 ) { flag = 1; break; } } if(flag == 0) { pos_mid_1.x = p1_x; pos_mid_1.y = p1_y; pos_mid_2.x = p1_x; pos_mid_2.y = p1_y; res = 1; } if(res == 1) { return res; } } flag = 0; //vertical for(i=0;i<12;i++) { flag = 0; if(i > p1_x) { small = p1_x; big = i; } else { small = i; big = p1_x; } for(h = small;h<=big ;h++) { if(h == p1_x) { } else { if(map[h][p1_y]!=0 ) { flag = 1; break; } } } if(flag == 0) { if(i > p2_x) { small = p2_x; big = i; } else { small = i; big = p2_x; } for(h = small;h<=big ;h++) { if(h == p2_x) { } else { if( map[h][p2_y]!=0 ) { flag = 1; break; } } } } if(flag == 0) { if(p1_y <= p2_y) { small = p1_y; big = p2_y; } else { small = p2_y; big = p1_y; } for(h = small + 1; h < big; h ++) { if(map[i][h] !=0) { flag = 1; break; } } } if(flag == 0) { pos_mid_2.x = i; pos_mid_2.y = p1_y; pos_mid_1.x = i; pos_mid_1.y = p2_y; res = 1; return res; } } //horizontal for(j=0;j<9;j++) { flag = 0; if(j > p1_y) { small = p1_y; big = j; } else { small = j; big = p1_y; } for(h = small;h<=big ;h++) { if(h == p1_y) { } else { if(map[p1_x][h]!=0 ) { flag = 1; break; } } } if(flag == 0) { if(j > p2_y) { small = p2_y; big = j; } else { small = j; big = p2_y; } for(h = small;h<=big ;h++) { if(h == p2_y) { } else { if(map[p2_x][h]!=0) { flag = 1; break; } } } } if(flag == 0) { if(p1_x <= p2_x) { small = p1_x; big = p2_x; } else { small = p2_x; big = p1_x; } for(h = small + 1 ; h < big; h ++) { if(map[h][j] !=0 ) { flag = 1; break; } } } if(flag == 0) { pos_mid_2.x = p1_x; pos_mid_2.y = j; pos_mid_1.x = p2_x; pos_mid_1.y = j; res = 1; return res; } } } return res; } - (void)mouseDown: (struct _GSEvent *)event { CGPoint point = GSEventGetLocationInWindow(event); int i; int p_x = (point.y - pos_ori.x) / 40 + 1; int p_y = (point.x - pos_ori.y) / 40 + 1; if(p_x > 0 && p_x < 11 && p_y > 0 && p_y < 8) { // map[p_x][p_y] = 0; if(status == 0 && map[p_x][p_y] != 0) { status = 1; pos_touch.x = p_x; pos_touch.y = p_y; } else if(status == 1) { int res = [ self check_link:pos_touch.x p1_y:pos_touch.y p2_x: p_x p2_y:p_y ]; //int res = 0; if(res == 1) { status = 2; pos_old.x = p_x; pos_old.y = p_y; pos_new.x = pos_touch.x; pos_new.y = pos_touch.y; } else if(res == 0) { status = 1; pos_touch.x = p_x; pos_touch.y = p_y; } } else if(status == 2) { } } [ self setNeedsDisplay]; } - (void)mouseUp: (struct _GSEvent *)event { CGPoint point = GSEventGetLocationInWindow(event); int i; } - (BOOL)canHandleGestures { return YES; } @end