From 6f99f7efde378e028a802030432280ea7a340b5a Mon Sep 17 00:00:00 2001 From: Leon Date: Sat, 26 Nov 2011 22:37:36 +0800 Subject: [PATCH] 1. fix multi touch index. 2. swap. 3. create gl view. --- cocos2dx/platform/ios/EAGLView.mm | 51 +++---- cocos2dx/support/CCPointExtension.cpp | 190 +++++++++++++------------- 2 files changed, 122 insertions(+), 119 deletions(-) diff --git a/cocos2dx/platform/ios/EAGLView.mm b/cocos2dx/platform/ios/EAGLView.mm index 3235c214a1..b9a9476732 100755 --- a/cocos2dx/platform/ios/EAGLView.mm +++ b/cocos2dx/platform/ios/EAGLView.mm @@ -146,12 +146,12 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; [self release]; return nil; } + touchesIntergerDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 4, NULL, NULL); + indexBitsUsed = 0x00000000; + + view = self; } - touchesIntergerDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 4, NULL, NULL); - indexBitsUsed = 0x00000000; - - view = self; return self; } @@ -398,30 +398,33 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; cocos2d::CCTouch *pTouch; for (UITouch *touch in touches) { - NSNumber *index = (NSNumber*)CFDictionaryGetValue(touchesIntergerDict, touch); - int unUsedIndex = 0; + 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; - pTouch->SetTouchInfo(0, x, y); + // 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; + pTouch->SetTouchInfo(0, x, y); + + CFDictionaryAddValue(touchesIntergerDict, touch, [NSNumber numberWithInt:unUsedIndex]); + + set.addObject(pTouch); + } - CFDictionaryAddValue(touchesIntergerDict, touch, [NSNumber numberWithInt:unUsedIndex]); - - set.addObject(pTouch); } + if (set.count() == 0) + return; + cocos2d::CCDirector::sharedDirector()->getOpenGLView()->touchesBegan(&set); } diff --git a/cocos2dx/support/CCPointExtension.cpp b/cocos2dx/support/CCPointExtension.cpp index d51961af79..ebd991301e 100644 --- a/cocos2dx/support/CCPointExtension.cpp +++ b/cocos2dx/support/CCPointExtension.cpp @@ -1,34 +1,34 @@ -/**************************************************************************** -Copyright (c) 2010-2011 cocos2d-x.org +/**************************************************************************** +Copyright (c) 2010-2011 cocos2d-x.org Copyright (c) 2007 Scott Lembcke -Copyright (c) 2010 Lam Pham - -http://www.cocos2d-x.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ - -#include "CCPointExtension.h" -#include "ccMacros.h" // FLT_EPSILON -#include -namespace cocos2d { - +Copyright (c) 2010 Lam Pham + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ + +#include "CCPointExtension.h" +#include "ccMacros.h" // FLT_EPSILON +#include +namespace cocos2d { + #define kCCPointEpsilon FLT_EPSILON CGFloat @@ -72,7 +72,7 @@ float clampf(float value, float min_inclusive, float max_inclusive) float ftmp; ftmp = min_inclusive; min_inclusive = max_inclusive; - max_inclusive = min_inclusive; + max_inclusive = ftmp; } return value < min_inclusive ? min_inclusive : value < max_inclusive? value : max_inclusive; } @@ -119,79 +119,79 @@ CCPoint ccpRotateByAngle(const CCPoint& v, const CCPoint& pivot, float angle) CCPoint r = ccpSub(v, pivot); float cosa = cosf(angle), sina = sinf(angle); float t = r.x; - r.x = t*cosa - r.y*sina + pivot.x; + r.x = t*cosa - r.y*sina + pivot.x; r.y = t*sina + r.y*cosa + pivot.y; return r; } - -bool ccpSegmentIntersect(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D) -{ - float S, T; - - if( ccpLineIntersect(A, B, C, D, &S, &T ) - && (S >= 0.0f && S <= 1.0f && T >= 0.0f && T <= 1.0f) ) - return true; - - return false; -} - -CCPoint ccpIntersectPoint(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D) -{ - float S, T; - - if( ccpLineIntersect(A, B, C, D, &S, &T) ) - { - // Point of intersection - CCPoint P; - P.x = A.x + S * (B.x - A.x); - P.y = A.y + S * (B.y - A.y); - return P; - } - - return CCPointZero; + +bool ccpSegmentIntersect(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D) +{ + float S, T; + + if( ccpLineIntersect(A, B, C, D, &S, &T ) + && (S >= 0.0f && S <= 1.0f && T >= 0.0f && T <= 1.0f) ) + return true; + + return false; +} + +CCPoint ccpIntersectPoint(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D) +{ + float S, T; + + if( ccpLineIntersect(A, B, C, D, &S, &T) ) + { + // Point of intersection + CCPoint P; + P.x = A.x + S * (B.x - A.x); + P.y = A.y + S * (B.y - A.y); + return P; + } + + return CCPointZero; } bool ccpLineIntersect(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D, float *S, float *T) { - // FAIL: Line undefined - if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) ) - { - return false; - } - const float BAx = B.x - A.x; - const float BAy = B.y - A.y; - const float DCx = D.x - C.x; - const float DCy = D.y - C.y; - const float ACx = A.x - C.x; - const float ACy = A.y - C.y; - - const float denom = DCy*BAx - DCx*BAy; - - *S = DCx*ACy - DCy*ACx; - *T = BAx*ACy - BAy*ACx; - - if (denom == 0) - { - if (*S == 0 || *T == 0) - { - // Lines incident - return true; - } - // Lines parallel and not incident - return false; - } - - *S = *S / denom; - *T = *T / denom; - - // Point of intersection - // CGPoint P; - // P.x = A.x + *S * (B.x - A.x); - // P.y = A.y + *S * (B.y - A.y); - + // FAIL: Line undefined + if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) ) + { + return false; + } + const float BAx = B.x - A.x; + const float BAy = B.y - A.y; + const float DCx = D.x - C.x; + const float DCy = D.y - C.y; + const float ACx = A.x - C.x; + const float ACy = A.y - C.y; + + const float denom = DCy*BAx - DCx*BAy; + + *S = DCx*ACy - DCy*ACx; + *T = BAx*ACy - BAy*ACx; + + if (denom == 0) + { + if (*S == 0 || *T == 0) + { + // Lines incident + return true; + } + // Lines parallel and not incident + return false; + } + + *S = *S / denom; + *T = *T / denom; + + // Point of intersection + // CGPoint P; + // P.x = A.x + *S * (B.x - A.x); + // P.y = A.y + *S * (B.y - A.y); + return true; } @@ -201,4 +201,4 @@ float ccpAngle(const CCPoint& a, const CCPoint& b) if( fabs(angle) < kCCPointEpsilon ) return 0.f; return angle; } -}//namespace cocos2d +}//namespace cocos2d