From d19a1012a4571200214e7e13b2673a22bdd05e6a Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Thu, 30 Jan 2014 15:11:37 -0800 Subject: [PATCH] Adds the possibility to create EAGLView with an EAGLView --- cocos/2d/platform/ios/CCEGLView.h | 9 ++- cocos/2d/platform/ios/CCEGLView.mm | 97 +++++++++++++++++++++--------- 2 files changed, 77 insertions(+), 29 deletions(-) diff --git a/cocos/2d/platform/ios/CCEGLView.h b/cocos/2d/platform/ios/CCEGLView.h index 70e9575608..8001cd0582 100644 --- a/cocos/2d/platform/ios/CCEGLView.h +++ b/cocos/2d/platform/ios/CCEGLView.h @@ -45,7 +45,11 @@ public: static EGLView* createWithSize(const std::string& viewName, Size size, float frameZoomFactor = 1.0f); static EGLView* createWithFullScreen(const std::string& viewName); - virtual bool setContentScaleFactor(float contentScaleFactor); + bool setContentScaleFactor(float contentScaleFactor); + float getContentScaleFactor() const; + bool isRetinaDisplay() const { return getContentScaleFactor() == 2.0; } + + void* getEAGLView() const { return _eaglview; } // overrides virtual bool isOpenGLReady() override; @@ -57,10 +61,11 @@ protected: EGLView(); virtual ~EGLView(); + bool initWithEAGLView(void* eaglview); bool initWithSize(const std::string& viewName, Size size, float frameZoomFactor); bool initWithFullScreen(const std::string& viewName); - void *_glview; + void *_eaglview; }; NS_CC_END diff --git a/cocos/2d/platform/ios/CCEGLView.mm b/cocos/2d/platform/ios/CCEGLView.mm index 520f4fe30d..ea4fc46f98 100644 --- a/cocos/2d/platform/ios/CCEGLView.mm +++ b/cocos/2d/platform/ios/CCEGLView.mm @@ -27,6 +27,8 @@ #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS +#import + #include "CCEAGLView.h" #include "CCDirectorCaller.h" #include "CCEGLView.h" @@ -35,6 +37,17 @@ NS_CC_BEGIN +EGLView* EGLView::createWithEAGLView(void *eaglview) +{ + auto ret = new EGLView; + if(ret && ret->initWithEAGLView(eaglview)) { + ret->autorelease(); + return ret; + } + + return nullptr; +} + EGLView* EGLView::create(const std::string& viewName) { auto ret = new EGLView; @@ -70,41 +83,60 @@ EGLView* EGLView::createWithFullScreen(const std::string& viewName) EGLView::EGLView() { -// CGRect r = CGRectMake(0,0,300,300); -// CCEAGLView *glView = [CCEAGLView viewWithFrame:r -// pixelFormat: kEAGLColorFormatRGB565 -// depthFormat: GL_DEPTH24_STENCIL8_OES -// preserveBackbuffer: NO -// sharegroup: nil -// multiSampling: NO -// numberOfSamples: 0]; -// [__glView setMultipleTouchEnabled:YES]; -// -// _screenSize.width = _designResolutionSize.width = [glview getWidth]; -// _screenSize.height = _designResolutionSize.height = [glview getHeight]; -// -// _glview = glview; } EGLView::~EGLView() { - CCEAGLView *glview = (CCEAGLView*) _glview; + CCEAGLView *glview = (CCEAGLView*) _eaglview; [glview release]; } +bool EGLView::initWithEAGLView(void *eaglview) +{ + _eaglview = eaglview; + CCEAGLView *glview = (CCEAGLView*) _eaglview; + + _screenSize.width = _designResolutionSize.width = [glview getWidth]; + _screenSize.height = _designResolutionSize.height = [glview getHeight]; +// _scaleX = _scaleY = [glview contentScaleFactor]; + + return true; +} + bool EGLView::initWithSize(const std::string& viewName, Size size, float frameZoomFactor) { + CGRect r = CGRectMake(0,0,size.width, size.height); + CCEAGLView *eaglview = [CCEAGLView viewWithFrame: r + pixelFormat: kEAGLColorFormatRGB565 + depthFormat: GL_DEPTH24_STENCIL8_OES + preserveBackbuffer: NO + sharegroup: nil + multiSampling: NO + numberOfSamples: 0]; + [eaglview setMultipleTouchEnabled:YES]; + + _screenSize.width = _designResolutionSize.width = [eaglview getWidth]; + _screenSize.height = _designResolutionSize.height = [eaglview getHeight]; +// _scaleX = _scaleY = [eaglview contentScaleFactor]; + + _eaglview = eaglview; + return true; } bool EGLView::initWithFullScreen(const std::string& viewName) { - return true; + CGRect rect = [[UIScreen mainScreen] bounds]; + Size size; + size.width = rect.size.width; + size.height = rect.size.height; + + return initWithSize(viewName, size, 1); } bool EGLView::isOpenGLReady() { - return _glview != nullptr; + return _eaglview != nullptr; } bool EGLView::setContentScaleFactor(float contentScaleFactor) @@ -112,41 +144,52 @@ bool EGLView::setContentScaleFactor(float contentScaleFactor) CC_ASSERT(_resolutionPolicy == ResolutionPolicy::UNKNOWN); // cannot enable retina mode _scaleX = _scaleY = contentScaleFactor; - CCEAGLView *glview = (CCEAGLView*) _glview; - [glview setNeedsLayout]; + CCEAGLView *eaglview = (CCEAGLView*) _eaglview; + [eaglview setNeedsLayout]; return true; } +float EGLView::getContentScaleFactor() const +{ + CCEAGLView *eaglview = (CCEAGLView*) _eaglview; + + float scaleFactor = [eaglview contentScaleFactor]; + +// CCASSERT(scaleFactor == _scaleX == _scaleY, "Logic error in EGLView::getContentScaleFactor"); + + return scaleFactor; +} + void EGLView::end() { [CCDirectorCaller destroy]; // destroy EAGLView - CCEAGLView *glview = (CCEAGLView*) _glview; + CCEAGLView *eaglview = (CCEAGLView*) _eaglview; - [glview removeFromSuperview]; - [glview release]; + [eaglview removeFromSuperview]; + [eaglview release]; } void EGLView::swapBuffers() { - CCEAGLView *glview = (CCEAGLView*) _glview; - [glview swapBuffers]; + CCEAGLView *eaglview = (CCEAGLView*) _eaglview; + [eaglview swapBuffers]; } void EGLView::setIMEKeyboardState(bool open) { - CCEAGLView *glview = (CCEAGLView*) _glview; + CCEAGLView *eaglview = (CCEAGLView*) _eaglview; if (open) { - [glview becomeFirstResponder]; + [eaglview becomeFirstResponder]; } else { - [glview resignFirstResponder]; + [eaglview resignFirstResponder]; } }