温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

cocos2d-x从零入门03---依然是贺岁篇

发布时间:2020-06-13 21:31:41 来源:网络 阅读:548 作者:将才 栏目:游戏开发

  依然是贺岁篇!!亲,不要不耐烦哈!在上节中和大家糊里糊涂地分析下了HelloWorld运行的逻辑,细心的朋友肯定会发现,我提到过显示窗口、消息循环,发现问题所在了吧,就是为啥没有注册窗口类,创建窗口和窗口处理函数??

 我想你肯定再说我一点也不专业,实不相瞒,确实是很不专业,来来,继续分析,我们回到  CCApplication::run():

  1. int CCApplication::run() 
  2.     PVRFrameEnableControlWindow(false); 
  3.  
  4.     // Main message loop: 
  5.     MSG msg; 
  6.     LARGE_INTEGER nFreq; 
  7.     LARGE_INTEGER nLast; 
  8.     LARGE_INTEGER nNow; 
  9.  
  10.     QueryPerformanceFrequency(&nFreq); 
  11.     QueryPerformanceCounter(&nLast); 
  12.  
  13.     // Initialize instance and cocos2d. 
  14.     //看到了吧,这里初始化实例,再找到其定义 
  15.     if (!applicationDidFinishLaunching()) 
  16.     { 
  17.         return 0; 
  18.     } 
  19.      
  20.     //关键在这里啊,亲,进来看看呗 
  21.     CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView(); 
  22.     pMainWnd->centerWindow(); 
  23.  
  24.     //显示窗口 
  25.     ShowWindow(pMainWnd->getHWnd(), SW_SHOW); 
  26.  
  27.     //windows程序的消息循环,哈哈,找到你了 
  28.     while (1) 
  29.     { 
  30.         if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
  31.         { 
  32.             // Get current time tick. 
  33.             QueryPerformanceCounter(&nNow); 
  34.  
  35.             // If it's the time to draw next frame, draw it, else sleep a while. 
  36.             if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart) 
  37.             { 
  38.                 nLast.QuadPart = nNow.QuadPart; 
  39.                 CCDirector::sharedDirector()->mainLoop(); 
  40.             } 
  41.             else 
  42.             { 
  43.                 Sleep(0); 
  44.             } 
  45.             continue
  46.         } 
  47.  
  48.         if (WM_QUIT == msg.message) 
  49.         { 
  50.             // Quit message loop. 
  51.             break
  52.         } 
  53.  
  54.         // Deal with windows message. 
  55.         if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg)) 
  56.         { 
  57.             TranslateMessage(&msg); 
  58.             DispatchMessage(&msg); 
  59.         } 
  60.     } 
  61.  
  62.     return (int) msg.wParam; 

  63.  

请看22行代码,老规矩,进去看看,不进虎穴,焉得虎子?

跳转到sharedOpenGLView的定义:

  1. CCEGLView* CCEGLView::sharedOpenGLView() 
  2.     //如果空就new一个 
  3.     static CCEGLView* s_pEglView = NULL; 
  4.     if (s_pEglView == NULL) 
  5.     { 
  6.         //新建 
  7.         s_pEglView = new CCEGLView(); 
  8.     } 
  9.     return s_pEglView; 

