Merge pull request #7843 from huangshiwu/v3_glviewattrs

V3 glviewattrs
This commit is contained in:
minggo 2014-08-25 09:46:19 +08:00
commit dcea3b6134
28 changed files with 194 additions and 42 deletions

View File

@ -102,6 +102,15 @@ public:
*/
virtual void setAnimationInterval(double interval) = 0;
//subclass override the function to set OpenGL context attribution instead of use default value
//and now can only set six attributions:redBits,greenBits,blueBits,alphaBits,depthBits,stencilBits
//default value are(5,6,5,0,16,0), usually use as follows:
/*void AppDelegate::initGLContextAttrs(){
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}*/
virtual void initGLContextAttrs() {}
/**
@brief Get current language config
@return Current language config

View File

@ -70,6 +70,19 @@ namespace {
}
//default context attributions are setted as follows
GLContextAttrs GLView::_glContextAttrs = {5, 6, 5, 0, 16, 0};
void GLView::setGLContextAttrs(GLContextAttrs& glContextAttrs)
{
_glContextAttrs = glContextAttrs;
}
GLContextAttrs GLView::getGLContextAttrs()
{
return _glContextAttrs;
}
GLView::GLView()
: _scaleX(1.0f)
, _scaleY(1.0f)

View File

@ -64,6 +64,16 @@ enum class ResolutionPolicy
UNKNOWN,
};
struct GLContextAttrs
{
int redBits;
int greenBits;
int blueBits;
int alphaBits;
int depthBits;
int stencilBits;
};
NS_CC_BEGIN
/**
@ -98,6 +108,11 @@ public:
virtual bool windowShouldClose() { return false; };
//static method and member so that we can modify it on all platforms before create OpenGL context
static void setGLContextAttrs(GLContextAttrs& glContextAttrs);
static GLContextAttrs getGLContextAttrs();
static GLContextAttrs _glContextAttrs;
/**
* Polls input events. Subclass must implement methods if platform
* does not provide event callbacks.

View File

@ -30,13 +30,13 @@ THE SOFTWARE.
#include "base/CCDirector.h"
#include "base/ccMacros.h"
#include "jni/IMEJni.h"
#include "jni/JniHelper.h"
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#include "CCGL.h"
#include <stdlib.h>
#include <android/log.h>
// <EGL/egl.h> exists since android 2.3
#include <EGL/egl.h>
PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT = 0;

View File

@ -51,6 +51,7 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
// ===========================================================
private Cocos2dxGLSurfaceView mGLSurfaceView;
private int[] glContextAttrs;
private Cocos2dxHandler mHandler;
private static Cocos2dxActivity sContext = null;
private Cocos2dxVideoHelper mVideoHelper = null;
@ -95,11 +96,16 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
Cocos2dxHelper.init(this);
this.glContextAttrs = getGLContextAttrs();
this.init();
if (mVideoHelper == null) {
mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout);
}
}
//native method,call GLViewImpl::getGLContextAttrs() to get the OpenGL ES context attributions
private static native int[] getGLContextAttrs();
// ===========================================================
// Getter & Setter
@ -193,7 +199,7 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
// Switch to supported OpenGL (ARGB888) mode on emulator
if (isAndroidEmulator())
this.mGLSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0);
this.mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
this.mGLSurfaceView.setCocos2dxEditText(edittext);
@ -203,7 +209,12 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
}
public Cocos2dxGLSurfaceView onCreateView() {
return new Cocos2dxGLSurfaceView(this);
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
glSurfaceView.setEGLConfigChooser(this.glContextAttrs[0], this.glContextAttrs[1],this.glContextAttrs[2],
this.glContextAttrs[3],this.glContextAttrs[4],this.glContextAttrs[5]);
return glSurfaceView;
}
private final static boolean isAndroidEmulator() {

View File

@ -64,7 +64,7 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi
glview->setFrameSize(w, h);
director->setOpenGLView(glview);
cocos_android_app_init(env, thiz);
//cocos_android_app_init(env, thiz);
cocos2d::Application::getInstance()->run();
}
@ -79,7 +79,22 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi
director->getEventDispatcher()->dispatchEvent(&recreatedEvent);
director->setGLDefaultValues();
}
}
jintArray Java_org_cocos2dx_lib_Cocos2dxActivity_getGLContextAttrs(JNIEnv* env, jobject thiz)
{
cocos_android_app_init(env, thiz);
cocos2d::Application::getInstance()->initGLContextAttrs();
GLContextAttrs _glContextAttrs = GLView::getGLContextAttrs();
int tmp[6] = {_glContextAttrs.redBits, _glContextAttrs.greenBits, _glContextAttrs.blueBits,
_glContextAttrs.alphaBits, _glContextAttrs.depthBits, _glContextAttrs.stencilBits};
jintArray glContextAttrsJava = env->NewIntArray(6);
env->SetIntArrayRegion(glContextAttrsJava, 0, 6, tmp);
return glContextAttrsJava;
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnSurfaceChanged(JNIEnv* env, jobject thiz, jint w, jint h)

View File

@ -337,7 +337,6 @@ GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName, const
return nullptr;
}
bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
{
setViewName(viewName);
@ -345,6 +344,12 @@ bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float fram
_frameZoomFactor = frameZoomFactor;
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
glfwWindowHint(GLFW_RED_BITS,_glContextAttrs.redBits);
glfwWindowHint(GLFW_GREEN_BITS,_glContextAttrs.greenBits);
glfwWindowHint(GLFW_BLUE_BITS,_glContextAttrs.blueBits);
glfwWindowHint(GLFW_ALPHA_BITS,_glContextAttrs.alphaBits);
glfwWindowHint(GLFW_DEPTH_BITS,_glContextAttrs.depthBits);
glfwWindowHint(GLFW_STENCIL_BITS,_glContextAttrs.stencilBits);
_mainWindow = glfwCreateWindow(rect.size.width * _frameZoomFactor,
rect.size.height * _frameZoomFactor,

View File

@ -52,6 +52,10 @@ public:
/** creates a GLViewImpl with a name in fullscreen mode */
static GLViewImpl* createWithFullScreen(const std::string& viewName);
static void convertAttrs();
static void* _pixelFormat;
static int _depthFormat;
/** sets the content scale factor */
bool setContentScaleFactor(float contentScaleFactor);

