issue #1177: Added some common methods for CCPoint,CCSize,CCRect. Added MutiTouchTest demo.

This commit is contained in:
James Chen 2012-04-23 14:30:38 +08:00
parent 0c6d21c0f9
commit 521155e9e2
18 changed files with 539 additions and 321 deletions

View File

@ -25,6 +25,9 @@ bool AppDelegate::initInstance() {
CC_BREAK_IF(! pMainWnd
|| ! pMainWnd->Create(TEXT("cocos2d: Hello World"), 480, 320));
// set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line.
//pMainWnd->setDesignResolutionSize(480, 320);
#endif // CC_PLATFORM_WIN32
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

View File

@ -25,10 +25,10 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi
{
if (!cocos2d::CCDirector::sharedDirector()->getOpenGLView())
{
cocos2d::CCEGLView *view = &cocos2d::CCEGLView::sharedOpenGLView();
view->setFrameWidthAndHeight(w, h);
cocos2d::CCEGLView *view = &cocos2d::CCEGLView::sharedOpenGLView();
view->setFrameSize(w, h);
// if you want to run in WVGA with HVGA resource, set it
// view->create(480, 320); Please change it to (320, 480) if you're in portrait mode.
// view->setDesignResolutionSize(480, 320); Please change it to (320, 480) if you're in portrait mode.
cocos2d::CCDirector::sharedDirector()->setOpenGLView(view);
AppDelegate *pAppDelegate = new AppDelegate();

View File

@ -61,7 +61,7 @@
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="libcocos2d.lib"
AdditionalDependencies="libcocos2d.lib libGLESv2.lib"
OutputFile="$(OutDir)\$(ProjectName).exe"
LinkIncremental="2"
AdditionalLibraryDirectories="$(OutDir)"

View File

@ -73,15 +73,14 @@ bool CCConfiguration::init(void)
CCLOG("cocos2d: GL supports NPOT textures: %s", (m_bSupportsNPOT ? "YES" : "NO"));
CCLOG("cocos2d: GL supports discard_framebuffer: %s", (m_bSupportsDiscardFramebuffer ? "YES" : "NO"));
bool bEnableProfilers = false;
#if CC_ENABLE_PROFILERS
bEnableProfilers = true;
#endif
CCLOG("cocos2d: compiled with Profiling Support: %s",
#if CC_ENABLE_PROFILERS
"YES - *** Disable it when you finish profiling ***"
#else
"NO"
#endif
);
bEnableProfilers ? "YES - *** Disable it when you finish profiling ***" : "NO");
#if CC_ENABLE_GL_STATE_CACHE == 0
CCLOG("");

View File

@ -23,17 +23,33 @@ THE SOFTWARE.
****************************************************************************/
#include "CCGeometry.h"
#include "ccMacros.h"
// implementation of CCPoint
NS_CC_BEGIN
CCPoint::CCPoint(void)
{
x = 0;
y = 0;
setPoint(0.0f, 0.0f);
}
CCPoint::CCPoint(float x, float y)
{
setPoint(x, y);
}
CCPoint::CCPoint(const CCPoint& other)
{
setPoint(other.x, other.y);
}
CCPoint& CCPoint::operator= (const CCPoint& other)
{
setPoint(other.x, other.y);
return *this;
}
void CCPoint::setPoint(float x, float y)
{
this->x = x;
this->y = y;
@ -48,11 +64,26 @@ bool CCPoint::CCPointEqualToPoint(const CCPoint& point1, const CCPoint& point2)
CCSize::CCSize(void)
{
width = 0;
height = 0;
setSize(0.0f, 0.0f);
}
CCSize::CCSize(float width, float height)
{
setSize(width, height);
}
CCSize::CCSize(const CCSize& other)
{
setSize(other.width, other.height);
}
CCSize& CCSize::operator= (const CCSize& other)
{
setSize(other.width, other.height);
return *this;
}
void CCSize::setSize(float width, float height)
{
this->width = width;
this->height = height;
@ -67,17 +98,29 @@ bool CCSize::CCSizeEqualToSize(const CCSize& size1, const CCSize& size2)
CCRect::CCRect(void)
{
origin.x = 0;
origin.y = 0;
size.width = 0;
size.height = 0;
setRect(0.0f, 0.0f, 0.0f, 0.0f);
}
CCRect::CCRect(float x, float y, float width, float height)
{
setRect(x, y, width, height);
}
CCRect::CCRect(const CCRect& other)
{
setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
}
CCRect& CCRect::operator= (const CCRect& other)
{
setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
return *this;
}
void CCRect::setRect(float x, float y, float width, float height)
{
// Only support that, the width and height > 0
CC_ASSERT(width >= 0 && height >= 0);
CCAssert(width >= 0.0f && height >= 0.0f, "width and height of Rect must not less than 0.");
origin.x = x;
origin.y = y;

View File

@ -38,10 +38,11 @@ public:
float y;
public:
CCPoint();
CCPoint(float x, float y);
CCPoint(const CCPoint& other);
CCPoint& operator= (const CCPoint& other);
void setPoint(float x, float y);
public:
static bool CCPointEqualToPoint(const CCPoint& point1, const CCPoint& point2);
};
@ -55,7 +56,9 @@ public:
public:
CCSize();
CCSize(float width, float height);
CCSize(const CCSize& other);
CCSize& operator= (const CCSize& other);
void setSize(float width, float height);
public:
static bool CCSizeEqualToSize(const CCSize& size1, const CCSize& size2);
};
@ -69,7 +72,9 @@ public:
public:
CCRect();
CCRect(float x, float y, float width, float height);
CCRect(const CCRect& other);
CCRect& operator= (const CCRect& other);
void setRect(float x, float y, float width, float height);
public:
//! return the leftmost x-value of 'rect'
static CCFloat CCRectGetMinX(const CCRect& rect);

View File

@ -6,14 +6,9 @@
#include "CCDictionary.h"
#include "CCInteger.h"
#undef COCOS2D_DEBUG;
#define COCOS2D_DEBUG 1
NS_CC_BEGIN
#define MAX_TOUCHES 5
static CCTouch* s_pTouches[MAX_TOUCHES] = { NULL };
static CCTouch* s_pTouches[CC_MAX_TOUCHES] = { NULL };
static unsigned int s_indexBitsUsed = 0;
static CCDictionary s_TouchesIntergerDict;
@ -22,7 +17,7 @@ static int getUnUsedIndex()
int i;
int temp = s_indexBitsUsed;
for (i = 0; i < MAX_TOUCHES; i++) {
for (i = 0; i < CC_MAX_TOUCHES; i++) {
if (! (temp & 0x00000001)) {
s_indexBitsUsed |= (1 << i);
return i;
@ -37,7 +32,7 @@ static int getUnUsedIndex()
static void removeUsedIndexBit(int index)
{
if (index < 0 || index >= MAX_TOUCHES)
if (index < 0 || index >= CC_MAX_TOUCHES)
{
return;
}
@ -48,8 +43,9 @@ static void removeUsedIndexBit(int index)
}
CCEGLViewProtocol::CCEGLViewProtocol()
: m_pDelegate(NULL)
, m_fScreenScaleFactor(1.0f)
: m_bNeedScale(false)
, m_pDelegate(NULL)
, m_fScreenScaleFactor(1.0f)
{
}
@ -64,9 +60,46 @@ bool CCEGLViewProtocol::isIpad()
return false;
}
void CCEGLViewProtocol::setFrameSize(float width, float height)
{
m_sSizeInPixel.setSize(width, height);
m_rcViewPort.size.setSize(width, height);
}
void CCEGLViewProtocol::setDesignResolutionSize(float width, float height)
{
if (width == 0.0f || height == 0.0f)
{
return;
}
m_sSizeInPoint.setSize(width, height);
// calculate the factor and the rect of viewport
m_fScreenScaleFactor = MIN((float)m_sSizeInPixel.width / m_sSizeInPoint.width,
(float)m_sSizeInPixel.height / m_sSizeInPoint.height);
int viewPortW = (int)(m_sSizeInPoint.width * m_fScreenScaleFactor);
int viewPortH = (int)(m_sSizeInPoint.height * m_fScreenScaleFactor);
m_rcViewPort.origin.x = (m_sSizeInPixel.width - viewPortW) / 2;
m_rcViewPort.origin.y = (m_sSizeInPixel.height - viewPortH) / 2;
m_rcViewPort.size.width = viewPortW;
m_rcViewPort.size.height = viewPortH;
CCLOG("m_fScreenScaleFactor = %f", m_fScreenScaleFactor);
m_bNeedScale = true;
}
CCSize CCEGLViewProtocol::getSize()
{
CCSize size(m_sSizeInPoint.width, m_sSizeInPoint.height);
CCSize size;
if (m_bNeedScale)
{
size.setSize(m_sSizeInPoint.width, m_sSizeInPoint.height);
}
else
{
size.setSize(m_sSizeInPixel.width, m_sSizeInPixel.height);
}
return size;
}
@ -97,20 +130,40 @@ void CCEGLViewProtocol::setContentScaleFactor(float contentScaleFactor)
void CCEGLViewProtocol::setViewPortInPoints(float x , float y , float w , float h)
{
float factor = m_fScreenScaleFactor / CC_CONTENT_SCALE_FACTOR();
glViewport((GLint)(x * factor) + m_rcViewPort.origin.x,
(GLint)(y * factor) + m_rcViewPort.origin.y,
(GLsizei)(w * factor),
(GLsizei)(h * factor));
if (m_bNeedScale)
{
float factor = m_fScreenScaleFactor / CC_CONTENT_SCALE_FACTOR();
glViewport((GLint)(x * factor) + m_rcViewPort.origin.x,
(GLint)(y * factor) + m_rcViewPort.origin.y,
(GLsizei)(w * factor),
(GLsizei)(h * factor));
}
else
{
glViewport((GLint)x,
(GLint)y,
(GLsizei)w,
(GLsizei)h);
}
}
void CCEGLViewProtocol::setScissorInPoints(float x , float y , float w , float h)
{
float factor = m_fScreenScaleFactor / CC_CONTENT_SCALE_FACTOR();
glScissor((GLint)(x * factor) + m_rcViewPort.origin.x,
(GLint)(y * factor) + m_rcViewPort.origin.y,
(GLsizei)(w * factor),
(GLsizei)(h * factor));
if (m_bNeedScale)
{
float factor = m_fScreenScaleFactor / CC_CONTENT_SCALE_FACTOR();
glScissor((GLint)(x * factor) + m_rcViewPort.origin.x,
(GLint)(y * factor) + m_rcViewPort.origin.y,
(GLsizei)(w * factor),
(GLsizei)(h * factor));
}
else
{
glScissor((GLint)x,
(GLint)y,
(GLsizei)w,
(GLsizei)h);
}
}
float CCEGLViewProtocol::getMainScreenScale()

View File

@ -5,6 +5,8 @@
NS_CC_BEGIN
#define CC_MAX_TOUCHES 5
class EGLTouchDelegate;
class CCSet;
@ -22,6 +24,8 @@ public:
virtual bool isIpad();
virtual CCRect getViewPort();
virtual CCSize getSize();
virtual void setFrameSize(float width, float height);
virtual void setDesignResolutionSize(float width, float height);
virtual void setTouchDelegate(EGLTouchDelegate * pDelegate);
virtual float getScreenScaleFactor();
virtual bool canSetContentScaleFactor();
@ -39,6 +43,7 @@ public:
private:
void getSetOfTouchesEndOrCancel(CCSet& set, int num, int ids[], float xs[], float ys[]);
protected:
bool m_bNeedScale;
EGLTouchDelegate* m_pDelegate;
float m_fScreenScaleFactor;
CCSize m_sSizeInPixel;

View File

@ -48,48 +48,15 @@ void initExtensions() {
NS_CC_BEGIN
CCEGLView::CCEGLView()
: m_bNotHVGA(false)
{
initExtensions();
}
void CCEGLView::setFrameWidthAndHeight(int width, int height)
{
m_sSizeInPixel.width = width;
m_sSizeInPixel.height = height;
create(width, height);
}
void CCEGLView::create(int width, int height)
{
if (width == 0 || height == 0)
{
return;
}
m_sSizeInPoint.width = width;
m_sSizeInPoint.height = height;
// calculate the factor and the rect of viewport
m_fScreenScaleFactor = MIN((float)m_sSizeInPixel.width / m_sSizeInPoint.width,
(float)m_sSizeInPixel.height / m_sSizeInPoint.height);
int viewPortW = (int)(m_sSizeInPoint.width * m_fScreenScaleFactor);
int viewPortH = (int)(m_sSizeInPoint.height * m_fScreenScaleFactor);
m_rcViewPort.origin.x = (m_sSizeInPixel.width - viewPortW) / 2;
m_rcViewPort.origin.y = (m_sSizeInPixel.height - viewPortH) / 2;
m_rcViewPort.size.width = viewPortW;
m_rcViewPort.size.height = viewPortH;
CCLog("m_fScreenScaleFactor = %f", m_fScreenScaleFactor);
m_bNotHVGA = true;
}
CCEGLView::~CCEGLView()
{
}
bool CCEGLView::isOpenGLReady()
{
return (m_sSizeInPixel.width != 0 && m_sSizeInPixel.height != 0);

View File

@ -37,23 +37,10 @@ public:
virtual ~CCEGLView();
bool isOpenGLReady();
/**
* the width and height is the real size of phone
*/
void setFrameWidthAndHeight(int width, int height);
/**
* create a drawing rect,
* the width and heiht is the resource size match best
*/
void create(int width, int height);
// keep compatible
void end();
void swapBuffers();
void setIMEKeyboardState(bool bOpen);
// static function
@ -61,10 +48,6 @@ public:
@brief get the shared main open gl window
*/
static CCEGLView& sharedOpenGLView();
private:
bool m_bNotHVGA;
};
NS_CC_END

View File

@ -237,9 +237,6 @@ bool CCEGLView::Create(LPCTSTR pTitle, int w, int h)
CC_BREAK_IF(! m_hWnd);
m_sSizeInPoint.width = (float)w;
m_sSizeInPoint.height = (float)h;
resize(w, h);
// init egl
@ -268,7 +265,8 @@ LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
case WM_LBUTTONDOWN:
if (m_pDelegate && MK_LBUTTON == wParam)
{
CCPoint pt((float)LOWORD(lParam), (float)HIWORD(lParam));
POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
CCPoint pt(point.x, point.y);
if (CCRect::CCRectContainsPoint(m_rcViewPort, pt))
{
m_bCaptured = true;
@ -282,7 +280,8 @@ LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
case WM_MOUSEMOVE:
if (MK_LBUTTON == wParam && m_bCaptured)
{
CCPoint pt((float)LOWORD(lParam), (float)HIWORD(lParam));
POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
CCPoint pt(point.x, point.y);
int id = 0;
handleTouchesMove(1, &id, &pt.x, &pt.y);
}
@ -291,7 +290,8 @@ LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
case WM_LBUTTONUP:
if (m_bCaptured)
{
CCPoint pt((float)LOWORD(lParam), (float)HIWORD(lParam));
POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
CCPoint pt(point.x, point.y);
int id = 0;
handleTouchesEnd(1, &id, &pt.x, &pt.y);
@ -459,21 +459,7 @@ void CCEGLView::resize(int width, int height)
m_pEGL->resizeSurface();
}
// calculate view port in pixels
float viewPortW = m_sSizeInPoint.width * m_fScreenScaleFactor;
float viewPortH = m_sSizeInPoint.height * m_fScreenScaleFactor;
GetClientRect(m_hWnd, &rcClient);
// calculate client new width and height
float newW = rcClient.right - rcClient.left;
float newH = rcClient.bottom - rcClient.top;
// calculate new view port
m_rcViewPort.origin.x = rcClient.left + (newW - viewPortW) / 2;
m_rcViewPort.origin.y = rcClient.top + (newH - viewPortH) / 2;
m_rcViewPort.size.width = viewPortW;
m_rcViewPort.size.height = viewPortH;
setFrameSize(width, height);
}
void CCEGLView::centerWindow()

View File

@ -55,6 +55,7 @@ tests/NodeTest/NodeTest.cpp \
tests/TextInputTest/TextInputTest.cpp \
tests/MenuTest/MenuTest.cpp \
tests/MotionStreakTest/MotionStreakTest.cpp \
tests/MutiTouchTest/MutiTouchTest.cpp \
tests/ParallaxTest/ParallaxTest.cpp \
tests/ParticleTest/ParticleTest.cpp \
tests/PerformanceTest/PerformanceNodeChildrenTest.cpp \

View File

@ -24,9 +24,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi
if (!cocos2d::CCDirector::sharedDirector()->getOpenGLView())
{
cocos2d::CCEGLView *view = &cocos2d::CCEGLView::sharedOpenGLView();
view->setFrameWidthAndHeight(w, h);
view->setFrameSize(w, h);
// if you want to run in WVGA with HVGA resource, set it
// view->create(480, 320);
// view->setDesignResolutionSize(480, 320);
cocos2d::CCDirector::sharedDirector()->setOpenGLView(view);
AppDelegate *pAppDelegate = new AppDelegate();

View File

@ -1179,6 +1179,18 @@
>
</File>
</Filter>
<Filter
Name="MutiTouchTest"
>
<File
RelativePath="..\tests\MutiTouchTest\MutiTouchTest.cpp"
>
</File>
<File
RelativePath="..\tests\MutiTouchTest\MutiTouchTest.h"
>
</File>
</Filter>
</Filter>
</Filter>
</Files>

View File

@ -0,0 +1,129 @@
#include "MutiTouchTest.h"
static ccColor3B s_TouchColors[CC_MAX_TOUCHES] = {
ccYELLOW,
ccBLUE,
ccGREEN,
ccRED,
ccMAGENTA
};
class TouchPoint : public CCNode
{
public:
TouchPoint()
{
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
}
virtual void draw()
{
ccDrawColor4B(m_TouchColor.r, m_TouchColor.g, m_TouchColor.b, 255);
glLineWidth(10);
ccDrawLine( ccp(0, m_pTouchPoint.y), ccp(getContentSize().width, m_pTouchPoint.y) );
ccDrawLine( ccp(m_pTouchPoint.x, 0), ccp(m_pTouchPoint.x, getContentSize().height) );
glLineWidth(1);
ccPointSize(30);
ccDrawPoint(m_pTouchPoint);
}
void setTouchPos(const CCPoint& pt)
{
m_pTouchPoint = pt;
}
void setTouchColor(ccColor3B color)
{
m_TouchColor = color;
}
static TouchPoint* touchPointWithParent(CCNode* pParent)
{
TouchPoint* pRet = new TouchPoint();
pRet->setContentSize(pParent->getContentSize());
pRet->setAnchorPoint(ccp(0.0f, 0.0f));
pRet->autorelease();
return pRet;
}
private:
CCPoint m_pTouchPoint;
ccColor3B m_TouchColor;
};
bool MutiTouchTestLayer::init()
{
if (CCLayer::init())
{
setIsTouchEnabled(true);
return true;
}
return false;
}
static CCDictionary s_dic;
void MutiTouchTestLayer::registerWithTouchDispatcher(void)
{
CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);
}
void MutiTouchTestLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator iter = pTouches->begin();
for (; iter != pTouches->end(); iter++)
{
CCTouch* pTouch = (CCTouch*)(*iter);
TouchPoint* pTouchPoint = TouchPoint::touchPointWithParent(this);
CCPoint location = pTouch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
pTouchPoint->setTouchPos(location);
pTouchPoint->setTouchColor(s_TouchColors[pTouch->getID()]);
addChild(pTouchPoint);
s_dic.setObject(pTouchPoint, pTouch->getID());
}
}
void MutiTouchTestLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator iter = pTouches->begin();
for (; iter != pTouches->end(); iter++)
{
CCTouch* pTouch = (CCTouch*)(*iter);
TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(pTouch->getID());
CCPoint location = pTouch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
pTP->setTouchPos(location);
}
}
void MutiTouchTestLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator iter = pTouches->begin();
for (; iter != pTouches->end(); iter++)
{
CCTouch* pTouch = (CCTouch*)(*iter);
TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(pTouch->getID());
removeChild(pTP, true);
s_dic.removeObjectForKey(pTouch->getID());
}
}
void MutiTouchTestLayer::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)
{
ccTouchesEnded(pTouches, pEvent);
}
void MutiTouchTestScene::runThisTest()
{
MutiTouchTestLayer* pLayer = MutiTouchTestLayer::node();
addChild(pLayer, 0);
CCDirector::sharedDirector()->replaceScene(this);
}

View File

@ -0,0 +1,26 @@
#ifndef __MUTITOUCHTEST_H__
#define __MUTITOUCHTEST_H__
#include "../testBasic.h"
class MutiTouchTestLayer : public CCLayer
{
public:
bool init();
virtual void registerWithTouchDispatcher(void);
virtual void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
virtual void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
virtual void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
virtual void ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
LAYER_NODE_FUNC(MutiTouchTestLayer)
};
class MutiTouchTestScene : public TestScene
{
public:
virtual void runThisTest();
};
#endif /* __MUTITOUCHTEST_H__ */

View File

@ -123,6 +123,9 @@ static TestScene* CreateTestScene(int nIdx)
case TEST_SHADER:
pScene = new ShaderTestScene();
break;
case TEST_MUTITOUCH:
pScene = new MutiTouchTestScene();
break;
default:
break;
}

View File

@ -55,6 +55,7 @@
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
#include "ExtensionsTest/ExtensionsTest.h"
#include "MutiTouchTest/MutiTouchTest.h"
enum
{
@ -102,6 +103,7 @@ enum
TEST_TEXTURECACHE,
TEST_EXTENSIONS,
TEST_SHADER,
TEST_MUTITOUCH,
TESTS_COUNT,
};
@ -149,7 +151,8 @@ const std::string g_aTestNames[TESTS_COUNT] = {
"CurrentLanguageTest",
"TextureCacheTest",
"ExtensionsTest",
"ShaderTest"
"ShaderTest",
"MutiTouchTest"
};
#endif