cocos2d-x从零入门03---依然是贺岁篇
依然是贺岁篇!!亲,不要不耐烦哈!在上节中和大家糊里糊涂地分析下了HelloWorld运行的逻辑,细心的朋友肯定会发现,我提到过显示窗口、消息循环,发现问题所在了吧,就是为啥没有注册窗口类,创建窗口和窗口处理函数??
我想你肯定再说我一点也不专业,实不相瞒,确实是很不专业,来来,继续分析,我们回到 CCApplication::run():
intCCApplication::run(){PVRFrameEnableControlWindow(false);//Mainmessageloop:MSGmsg;LARGE_INTEGERnFreq;LARGE_INTEGERnLast;LARGE_INTEGERnNow;QueryPerformanceFrequency(&nFreq);QueryPerformanceCounter(&nLast);//Initializeinstanceandcocos2d.//看到了吧,这里初始化实例,再找到其定义if(!applicationDidFinishLaunching()){return0;}//关键在这里啊,亲,进来看看呗CCEGLView*pMainWnd=CCEGLView::sharedOpenGLView();pMainWnd->centerWindow();//显示窗口ShowWindow(pMainWnd->getHWnd(),SW_SHOW);//windows程序的消息循环,哈哈,找到你了while(1){if(!PeekMessage(&msg,NULL,0,0,PM_REMOVE)){//Getcurrenttimetick.QueryPerformanceCounter(&nNow);//Ifit'sthetimetodrawnextframe,drawit,elsesleepawhile.if(nNow.QuadPart-nLast.QuadPart>m_nAnimationInterval.QuadPart){nLast.QuadPart=nNow.QuadPart;CCDirector::sharedDirector()->mainLoop();}else{Sleep(0);}continue;}if(WM_QUIT==msg.message){//Quitmessageloop.break;}//Dealwithwindowsmessage.if(!m_hAccelTable||!TranslateAccelerator(msg.hwnd,m_hAccelTable,&msg)){TranslateMessage(&msg);DispatchMessage(&msg);}}return(int)msg.wParam;}
请看22行代码,老规矩,进去看看,不进虎穴,焉得虎子?
跳转到sharedOpenGLView的定义:
CCEGLView*CCEGLView::sharedOpenGLView(){//如果空就new一个staticCCEGLView*s_pEglView=NULL;if(s_pEglView==NULL){//新建s_pEglView=newCCEGLView();}returns_pEglView;}
仔细看看此时的文件,你会发现你想要的都在这里,我截取部分吧:
//注册窗口类并创建窗口boolCCEGLView::Create(LPCTSTRpTitle,intw,inth){boolbRet=false;do{CC_BREAK_IF(m_hWnd);HINSTANCEhInstance=GetModuleHandle(NULL);WNDCLASSwc;//WindowsClassStructure//RedrawOnSize,AndOwnDCForWindow.wc.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC;wc.lpfnWndProc=_WindowProc;//WndProcHandlesMessageswc.cbClsExtra=0;//NoExtraWindowDatawc.cbWndExtra=0;//NoExtraWindowDatawc.hInstance=hInstance;//SetTheInstancewc.hIcon=LoadIcon(NULL,IDI_WINLOGO);//LoadTheDefaultIconwc.hCursor=LoadCursor(NULL,IDC_ARROW);//LoadTheArrowPointerwc.hbrBackground=NULL;//NoBackgroundRequiredForGLwc.lpszMenuName=m_menu;//wc.lpszClassName=kWindowClassName;//SetTheClassNameCC_BREAK_IF(!RegisterClass(&wc)&&1410!=GetLastError());//centerwindowpositionRECTrcDesktop;GetWindowRect(GetDesktopWindow(),&rcDesktop);WCHARwszBuf[50]={0};MultiByteToWideChar(CP_UTF8,0,m_szViewName,-1,wszBuf,sizeof(wszBuf));//createwindowm_hWnd=CreateWindowEx(WS_EX_APPWINDOW|WS_EX_WINDOWEDGE,//ExtendedStyleForTheWindowkWindowClassName,//ClassNamewszBuf,//WindowTitleWS_CAPTION|WS_POPUPWINDOW|WS_MINIMIZEBOX,//DefinedWindowStyle0,0,//WindowPosition0,//WindowWidth0,//WindowHeightNULL,//NoParentWindowNULL,//NoMenuhInstance,//InstanceNULL);CC_BREAK_IF(!m_hWnd);resize(w,h);bRet=initGL();CC_BREAK_IF(!bRet);s_pMainWindow=this;bRet=true;}while(0);returnbRet;}
//窗口处理函数LRESULTCCEGLView::WindowProc(UINTmessage,WPARAMwParam,LPARAMlParam){BOOLbProcessed=FALSE;switch(message){caseWM_LBUTTONDOWN:if(m_pDelegate&&MK_LBUTTON==wParam){POINTpoint={(short)LOWORD(lParam),(short)HIWORD(lParam)};CCPointpt(point.x/CC_CONTENT_SCALE_FACTOR(),point.y/CC_CONTENT_SCALE_FACTOR());CCPointtmp=ccp(pt.x,m_obScreenSize.height-pt.y);if(m_obViewPortRect.equals(CCRectZero)||m_obViewPortRect.containsPoint(tmp)){m_bCaptured=true;SetCapture(m_hWnd);intid=0;pt.x*=m_windowTouchScaleX;pt.y*=m_windowTouchScaleY;handleTouchesBegin(1,&id,&pt.x,&pt.y);}}break;caseWM_MOUSEMOVE:if(MK_LBUTTON==wParam&&m_bCaptured){POINTpoint={(short)LOWORD(lParam),(short)HIWORD(lParam)};CCPointpt(point.x/CC_CONTENT_SCALE_FACTOR(),point.y/CC_CONTENT_SCALE_FACTOR());intid=0;pt.x*=m_windowTouchScaleX;pt.y*=m_windowTouchScaleY;handleTouchesMove(1,&id,&pt.x,&pt.y);}break;caseWM_LBUTTONUP:if(m_bCaptured){POINTpoint={(short)LOWORD(lParam),(short)HIWORD(lParam)};CCPointpt(point.x/CC_CONTENT_SCALE_FACTOR(),point.y/CC_CONTENT_SCALE_FACTOR());intid=0;pt.x*=m_windowTouchScaleX;pt.y*=m_windowTouchScaleY;handleTouchesEnd(1,&id,&pt.x,&pt.y);ReleaseCapture();m_bCaptured=false;}break;caseWM_SIZE:switch(wParam){caseSIZE_RESTORED:CCApplication::sharedApplication()->applicationWillEnterForeground();break;caseSIZE_MINIMIZED:CCApplication::sharedApplication()->applicationDidEnterBackground();break;}break;caseWM_KEYDOWN:if(wParam==VK_F1||wParam==VK_F2){CCDirector*pDirector=CCDirector::sharedDirector();if(GetKeyState(VK_LSHIFT)<0||GetKeyState(VK_RSHIFT)<0||GetKeyState(VK_SHIFT)<0)pDirector->getKeypadDispatcher()->dispatchKeypadMSG(wParam==VK_F1?kTypeBackClicked:kTypeMenuClicked);}if(m_lpfnAccelerometerKeyHook!=NULL){(*m_lpfnAccelerometerKeyHook)(message,wParam,lParam);}break;caseWM_KEYUP:if(m_lpfnAccelerometerKeyHook!=NULL){(*m_lpfnAccelerometerKeyHook)(message,wParam,lParam);}break;caseWM_CHAR:{if(wParam<0x20){if(VK_BACK==wParam){CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();}elseif(VK_RETURN==wParam){CCIMEDispatcher::sharedDispatcher()->dispatchInsertText("\n",1);}elseif(VK_TAB==wParam){//tabinput}elseif(VK_ESCAPE==wParam){//ESCinput//CCDirector::sharedDirector()->end();}}elseif(wParam<128){//asciicharCCIMEDispatcher::sharedDispatcher()->dispatchInsertText((constchar*)&wParam,1);}else{charszUtf8[8]={0};intnLen=WideCharToMultiByte(CP_UTF8,0,(LPCWSTR)&wParam,1,szUtf8,sizeof(szUtf8),NULL,NULL);CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8,nLen);}if(m_lpfnAccelerometerKeyHook!=NULL){(*m_lpfnAccelerometerKeyHook)(message,wParam,lParam);}}break;caseWM_PAINT:PAINTSTRUCTps;BeginPaint(m_hWnd,&ps);EndPaint(m_hWnd,&ps);break;caseWM_CLOSE:CCDirector::sharedDirector()->end();break;caseWM_DESTROY:destroyGL();PostQuitMessage(0);break;default:if(m_wndproc){m_wndproc(message,wParam,lParam,&bProcessed);if(bProcessed)break;}returnDefWindowProc(m_hWnd,message,wParam,lParam);}if(m_wndproc&&!bProcessed){m_wndproc(message,wParam,lParam,&bProcessed);}return0;}
到目前为此,windows程序的那三把斧头都找到了。。。。也不早了,想睡觉觉去了。
第一次写学习博文,不足之处请原谅,由于水平有限,此博文仅是为了抛砖引玉,如有错误之处,忘勘正,勘正热线QQ947491240,谢谢!!我们下节见!!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。