View File

@ -36,6 +36,9 @@
NS_CC_BEGIN
void* GLViewImpl::_pixelFormat = kEAGLColorFormatRGB565;
int GLViewImpl::_depthFormat = GL_DEPTH_COMPONENT16;
GLViewImpl* GLViewImpl::createWithEAGLView(void *eaglview)
{
auto ret = new GLViewImpl;
@ -80,6 +83,18 @@ GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName)
return nullptr;
}
void GLViewImpl::convertAttrs()
{
if(_glContextAttrs.redBits==8 && _glContextAttrs.greenBits==8 && _glContextAttrs.blueBits==8 && _glContextAttrs.alphaBits==8)
{
_pixelFormat = kEAGLColorFormatRGBA8;
}
if(_glContextAttrs.depthBits==24 && _glContextAttrs.stencilBits==8)
{
_depthFormat = GL_DEPTH24_STENCIL8_OES;
}
}
GLViewImpl::GLViewImpl()
{
}
@ -105,13 +120,15 @@ bool GLViewImpl::initWithEAGLView(void *eaglview)
bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
{
CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
convertAttrs();
CCEAGLView *eaglview = [CCEAGLView viewWithFrame: r
pixelFormat: kEAGLColorFormatRGB565
depthFormat: GL_DEPTH24_STENCIL8_OES
pixelFormat: (NSString*)_pixelFormat
depthFormat: _depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0];
[eaglview setMultipleTouchEnabled:YES];
_screenSize.width = _designResolutionSize.width = [eaglview getWidth];

View File

@ -63,6 +63,7 @@ Application::~Application()
int Application::run()
{
initGLContextAttrs();
// Initialize instance and cocos2d.
if (! applicationDidFinishLaunching())
{

View File

@ -64,6 +64,7 @@ Application::~Application()
int Application::run()
{
initGLContextAttrs();
if(!applicationDidFinishLaunching())
{
return 1;

View File

@ -69,6 +69,8 @@ int Application::run()
QueryPerformanceFrequency(&nFreq);
QueryPerformanceCounter(&nLast);
initGLContextAttrs();
// Initialize instance and cocos2d.
if (!applicationDidFinishLaunching())
{

View File

@ -38,6 +38,10 @@ static AppDelegate s_sharedApplication;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
cocos2d::Application *app = cocos2d::Application::getInstance();
app->initGLContextAttrs();
cocos2d::GLViewImpl::convertAttrs();
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
@ -45,12 +49,12 @@ static AppDelegate s_sharedApplication;
// Init the CCEAGLView
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH24_STENCIL8_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0];
pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat
depthFormat: cocos2d::GLViewImpl::_depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0 ];
// Use RootViewController manage CCEAGLView
_viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
@ -77,7 +81,7 @@ static AppDelegate s_sharedApplication;
cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
cocos2d::Application::getInstance()->run();
app->run();
return YES;
}

View File

@ -11,6 +11,17 @@ AppDelegate::~AppDelegate()
{
}
//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
//set OpenGL context attributions,now can only set six attributions:
//red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();

View File

@ -14,6 +14,8 @@ public:
AppDelegate();
virtual ~AppDelegate();
virtual void initGLContextAttrs();
/**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.

View File

@ -17,6 +17,13 @@ AppDelegate::~AppDelegate()
{
}
void AppDelegate::initGLContextAttrs()
{
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();

View File

@ -14,6 +14,8 @@ public:
AppDelegate();
virtual ~AppDelegate();
virtual void initGLContextAttrs();
/**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.

View File

@ -38,19 +38,25 @@ static AppDelegate s_sharedApplication;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
cocos2d::Application *app = cocos2d::Application::getInstance();
app->initGLContextAttrs();
cocos2d::GLViewImpl::convertAttrs();
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH_COMPONENT16
preserveBackbuffer: NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
// Use RootViewController manage CCEAGLView
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat
depthFormat: cocos2d::GLViewImpl::_depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0];
// Use RootViewController manage CCEAGLView
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
viewController.view = eaglView;
@ -75,7 +81,6 @@ static AppDelegate s_sharedApplication;
cocos2d::GLViewImpl *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
cocos2d::Application *app = cocos2d::Application::getInstance();
app->run();
return YES;
}

View File

@ -43,6 +43,17 @@ AppDelegate::~AppDelegate()
cocostudio::ArmatureDataManager::destroyInstance();
}
//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
//set OpenGL context attributions,now can only set six attributions:
//red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
bool AppDelegate::applicationDidFinishLaunching()
{
// As an example, load config file

View File

@ -39,6 +39,8 @@ public:
AppDelegate();
virtual ~AppDelegate();
virtual void initGLContextAttrs();
/**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.

View File

@ -27,12 +27,4 @@ import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
public class AppActivity extends Cocos2dxActivity {
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
// TestCpp should create stencil buffer
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
return glSurfaceView;
}
}

View File

@ -42,16 +42,18 @@ static AppDelegate s_sharedApplication;
{
cocos2d::Application *app = cocos2d::Application::getInstance();
app->initGLContextAttrs();
cocos2d::GLViewImpl::convertAttrs();
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
// Init the CCEAGLView
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH24_STENCIL8_OES
pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat
depthFormat: cocos2d::GLViewImpl::_depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
@ -79,7 +81,7 @@ static AppDelegate s_sharedApplication;
[window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarHidden:true];
// IMPORTANT: Setting the GLView should be done after creating the RootViewController
cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);

View File

@ -21,6 +21,13 @@ AppDelegate::~AppDelegate()
//CCScriptEngineManager::destroyInstance();
}
void AppDelegate::initGLContextAttrs()
{
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
bool AppDelegate::applicationDidFinishLaunching()
{
// register lua engine

View File

@ -14,6 +14,7 @@ public:
AppDelegate();
virtual ~AppDelegate();
virtual void initGLContextAttrs();
/**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.

View File

@ -38,14 +38,17 @@
static AppDelegate s_sharedApplication;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
cocos2d::Application *app = cocos2d::Application::getInstance();
app->initGLContextAttrs();
cocos2d::GLViewImpl::convertAttrs();
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH_COMPONENT16
pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat
depthFormat: cocos2d::GLViewImpl::_depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
@ -76,7 +79,6 @@ static AppDelegate s_sharedApplication;
cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
cocos2d::Application *app = cocos2d::Application::getInstance();
app->run();
return YES;
}

View File

@ -18,6 +18,13 @@ AppDelegate::~AppDelegate()
SimpleAudioEngine::end();
}
void AppDelegate::initGLContextAttrs()
{
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
bool AppDelegate::applicationDidFinishLaunching()
{
// register lua engine

View File

@ -14,6 +14,7 @@ public:
AppDelegate();
virtual ~AppDelegate();
void initGLContextAttrs();
/**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.

View File

@ -39,13 +39,16 @@ static AppDelegate s_sharedApplication;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
cocos2d::Application *app = cocos2d::Application::getInstance();
app->initGLContextAttrs();
cocos2d::GLViewImpl::convertAttrs();
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH24_STENCIL8_OES
pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat
depthFormat: cocos2d::GLViewImpl::_depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
@ -79,7 +82,7 @@ static AppDelegate s_sharedApplication;
cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
cocos2d::Application::getInstance()->run();
app->run();
return YES;
}