仔细看看此时的文件,你会发现你想要的都在这里,我截取部分吧:

  1. //注册窗口类并创建窗口 
  2. bool CCEGLView::Create(LPCTSTR pTitle, int w, int h) 
  3.     bool bRet = false
  4.     do  
  5.     { 
  6.         CC_BREAK_IF(m_hWnd); 
  7.  
  8.         HINSTANCE hInstance = GetModuleHandle( NULL ); 
  9.         WNDCLASS  wc;        // Windows Class Structure 
  10.  
  11.         // Redraw On Size, And Own DC For Window. 
  12.         wc.style          = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;   
  13.         wc.lpfnWndProc    = _WindowProc;                    // WndProc Handles Messages 
  14.         wc.cbClsExtra     = 0;                              // No Extra Window Data 
  15.         wc.cbWndExtra     = 0;                                // No Extra Window Data 
  16.         wc.hInstance      = hInstance;                        // Set The Instance 
  17.         wc.hIcon          = LoadIcon( NULL, IDI_WINLOGO );    // Load The Default Icon 
  18.         wc.hCursor        = LoadCursor( NULL, IDC_ARROW );    // Load The Arrow Pointer 
  19.         wc.hbrBackground  = NULL;                           // No Background Required For GL 
  20.         wc.lpszMenuName   = m_menu;                         //  
  21.         wc.lpszClassName  = kWindowClassName;               // Set The Class Name 
  22.  
  23.         CC_BREAK_IF(! RegisterClass(&wc) && 1410 != GetLastError());         
  24.  
  25.         // center window position 
  26.         RECT rcDesktop; 
  27.         GetWindowRect(GetDesktopWindow(), &rcDesktop); 
  28.  
  29.         WCHAR wszBuf[50] = {0}; 
  30.         MultiByteToWideChar(CP_UTF8, 0, m_szViewName, -1, wszBuf, sizeof(wszBuf)); 
  31.  
  32.         // create window 
  33.         m_hWnd = CreateWindowEx( 
  34.             WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,    // Extended Style For The Window 
  35.             kWindowClassName,                                    // Class Name 
  36.             wszBuf,                                                // Window Title 
  37.             WS_CAPTION | WS_POPUPWINDOW | WS_MINIMIZEBOX,        // Defined Window Style 
  38.             0, 0,                                                // Window Position 
  39.             0,                                                  // Window Width 
  40.             0,                                                  // Window Height 
  41.             NULL,                                                // No Parent Window 
  42.             NULL,                                                // No Menu 
  43.             hInstance,                                            // Instance 
  44.             NULL ); 
  45.  
  46.         CC_BREAK_IF(! m_hWnd); 
  47.  
  48.         resize(w, h); 
  49.  
  50.         bRet = initGL(); 
  51.         CC_BREAK_IF(!bRet); 
  52.          
  53.         s_pMainWindow = this
  54.         bRet = true
  55.     } while (0); 
  56.  
  57.     return bRet; 

  1. //窗口处理函数 
  2. LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  3.     BOOL bProcessed = FALSE; 
  4.  
  5.     switch (message) 
  6.     { 
  7.     case WM_LBUTTONDOWN: 
  8.         if (m_pDelegate && MK_LBUTTON == wParam) 
  9.         { 
  10.             POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)}; 
  11.             CCPoint pt(point.x/CC_CONTENT_SCALE_FACTOR(), point.y/CC_CONTENT_SCALE_FACTOR()); 
  12.             CCPoint tmp = ccp(pt.x, m_obScreenSize.height - pt.y); 
  13.             if (m_obViewPortRect.equals(CCRectZero) || m_obViewPortRect.containsPoint(tmp)) 
  14.             { 
  15.                 m_bCaptured = true
  16.                 SetCapture(m_hWnd); 
  17.                 int id = 0; 
  18.                 pt.x *= m_windowTouchScaleX; 
  19.                 pt.y *= m_windowTouchScaleY; 
  20.                 handleTouchesBegin(1, &id, &pt.x, &pt.y); 
  21.             } 
  22.         } 
  23.         break
  24.  
  25.     case WM_MOUSEMOVE: 
  26.         if (MK_LBUTTON == wParam && m_bCaptured) 
  27.         { 
  28.             POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)}; 
  29.             CCPoint pt(point.x/CC_CONTENT_SCALE_FACTOR(), point.y/CC_CONTENT_SCALE_FACTOR()); 
  30.             int id = 0; 
  31.             pt.x *= m_windowTouchScaleX; 
  32.             pt.y *= m_windowTouchScaleY; 
  33.             handleTouchesMove(1, &id, &pt.x, &pt.y); 
  34.         } 
  35.         break
  36.  
  37.     case WM_LBUTTONUP: 
  38.         if (m_bCaptured) 
  39.         { 
  40.             POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)}; 
  41.             CCPoint pt(point.x/CC_CONTENT_SCALE_FACTOR(), point.y/CC_CONTENT_SCALE_FACTOR()); 
  42.             int id = 0; 
  43.             pt.x *= m_windowTouchScaleX; 
  44.             pt.y *= m_windowTouchScaleY; 
  45.             handleTouchesEnd(1, &id, &pt.x, &pt.y); 
  46.  
  47.             ReleaseCapture(); 
  48.             m_bCaptured = false
  49.         } 
  50.         break
  51.     case WM_SIZE: 
  52.         switch (wParam) 
  53.         { 
  54.         case SIZE_RESTORED: 
  55.             CCApplication::sharedApplication()->applicationWillEnterForeground(); 
  56.             break
  57.         case SIZE_MINIMIZED: 
  58.             CCApplication::sharedApplication()->applicationDidEnterBackground(); 
  59.             break
  60.         } 
  61.         break
  62.     case WM_KEYDOWN: 
  63.         if (wParam == VK_F1 || wParam == VK_F2) 
  64.         { 
  65.             CCDirector* pDirector = CCDirector::sharedDirector(); 
  66.             if (GetKeyState(VK_LSHIFT) < 0 ||  GetKeyState(VK_RSHIFT) < 0 || GetKeyState(VK_SHIFT) < 0) 
  67.                 pDirector->getKeypadDispatcher()->dispatchKeypadMSG(wParam == VK_F1 ? kTypeBackClicked : kTypeMenuClicked); 
  68.         } 
  69.         if ( m_lpfnAccelerometerKeyHook!=NULL ) 
  70.         { 
  71.             (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam ); 
  72.         } 
  73.         break
  74.     case WM_KEYUP: 
  75.         if ( m_lpfnAccelerometerKeyHook!=NULL ) 
  76.         { 
  77.             (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam ); 
  78.         } 
  79.         break
  80.     case WM_CHAR: 
  81.         { 
  82.             if (wParam < 0x20) 
  83.             { 
  84.                 if (VK_BACK == wParam) 
  85.                 { 
  86.                     CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward(); 
  87.                 } 
  88.                 else if (VK_RETURN == wParam) 
  89.                 { 
  90.                     CCIMEDispatcher::sharedDispatcher()->dispatchInsertText("\n", 1); 
  91.                 } 
  92.                 else if (VK_TAB == wParam) 
  93.                 { 
  94.                     // tab input 
  95.                 } 
  96.                 else if (VK_ESCAPE == wParam) 
  97.                 { 
  98.                     // ESC input 
  99.                     //CCDirector::sharedDirector()->end(); 
  100.                 } 
  101.             } 
  102.             else if (wParam < 128) 
  103.             { 
  104.                 // ascii char 
  105.                 CCIMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&wParam, 1); 
  106.             } 
  107.             else 
  108.             { 
  109.                 char szUtf8[8] = {0}; 
  110.                 int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)&wParam, 1, szUtf8, sizeof(szUtf8), NULL, NULL); 
  111.                 CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen); 
  112.             } 
  113.             if ( m_lpfnAccelerometerKeyHook!=NULL ) 
  114.             { 
  115.                 (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam ); 
  116.             } 
  117.         } 
  118.         break
  119.     case WM_PAINT: 
  120.         PAINTSTRUCT ps; 
  121.         BeginPaint(m_hWnd, &ps); 
  122.         EndPaint(m_hWnd, &ps); 
  123.         break
  124.  
  125.     case WM_CLOSE: 
  126.         CCDirector::sharedDirector()->end(); 
  127.         break
  128.  
  129.     case WM_DESTROY: 
  130.         destroyGL(); 
  131.         PostQuitMessage(0); 
  132.         break
  133.  
  134.     default
  135.         if (m_wndproc) 
  136.         { 
  137.              
  138.             m_wndproc(message, wParam, lParam, &bProcessed); 
  139.             if (bProcessed) break
  140.         } 
  141.         return DefWindowProc(m_hWnd, message, wParam, lParam); 
  142.     } 
  143.  
  144.     if (m_wndproc && !bProcessed) 
  145.     { 
  146.         m_wndproc(message, wParam, lParam, &bProcessed); 
  147.     } 
  148.     return 0; 

到目前为此,windows程序的那三把斧头都找到了。。。。也不早了,想睡觉觉去了。

第一次写学习博文,不足之处请原谅,由于水平有限,此博文仅是为了抛砖引玉,如有错误之处,忘勘正,勘正热线QQ947491240,谢谢!!我们下节见!!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI