2010-11-12 17:16:15 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2014-01-07 11:25:07 +08:00
|
|
|
http://www.cocos2d-x.org
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2014-01-07 11:25:07 +08:00
|
|
|
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:
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2014-01-07 11:25:07 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2014-01-07 11:25:07 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-01-28 08:44:10 +08:00
|
|
|
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
2014-01-28 08:44:10 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
|
|
|
|
|
2014-01-31 07:11:37 +08:00
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "CCEAGLView-ios.h"
|
|
|
|
#include "CCDirectorCaller-ios.h"
|
|
|
|
#include "CCGLViewImpl-ios.h"
|
2011-03-09 16:19:20 +08:00
|
|
|
#include "CCSet.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCTouch.h"
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2014-08-21 09:35:32 +08:00
|
|
|
void* GLViewImpl::_pixelFormat = kEAGLColorFormatRGB565;
|
|
|
|
int GLViewImpl::_depthFormat = GL_DEPTH_COMPONENT16;
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
GLViewImpl* GLViewImpl::createWithEAGLView(void *eaglview)
|
2014-01-31 07:11:37 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto ret = new (std::nothrow) GLViewImpl;
|
2014-01-31 07:11:37 +08:00
|
|
|
if(ret && ret->initWithEAGLView(eaglview)) {
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
GLViewImpl* GLViewImpl::create(const std::string& viewName)
|
2014-01-24 07:36:55 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto ret = new (std::nothrow) GLViewImpl;
|
2014-01-31 07:40:56 +08:00
|
|
|
if(ret && ret->initWithFullScreen(viewName)) {
|
2014-01-24 07:36:55 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
GLViewImpl* GLViewImpl::createWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
|
2014-01-24 07:36:55 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto ret = new (std::nothrow) GLViewImpl;
|
2014-01-31 07:40:56 +08:00
|
|
|
if(ret && ret->initWithRect(viewName, rect, frameZoomFactor)) {
|
2014-01-24 07:36:55 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName)
|
2014-01-24 07:36:55 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto ret = new (std::nothrow) GLViewImpl();
|
2014-01-24 07:36:55 +08:00
|
|
|
if(ret && ret->initWithFullScreen(viewName)) {
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-22 10:55:39 +08:00
|
|
|
void GLViewImpl::convertAttrs()
|
2014-08-21 09:35:32 +08:00
|
|
|
{
|
2014-08-22 16:22:16 +08:00
|
|
|
if(_glContextAttrs.redBits==8 && _glContextAttrs.greenBits==8 && _glContextAttrs.blueBits==8 && _glContextAttrs.alphaBits==8)
|
2014-08-21 09:35:32 +08:00
|
|
|
{
|
|
|
|
_pixelFormat = kEAGLColorFormatRGBA8;
|
|
|
|
}
|
2014-08-22 16:22:16 +08:00
|
|
|
if(_glContextAttrs.depthBits==24 && _glContextAttrs.stencilBits==8)
|
2014-08-21 09:35:32 +08:00
|
|
|
{
|
|
|
|
_depthFormat = GL_DEPTH24_STENCIL8_OES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
GLViewImpl::GLViewImpl()
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
GLViewImpl::~GLViewImpl()
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2014-09-15 16:23:57 +08:00
|
|
|
//CCEAGLView *glview = (CCEAGLView*) _eaglview;
|
2014-08-28 01:26:09 +08:00
|
|
|
//[glview release];
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
bool GLViewImpl::initWithEAGLView(void *eaglview)
|
2014-01-31 07:11:37 +08:00
|
|
|
{
|
|
|
|
_eaglview = eaglview;
|
|
|
|
CCEAGLView *glview = (CCEAGLView*) _eaglview;
|
|
|
|
|
|
|
|
_screenSize.width = _designResolutionSize.width = [glview getWidth];
|
|
|
|
_screenSize.height = _designResolutionSize.height = [glview getHeight];
|
|
|
|
// _scaleX = _scaleY = [glview contentScaleFactor];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
|
2014-01-24 07:36:55 +08:00
|
|
|
{
|
2014-01-31 07:40:56 +08:00
|
|
|
CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
|
2014-08-22 10:55:39 +08:00
|
|
|
convertAttrs();
|
2014-01-31 07:11:37 +08:00
|
|
|
CCEAGLView *eaglview = [CCEAGLView viewWithFrame: r
|
2014-08-21 09:35:32 +08:00
|
|
|
pixelFormat: (NSString*)_pixelFormat
|
|
|
|
depthFormat: _depthFormat
|
2014-01-31 07:11:37 +08:00
|
|
|
preserveBackbuffer: NO
|
|
|
|
sharegroup: nil
|
|
|
|
multiSampling: NO
|
|
|
|
numberOfSamples: 0];
|
2014-08-21 09:35:32 +08:00
|
|
|
|
2014-01-31 07:11:37 +08:00
|
|
|
[eaglview setMultipleTouchEnabled:YES];
|
|
|
|
|
2014-08-22 16:22:16 +08:00
|
|
|
_screenSize.width = _designResolutionSize.width = [eaglview getWidth];
|
|
|
|
_screenSize.height = _designResolutionSize.height = [eaglview getHeight];
|
2014-01-31 07:11:37 +08:00
|
|
|
// _scaleX = _scaleY = [eaglview contentScaleFactor];
|
|
|
|
|
|
|
|
_eaglview = eaglview;
|
|
|
|
|
2014-01-24 07:36:55 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
bool GLViewImpl::initWithFullScreen(const std::string& viewName)
|
2014-01-24 07:36:55 +08:00
|
|
|
{
|
2014-01-31 07:11:37 +08:00
|
|
|
CGRect rect = [[UIScreen mainScreen] bounds];
|
2014-01-31 07:40:56 +08:00
|
|
|
Rect r;
|
|
|
|
r.origin.x = rect.origin.x;
|
|
|
|
r.origin.y = rect.origin.y;
|
|
|
|
r.size.width = rect.size.width;
|
|
|
|
r.size.height = rect.size.height;
|
2014-01-31 07:11:37 +08:00
|
|
|
|
2014-01-31 07:40:56 +08:00
|
|
|
return initWithRect(viewName, r, 1);
|
2014-01-24 07:36:55 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
bool GLViewImpl::isOpenGLReady()
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2014-01-31 07:11:37 +08:00
|
|
|
return _eaglview != nullptr;
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
2014-01-24 07:36:55 +08:00
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
bool GLViewImpl::setContentScaleFactor(float contentScaleFactor)
|
2012-04-23 15:40:46 +08:00
|
|
|
{
|
2014-01-28 11:16:09 +08:00
|
|
|
CC_ASSERT(_resolutionPolicy == ResolutionPolicy::UNKNOWN); // cannot enable retina mode
|
2014-01-24 07:36:55 +08:00
|
|
|
_scaleX = _scaleY = contentScaleFactor;
|
2014-01-28 11:16:09 +08:00
|
|
|
|
2014-01-31 07:11:37 +08:00
|
|
|
CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
|
|
|
|
[eaglview setNeedsLayout];
|
2014-01-24 07:36:55 +08:00
|
|
|
|
|
|
|
return true;
|
2012-04-23 15:40:46 +08:00
|
|
|
}
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
float GLViewImpl::getContentScaleFactor() const
|
2014-01-31 07:11:37 +08:00
|
|
|
{
|
|
|
|
CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
|
|
|
|
|
|
|
|
float scaleFactor = [eaglview contentScaleFactor];
|
|
|
|
|
2014-01-31 07:40:56 +08:00
|
|
|
// CCASSERT(scaleFactor == _scaleX == _scaleY, "Logic error in GLView::getContentScaleFactor");
|
2014-01-31 07:11:37 +08:00
|
|
|
|
|
|
|
return scaleFactor;
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLViewImpl::end()
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2013-06-21 08:46:22 +08:00
|
|
|
[CCDirectorCaller destroy];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// destroy EAGLView
|
2014-01-31 07:11:37 +08:00
|
|
|
CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
|
2014-01-28 11:16:09 +08:00
|
|
|
|
2014-01-31 07:11:37 +08:00
|
|
|
[eaglview removeFromSuperview];
|
2014-08-28 01:26:09 +08:00
|
|
|
//[eaglview release];
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLViewImpl::swapBuffers()
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2014-01-31 07:11:37 +08:00
|
|
|
CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
|
|
|
|
[eaglview swapBuffers];
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
2011-04-19 18:11:57 +08:00
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLViewImpl::setIMEKeyboardState(bool open)
|
2011-04-19 18:11:57 +08:00
|
|
|
{
|
2014-01-31 07:11:37 +08:00
|
|
|
CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
|
2014-01-28 11:16:09 +08:00
|
|
|
|
|
|
|
if (open)
|
2011-04-19 18:11:57 +08:00
|
|
|
{
|
2014-01-31 07:11:37 +08:00
|
|
|
[eaglview becomeFirstResponder];
|
2011-04-19 18:11:57 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-31 07:11:37 +08:00
|
|
|
[eaglview resignFirstResponder];
|
2011-04-19 18:11:57 +08:00
|
|
|
}
|
2011-02-23 18:22:05 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2014-01-28 08:44:10 +08:00
|
|
|
#endif // CC_PLATFOR_IOS
|
|
|
|
|