cocos-2dx v3.5,windows的opengl初始化:
0. main.cpp中会直接调用Application::getInstance()->run(),
int Application::run() { PVRFrameEnableControlWindow(false); // Main message loop: LARGE_INTEGER nLast; LARGE_INTEGER nNow; QueryPerformanceCounter(&nLast); <span style="color:#009900;"> //1-opengl像素格式等参数初始化</span> <strong>initGLContextAttrs();</strong> <span style="color:#009900;"> //2-cocos初始化</span> if (!applicationDidFinishLaunching()) { return 0; } auto director = Director::getInstance(); auto glview = director->getOpenGLView(); // Retain glview to avoid glview being released in the while loop glview->retain(); while(!glview->windowShouldClose()) { QueryPerformanceCounter(&nNow); if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart) { nLast.QuadPart = nNow.QuadPart - (nNow.QuadPart % _animationInterval.QuadPart); director->mainLoop(); glview->pollEvents(); } else { Sleep(1); } } // Director should still do a cleanup if the window was closed manually. if (glview->isOpenGLReady()) { director->end(); director->mainLoop(); director = nullptr; } glview->release(); return true; }
1.下面初始化了opgl所使用的rgba所要使用的位数以及depthBit,stencilBits
void AppDelegate::initGLContextAttrs() { GLContextAttrs glContextAttrs = {8,8,24,8}; GLView::setGLContextAttrs(glContextAttrs); }
2.cocos程序初始化入口:
bool AppDelegate::applicationDidFinishLaunching()
auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { <strong> glview = GLViewImpl::create("Cpp Empty Test");</strong> director->setOpenGLView(glview); }
3.create opgl窗口
GLViewImpl* GLViewImpl::create(const std::string& viewName) { auto ret = new (std::nothrow) GLViewImpl; if(ret &&<strong> ret->initWithRect(viewName,Rect(0,960,640),1)</strong>) { ret->autorelease(); return ret; } return nullptr; }4.默认使用640,960的矩形创建窗口,下面会对窗口初始化参数值,像素格式是OpenGL窗口的重要属性,它包括是否使用双缓冲,颜色位数和类型以及深度位数等。
bool GLViewImpl::initWithRect(const std::string& viewName,Rect rect,float frameZoomFactor) { setViewName(viewName); _frameZoomFactor = frameZoomFactor;
<span style="color:#009900;"> //windows像素格式</span>
<strong> lfwWindowHint(GLFW_RESIZABLE,GL_FALSE); //不可改变窗口大小 glfwWindowHint(GLFW_RED_BITS,_glContextAttrs.redBits); //1中初始化的参数传入 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);</strong>
<span style="color:#009900;">//windows窗口创建</span>
_mainWindow = <strong>glfwCreateWindow</strong>(rect.size.width * _frameZoomFactor,rect.size.height * _frameZoomFactor,_viewName.c_str(),_monitor,nullptr); glfwMakeContextCurrent(_mainWindow);<span style="color:#009900;">//make当前windows线程的上下文context</span> glfwSetMouseButtonCallback(_mainWindow,GLFWEventHandler::onGLFWMouseCallBack); glfwSetCursorPosCallback(_mainWindow,GLFWEventHandler::onGLFWMouseMoveCallBack); glfwSetScrollCallback(_mainWindow,GLFWEventHandler::onGLFWMouseScrollCallback); glfwSetCharCallback(_mainWindow,GLFWEventHandler::onGLFWCharCallback); glfwSetKeyCallback(_mainWindow,GLFWEventHandler::onGLFWKeyCallback); glfwSetWindowPosCallback(_mainWindow,GLFWEventHandler::onGLFWWindowPosCallback); glfwSetFramebufferSizeCallback(_mainWindow,GLFWEventHandler::onGLFWframebuffersize); glfwSetWindowSizeCallback(_mainWindow,GLFWEventHandler::onGLFWWindowSizeFunCallback); glfwSetWindowIconifyCallback(_mainWindow,GLFWEventHandler::onGLFWWindowIconifyCallback); setFrameSize(rect.size.width,rect.size.height); <span style="color:#009900;">// OPENGL版本检查</span> const GLubyte* glVersion = glGetString(GL_VERSION); if ( utils::atof((const char*)glVersion) < 1.5 ) { char strComplain[256] = {0}; sprintf(strComplain,"OpenGL 1.5 or higher is required (your version is %s). Please upgrade the driver of your video card.",glVersion); MessageBox(strComplain,"OpenGL version too old"); return false; }
<span style="color:#009900;">//初始化GLEW,GLEW(OpenGL Extension Wrangler Library)是一个跨平台的C/C++库,用来查询和加载OpenGL扩展</span> initGlew(); // Enable point size by default. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); return true; }
5.判断GLEW是否初始化成功
bool GLViewImpl::initGlew() { #if (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) GLenum GlewInitResult = glewInit();<span style="color:#009900;">//初始化结果</span> if (GLEW_OK != GlewInitResult) { MessageBox((char *)glewGetErrorString(GlewInitResult),"OpenGL error"); return false; } if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) { log("Ready for GLSL"); } else { log("Not totally ready :("); } if (glewIsSupported("GL_VERSION_2_0")) { log("Ready for OpenGL 2.0"); } else { log("OpenGL 2.0 not supported"); } #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
if(glew_dynamic_binding() == false)<span style="color:#009900;">//启用帧缓冲对象</span> { MessageBox("No OpenGL framebuffer support. Please upgrade the driver of your video card.","OpenGL error"); return false; } #endif #endif // (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) return true; }
通过上面的过程,cocos-2dx的opengl初始化过程就搞定了。通过initGLContextAttrs 设置opengl像素格式,
在applicationDidFinishLaunching中create opengl窗口,创建过程中,将先前设定的像素格式传入,然后根据参数创建默认大小为(960,640)的窗口。
不同平台上面,OpenGL的初始化流程不完全一样。详细的区别可以查看平台相关的CCGLViewImpl-平台(如CCGLViewImpl-android)类,
cocos2d-x-3.5\cocos\platform\下
接下来就是游戏过程了(见下一章) 正在学习cocos-2dx 关于opengl渲染相关的东西,这里就抛砖引玉了。