From e9aa3743fa5a7f43ca1c9fc5b93754e41ed45cc5 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 3 Aug 2012 13:56:18 +0800 Subject: [PATCH] issue #1405: muti-resolution works ok on linux. --- cocos2dx/platform/linux/CCApplication.cpp | 31 +++-- cocos2dx/platform/linux/CCApplication.h | 12 +- cocos2dx/platform/linux/CCEGLView.cpp | 133 +++++--------------- cocos2dx/platform/linux/CCEGLView.h | 47 +------ cocos2dx/platform/linux/CCFileUtils.cpp | 135 ++------------------- cocos2dx/platform/win32/CCFileUtils.cpp | 4 - samples/HelloCpp/proj.linux/main.cpp | 16 +-- samples/TestCpp/proj.linux/main.cpp | 11 +- samples/TestJavascript/proj.linux/main.cpp | 16 +-- 9 files changed, 84 insertions(+), 321 deletions(-) diff --git a/cocos2dx/platform/linux/CCApplication.cpp b/cocos2dx/platform/linux/CCApplication.cpp index 1d71c13c1f..703341200a 100644 --- a/cocos2dx/platform/linux/CCApplication.cpp +++ b/cocos2dx/platform/linux/CCApplication.cpp @@ -7,11 +7,14 @@ #include "CCApplication.h" #include #include - +#include #include "CCDirector.h" NS_CC_BEGIN + +static std::string s_strRootResPath = ""; + // sharedApplication pointer CCApplication * CCApplication::sm_pSharedApplication = 0; @@ -58,22 +61,28 @@ int CCApplication::run() return -1; } -void CCApplication::statusBarFrame(CCRect * rect) { - - if (rect) - { - // linux doesn't have status bar. - *rect = CCRectMake(0, 0, 0, 0); - } - -} - void CCApplication::setAnimationInterval(double interval) { //TODO do something else m_nAnimationInterval = interval*1000.0f; } +void CCApplication::setResourceRootPath(const char* pszRootResDir) +{ + if (pszRootResDir) + { + s_strRootResPath = pszRootResDir; + if (s_strRootResPath[s_strRootResPath.length()-1] != '/') + { + s_strRootResPath += '/'; + } + } +} + +const char* CCApplication::getResourceRootPath(void) +{ + return s_strRootResPath.c_str(); +} ////////////////////////////////////////////////////////////////////////// // static member function ////////////////////////////////////////////////////////////////////////// diff --git a/cocos2dx/platform/linux/CCApplication.h b/cocos2dx/platform/linux/CCApplication.h index b40b2d93f7..5ac9ed8668 100644 --- a/cocos2dx/platform/linux/CCApplication.h +++ b/cocos2dx/platform/linux/CCApplication.h @@ -26,11 +26,6 @@ public: */ void setAnimationInterval(double interval); - /** - @brief Get status bar rectangle in EGLView window. - */ - void statusBarFrame(CCRect * rect); - /** @brief Run the message loop. */ @@ -42,8 +37,13 @@ public: */ static CCApplication& sharedApplication(); - /* override functions */ + /* override functions */ virtual ccLanguageType getCurrentLanguage(); + + /* set the Resource root path */ + void setResourceRootPath(const char* pszRootResDir); + /* get the Resource root path */ + const char* getResourceRootPath(void); protected: long m_nAnimationInterval; //micro second diff --git a/cocos2dx/platform/linux/CCEGLView.cpp b/cocos2dx/platform/linux/CCEGLView.cpp index bb714a7b10..0cc4c7375c 100644 --- a/cocos2dx/platform/linux/CCEGLView.cpp +++ b/cocos2dx/platform/linux/CCEGLView.cpp @@ -6,12 +6,8 @@ */ #include "CCEGLView.h" - #include "CCGL.h" - #include "GL/glfw.h" - -#include "CCSet.h" #include "ccMacros.h" #include "CCDirector.h" #include "touch_dispatcher/CCTouch.h" @@ -78,18 +74,10 @@ bool initExtensions() { } NS_CC_BEGIN -static CCEGLView* s_pMainWindow = NULL; CCEGLView::CCEGLView() -: m_bCaptured(false) -, m_bOrientationReverted(false) -, m_bOrientationInitVertical(false) -, bIsInit(false) -, m_fScreenScaleFactor(1.0f) +: bIsInit(false) { - m_pTouch = new CCTouch; - m_pSet = new CCSet; - m_sSizeInPoint.width = m_sSizeInPoint.height = 0; } CCEGLView::~CCEGLView() @@ -117,7 +105,6 @@ void charEventHandle(int iCharID,int iCharState) { // ascii char CCIMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&iCharID, 1); - } void mouseButtonEventHandle(int iMouseID,int iMouseState) { @@ -126,15 +113,14 @@ void mouseButtonEventHandle(int iMouseID,int iMouseState) { int x,y; glfwGetMousePos(&x, &y); CCPoint oPoint((float)x,(float)y); - - if (!CCRect::CCRectContainsPoint(s_pMainWindow->m_rcViewPort,oPoint)) { + /* + if (!CCRect::CCRectContainsPoint(s_pMainWindow->m_rcViewPort,oPoint)) + { CCLOG("not in the viewport"); return; } - + */ int id = 0; - s_pMainWindow->m_pSet->addObject(s_pMainWindow->m_pTouch); - s_pMainWindow->m_mousePoint = oPoint; if (iMouseState == GLFW_PRESS) { CCEGLView::sharedOpenGLView().handleTouchesBegin(1, &id, &oPoint.x, &oPoint.y); @@ -149,27 +135,21 @@ void mousePosEventHandle(int iPosX,int iPosY) { //to test move if (iButtonState == GLFW_PRESS) { - if (iPosX!=(int)s_pMainWindow->m_mousePoint.x||iPosY!=(int)s_pMainWindow->m_mousePoint.y) { - //it movies - float x = (float)(iPosX- s_pMainWindow->m_rcViewPort.origin.x) / s_pMainWindow->m_fScreenScaleFactor; - float y = (float)(iPosY - s_pMainWindow->m_rcViewPort.origin.y) / s_pMainWindow->m_fScreenScaleFactor; - int id = 0; - CCEGLView::sharedOpenGLView().handleTouchesMove(1, &id, &x, &y); - //update new mouse pos - s_pMainWindow->m_mousePoint.x = iPosX; - s_pMainWindow->m_mousePoint.y = iPosY; - } + int id = 0; + float x = (float)iPosX; + float y = (float)iPosY; + CCEGLView::sharedOpenGLView().handleTouchesMove(1, &id, &x, &y); } } -bool CCEGLView::Create(const char* pTitle, int iPixelWidth, int iPixelHeight, int iWidth, int iHeight, int iDepth) { - bool eResult; +void CCEGLView::setSize(float width, float height) +{ + bool eResult = false; int u32GLFWFlags = GLFW_WINDOW; //create the window by glfw. //check - CCAssert(iPixelWidth!=0&&iPixelHeight!=0, "invalid window's size equal 0"); - CCAssert(iWidth!=0&&iHeight!=0, "invalid the size in points equal 0"); + CCAssert(width!=0&&height!=0, "invalid window's size equal 0"); //Inits GLFW eResult = glfwInit() != GL_FALSE; @@ -181,6 +161,7 @@ bool CCEGLView::Create(const char* pTitle, int iPixelWidth, int iPixelHeight, in /* Updates window hint */ glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE); + int iDepth = 16; // set default value /* Depending on video depth */ switch(iDepth) { @@ -188,7 +169,7 @@ bool CCEGLView::Create(const char* pTitle, int iPixelWidth, int iPixelHeight, in case 16: { /* Updates video mode */ - eResult = (glfwOpenWindow(iPixelWidth, iPixelHeight, 5, 6, 5, 0, 16, 0, (int)u32GLFWFlags) != false) ? true : false; + eResult = (glfwOpenWindow(width, height, 5, 6, 5, 0, 16, 0, (int)u32GLFWFlags) != false) ? true : false; break; } @@ -197,7 +178,7 @@ bool CCEGLView::Create(const char* pTitle, int iPixelWidth, int iPixelHeight, in case 24: { /* Updates video mode */ - eResult = (glfwOpenWindow(iPixelWidth, iPixelHeight, 8, 8, 8, 0, 16, 0, (int)u32GLFWFlags) != false) ? true : false; + eResult = (glfwOpenWindow(width, height, 8, 8, 8, 0, 16, 0, (int)u32GLFWFlags) != false) ? true : false; break; } @@ -207,7 +188,7 @@ bool CCEGLView::Create(const char* pTitle, int iPixelWidth, int iPixelHeight, in case 32: { /* Updates video mode */ - eResult = (glfwOpenWindow(iPixelWidth, iPixelHeight, 8, 8, 8, 8, 16, 0, (int)u32GLFWFlags) != GL_FALSE) ? true :false; + eResult = (glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 0, (int)u32GLFWFlags) != GL_FALSE) ? true :false; break; } } @@ -217,32 +198,15 @@ bool CCEGLView::Create(const char* pTitle, int iPixelWidth, int iPixelHeight, in { /* Updates actual size */ - glfwGetWindowSize(&iPixelWidth, &iPixelHeight); + // glfwGetWindowSize(&width, &height); - //assign screen size and point's size - m_sSizeInPixel.width = iPixelWidth; - m_sSizeInPixel.height = iPixelHeight; - - m_sSizeInPoint.width = iWidth; - m_sSizeInPoint.height = iHeight; - - // 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; + CCEGLViewProtocol::setSize(width, height); /* Updates its title */ - glfwSetWindowTitle(pTitle); + glfwSetWindowTitle("Cocos2dx-Linux"); //set the init flag bIsInit = true; - s_pMainWindow = this; //register the glfw key event glfwSetKeyCallback(keyEventHandle); @@ -261,12 +225,6 @@ bool CCEGLView::Create(const char* pTitle, int iPixelWidth, int iPixelHeight, in } initGL(); } - return true; -} - -CCSize CCEGLView::getSize() -{ - return CCSize((float)(m_sSizeInPoint.width), (float)(m_sSizeInPoint.height)); } bool CCEGLView::isOpenGLReady() @@ -278,14 +236,10 @@ void CCEGLView::end() { /* Exits from GLFW */ glfwTerminate(); + delete this; exit(0); } -/* -void CCEGLView::setTouchDelegate(EGLTouchDelegate * pDelegate) { - //TODO touch event - m_pDelegate = pDelegate; -} -*/ + void CCEGLView::swapBuffers() { if (bIsInit) { /* Swap buffers */ @@ -293,32 +247,6 @@ void CCEGLView::swapBuffers() { } } -int CCEGLView::setDeviceOrientation(int eOritation) { - CCLog("warning:could not setDeviceOrientation after initialized"); - return -1; -} - -void CCEGLView::setViewPortInPoints(float x, float y, float w, float h) { -// TODO - if (bIsInit) { - float factor = m_fScreenScaleFactor / CC_CONTENT_SCALE_FACTOR(); - glViewport((GLint)(x * factor) + m_rcViewPort.origin.x, - (GLint)(y * factor) + m_rcViewPort.origin.y, - (GLint)(w * factor), - (GLint)(h * factor)); - } -} -void CCEGLView::setScissorInPoints(float x, float y, float w, float h) { - //TODO - if (bIsInit) { - float factor = m_fScreenScaleFactor / CC_CONTENT_SCALE_FACTOR(); - glScissor((GLint)(x * factor) + m_rcViewPort.origin.x, - (GLint)(y * factor) + m_rcViewPort.origin.y, - (GLint)(w * factor), - (GLint)(h * factor)); - } -} - void CCEGLView::setIMEKeyboardState(bool bOpen) { } @@ -364,19 +292,14 @@ void CCEGLView::destroyGL() */ } -bool CCEGLView::canSetContentScaleFactor() { - return false; -} - -void CCEGLView::setContentScaleFactor(float contentScaleFactor) { - CCLog("could not set contentScaleFactor after initialized"); - -} - CCEGLView& CCEGLView::sharedOpenGLView() { - CC_ASSERT(s_pMainWindow); - return *s_pMainWindow; + static CCEGLView* s_pEglView = NULL; + if (s_pEglView == NULL) + { + s_pEglView = new CCEGLView(); + } + return *s_pEglView; } NS_CC_END diff --git a/cocos2dx/platform/linux/CCEGLView.h b/cocos2dx/platform/linux/CCEGLView.h index fd1a876fb3..1524eb67fe 100644 --- a/cocos2dx/platform/linux/CCEGLView.h +++ b/cocos2dx/platform/linux/CCEGLView.h @@ -16,10 +16,6 @@ bool initExtensions(); NS_CC_BEGIN -class CCSet; -class CCTouch; -class EGLTouchDelegate; - class CCEGLView : public CCEGLViewProtocol{ public: CCEGLView(); @@ -35,31 +31,11 @@ public: * iWidth ,height: the point size, which may scale. * iDepth is not the buffer depth of opengl, it indicate how may bits for a pixel */ - virtual bool Create(const char* pTitle, int iPixelWidth, int iPixelHeight, int iWidth, int iHeight, int iDepth=16); - - CCSize getSize(); - bool isOpenGLReady(); - virtual void end(); - void swapBuffers(); - bool canSetContentScaleFactor(); - void setContentScaleFactor(float contentScaleFactor); - - int setDeviceOrientation(int eOritation); - void setViewPortInPoints(float x, float y, float w, float h); - void setScissorInPoints(float x, float y, float w, float h); - - void setIMEKeyboardState(bool bOpen); - - /** - * not essential - */ -// void centerWindow(); -// void setScreenScale(float factor); - - /** - * the width and height is the real size of phone - */ - void setFrameWidthAndHeight(int width, int height); + virtual void setSize(float width, float height); + virtual bool isOpenGLReady(); + virtual void end(); + virtual void swapBuffers(); + virtual void setIMEKeyboardState(bool bOpen); /** @brief get the shared main open gl window @@ -69,23 +45,10 @@ private: bool initGL(); void destroyGL(); private: - bool m_bCaptured; - bool m_bOrientationReverted; - bool m_bOrientationInitVertical; - CCSet * m_pSet; - CCTouch * m_pTouch; - //store current mouse point for moving, valid if and only if the mouse pressed CCPoint m_mousePoint; - - CCSize m_sSizeInPixel; - CCSize m_sSizeInPoint; - CCRect m_rcViewPort; - bool bIsInit; - int m_eInitOrientation; - float m_fScreenScaleFactor; }; NS_CC_END diff --git a/cocos2dx/platform/linux/CCFileUtils.cpp b/cocos2dx/platform/linux/CCFileUtils.cpp index 71edf75efb..df17a9572e 100644 --- a/cocos2dx/platform/linux/CCFileUtils.cpp +++ b/cocos2dx/platform/linux/CCFileUtils.cpp @@ -6,38 +6,18 @@ */ #include "CCCommon.h" #include "ccMacros.h" - #include "platform/CCFileUtilsCommon_cpp.h" #include "CCFileUtils.h" +#include "CCApplication.h" #include "CCString.h" - #include using namespace std; -#define CC_RETINA_DISPLAY_FILENAME_SUFFIX "-hd" -#define CC_IPAD_FILENAME_SUFFIX "-ipad" -#define CC_IPAD_DISPLAY_RETINA_SUPPFIX "-ipadhd" - - NS_CC_BEGIN -static string s_strResourcePath = ""; - static CCFileUtils* s_pFileUtils = NULL; -static void _CheckPath() -{ -// if (! s_pszResourcePath[0]) -// { -// WCHAR wszPath[MAX_PATH]; -// int nNum = WideCharToMultiByte(CP_ACP, 0, wszPath, -// GetCurrentDirectoryW(sizeof(wszPath), wszPath), -// s_pszResourcePath, MAX_PATH, NULL, NULL); -// s_pszResourcePath[nNum] = '\\'; -// } -} - CCFileUtils* CCFileUtils::sharedFileUtils() { if (s_pFileUtils == NULL) @@ -62,116 +42,23 @@ void CCFileUtils::purgeCachedEntries() } -void CCFileUtils::setResourcePath(const char* pszResourcePath) { - CCAssert(pszResourcePath != NULL, "[FileUtils setResourcePath] -- wrong resource path"); - -// s_strResourcePath = pszResourcePath; - /* Sets current directory */ - if(chdir(pszResourcePath) < 0) - { - CCLog("set base folder error"); - } -} - -const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath, ccResolutionType *pResolutionType) +const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) { - return pszRelativePath; -// CCString* pRet = new CCString(); -// pRet->autorelease(); -// pRet->m_sString = s_strResourcePath + pszRelativePath; -// return pRet->m_sString.c_str(); - //TODO fullPathFromRelativePath - /* - _CheckPath(); + const char* pszRootPath = CCApplication::sharedApplication().getResourceRootPath(); - CCString * pRet = new CCString(); - pRet->autorelease(); - if ((strlen(pszRelativePath) > 1 && pszRelativePath[1] == ':')) + CCString* pRet = CCString::create(pszRootPath); + const char* resDir = CCFileUtils::sharedFileUtils()->getResourceDirectory(); + if (resDir != NULL) { - // path start with "x:", is absolute path - pRet->m_sString = pszRelativePath; + pRet->m_sString += resDir; } - else if (strlen(pszRelativePath) > 0 - && ('/' == pszRelativePath[0] || '\\' == pszRelativePath[0])) + + if (pszRelativePath != NULL) { - // path start with '/' or '\', is absolute path without driver name - char szDriver[3] = {s_strResourcePath[0], s_strResourcePath[1], 0}; - pRet->m_sString = szDriver; - pRet->m_sString += pszRelativePath; - } - else - { - pRet->m_sString = s_strResourcePath; pRet->m_sString += pszRelativePath; } - // is ipad? - CCSize winSize = CCDirector::sharedDirector()->getWinSize(); - bool isIpad = (winSize.width == 1024 || winSize.height == 768); - - std::string hiRes = pRet->m_sString.c_str(); - std::string::size_type pos = hiRes.find_last_of("/\\"); - std::string::size_type dotPos = hiRes.find_last_of("."); - *pResolutionType = kCCResolutioniPhone; - - if (isIpad) - { - if (CC_CONTENT_SCALE_FACTOR() == 1.0f) - { - // ipad - - if (std::string::npos != dotPos && dotPos > pos) - { - hiRes.insert(dotPos, CC_IPAD_FILENAME_SUFFIX); - } - else - { - hiRes.append(CC_IPAD_FILENAME_SUFFIX); - } - - *pResolutionType = kCCResolutioniPad; - } - else - { - // ipad retina - - if (std::string::npos != dotPos && dotPos > pos) - { - hiRes.insert(dotPos, CC_IPAD_DISPLAY_RETINA_SUPPFIX); - } - else - { - hiRes.append(CC_IPAD_DISPLAY_RETINA_SUPPFIX); - } - - *pResolutionType = kCCResolutioniPadRetinaDisplay; - } - } - else - { - if (CC_CONTENT_SCALE_FACTOR() != 1.0f) - { - // iphone retina - if (std::string::npos != dotPos && dotPos > pos) - { - hiRes.insert(dotPos, CC_RETINA_DISPLAY_FILENAME_SUFFIX); - } - else - { - hiRes.append(CC_RETINA_DISPLAY_FILENAME_SUFFIX); - } - - *pResolutionType = kCCResolutioniPhoneRetinaDisplay; - } - } - -// int attrib = GetFileAttributesA(hiRes.c_str()); -// if (attrib != INVALID_FILE_ATTRIBUTES && ! (FILE_ATTRIBUTE_DIRECTORY & attrib)) -// { -// pRet->m_sString.swap(hiRes); -// } - return pRet->m_sString.c_str(); -*/ + return pRet->getCString(); } @@ -216,7 +103,7 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz string CCFileUtils::getWriteablePath() { //return current resource path - return s_strResourcePath; + return CCApplication::sharedApplication().getResourceRootPath(); } NS_CC_END diff --git a/cocos2dx/platform/win32/CCFileUtils.cpp b/cocos2dx/platform/win32/CCFileUtils.cpp index efd178b3b4..7c4fb34583 100644 --- a/cocos2dx/platform/win32/CCFileUtils.cpp +++ b/cocos2dx/platform/win32/CCFileUtils.cpp @@ -26,10 +26,6 @@ THE SOFTWARE. #include #include "CCDirector.h" -#define CC_RETINA_DISPLAY_FILENAME_SUFFIX "-hd" -#define CC_IPAD_FILENAME_SUFFIX "-ipad" -#define CC_IPAD_DISPLAY_RETINA_SUPPFIX "-ipadhd" - using namespace std; NS_CC_BEGIN diff --git a/samples/HelloCpp/proj.linux/main.cpp b/samples/HelloCpp/proj.linux/main.cpp index b0e5d47eb4..ce6d26da81 100644 --- a/samples/HelloCpp/proj.linux/main.cpp +++ b/samples/HelloCpp/proj.linux/main.cpp @@ -5,16 +5,12 @@ #include "CCEGLView.h" USING_NS_CC; -int main(int argc, char **argv) { - +int main(int argc, char **argv) +{ // create the application instance - AppDelegate app; - - CCEGLView * pMainWnd = new CCEGLView(); - if(! pMainWnd || ! pMainWnd->Create("cocos2d: Hello World", 480, 320 ,480, 320)){ - return -1; - } - - CCFileUtils::sharedFileUtils()->setResourcePath("../Resources/"); + AppDelegate app; + CCApplication::sharedApplication().setResourceRootPath("../Resources/"); + CCEGLView& eglView = CCEGLView::sharedOpenGLView(); + eglView.setSize(960, 640 ); return CCApplication::sharedApplication().run(); } diff --git a/samples/TestCpp/proj.linux/main.cpp b/samples/TestCpp/proj.linux/main.cpp index 57bbdf98ec..86827ea32f 100644 --- a/samples/TestCpp/proj.linux/main.cpp +++ b/samples/TestCpp/proj.linux/main.cpp @@ -5,13 +5,10 @@ USING_NS_CC; int main(int argc, char **argv) { // create the application instance - AppDelegate app; + AppDelegate app; + CCApplication::sharedApplication().setResourceRootPath("../Resources/"); + CCEGLView& eglView = CCEGLView::sharedOpenGLView(); + eglView.setSize(480, 320 ); - CCEGLView * pMainWnd = new CCEGLView(); - if(! pMainWnd || ! pMainWnd->Create("cocos2d: Hello World", 480, 320 ,480, 320)){ - return -1; - } - - CCFileUtils::sharedFileUtils()->setResourcePath("../Resources/"); return CCApplication::sharedApplication().run(); } diff --git a/samples/TestJavascript/proj.linux/main.cpp b/samples/TestJavascript/proj.linux/main.cpp index b3db062d19..ad28f877f0 100644 --- a/samples/TestJavascript/proj.linux/main.cpp +++ b/samples/TestJavascript/proj.linux/main.cpp @@ -8,17 +8,9 @@ USING_NS_CC; int main(int argc, char **argv) { // create the application instance - AppDelegate app; - - CCEGLView * pMainWnd = new CCEGLView(); - if(! pMainWnd || ! pMainWnd->Create("cocos2d: Hello World", 480, 320 ,480, 320)){ - return -1; - } - - CCFileUtils::sharedFileUtils()->setResourcePath("../Resources/"); - -// CCEGLView& eglView = CCEGLView::sharedOpenGLView(); -// eglView.setViewName("Hello World"); -// eglView.setFrameSize(480, 320); + AppDelegate app; + CCApplication::sharedApplication().setResourceRootPath("../Resources/"); + CCEGLView& eglView = CCEGLView::sharedOpenGLView(); + eglView.setSize(480, 320 ); return CCApplication::sharedApplication().run(); }