From 2a03bea1a4c0e25f3ed09ca0c5d60a3098fe199d Mon Sep 17 00:00:00 2001 From: huangshiwu Date: Thu, 21 Aug 2014 09:35:32 +0800 Subject: [PATCH 1/3] OpenGL context attributions setting --- cocos/platform/CCApplicationProtocol.h | 2 + cocos/platform/CCGLView.cpp | 10 ++++ cocos/platform/CCGLView.h | 3 + cocos/platform/android/CCGLViewImpl.cpp | 9 ++- cocos/platform/android/CCGLViewImpl.h | 1 + .../org/cocos2dx/lib/Cocos2dxActivity.java | 55 +++++++++++++++++-- .../src/org/cocos2dx/lib/Cocos2dxHelper.java | 4 ++ cocos/platform/android/javaactivity.cpp | 8 ++- .../Java_org_cocos2dx_lib_Cocos2dxHelper.cpp | 13 +++++ .../Java_org_cocos2dx_lib_Cocos2dxHelper.h | 1 + cocos/platform/desktop/CCGLViewImpl.cpp | 10 ++++ cocos/platform/desktop/CCGLViewImpl.h | 1 + cocos/platform/ios/CCGLViewImpl.h | 6 ++ cocos/platform/ios/CCGLViewImpl.mm | 32 +++++++++-- cocos/platform/linux/CCApplication.cpp | 1 + cocos/platform/mac/CCApplication.mm | 1 + cocos/platform/win32/CCApplication.cpp | 2 + tests/cpp-empty-test/Classes/AppDelegate.cpp | 9 +++ tests/cpp-empty-test/Classes/AppDelegate.h | 2 + .../cpp-empty-test/proj.ios/AppController.mm | 21 ++++--- tests/cpp-tests/Classes/AppDelegate.cpp | 11 ++++ tests/cpp-tests/Classes/AppDelegate.h | 2 + .../proj.ios/Classes/testsAppDelegate.mm | 15 +++-- .../project/Classes/AppDelegate.cpp | 9 +++ .../project/Classes/AppDelegate.h | 1 + .../lua-tests/project/Classes/AppDelegate.cpp | 9 +++ tests/lua-tests/project/Classes/AppDelegate.h | 1 + 27 files changed, 217 insertions(+), 22 deletions(-) diff --git a/cocos/platform/CCApplicationProtocol.h b/cocos/platform/CCApplicationProtocol.h index 32727027e2..f490e390e6 100644 --- a/cocos/platform/CCApplicationProtocol.h +++ b/cocos/platform/CCApplicationProtocol.h @@ -102,6 +102,8 @@ public: */ virtual void setAnimationInterval(double interval) = 0; + virtual bool setOGLCntattrs() { return false; } + /** @brief Get current language config @return Current language config diff --git a/cocos/platform/CCGLView.cpp b/cocos/platform/CCGLView.cpp index f7cdf090ac..52d42d473f 100644 --- a/cocos/platform/CCGLView.cpp +++ b/cocos/platform/CCGLView.cpp @@ -70,6 +70,16 @@ namespace { } +int* GLView::_OGLCntattrs = new int[6]; + +void GLView::setOGLCntattrs(int* OGLCntattrs) +{ + for(int i = 0; i < 6; i++) + { + _OGLCntattrs[i] = OGLCntattrs[i]; + } +} + GLView::GLView() : _scaleX(1.0f) , _scaleY(1.0f) diff --git a/cocos/platform/CCGLView.h b/cocos/platform/CCGLView.h index cb0e133702..9d829c3703 100644 --- a/cocos/platform/CCGLView.h +++ b/cocos/platform/CCGLView.h @@ -98,6 +98,9 @@ public: virtual bool windowShouldClose() { return false; }; + static void setOGLCntattrs(int* OGLCntattrs); + static int* _OGLCntattrs; + /** * Polls input events. Subclass must implement methods if platform * does not provide event callbacks. diff --git a/cocos/platform/android/CCGLViewImpl.cpp b/cocos/platform/android/CCGLViewImpl.cpp index 90fdc30df7..8f127ca176 100644 --- a/cocos/platform/android/CCGLViewImpl.cpp +++ b/cocos/platform/android/CCGLViewImpl.cpp @@ -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 #include - // exists since android 2.3 #include PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT = 0; @@ -73,6 +73,13 @@ GLViewImpl* GLViewImpl::create(const std::string& viewName) return nullptr; } +GLViewImpl* GLViewImpl::createWithOGLCntattrs(const std::string& viewName) +{ + activityInitWithOGLCntattrsJni(_OGLCntattrs); + + return nullptr; +} + GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName) { auto ret = new GLViewImpl(); diff --git a/cocos/platform/android/CCGLViewImpl.h b/cocos/platform/android/CCGLViewImpl.h index 820189e148..c437793201 100644 --- a/cocos/platform/android/CCGLViewImpl.h +++ b/cocos/platform/android/CCGLViewImpl.h @@ -41,6 +41,7 @@ public: // static function static GLViewImpl* create(const std::string &viewname); + static GLViewImpl* createWithOGLCntattrs(const std::string& viewName); static GLViewImpl* createWithRect(const std::string& viewName, Rect rect, float frameZoomFactor = 1.0f); static GLViewImpl* createWithFullScreen(const std::string& viewName); diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java index 78929fb300..a3be94bd7d 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -84,11 +84,15 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe Cocos2dxHelper.init(this); - this.init(); - if (mVideoHelper == null) { - mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout); - } + preCreateOGLCtx(); + //this.init(); + //this.reinit(); + //if (mVideoHelper == null) { + //mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout); + //} } + + private static native void preCreateOGLCtx(); // =========================================================== // Getter & Setter @@ -190,6 +194,49 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe // Set framelayout as the content view setContentView(mFrameLayout); } + + public void initWithOGLCntattrs(int[] OGLCntattrs) { + + // FrameLayout + ViewGroup.LayoutParams framelayout_params = + new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT); + mFrameLayout = new FrameLayout(this); + mFrameLayout.setLayoutParams(framelayout_params); + + // Cocos2dxEditText layout + ViewGroup.LayoutParams edittext_layout_params = + new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + Cocos2dxEditText edittext = new Cocos2dxEditText(this); + edittext.setLayoutParams(edittext_layout_params); + + // ...add to FrameLayout + mFrameLayout.addView(edittext); + + // Cocos2dxGLSurfaceView + this.mGLSurfaceView = new Cocos2dxGLSurfaceView(this); + + //this.mGLSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8); + this.mGLSurfaceView.setEGLConfigChooser(OGLCntattrs[0], OGLCntattrs[1],OGLCntattrs[2],OGLCntattrs[3],OGLCntattrs[4],OGLCntattrs[5]); + + // ...add to FrameLayout + mFrameLayout.addView(this.mGLSurfaceView); + + // Switch to supported OpenGL (ARGB888) mode on emulator + if (isAndroidEmulator()) + this.mGLSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0); + + this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer()); + this.mGLSurfaceView.setCocos2dxEditText(edittext); + + // Set framelayout as the content view + setContentView(mFrameLayout); + + if (mVideoHelper == null) { + mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout); + } + } public Cocos2dxGLSurfaceView onCreateView() { return new Cocos2dxGLSurfaceView(this); diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index 3cabec4822..039b721e37 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -102,6 +102,10 @@ public class Cocos2dxHelper { public static Activity getActivity() { return sActivity; } + + public static void activityInitWithOGLCntattrs(int[] OGLCntattrs){ + ((Cocos2dxActivity)sActivity).initWithOGLCntattrs(OGLCntattrs); + } public static void addOnActivityResultListener(OnActivityResultListener listener) { onActivityResultListeners.add(listener); diff --git a/cocos/platform/android/javaactivity.cpp b/cocos/platform/android/javaactivity.cpp index b16482600e..c5ed358707 100644 --- a/cocos/platform/android/javaactivity.cpp +++ b/cocos/platform/android/javaactivity.cpp @@ -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,13 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi director->getEventDispatcher()->dispatchEvent(&recreatedEvent); director->setGLDefaultValues(); } +} +void Java_org_cocos2dx_lib_Cocos2dxActivity_preCreateOGLCtx(JNIEnv* env, jobject thiz) +{ + cocos_android_app_init(env, thiz); + cocos2d::Application::getInstance()->setOGLCntattrs(); + GLViewImpl::createWithOGLCntattrs("Android app"); } void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnSurfaceChanged(JNIEnv* env, jobject thiz, jint w, jint h) diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp index 3449bbbc9d..ad20af8993 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp @@ -209,6 +209,19 @@ void disableAccelerometerJni() { } } +void activityInitWithOGLCntattrsJni(int* OGLCntattrs) { + JniMethodInfo t; + + if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "activityInitWithOGLCntattrs", "([I)V")) { + jintArray OGLCntattrsJava = t.env->NewIntArray(6); + t.env->SetIntArrayRegion(OGLCntattrsJava, 0, 6, OGLCntattrs); + + t.env->CallStaticVoidMethod(t.classID, t.methodID, OGLCntattrsJava); + t.env->DeleteLocalRef(t.classID); + CCLOG("call Cocos2dxHelper activityInitWithOGLCntattrs"); + } +} + // functions for UserDefault bool getBoolForKeyJNI(const char* key, bool defaultValue) { diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h index 34d521a911..10f5e9d18b 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h @@ -39,6 +39,7 @@ extern std::string getFileDirectoryJNI(); extern void enableAccelerometerJni(); extern void disableAccelerometerJni(); extern void setAccelerometerIntervalJni(float interval); +extern void activityInitWithOGLCntattrsJni(int* OGLCntattrs); // functions for UserDefault extern bool getBoolForKeyJNI(const char* key, bool defaultValue); extern int getIntegerForKeyJNI(const char* key, int defaultValue); diff --git a/cocos/platform/desktop/CCGLViewImpl.cpp b/cocos/platform/desktop/CCGLViewImpl.cpp index 515e1272cf..4729009d25 100644 --- a/cocos/platform/desktop/CCGLViewImpl.cpp +++ b/cocos/platform/desktop/CCGLViewImpl.cpp @@ -328,6 +328,15 @@ GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName, const return nullptr; } +void GLViewImpl::convertattrs() +{ + glfwWindowHint(GLFW_RED_BITS,_OGLCntattrs[0]); + glfwWindowHint(GLFW_GREEN_BITS,_OGLCntattrs[1]); + glfwWindowHint(GLFW_BLUE_BITS,_OGLCntattrs[2]); + glfwWindowHint(GLFW_ALPHA_BITS,_OGLCntattrs[3]); + glfwWindowHint(GLFW_DEPTH_BITS,_OGLCntattrs[4]); + glfwWindowHint(GLFW_STENCIL_BITS,_OGLCntattrs[5]); +} bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor) { @@ -336,6 +345,7 @@ bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float fram _frameZoomFactor = frameZoomFactor; glfwWindowHint(GLFW_RESIZABLE,GL_FALSE); + convertattrs(); _mainWindow = glfwCreateWindow(rect.size.width * _frameZoomFactor, rect.size.height * _frameZoomFactor, diff --git a/cocos/platform/desktop/CCGLViewImpl.h b/cocos/platform/desktop/CCGLViewImpl.h index a9befbbd51..cbacea34ad 100644 --- a/cocos/platform/desktop/CCGLViewImpl.h +++ b/cocos/platform/desktop/CCGLViewImpl.h @@ -148,6 +148,7 @@ protected: friend class GLFWEventHandler; private: + void convertattrs(); CC_DISALLOW_COPY_AND_ASSIGN(GLViewImpl); }; diff --git a/cocos/platform/ios/CCGLViewImpl.h b/cocos/platform/ios/CCGLViewImpl.h index 795e0077b9..d3950e5f38 100644 --- a/cocos/platform/ios/CCGLViewImpl.h +++ b/cocos/platform/ios/CCGLViewImpl.h @@ -52,6 +52,12 @@ public: /** creates a GLViewImpl with a name in fullscreen mode */ static GLViewImpl* createWithFullScreen(const std::string& viewName); + + static void convert(int* _OGLCntattrs); + static void* _pixelFormat; + static int _depthFormat; + + void setSize(); /** sets the content scale factor */ bool setContentScaleFactor(float contentScaleFactor); diff --git a/cocos/platform/ios/CCGLViewImpl.mm b/cocos/platform/ios/CCGLViewImpl.mm index 567d55bc43..8438cc968a 100644 --- a/cocos/platform/ios/CCGLViewImpl.mm +++ b/cocos/platform/ios/CCGLViewImpl.mm @@ -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::convert(int* OGLCntattrs) +{ + if(OGLCntattrs[0]==8 && OGLCntattrs[1]==8 && OGLCntattrs[2]==8 && OGLCntattrs[3]==8) + { + _pixelFormat = kEAGLColorFormatRGBA8; + } + if(OGLCntattrs[4]==24 && OGLCntattrs[5]==8) + { + _depthFormat = GL_DEPTH24_STENCIL8_OES; + } +} + GLViewImpl::GLViewImpl() { } @@ -105,17 +120,19 @@ 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); + convert(_OGLCntattrs); 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]; - _screenSize.height = _designResolutionSize.height = [eaglview getHeight]; + //_screenSize.width = _designResolutionSize.width = [eaglview getWidth]; + //_screenSize.height = _designResolutionSize.height = [eaglview getHeight]; // _scaleX = _scaleY = [eaglview contentScaleFactor]; _eaglview = eaglview; @@ -123,6 +140,13 @@ bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float fram return true; } +void GLViewImpl::setSize() +{ + CCEAGLView *eaglview = (CCEAGLView*) _eaglview; + _screenSize.width = _designResolutionSize.width = [eaglview getWidth]; + _screenSize.height = _designResolutionSize.height = [eaglview getHeight]; +} + bool GLViewImpl::initWithFullScreen(const std::string& viewName) { CGRect rect = [[UIScreen mainScreen] bounds]; diff --git a/cocos/platform/linux/CCApplication.cpp b/cocos/platform/linux/CCApplication.cpp index 14391ea3db..f95ba45e0b 100644 --- a/cocos/platform/linux/CCApplication.cpp +++ b/cocos/platform/linux/CCApplication.cpp @@ -63,6 +63,7 @@ Application::~Application() int Application::run() { + setOGLCntattrs(); // Initialize instance and cocos2d. if (! applicationDidFinishLaunching()) { diff --git a/cocos/platform/mac/CCApplication.mm b/cocos/platform/mac/CCApplication.mm index c635d445b4..3a41f1377e 100644 --- a/cocos/platform/mac/CCApplication.mm +++ b/cocos/platform/mac/CCApplication.mm @@ -64,6 +64,7 @@ Application::~Application() int Application::run() { + setOGLCntattrs(); if(!applicationDidFinishLaunching()) { return 1; diff --git a/cocos/platform/win32/CCApplication.cpp b/cocos/platform/win32/CCApplication.cpp index 01dd2d82f2..55d7a85b11 100644 --- a/cocos/platform/win32/CCApplication.cpp +++ b/cocos/platform/win32/CCApplication.cpp @@ -69,6 +69,8 @@ int Application::run() QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nLast); + setOGLCntattrs(); + // Initialize instance and cocos2d. if (!applicationDidFinishLaunching()) { diff --git a/tests/cpp-empty-test/Classes/AppDelegate.cpp b/tests/cpp-empty-test/Classes/AppDelegate.cpp index 036c732ca7..511f3530df 100644 --- a/tests/cpp-empty-test/Classes/AppDelegate.cpp +++ b/tests/cpp-empty-test/Classes/AppDelegate.cpp @@ -17,6 +17,15 @@ AppDelegate::~AppDelegate() { } +bool AppDelegate::setOGLCntattrs() +{ + int OGLCntattrs[] = {8, 8, 8, 8, 24, 8}; + + GLView::setOGLCntattrs(OGLCntattrs); + + return true; +} + bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); diff --git a/tests/cpp-empty-test/Classes/AppDelegate.h b/tests/cpp-empty-test/Classes/AppDelegate.h index 18ee8aeb63..2f4e608890 100644 --- a/tests/cpp-empty-test/Classes/AppDelegate.h +++ b/tests/cpp-empty-test/Classes/AppDelegate.h @@ -14,6 +14,8 @@ public: AppDelegate(); virtual ~AppDelegate(); + virtual bool setOGLCntattrs(); + /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. diff --git a/tests/cpp-empty-test/proj.ios/AppController.mm b/tests/cpp-empty-test/proj.ios/AppController.mm index daa056bca0..6171e0a739 100644 --- a/tests/cpp-empty-test/proj.ios/AppController.mm +++ b/tests/cpp-empty-test/proj.ios/AppController.mm @@ -38,19 +38,25 @@ static AppDelegate s_sharedApplication; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + cocos2d::Application *app = cocos2d::Application::getInstance(); + app->setOGLCntattrs(); + // 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] + auto glview = cocos2d::GLViewImpl::createWithFullScreen("ios cpp-empty-test"); + CCEAGLView *eaglView = (CCEAGLView*)glview->getEAGLView(); + /*CCEAGLView eaglView = [CCEAGLView viewWithFrame: [window bounds] pixelFormat: kEAGLColorFormatRGBA8 depthFormat: GL_DEPTH_COMPONENT16 preserveBackbuffer: NO - sharegroup:nil - multiSampling:NO - numberOfSamples:0]; + sharegroup: nil + multiSampling: NO + numberOfSamples: 0];*/ - // Use RootViewController manage CCEAGLView + + // Use RootViewController manage CCEAGLView viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; viewController.wantsFullScreenLayout = YES; viewController.view = eaglView; @@ -71,11 +77,12 @@ static AppDelegate s_sharedApplication; [[UIApplication sharedApplication] setStatusBarHidden: YES]; + glview->setSize(); // IMPORTANT: Setting the GLView should be done after creating the RootViewController - cocos2d::GLViewImpl *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView); + //cocos2d::GLViewImpl *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView); cocos2d::Director::getInstance()->setOpenGLView(glview); - cocos2d::Application *app = cocos2d::Application::getInstance(); + //cocos2d::Application *app = cocos2d::Application::getInstance(); app->run(); return YES; } diff --git a/tests/cpp-tests/Classes/AppDelegate.cpp b/tests/cpp-tests/Classes/AppDelegate.cpp index b5daf4a333..f97219d86f 100644 --- a/tests/cpp-tests/Classes/AppDelegate.cpp +++ b/tests/cpp-tests/Classes/AppDelegate.cpp @@ -43,6 +43,17 @@ AppDelegate::~AppDelegate() cocostudio::ArmatureDataManager::destroyInstance(); } +bool AppDelegate::setOGLCntattrs() +{ + //set OpenGL context attributions + //red,green,blue,alpha,depth,stencil + int OGLCntattrs[] = {8, 8, 8, 8, 24, 8}; + + GLView::setOGLCntattrs(OGLCntattrs); + + return true; +} + bool AppDelegate::applicationDidFinishLaunching() { // As an example, load config file diff --git a/tests/cpp-tests/Classes/AppDelegate.h b/tests/cpp-tests/Classes/AppDelegate.h index 4cc79a14eb..44cb8cace2 100644 --- a/tests/cpp-tests/Classes/AppDelegate.h +++ b/tests/cpp-tests/Classes/AppDelegate.h @@ -39,6 +39,8 @@ public: AppDelegate(); virtual ~AppDelegate(); + virtual bool setOGLCntattrs(); + /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. diff --git a/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm b/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm index 2e725af131..ffa2939402 100644 --- a/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm +++ b/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm @@ -42,20 +42,23 @@ static AppDelegate s_sharedApplication; { cocos2d::Application *app = cocos2d::Application::getInstance(); + app->setOGLCntattrs(); // 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] + auto glview = cocos2d::GLViewImpl::createWithFullScreen("ios cpp-tests"); + CCEAGLView *eaglView = (CCEAGLView*)glview->getEAGLView(); + /*CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds] pixelFormat: kEAGLColorFormatRGBA8 depthFormat: GL_DEPTH24_STENCIL8_OES preserveBackbuffer: NO sharegroup: nil multiSampling: NO - numberOfSamples: 0 ]; + numberOfSamples: 0 ];*/ [eaglView setMultipleTouchEnabled:YES]; @@ -79,9 +82,11 @@ static AppDelegate s_sharedApplication; [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden:true]; - + + glview->setSize(); + // IMPORTANT: Setting the GLView should be done after creating the RootViewController - cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView); + //cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView); cocos2d::Director::getInstance()->setOpenGLView(glview); app->run(); diff --git a/tests/lua-empty-test/project/Classes/AppDelegate.cpp b/tests/lua-empty-test/project/Classes/AppDelegate.cpp index 295ebc76a1..d07d5656af 100644 --- a/tests/lua-empty-test/project/Classes/AppDelegate.cpp +++ b/tests/lua-empty-test/project/Classes/AppDelegate.cpp @@ -21,6 +21,15 @@ AppDelegate::~AppDelegate() //CCScriptEngineManager::destroyInstance(); } +bool AppDelegate::setOGLCntattrs() +{ + int OGLCntattrs[] = {8, 8, 8, 8, 24, 8}; + + GLView::setOGLCntattrs(OGLCntattrs); + + return true; +} + bool AppDelegate::applicationDidFinishLaunching() { // register lua engine diff --git a/tests/lua-empty-test/project/Classes/AppDelegate.h b/tests/lua-empty-test/project/Classes/AppDelegate.h index b708f4bdca..848945dd5c 100644 --- a/tests/lua-empty-test/project/Classes/AppDelegate.h +++ b/tests/lua-empty-test/project/Classes/AppDelegate.h @@ -14,6 +14,7 @@ public: AppDelegate(); virtual ~AppDelegate(); + virtual bool setOGLCntattrs(); /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. diff --git a/tests/lua-tests/project/Classes/AppDelegate.cpp b/tests/lua-tests/project/Classes/AppDelegate.cpp index c38a9a1949..121b74f4bb 100644 --- a/tests/lua-tests/project/Classes/AppDelegate.cpp +++ b/tests/lua-tests/project/Classes/AppDelegate.cpp @@ -18,6 +18,15 @@ AppDelegate::~AppDelegate() SimpleAudioEngine::end(); } +bool AppDelegate::setOGLCntattrs() +{ + int OGLCntattrs[] = {8, 8, 8, 8, 24, 8}; + + GLView::setOGLCntattrs(OGLCntattrs); + + return true; +} + bool AppDelegate::applicationDidFinishLaunching() { // register lua engine diff --git a/tests/lua-tests/project/Classes/AppDelegate.h b/tests/lua-tests/project/Classes/AppDelegate.h index 5cf478d052..b6791cc935 100644 --- a/tests/lua-tests/project/Classes/AppDelegate.h +++ b/tests/lua-tests/project/Classes/AppDelegate.h @@ -14,6 +14,7 @@ public: AppDelegate(); virtual ~AppDelegate(); + bool setOGLCntattrs(); /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. From 7dec71528841c1859ddff900d9dde85fcfca02a5 Mon Sep 17 00:00:00 2001 From: huangshiwu Date: Fri, 22 Aug 2014 10:55:39 +0800 Subject: [PATCH 2/3] OpenGL context attributions setting revise --- cocos/platform/CCApplicationProtocol.h | 3 +- cocos/platform/CCGLView.cpp | 10 ++-- cocos/platform/CCGLView.h | 15 ++++- cocos/platform/android/CCGLViewImpl.cpp | 8 +-- cocos/platform/android/CCGLViewImpl.h | 5 +- .../org/cocos2dx/lib/Cocos2dxActivity.java | 60 ++++--------------- .../src/org/cocos2dx/lib/Cocos2dxHelper.java | 4 -- cocos/platform/android/javaactivity.cpp | 15 ++++- .../Java_org_cocos2dx_lib_Cocos2dxHelper.cpp | 13 ---- .../Java_org_cocos2dx_lib_Cocos2dxHelper.h | 1 - cocos/platform/desktop/CCGLViewImpl.cpp | 17 ++---- cocos/platform/desktop/CCGLViewImpl.h | 1 - cocos/platform/ios/CCGLViewImpl.h | 2 +- cocos/platform/ios/CCGLViewImpl.mm | 8 +-- cocos/platform/linux/CCApplication.cpp | 2 +- cocos/platform/mac/CCApplication.mm | 2 +- cocos/platform/win32/CCApplication.cpp | 2 +- tests/cpp-empty-test/Classes/AppDelegate.cpp | 8 +-- tests/cpp-empty-test/Classes/AppDelegate.h | 2 +- .../cpp-empty-test/proj.ios/AppController.mm | 18 +++--- tests/cpp-tests/Classes/AppDelegate.cpp | 10 ++-- tests/cpp-tests/Classes/AppDelegate.h | 2 +- .../proj.ios/Classes/testsAppDelegate.mm | 17 +++--- .../project/Classes/AppDelegate.cpp | 8 +-- .../project/Classes/AppDelegate.h | 2 +- .../lua-tests/project/Classes/AppDelegate.cpp | 8 +-- tests/lua-tests/project/Classes/AppDelegate.h | 2 +- 27 files changed, 95 insertions(+), 150 deletions(-) diff --git a/cocos/platform/CCApplicationProtocol.h b/cocos/platform/CCApplicationProtocol.h index f490e390e6..5f4aa94829 100644 --- a/cocos/platform/CCApplicationProtocol.h +++ b/cocos/platform/CCApplicationProtocol.h @@ -102,7 +102,8 @@ public: */ virtual void setAnimationInterval(double interval) = 0; - virtual bool setOGLCntattrs() { return false; } + //initialize the OpenGL/OpenGL ES context attribution here for all platforms; + virtual void initContextAttrs() {} /** @brief Get current language config diff --git a/cocos/platform/CCGLView.cpp b/cocos/platform/CCGLView.cpp index 52d42d473f..64d733d804 100644 --- a/cocos/platform/CCGLView.cpp +++ b/cocos/platform/CCGLView.cpp @@ -70,14 +70,12 @@ namespace { } -int* GLView::_OGLCntattrs = new int[6]; +//default context attributions are setted as follows +ContextAttrs GLView::_contextAttrs = {5, 6, 5, 0, 16, 0}; -void GLView::setOGLCntattrs(int* OGLCntattrs) +void GLView::setContextAttrs(ContextAttrs& contextAttrs) { - for(int i = 0; i < 6; i++) - { - _OGLCntattrs[i] = OGLCntattrs[i]; - } + _contextAttrs = contextAttrs; } GLView::GLView() diff --git a/cocos/platform/CCGLView.h b/cocos/platform/CCGLView.h index 9d829c3703..1f4b746fe6 100644 --- a/cocos/platform/CCGLView.h +++ b/cocos/platform/CCGLView.h @@ -64,6 +64,16 @@ enum class ResolutionPolicy UNKNOWN, }; +struct ContextAttrs +{ + int redBits; + int greenBits; + int blueBits; + int alphaBits; + int depthBits; + int stencilBits; +}; + NS_CC_BEGIN /** @@ -98,8 +108,9 @@ public: virtual bool windowShouldClose() { return false; }; - static void setOGLCntattrs(int* OGLCntattrs); - static int* _OGLCntattrs; + //static method and member so that we can modify it on all platforms before create OpenGL context + static void setContextAttrs(ContextAttrs& contextAttrs); + static ContextAttrs _contextAttrs; /** * Polls input events. Subclass must implement methods if platform diff --git a/cocos/platform/android/CCGLViewImpl.cpp b/cocos/platform/android/CCGLViewImpl.cpp index 8f127ca176..fc5e322305 100644 --- a/cocos/platform/android/CCGLViewImpl.cpp +++ b/cocos/platform/android/CCGLViewImpl.cpp @@ -73,11 +73,9 @@ GLViewImpl* GLViewImpl::create(const std::string& viewName) return nullptr; } -GLViewImpl* GLViewImpl::createWithOGLCntattrs(const std::string& viewName) -{ - activityInitWithOGLCntattrsJni(_OGLCntattrs); - - return nullptr; +ContextAttrs GLViewImpl::getContextAttrs() +{ + return _contextAttrs; } GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName) diff --git a/cocos/platform/android/CCGLViewImpl.h b/cocos/platform/android/CCGLViewImpl.h index c437793201..43fae39323 100644 --- a/cocos/platform/android/CCGLViewImpl.h +++ b/cocos/platform/android/CCGLViewImpl.h @@ -41,10 +41,13 @@ public: // static function static GLViewImpl* create(const std::string &viewname); - static GLViewImpl* createWithOGLCntattrs(const std::string& viewName); static GLViewImpl* createWithRect(const std::string& viewName, Rect rect, float frameZoomFactor = 1.0f); static GLViewImpl* createWithFullScreen(const std::string& viewName); + //return context attribution to Java class Cocos2dxActivity which really + //create OpenGL ES context on android platform only + static ContextAttrs getContextAttrs(); + bool isOpenGLReady() override; void end() override; void swapBuffers() override; diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java index 012cc657b1..04a53b4e63 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -95,15 +95,16 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe Cocos2dxHelper.init(this); - preCreateOGLCtx(); - //this.init(); - //this.reinit(); - //if (mVideoHelper == null) { - //mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout); - //} + int[] OGLCtxattrs = getContextAttrs(); + this.initWithOGLCtxattrs(OGLCtxattrs); + + if (mVideoHelper == null) { + mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout); + } } - private static native void preCreateOGLCtx(); + //native method,call GLViewImpl::getContextAttrs() to get the OpenGL ES context attributions + private static native int[] getContextAttrs(); // =========================================================== // Getter & Setter @@ -170,43 +171,7 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe // =========================================================== // Methods // =========================================================== - public void init() { - - // FrameLayout - ViewGroup.LayoutParams framelayout_params = - new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.MATCH_PARENT); - mFrameLayout = new FrameLayout(this); - mFrameLayout.setLayoutParams(framelayout_params); - - // Cocos2dxEditText layout - ViewGroup.LayoutParams edittext_layout_params = - new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT); - Cocos2dxEditText edittext = new Cocos2dxEditText(this); - edittext.setLayoutParams(edittext_layout_params); - - // ...add to FrameLayout - mFrameLayout.addView(edittext); - - // Cocos2dxGLSurfaceView - this.mGLSurfaceView = this.onCreateView(); - - // ...add to FrameLayout - mFrameLayout.addView(this.mGLSurfaceView); - - // Switch to supported OpenGL (ARGB888) mode on emulator - if (isAndroidEmulator()) - this.mGLSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0); - - this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer()); - this.mGLSurfaceView.setCocos2dxEditText(edittext); - - // Set framelayout as the content view - setContentView(mFrameLayout); - } - - public void initWithOGLCntattrs(int[] OGLCntattrs) { + public void initWithOGLCtxattrs(int[] OGLCtxattrs) { // FrameLayout ViewGroup.LayoutParams framelayout_params = @@ -228,8 +193,7 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe // Cocos2dxGLSurfaceView this.mGLSurfaceView = new Cocos2dxGLSurfaceView(this); - //this.mGLSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8); - this.mGLSurfaceView.setEGLConfigChooser(OGLCntattrs[0], OGLCntattrs[1],OGLCntattrs[2],OGLCntattrs[3],OGLCntattrs[4],OGLCntattrs[5]); + this.mGLSurfaceView.setEGLConfigChooser(OGLCtxattrs[0], OGLCtxattrs[1],OGLCtxattrs[2],OGLCtxattrs[3],OGLCtxattrs[4],OGLCtxattrs[5]); // ...add to FrameLayout mFrameLayout.addView(this.mGLSurfaceView); @@ -243,10 +207,6 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe // Set framelayout as the content view setContentView(mFrameLayout); - - if (mVideoHelper == null) { - mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout); - } } public Cocos2dxGLSurfaceView onCreateView() { diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index 95a195aa28..ea102e7f73 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -102,10 +102,6 @@ public class Cocos2dxHelper { public static Activity getActivity() { return sActivity; } - - public static void activityInitWithOGLCntattrs(int[] OGLCntattrs){ - ((Cocos2dxActivity)sActivity).initWithOGLCntattrs(OGLCntattrs); - } public static void addOnActivityResultListener(OnActivityResultListener listener) { onActivityResultListeners.add(listener); diff --git a/cocos/platform/android/javaactivity.cpp b/cocos/platform/android/javaactivity.cpp index c5ed358707..fd43b859df 100644 --- a/cocos/platform/android/javaactivity.cpp +++ b/cocos/platform/android/javaactivity.cpp @@ -81,11 +81,20 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } } -void Java_org_cocos2dx_lib_Cocos2dxActivity_preCreateOGLCtx(JNIEnv* env, jobject thiz) +jintArray Java_org_cocos2dx_lib_Cocos2dxActivity_getContextAttrs(JNIEnv* env, jobject thiz) { cocos_android_app_init(env, thiz); - cocos2d::Application::getInstance()->setOGLCntattrs(); - GLViewImpl::createWithOGLCntattrs("Android app"); + cocos2d::Application::getInstance()->initContextAttrs(); + ContextAttrs _contextAttrs = GLViewImpl::getContextAttrs(); + + int tmp[6] = {_contextAttrs.redBits, _contextAttrs.greenBits, _contextAttrs.blueBits, + _contextAttrs.alphaBits, _contextAttrs.depthBits, _contextAttrs.stencilBits}; + + + jintArray contextAttrsJava = env->NewIntArray(6); + env->SetIntArrayRegion(contextAttrsJava, 0, 6, tmp); + + return contextAttrsJava; } void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnSurfaceChanged(JNIEnv* env, jobject thiz, jint w, jint h) diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp index 1353fb10cb..ef9fac20a4 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp @@ -209,19 +209,6 @@ void disableAccelerometerJni() { } } -void activityInitWithOGLCntattrsJni(int* OGLCntattrs) { - JniMethodInfo t; - - if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "activityInitWithOGLCntattrs", "([I)V")) { - jintArray OGLCntattrsJava = t.env->NewIntArray(6); - t.env->SetIntArrayRegion(OGLCntattrsJava, 0, 6, OGLCntattrs); - - t.env->CallStaticVoidMethod(t.classID, t.methodID, OGLCntattrsJava); - t.env->DeleteLocalRef(t.classID); - CCLOG("call Cocos2dxHelper activityInitWithOGLCntattrs"); - } -} - void setKeepScreenOnJni(bool value) { JniMethodInfo t; diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h index f3baea2852..7044a4e5e4 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h @@ -39,7 +39,6 @@ extern std::string getFileDirectoryJNI(); extern void enableAccelerometerJni(); extern void disableAccelerometerJni(); extern void setAccelerometerIntervalJni(float interval); -extern void activityInitWithOGLCntattrsJni(int* OGLCntattrs); extern void setKeepScreenOnJni(bool value); // functions for UserDefault extern bool getBoolForKeyJNI(const char* key, bool defaultValue); diff --git a/cocos/platform/desktop/CCGLViewImpl.cpp b/cocos/platform/desktop/CCGLViewImpl.cpp index 36e35aad9d..ef36d9134a 100644 --- a/cocos/platform/desktop/CCGLViewImpl.cpp +++ b/cocos/platform/desktop/CCGLViewImpl.cpp @@ -337,16 +337,6 @@ GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName, const return nullptr; } -void GLViewImpl::convertattrs() -{ - glfwWindowHint(GLFW_RED_BITS,_OGLCntattrs[0]); - glfwWindowHint(GLFW_GREEN_BITS,_OGLCntattrs[1]); - glfwWindowHint(GLFW_BLUE_BITS,_OGLCntattrs[2]); - glfwWindowHint(GLFW_ALPHA_BITS,_OGLCntattrs[3]); - glfwWindowHint(GLFW_DEPTH_BITS,_OGLCntattrs[4]); - glfwWindowHint(GLFW_STENCIL_BITS,_OGLCntattrs[5]); -} - bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor) { setViewName(viewName); @@ -354,7 +344,12 @@ bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float fram _frameZoomFactor = frameZoomFactor; glfwWindowHint(GLFW_RESIZABLE,GL_FALSE); - convertattrs(); + glfwWindowHint(GLFW_RED_BITS,_contextAttrs.redBits); + glfwWindowHint(GLFW_GREEN_BITS,_contextAttrs.greenBits); + glfwWindowHint(GLFW_BLUE_BITS,_contextAttrs.blueBits); + glfwWindowHint(GLFW_ALPHA_BITS,_contextAttrs.alphaBits); + glfwWindowHint(GLFW_DEPTH_BITS,_contextAttrs.depthBits); + glfwWindowHint(GLFW_STENCIL_BITS,_contextAttrs.stencilBits); _mainWindow = glfwCreateWindow(rect.size.width * _frameZoomFactor, rect.size.height * _frameZoomFactor, diff --git a/cocos/platform/desktop/CCGLViewImpl.h b/cocos/platform/desktop/CCGLViewImpl.h index c03fa9f31a..368e6d97a2 100644 --- a/cocos/platform/desktop/CCGLViewImpl.h +++ b/cocos/platform/desktop/CCGLViewImpl.h @@ -149,7 +149,6 @@ protected: friend class GLFWEventHandler; private: - void convertattrs(); CC_DISALLOW_COPY_AND_ASSIGN(GLViewImpl); }; diff --git a/cocos/platform/ios/CCGLViewImpl.h b/cocos/platform/ios/CCGLViewImpl.h index d3950e5f38..e3e32c3726 100644 --- a/cocos/platform/ios/CCGLViewImpl.h +++ b/cocos/platform/ios/CCGLViewImpl.h @@ -53,7 +53,7 @@ public: /** creates a GLViewImpl with a name in fullscreen mode */ static GLViewImpl* createWithFullScreen(const std::string& viewName); - static void convert(int* _OGLCntattrs); + static void convertAttrs(); static void* _pixelFormat; static int _depthFormat; diff --git a/cocos/platform/ios/CCGLViewImpl.mm b/cocos/platform/ios/CCGLViewImpl.mm index 8438cc968a..376d811e68 100644 --- a/cocos/platform/ios/CCGLViewImpl.mm +++ b/cocos/platform/ios/CCGLViewImpl.mm @@ -83,13 +83,13 @@ GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName) return nullptr; } -void GLViewImpl::convert(int* OGLCntattrs) +void GLViewImpl::convertAttrs() { - if(OGLCntattrs[0]==8 && OGLCntattrs[1]==8 && OGLCntattrs[2]==8 && OGLCntattrs[3]==8) + if(_contextAttrs.redBits==8 && _contextAttrs.greenBits==8 && _contextAttrs.blueBits==8 && _contextAttrs.alphaBits==8) { _pixelFormat = kEAGLColorFormatRGBA8; } - if(OGLCntattrs[4]==24 && OGLCntattrs[5]==8) + if(_contextAttrs.depthBits==24 && _contextAttrs.stencilBits==8) { _depthFormat = GL_DEPTH24_STENCIL8_OES; } @@ -120,7 +120,7 @@ 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); - convert(_OGLCntattrs); + convertAttrs(); CCEAGLView *eaglview = [CCEAGLView viewWithFrame: r pixelFormat: (NSString*)_pixelFormat depthFormat: _depthFormat diff --git a/cocos/platform/linux/CCApplication.cpp b/cocos/platform/linux/CCApplication.cpp index f95ba45e0b..d6a0d010e1 100644 --- a/cocos/platform/linux/CCApplication.cpp +++ b/cocos/platform/linux/CCApplication.cpp @@ -63,7 +63,7 @@ Application::~Application() int Application::run() { - setOGLCntattrs(); + initContextAttrs(); // Initialize instance and cocos2d. if (! applicationDidFinishLaunching()) { diff --git a/cocos/platform/mac/CCApplication.mm b/cocos/platform/mac/CCApplication.mm index 3a41f1377e..4f14e09c67 100644 --- a/cocos/platform/mac/CCApplication.mm +++ b/cocos/platform/mac/CCApplication.mm @@ -64,7 +64,7 @@ Application::~Application() int Application::run() { - setOGLCntattrs(); + initContextAttrs(); if(!applicationDidFinishLaunching()) { return 1; diff --git a/cocos/platform/win32/CCApplication.cpp b/cocos/platform/win32/CCApplication.cpp index 55d7a85b11..55f0378946 100644 --- a/cocos/platform/win32/CCApplication.cpp +++ b/cocos/platform/win32/CCApplication.cpp @@ -69,7 +69,7 @@ int Application::run() QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nLast); - setOGLCntattrs(); + initContextAttrs(); // Initialize instance and cocos2d. if (!applicationDidFinishLaunching()) diff --git a/tests/cpp-empty-test/Classes/AppDelegate.cpp b/tests/cpp-empty-test/Classes/AppDelegate.cpp index 511f3530df..1f5b5b799f 100644 --- a/tests/cpp-empty-test/Classes/AppDelegate.cpp +++ b/tests/cpp-empty-test/Classes/AppDelegate.cpp @@ -17,13 +17,11 @@ AppDelegate::~AppDelegate() { } -bool AppDelegate::setOGLCntattrs() +void AppDelegate::initContextAttrs() { - int OGLCntattrs[] = {8, 8, 8, 8, 24, 8}; + ContextAttrs contextAttrs = {8, 8, 8, 8, 24, 8}; - GLView::setOGLCntattrs(OGLCntattrs); - - return true; + GLView::setContextAttrs(contextAttrs); } bool AppDelegate::applicationDidFinishLaunching() { diff --git a/tests/cpp-empty-test/Classes/AppDelegate.h b/tests/cpp-empty-test/Classes/AppDelegate.h index 2f4e608890..5a7c8e54e3 100644 --- a/tests/cpp-empty-test/Classes/AppDelegate.h +++ b/tests/cpp-empty-test/Classes/AppDelegate.h @@ -14,7 +14,7 @@ public: AppDelegate(); virtual ~AppDelegate(); - virtual bool setOGLCntattrs(); + virtual void initContextAttrs(); /** @brief Implement Director and Scene init code here. diff --git a/tests/cpp-empty-test/proj.ios/AppController.mm b/tests/cpp-empty-test/proj.ios/AppController.mm index 6171e0a739..d5da4a98be 100644 --- a/tests/cpp-empty-test/proj.ios/AppController.mm +++ b/tests/cpp-empty-test/proj.ios/AppController.mm @@ -39,21 +39,21 @@ static AppDelegate s_sharedApplication; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { cocos2d::Application *app = cocos2d::Application::getInstance(); - app->setOGLCntattrs(); + app->initContextAttrs(); + 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]]; - auto glview = cocos2d::GLViewImpl::createWithFullScreen("ios cpp-empty-test"); - CCEAGLView *eaglView = (CCEAGLView*)glview->getEAGLView(); - /*CCEAGLView eaglView = [CCEAGLView viewWithFrame: [window bounds] - pixelFormat: kEAGLColorFormatRGBA8 - depthFormat: GL_DEPTH_COMPONENT16 + + CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds] + pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat + depthFormat: cocos2d::GLViewImpl::_depthFormat preserveBackbuffer: NO sharegroup: nil multiSampling: NO - numberOfSamples: 0];*/ + numberOfSamples: 0]; // Use RootViewController manage CCEAGLView @@ -77,12 +77,10 @@ static AppDelegate s_sharedApplication; [[UIApplication sharedApplication] setStatusBarHidden: YES]; - glview->setSize(); // IMPORTANT: Setting the GLView should be done after creating the RootViewController - //cocos2d::GLViewImpl *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView); + cocos2d::GLViewImpl *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView); cocos2d::Director::getInstance()->setOpenGLView(glview); - //cocos2d::Application *app = cocos2d::Application::getInstance(); app->run(); return YES; } diff --git a/tests/cpp-tests/Classes/AppDelegate.cpp b/tests/cpp-tests/Classes/AppDelegate.cpp index f97219d86f..a6a8cbb8cd 100644 --- a/tests/cpp-tests/Classes/AppDelegate.cpp +++ b/tests/cpp-tests/Classes/AppDelegate.cpp @@ -43,15 +43,15 @@ AppDelegate::~AppDelegate() cocostudio::ArmatureDataManager::destroyInstance(); } -bool AppDelegate::setOGLCntattrs() +//if you want a different context,just modify the value of contextAttrs +//it will takes effect on all platforms +void AppDelegate::initContextAttrs() { //set OpenGL context attributions //red,green,blue,alpha,depth,stencil - int OGLCntattrs[] = {8, 8, 8, 8, 24, 8}; + ContextAttrs contextAttrs = {8, 8, 8, 8, 24, 8}; - GLView::setOGLCntattrs(OGLCntattrs); - - return true; + GLView::setContextAttrs(contextAttrs); } bool AppDelegate::applicationDidFinishLaunching() diff --git a/tests/cpp-tests/Classes/AppDelegate.h b/tests/cpp-tests/Classes/AppDelegate.h index 44cb8cace2..1594eb8414 100644 --- a/tests/cpp-tests/Classes/AppDelegate.h +++ b/tests/cpp-tests/Classes/AppDelegate.h @@ -39,7 +39,7 @@ public: AppDelegate(); virtual ~AppDelegate(); - virtual bool setOGLCntattrs(); + virtual void initContextAttrs(); /** @brief Implement Director and Scene init code here. diff --git a/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm b/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm index ffa2939402..f6b23a9b91 100644 --- a/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm +++ b/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm @@ -42,7 +42,8 @@ static AppDelegate s_sharedApplication; { cocos2d::Application *app = cocos2d::Application::getInstance(); - app->setOGLCntattrs(); + app->initContextAttrs(); + cocos2d::GLViewImpl::convertAttrs(); // Override point for customization after application launch. @@ -50,15 +51,13 @@ static AppDelegate s_sharedApplication; window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; // Init the CCEAGLView - auto glview = cocos2d::GLViewImpl::createWithFullScreen("ios cpp-tests"); - CCEAGLView *eaglView = (CCEAGLView*)glview->getEAGLView(); - /*CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds] - pixelFormat: kEAGLColorFormatRGBA8 - depthFormat: GL_DEPTH24_STENCIL8_OES + CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds] + pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat + depthFormat: cocos2d::GLViewImpl::_depthFormat preserveBackbuffer: NO sharegroup: nil multiSampling: NO - numberOfSamples: 0 ];*/ + numberOfSamples: 0 ]; [eaglView setMultipleTouchEnabled:YES]; @@ -83,10 +82,8 @@ static AppDelegate s_sharedApplication; [[UIApplication sharedApplication] setStatusBarHidden:true]; - glview->setSize(); - // IMPORTANT: Setting the GLView should be done after creating the RootViewController - //cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView); + cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView); cocos2d::Director::getInstance()->setOpenGLView(glview); app->run(); diff --git a/tests/lua-empty-test/project/Classes/AppDelegate.cpp b/tests/lua-empty-test/project/Classes/AppDelegate.cpp index d07d5656af..55cf4845a1 100644 --- a/tests/lua-empty-test/project/Classes/AppDelegate.cpp +++ b/tests/lua-empty-test/project/Classes/AppDelegate.cpp @@ -21,13 +21,11 @@ AppDelegate::~AppDelegate() //CCScriptEngineManager::destroyInstance(); } -bool AppDelegate::setOGLCntattrs() +void AppDelegate::initContextAttrs() { - int OGLCntattrs[] = {8, 8, 8, 8, 24, 8}; + ContextAttrs contextAttrs = {8, 8, 8, 8, 24, 8}; - GLView::setOGLCntattrs(OGLCntattrs); - - return true; + GLView::setContextAttrs(contextAttrs); } bool AppDelegate::applicationDidFinishLaunching() diff --git a/tests/lua-empty-test/project/Classes/AppDelegate.h b/tests/lua-empty-test/project/Classes/AppDelegate.h index 848945dd5c..568bfb7cc3 100644 --- a/tests/lua-empty-test/project/Classes/AppDelegate.h +++ b/tests/lua-empty-test/project/Classes/AppDelegate.h @@ -14,7 +14,7 @@ public: AppDelegate(); virtual ~AppDelegate(); - virtual bool setOGLCntattrs(); + virtual void initContextAttrs(); /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. diff --git a/tests/lua-tests/project/Classes/AppDelegate.cpp b/tests/lua-tests/project/Classes/AppDelegate.cpp index 121b74f4bb..006dd687a1 100644 --- a/tests/lua-tests/project/Classes/AppDelegate.cpp +++ b/tests/lua-tests/project/Classes/AppDelegate.cpp @@ -18,13 +18,11 @@ AppDelegate::~AppDelegate() SimpleAudioEngine::end(); } -bool AppDelegate::setOGLCntattrs() +void AppDelegate::initContextAttrs() { - int OGLCntattrs[] = {8, 8, 8, 8, 24, 8}; + ContextAttrs contextAttrs = {8, 8, 8, 8, 24, 8}; - GLView::setOGLCntattrs(OGLCntattrs); - - return true; + GLView::setContextAttrs(contextAttrs); } bool AppDelegate::applicationDidFinishLaunching() diff --git a/tests/lua-tests/project/Classes/AppDelegate.h b/tests/lua-tests/project/Classes/AppDelegate.h index b6791cc935..90979dea7a 100644 --- a/tests/lua-tests/project/Classes/AppDelegate.h +++ b/tests/lua-tests/project/Classes/AppDelegate.h @@ -14,7 +14,7 @@ public: AppDelegate(); virtual ~AppDelegate(); - bool setOGLCntattrs(); + void initContextAttrs(); /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. From 07af58c0c23f592469c112dee64e0e51f00d9500 Mon Sep 17 00:00:00 2001 From: huangshiwu Date: Fri, 22 Aug 2014 16:22:16 +0800 Subject: [PATCH 3/3] OpenGL context attributions setting revise -2 --- cocos/platform/CCApplicationProtocol.h | 10 ++++++-- cocos/platform/CCGLView.cpp | 11 ++++++--- cocos/platform/CCGLView.h | 7 +++--- cocos/platform/android/CCGLViewImpl.cpp | 5 ---- cocos/platform/android/CCGLViewImpl.h | 4 ---- .../org/cocos2dx/lib/Cocos2dxActivity.java | 24 +++++++++++-------- cocos/platform/android/javaactivity.cpp | 16 ++++++------- cocos/platform/desktop/CCGLViewImpl.cpp | 12 +++++----- cocos/platform/ios/CCGLViewImpl.h | 2 -- cocos/platform/ios/CCGLViewImpl.mm | 15 ++++-------- cocos/platform/linux/CCApplication.cpp | 2 +- cocos/platform/mac/CCApplication.mm | 2 +- cocos/platform/win32/CCApplication.cpp | 2 +- .../proj.ios_mac/ios/AppController.mm | 18 ++++++++------ .../cpp-template-default/src/AppDelegate.cpp | 11 +++++++++ .../cpp-template-default/src/AppDelegate.h | 2 ++ tests/cpp-empty-test/Classes/AppDelegate.cpp | 6 ++--- tests/cpp-empty-test/Classes/AppDelegate.h | 2 +- .../cpp-empty-test/proj.ios/AppController.mm | 2 +- tests/cpp-tests/Classes/AppDelegate.cpp | 10 ++++---- tests/cpp-tests/Classes/AppDelegate.h | 2 +- .../org/cocos2dx/cpp_tests/AppActivity.java | 8 ------- .../proj.ios/Classes/testsAppDelegate.mm | 2 +- .../project/Classes/AppDelegate.cpp | 6 ++--- .../project/Classes/AppDelegate.h | 2 +- .../project/proj.ios/AppController.mm | 10 ++++---- .../lua-tests/project/Classes/AppDelegate.cpp | 6 ++--- tests/lua-tests/project/Classes/AppDelegate.h | 2 +- .../project/proj.ios_mac/ios/AppController.mm | 9 ++++--- 29 files changed, 111 insertions(+), 99 deletions(-) diff --git a/cocos/platform/CCApplicationProtocol.h b/cocos/platform/CCApplicationProtocol.h index 5f4aa94829..2e56e0524d 100644 --- a/cocos/platform/CCApplicationProtocol.h +++ b/cocos/platform/CCApplicationProtocol.h @@ -102,8 +102,14 @@ public: */ virtual void setAnimationInterval(double interval) = 0; - //initialize the OpenGL/OpenGL ES context attribution here for all platforms; - virtual void initContextAttrs() {} + //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 diff --git a/cocos/platform/CCGLView.cpp b/cocos/platform/CCGLView.cpp index 64d733d804..98ccb9784b 100644 --- a/cocos/platform/CCGLView.cpp +++ b/cocos/platform/CCGLView.cpp @@ -71,11 +71,16 @@ namespace { } //default context attributions are setted as follows -ContextAttrs GLView::_contextAttrs = {5, 6, 5, 0, 16, 0}; +GLContextAttrs GLView::_glContextAttrs = {5, 6, 5, 0, 16, 0}; -void GLView::setContextAttrs(ContextAttrs& contextAttrs) +void GLView::setGLContextAttrs(GLContextAttrs& glContextAttrs) { - _contextAttrs = contextAttrs; + _glContextAttrs = glContextAttrs; +} + +GLContextAttrs GLView::getGLContextAttrs() +{ + return _glContextAttrs; } GLView::GLView() diff --git a/cocos/platform/CCGLView.h b/cocos/platform/CCGLView.h index 1f4b746fe6..f8243770ed 100644 --- a/cocos/platform/CCGLView.h +++ b/cocos/platform/CCGLView.h @@ -64,7 +64,7 @@ enum class ResolutionPolicy UNKNOWN, }; -struct ContextAttrs +struct GLContextAttrs { int redBits; int greenBits; @@ -109,8 +109,9 @@ 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 setContextAttrs(ContextAttrs& contextAttrs); - static ContextAttrs _contextAttrs; + static void setGLContextAttrs(GLContextAttrs& glContextAttrs); + static GLContextAttrs getGLContextAttrs(); + static GLContextAttrs _glContextAttrs; /** * Polls input events. Subclass must implement methods if platform diff --git a/cocos/platform/android/CCGLViewImpl.cpp b/cocos/platform/android/CCGLViewImpl.cpp index fc5e322305..8431dcdcab 100644 --- a/cocos/platform/android/CCGLViewImpl.cpp +++ b/cocos/platform/android/CCGLViewImpl.cpp @@ -73,11 +73,6 @@ GLViewImpl* GLViewImpl::create(const std::string& viewName) return nullptr; } -ContextAttrs GLViewImpl::getContextAttrs() -{ - return _contextAttrs; -} - GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName) { auto ret = new GLViewImpl(); diff --git a/cocos/platform/android/CCGLViewImpl.h b/cocos/platform/android/CCGLViewImpl.h index 43fae39323..820189e148 100644 --- a/cocos/platform/android/CCGLViewImpl.h +++ b/cocos/platform/android/CCGLViewImpl.h @@ -44,10 +44,6 @@ public: static GLViewImpl* createWithRect(const std::string& viewName, Rect rect, float frameZoomFactor = 1.0f); static GLViewImpl* createWithFullScreen(const std::string& viewName); - //return context attribution to Java class Cocos2dxActivity which really - //create OpenGL ES context on android platform only - static ContextAttrs getContextAttrs(); - bool isOpenGLReady() override; void end() override; void swapBuffers() override; diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java index 04a53b4e63..1ad24349ab 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -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,16 +96,16 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe Cocos2dxHelper.init(this); - int[] OGLCtxattrs = getContextAttrs(); - this.initWithOGLCtxattrs(OGLCtxattrs); + this.glContextAttrs = getGLContextAttrs(); + this.init(); if (mVideoHelper == null) { mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout); } } - //native method,call GLViewImpl::getContextAttrs() to get the OpenGL ES context attributions - private static native int[] getContextAttrs(); + //native method,call GLViewImpl::getGLContextAttrs() to get the OpenGL ES context attributions + private static native int[] getGLContextAttrs(); // =========================================================== // Getter & Setter @@ -171,7 +172,7 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe // =========================================================== // Methods // =========================================================== - public void initWithOGLCtxattrs(int[] OGLCtxattrs) { + public void init() { // FrameLayout ViewGroup.LayoutParams framelayout_params = @@ -191,16 +192,14 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe mFrameLayout.addView(edittext); // Cocos2dxGLSurfaceView - this.mGLSurfaceView = new Cocos2dxGLSurfaceView(this); - - this.mGLSurfaceView.setEGLConfigChooser(OGLCtxattrs[0], OGLCtxattrs[1],OGLCtxattrs[2],OGLCtxattrs[3],OGLCtxattrs[4],OGLCtxattrs[5]); + this.mGLSurfaceView = this.onCreateView(); // ...add to FrameLayout mFrameLayout.addView(this.mGLSurfaceView); // 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); @@ -210,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() { diff --git a/cocos/platform/android/javaactivity.cpp b/cocos/platform/android/javaactivity.cpp index fd43b859df..d5d2f7df69 100644 --- a/cocos/platform/android/javaactivity.cpp +++ b/cocos/platform/android/javaactivity.cpp @@ -81,20 +81,20 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } } -jintArray Java_org_cocos2dx_lib_Cocos2dxActivity_getContextAttrs(JNIEnv* env, jobject thiz) +jintArray Java_org_cocos2dx_lib_Cocos2dxActivity_getGLContextAttrs(JNIEnv* env, jobject thiz) { cocos_android_app_init(env, thiz); - cocos2d::Application::getInstance()->initContextAttrs(); - ContextAttrs _contextAttrs = GLViewImpl::getContextAttrs(); + cocos2d::Application::getInstance()->initGLContextAttrs(); + GLContextAttrs _glContextAttrs = GLView::getGLContextAttrs(); - int tmp[6] = {_contextAttrs.redBits, _contextAttrs.greenBits, _contextAttrs.blueBits, - _contextAttrs.alphaBits, _contextAttrs.depthBits, _contextAttrs.stencilBits}; + int tmp[6] = {_glContextAttrs.redBits, _glContextAttrs.greenBits, _glContextAttrs.blueBits, + _glContextAttrs.alphaBits, _glContextAttrs.depthBits, _glContextAttrs.stencilBits}; - jintArray contextAttrsJava = env->NewIntArray(6); - env->SetIntArrayRegion(contextAttrsJava, 0, 6, tmp); + jintArray glContextAttrsJava = env->NewIntArray(6); + env->SetIntArrayRegion(glContextAttrsJava, 0, 6, tmp); - return contextAttrsJava; + return glContextAttrsJava; } void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnSurfaceChanged(JNIEnv* env, jobject thiz, jint w, jint h) diff --git a/cocos/platform/desktop/CCGLViewImpl.cpp b/cocos/platform/desktop/CCGLViewImpl.cpp index ef36d9134a..7ed74f4852 100644 --- a/cocos/platform/desktop/CCGLViewImpl.cpp +++ b/cocos/platform/desktop/CCGLViewImpl.cpp @@ -344,12 +344,12 @@ bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float fram _frameZoomFactor = frameZoomFactor; glfwWindowHint(GLFW_RESIZABLE,GL_FALSE); - glfwWindowHint(GLFW_RED_BITS,_contextAttrs.redBits); - glfwWindowHint(GLFW_GREEN_BITS,_contextAttrs.greenBits); - glfwWindowHint(GLFW_BLUE_BITS,_contextAttrs.blueBits); - glfwWindowHint(GLFW_ALPHA_BITS,_contextAttrs.alphaBits); - glfwWindowHint(GLFW_DEPTH_BITS,_contextAttrs.depthBits); - glfwWindowHint(GLFW_STENCIL_BITS,_contextAttrs.stencilBits); + 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, diff --git a/cocos/platform/ios/CCGLViewImpl.h b/cocos/platform/ios/CCGLViewImpl.h index e3e32c3726..4370151600 100644 --- a/cocos/platform/ios/CCGLViewImpl.h +++ b/cocos/platform/ios/CCGLViewImpl.h @@ -56,8 +56,6 @@ public: static void convertAttrs(); static void* _pixelFormat; static int _depthFormat; - - void setSize(); /** sets the content scale factor */ bool setContentScaleFactor(float contentScaleFactor); diff --git a/cocos/platform/ios/CCGLViewImpl.mm b/cocos/platform/ios/CCGLViewImpl.mm index 376d811e68..2bc9aec8d7 100644 --- a/cocos/platform/ios/CCGLViewImpl.mm +++ b/cocos/platform/ios/CCGLViewImpl.mm @@ -85,11 +85,11 @@ GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName) void GLViewImpl::convertAttrs() { - if(_contextAttrs.redBits==8 && _contextAttrs.greenBits==8 && _contextAttrs.blueBits==8 && _contextAttrs.alphaBits==8) + if(_glContextAttrs.redBits==8 && _glContextAttrs.greenBits==8 && _glContextAttrs.blueBits==8 && _glContextAttrs.alphaBits==8) { _pixelFormat = kEAGLColorFormatRGBA8; } - if(_contextAttrs.depthBits==24 && _contextAttrs.stencilBits==8) + if(_glContextAttrs.depthBits==24 && _glContextAttrs.stencilBits==8) { _depthFormat = GL_DEPTH24_STENCIL8_OES; } @@ -131,8 +131,8 @@ bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float fram [eaglview setMultipleTouchEnabled:YES]; - //_screenSize.width = _designResolutionSize.width = [eaglview getWidth]; - //_screenSize.height = _designResolutionSize.height = [eaglview getHeight]; + _screenSize.width = _designResolutionSize.width = [eaglview getWidth]; + _screenSize.height = _designResolutionSize.height = [eaglview getHeight]; // _scaleX = _scaleY = [eaglview contentScaleFactor]; _eaglview = eaglview; @@ -140,13 +140,6 @@ bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float fram return true; } -void GLViewImpl::setSize() -{ - CCEAGLView *eaglview = (CCEAGLView*) _eaglview; - _screenSize.width = _designResolutionSize.width = [eaglview getWidth]; - _screenSize.height = _designResolutionSize.height = [eaglview getHeight]; -} - bool GLViewImpl::initWithFullScreen(const std::string& viewName) { CGRect rect = [[UIScreen mainScreen] bounds]; diff --git a/cocos/platform/linux/CCApplication.cpp b/cocos/platform/linux/CCApplication.cpp index d6a0d010e1..c23d0f9883 100644 --- a/cocos/platform/linux/CCApplication.cpp +++ b/cocos/platform/linux/CCApplication.cpp @@ -63,7 +63,7 @@ Application::~Application() int Application::run() { - initContextAttrs(); + initGLContextAttrs(); // Initialize instance and cocos2d. if (! applicationDidFinishLaunching()) { diff --git a/cocos/platform/mac/CCApplication.mm b/cocos/platform/mac/CCApplication.mm index 4f14e09c67..bae4d94522 100644 --- a/cocos/platform/mac/CCApplication.mm +++ b/cocos/platform/mac/CCApplication.mm @@ -64,7 +64,7 @@ Application::~Application() int Application::run() { - initContextAttrs(); + initGLContextAttrs(); if(!applicationDidFinishLaunching()) { return 1; diff --git a/cocos/platform/win32/CCApplication.cpp b/cocos/platform/win32/CCApplication.cpp index 55f0378946..562c7835ed 100644 --- a/cocos/platform/win32/CCApplication.cpp +++ b/cocos/platform/win32/CCApplication.cpp @@ -69,7 +69,7 @@ int Application::run() QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nLast); - initContextAttrs(); + initGLContextAttrs(); // Initialize instance and cocos2d. if (!applicationDidFinishLaunching()) diff --git a/templates/cpp-template-default/proj.ios_mac/ios/AppController.mm b/templates/cpp-template-default/proj.ios_mac/ios/AppController.mm index 897519c84c..9341e9894e 100644 --- a/templates/cpp-template-default/proj.ios_mac/ios/AppController.mm +++ b/templates/cpp-template-default/proj.ios_mac/ios/AppController.mm @@ -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; } diff --git a/templates/cpp-template-default/src/AppDelegate.cpp b/templates/cpp-template-default/src/AppDelegate.cpp index beb7bc9e7a..5171cfd782 100644 --- a/templates/cpp-template-default/src/AppDelegate.cpp +++ b/templates/cpp-template-default/src/AppDelegate.cpp @@ -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(); diff --git a/templates/cpp-template-default/src/AppDelegate.h b/templates/cpp-template-default/src/AppDelegate.h index 18ee8aeb63..625506cc0d 100644 --- a/templates/cpp-template-default/src/AppDelegate.h +++ b/templates/cpp-template-default/src/AppDelegate.h @@ -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. diff --git a/tests/cpp-empty-test/Classes/AppDelegate.cpp b/tests/cpp-empty-test/Classes/AppDelegate.cpp index 1f5b5b799f..b1208d79f5 100644 --- a/tests/cpp-empty-test/Classes/AppDelegate.cpp +++ b/tests/cpp-empty-test/Classes/AppDelegate.cpp @@ -17,11 +17,11 @@ AppDelegate::~AppDelegate() { } -void AppDelegate::initContextAttrs() +void AppDelegate::initGLContextAttrs() { - ContextAttrs contextAttrs = {8, 8, 8, 8, 24, 8}; + GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; - GLView::setContextAttrs(contextAttrs); + GLView::setGLContextAttrs(glContextAttrs); } bool AppDelegate::applicationDidFinishLaunching() { diff --git a/tests/cpp-empty-test/Classes/AppDelegate.h b/tests/cpp-empty-test/Classes/AppDelegate.h index 5a7c8e54e3..4d5b8b0d31 100644 --- a/tests/cpp-empty-test/Classes/AppDelegate.h +++ b/tests/cpp-empty-test/Classes/AppDelegate.h @@ -14,7 +14,7 @@ public: AppDelegate(); virtual ~AppDelegate(); - virtual void initContextAttrs(); + virtual void initGLContextAttrs(); /** @brief Implement Director and Scene init code here. diff --git a/tests/cpp-empty-test/proj.ios/AppController.mm b/tests/cpp-empty-test/proj.ios/AppController.mm index d5da4a98be..fb70c76a07 100644 --- a/tests/cpp-empty-test/proj.ios/AppController.mm +++ b/tests/cpp-empty-test/proj.ios/AppController.mm @@ -39,7 +39,7 @@ static AppDelegate s_sharedApplication; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { cocos2d::Application *app = cocos2d::Application::getInstance(); - app->initContextAttrs(); + app->initGLContextAttrs(); cocos2d::GLViewImpl::convertAttrs(); // Override point for customization after application launch. diff --git a/tests/cpp-tests/Classes/AppDelegate.cpp b/tests/cpp-tests/Classes/AppDelegate.cpp index a6a8cbb8cd..fdfb12d7b8 100644 --- a/tests/cpp-tests/Classes/AppDelegate.cpp +++ b/tests/cpp-tests/Classes/AppDelegate.cpp @@ -43,15 +43,15 @@ AppDelegate::~AppDelegate() cocostudio::ArmatureDataManager::destroyInstance(); } -//if you want a different context,just modify the value of contextAttrs +//if you want a different context,just modify the value of glContextAttrs //it will takes effect on all platforms -void AppDelegate::initContextAttrs() +void AppDelegate::initGLContextAttrs() { - //set OpenGL context attributions + //set OpenGL context attributions,now can only set six attributions: //red,green,blue,alpha,depth,stencil - ContextAttrs contextAttrs = {8, 8, 8, 8, 24, 8}; + GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; - GLView::setContextAttrs(contextAttrs); + GLView::setGLContextAttrs(glContextAttrs); } bool AppDelegate::applicationDidFinishLaunching() diff --git a/tests/cpp-tests/Classes/AppDelegate.h b/tests/cpp-tests/Classes/AppDelegate.h index 1594eb8414..7fb3084ad5 100644 --- a/tests/cpp-tests/Classes/AppDelegate.h +++ b/tests/cpp-tests/Classes/AppDelegate.h @@ -39,7 +39,7 @@ public: AppDelegate(); virtual ~AppDelegate(); - virtual void initContextAttrs(); + virtual void initGLContextAttrs(); /** @brief Implement Director and Scene init code here. diff --git a/tests/cpp-tests/proj.android/src/org/cocos2dx/cpp_tests/AppActivity.java b/tests/cpp-tests/proj.android/src/org/cocos2dx/cpp_tests/AppActivity.java index 4934ed0bee..2c1e0e82e7 100644 --- a/tests/cpp-tests/proj.android/src/org/cocos2dx/cpp_tests/AppActivity.java +++ b/tests/cpp-tests/proj.android/src/org/cocos2dx/cpp_tests/AppActivity.java @@ -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; - } } diff --git a/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm b/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm index f6b23a9b91..7df7890aa2 100644 --- a/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm +++ b/tests/cpp-tests/proj.ios/Classes/testsAppDelegate.mm @@ -42,7 +42,7 @@ static AppDelegate s_sharedApplication; { cocos2d::Application *app = cocos2d::Application::getInstance(); - app->initContextAttrs(); + app->initGLContextAttrs(); cocos2d::GLViewImpl::convertAttrs(); // Override point for customization after application launch. diff --git a/tests/lua-empty-test/project/Classes/AppDelegate.cpp b/tests/lua-empty-test/project/Classes/AppDelegate.cpp index 55cf4845a1..8d0ac6aeb4 100644 --- a/tests/lua-empty-test/project/Classes/AppDelegate.cpp +++ b/tests/lua-empty-test/project/Classes/AppDelegate.cpp @@ -21,11 +21,11 @@ AppDelegate::~AppDelegate() //CCScriptEngineManager::destroyInstance(); } -void AppDelegate::initContextAttrs() +void AppDelegate::initGLContextAttrs() { - ContextAttrs contextAttrs = {8, 8, 8, 8, 24, 8}; + GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; - GLView::setContextAttrs(contextAttrs); + GLView::setGLContextAttrs(glContextAttrs); } bool AppDelegate::applicationDidFinishLaunching() diff --git a/tests/lua-empty-test/project/Classes/AppDelegate.h b/tests/lua-empty-test/project/Classes/AppDelegate.h index 568bfb7cc3..41d0b2ca88 100644 --- a/tests/lua-empty-test/project/Classes/AppDelegate.h +++ b/tests/lua-empty-test/project/Classes/AppDelegate.h @@ -14,7 +14,7 @@ public: AppDelegate(); virtual ~AppDelegate(); - virtual void initContextAttrs(); + virtual void initGLContextAttrs(); /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. diff --git a/tests/lua-empty-test/project/proj.ios/AppController.mm b/tests/lua-empty-test/project/proj.ios/AppController.mm index a4a76d5cc1..7cfd72062b 100644 --- a/tests/lua-empty-test/project/proj.ios/AppController.mm +++ b/tests/lua-empty-test/project/proj.ios/AppController.mm @@ -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; } diff --git a/tests/lua-tests/project/Classes/AppDelegate.cpp b/tests/lua-tests/project/Classes/AppDelegate.cpp index 006dd687a1..a9185b6afe 100644 --- a/tests/lua-tests/project/Classes/AppDelegate.cpp +++ b/tests/lua-tests/project/Classes/AppDelegate.cpp @@ -18,11 +18,11 @@ AppDelegate::~AppDelegate() SimpleAudioEngine::end(); } -void AppDelegate::initContextAttrs() +void AppDelegate::initGLContextAttrs() { - ContextAttrs contextAttrs = {8, 8, 8, 8, 24, 8}; + GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; - GLView::setContextAttrs(contextAttrs); + GLView::setGLContextAttrs(glContextAttrs); } bool AppDelegate::applicationDidFinishLaunching() diff --git a/tests/lua-tests/project/Classes/AppDelegate.h b/tests/lua-tests/project/Classes/AppDelegate.h index 90979dea7a..f8fbbdbb39 100644 --- a/tests/lua-tests/project/Classes/AppDelegate.h +++ b/tests/lua-tests/project/Classes/AppDelegate.h @@ -14,7 +14,7 @@ public: AppDelegate(); virtual ~AppDelegate(); - void initContextAttrs(); + void initGLContextAttrs(); /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. diff --git a/tests/lua-tests/project/proj.ios_mac/ios/AppController.mm b/tests/lua-tests/project/proj.ios_mac/ios/AppController.mm index 3305eb9259..12f7bd601f 100644 --- a/tests/lua-tests/project/proj.ios_mac/ios/AppController.mm +++ b/tests/lua-tests/project/proj.ios_mac/ios/AppController.mm @@ -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; }