From aa826970eb7f65e46d851884af50e5d61683ce85 Mon Sep 17 00:00:00 2001 From: minggo Date: Fri, 26 Nov 2010 11:38:10 +0800 Subject: [PATCH] fixed #224 --- cocos2dx/platform/iphone/EAGLView.h | 4 ++ cocos2dx/platform/iphone/EAGLView.mm | 65 +++++++++++++++++-- .../test.iphone/Classes/iphoneAppDelegate.mm | 1 + 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/cocos2dx/platform/iphone/EAGLView.h b/cocos2dx/platform/iphone/EAGLView.h index 76f6b89ba8..5c3b7c70e5 100755 --- a/cocos2dx/platform/iphone/EAGLView.h +++ b/cocos2dx/platform/iphone/EAGLView.h @@ -89,6 +89,7 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. BOOL discardFramebufferSupported_; @private CFMutableDictionaryRef touchesIntergerDict; + unsigned int indexBitsUsed; } /** creates an initializes an EAGLView with a frame and 0-bit depth buffer, and a RGB565 color buffer */ @@ -119,6 +120,7 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. @property(nonatomic,readonly) EAGLContext *context; @property(readonly) CFMutableDictionaryRef touchesIntergerDict; +@property(readwrite) unsigned int indexBitsUsed; /** EAGLView uses double-buffer. This method swaps the buffers */ -(void) swapBuffers; @@ -129,4 +131,6 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. -(int) getWidth; -(int) getHeight; +-(int) getUnUsedIndex; +-(void) removeUsedIndexBit:(int) index; @end diff --git a/cocos2dx/platform/iphone/EAGLView.mm b/cocos2dx/platform/iphone/EAGLView.mm index 6f4284204e..759e7a4fd0 100755 --- a/cocos2dx/platform/iphone/EAGLView.mm +++ b/cocos2dx/platform/iphone/EAGLView.mm @@ -71,8 +71,10 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. //CLASS IMPLEMENTATIONS: +#define MAX_TOUCHES 11 + static EAGLView *view; -static cocos2d::CCTouch *s_pTouches[4]; +static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; @interface EAGLView (Private) -(BOOL) setupSurface; @@ -84,6 +86,7 @@ static cocos2d::CCTouch *s_pTouches[4]; @synthesize pixelFormat=pixelformat_, depthFormat=depthFormat_; @synthesize context=context_; @synthesize touchesIntergerDict; +@synthesize indexBitsUsed; + (Class) layerClass { @@ -113,6 +116,7 @@ static cocos2d::CCTouch *s_pTouches[4]; - (id) init { touchesIntergerDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 4, NULL, NULL); + indexBitsUsed = 0x00000000; return self; } @@ -175,6 +179,35 @@ static cocos2d::CCTouch *s_pTouches[4]; return bound.height; } +-(int) getUnUsedIndex +{ + int i; + int temp = indexBitsUsed; + + for (i = 0; i < MAX_TOUCHES; i++) { + if (! (temp & 0x00000001)) { + indexBitsUsed |= (1 << i); + return i; + } + + temp >>= 1; + } + + // all bits are used + return -1; +} + +-(void) removeUsedIndexBit:(int) index +{ + if (index < 0 || index >= MAX_TOUCHES) { + return; + } + + unsigned int temp = 1 << index; + temp = ~temp; + indexBitsUsed &= temp; +} + -(BOOL) setupSurface { CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; @@ -266,18 +299,31 @@ static cocos2d::CCTouch *s_pTouches[4]; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { cocos2d::NSSet set; - int i = 0; + cocos2d::CCTouch *pTouch; + for (UITouch *touch in touches) { - s_pTouches[i] = new cocos2d::CCTouch(); + NSNumber *index = (NSNumber*)CFDictionaryGetValue(touchesIntergerDict, touch); + int unUsedIndex = 0; + // it is a new touch + if (! index) { + unUsedIndex = [self getUnUsedIndex]; + + // The touches is more than MAX_TOUCHES ? + if (unUsedIndex == -1) { + return; + } + + pTouch = s_pTouches[unUsedIndex] = new cocos2d::CCTouch(); + } + float x = [touch locationInView: [touch view]].x; float y = [touch locationInView: [touch view]].y; - s_pTouches[i] ->SetTouchInfo(0, x, y); + pTouch->SetTouchInfo(0, x, y); - CFDictionaryAddValue(touchesIntergerDict, touch, [NSNumber numberWithInt:i]); + CFDictionaryAddValue(touchesIntergerDict, touch, [NSNumber numberWithInt:unUsedIndex]); - set.addObject(s_pTouches[i]); - ++i; + set.addObject(pTouch); } cocos2d::CCDirector::sharedDirector()->getOpenGLView()->touchesBegan(&set); @@ -332,10 +378,13 @@ static cocos2d::CCTouch *s_pTouches[4]; set.addObject(pTouch); CFDictionaryRemoveValue(touchesIntergerDict, touch); pTouch->release(); + s_pTouches[[index intValue]] = NULL; + [self removeUsedIndexBit:[index intValue]]; } cocos2d::CCDirector::sharedDirector()->getOpenGLView()->touchesEnded(&set); } + - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { cocos2d::NSSet set; @@ -359,6 +408,8 @@ static cocos2d::CCTouch *s_pTouches[4]; set.addObject(pTouch); CFDictionaryRemoveValue(touchesIntergerDict, touch); pTouch->release(); + s_pTouches[[index intValue]] = NULL; + [self removeUsedIndexBit:[index intValue]]; } cocos2d::CCDirector::sharedDirector()->getOpenGLView()->touchesCancelled(&set); diff --git a/tests/test.iphone/Classes/iphoneAppDelegate.mm b/tests/test.iphone/Classes/iphoneAppDelegate.mm index 25a7ecdafd..30bccfd269 100644 --- a/tests/test.iphone/Classes/iphoneAppDelegate.mm +++ b/tests/test.iphone/Classes/iphoneAppDelegate.mm @@ -27,6 +27,7 @@ pixelFormat: kEAGLColorFormatRGBA8 depthFormat: 0 preserveBackbuffer: NO]; + [__glView setMultipleTouchEnabled:YES]; [window addSubview: __glView]; [window makeKeyAndVisible];