axmol/cocos/platform/ios/CCGLViewImpl-ios.mm

218 lines
5.9 KiB
Plaintext
Raw Normal View History

2010-11-12 17:16:15 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
2010-11-12 17:16:15 +08:00
http://www.cocos2d-x.org
2010-11-12 17:16:15 +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
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
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
#include "platform/CCPlatformConfig.h"
2014-01-28 08:44:10 +08:00
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
#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)
{
auto ret = new (std::nothrow) GLViewImpl;
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)
{
auto ret = new (std::nothrow) GLViewImpl;
2014-01-31 07:40:56 +08:00
if(ret && ret->initWithFullScreen(viewName)) {
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)
{
auto ret = new (std::nothrow) GLViewImpl;
2014-01-31 07:40:56 +08:00
if(ret && ret->initWithRect(viewName, rect, frameZoomFactor)) {
ret->autorelease();
return ret;
}
return nullptr;
}
2014-07-31 00:53:04 +08:00
GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName)
{
auto ret = new (std::nothrow) GLViewImpl();
if(ret && ret->initWithFullScreen(viewName)) {
ret->autorelease();
return ret;
}
return nullptr;
}
void GLViewImpl::convertAttrs()
2014-08-21 09:35:32 +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;
}
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;
//[glview release];
2010-11-12 17:16:15 +08:00
}
2014-07-31 00:53:04 +08:00
bool GLViewImpl::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;
}
2014-07-31 00:53:04 +08:00
bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
{
2014-01-31 07:40:56 +08:00
CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
convertAttrs();
CCEAGLView *eaglview = [CCEAGLView viewWithFrame: r
2014-08-21 09:35:32 +08:00
pixelFormat: (NSString*)_pixelFormat
depthFormat: _depthFormat
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;
}
2014-07-31 00:53:04 +08:00
bool GLViewImpl::initWithFullScreen(const std::string& viewName)
{
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:40:56 +08:00
return initWithRect(viewName, r, 1);
}
2014-07-31 00:53:04 +08:00
bool GLViewImpl::isOpenGLReady()
2010-11-12 17:16:15 +08:00
{
return _eaglview != nullptr;
2010-11-12 17:16:15 +08:00
}
2014-07-31 00:53:04 +08:00
bool GLViewImpl::setContentScaleFactor(float contentScaleFactor)
{
2014-01-28 11:16:09 +08:00
CC_ASSERT(_resolutionPolicy == ResolutionPolicy::UNKNOWN); // cannot enable retina mode
_scaleX = _scaleY = contentScaleFactor;
2014-01-28 11:16:09 +08:00
CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
[eaglview setNeedsLayout];
return true;
}
2010-11-12 17:16:15 +08:00
2014-07-31 00:53:04 +08:00
float GLViewImpl::getContentScaleFactor() const
{
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");
return scaleFactor;
}
2014-07-31 00:53:04 +08:00
void GLViewImpl::end()
2010-11-12 17:16:15 +08:00
{
[CCDirectorCaller destroy];
// destroy EAGLView
CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
2014-01-28 11:16:09 +08:00
[eaglview removeFromSuperview];
[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
{
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
{
CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
2014-01-28 11:16:09 +08:00
if (open)
2011-04-19 18:11:57 +08:00
{
[eaglview becomeFirstResponder];
2011-04-19 18:11:57 +08:00
}
else
{
[eaglview resignFirstResponder];
2011-04-19 18:11:57 +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