上一篇我们配置了运行环境,但是并不完美,MFC窗口 和 cosos2d 窗口是分开运行的。 如果用来做工具 看起来不太好看,这一篇我们将修改cocos2d 代码,让其运行在MFC控件上
参考:http://blog.csdn.net/akof1314/article/details/8133800
要把cocos2d 窗口运行在 MFC 控件上, 我们就要找到这个窗口的句柄,下面我们来一步步找,看看怎样得到这个窗口句柄
1.首先我们来分析cocos2d的运行机制
打开cocos2d::Application::getInstance()->run(); run()函数的源码:
- intApplication::run()
- {
- PVRFrameEnableControlWindow(false);
- //Mainmessageloop:
- LARGE_INTEGERnFreq;
- LARGE_INTEGERnLast;
- LARGE_INTEGERnNow;
- QueryPerformanceFrequency(&nFreq);
- QueryPerformanceCounter(&nLast);
- //这里调用了AppDelegate中的<spanstyle="font-family:Arial,sans-serif;">applicationDidFinishLaunching()
- //Initializeinstanceandcocos2d.
- if(!applicationDidFinishLaunching())
- return0;
- }
- //那么游戏窗口一定是在这之前创建的</span>
- autodirector=Director::getInstance();
- autoglview=director->getOpenGLView();
- //Retainglviewtoavoidglviewbeingreleasedinthewhileloop
- glview->retain();
- //下面是游戏主循环</span>
- while(!glview->windowShouldClose())
- {
- QueryPerformanceCounter(&nNow);
- if(nNow.QuadPart-nLast.QuadPart>_animationInterval.QuadPart)
- nLast.QuadPart=nNow.QuadPart;
- director->mainLoop();
- glview->pollEvents();
- }
- else
- Sleep(0);
- //Directorshouldstilldoacleanupifthewindowwasclosedmanually.
- if(glview->isOpenGLReady())
- director->end();
- director->mainLoop();
- director=nullptr;
- glview->release();
- returntrue;
- }