中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

龙川县| 黎平县| 泗水县| 孝义市| 阿鲁科尔沁旗| 类乌齐县| 治多县| 南漳县| 凉山| 周口市| 江都市| 托克逊县| 神农架林区| 盐城市| 东莞市| 太保市| 石城县| 炉霍县| 阿勒泰市| 阳朔县| 巨鹿县| 柳林县| 黄冈市| 资溪县| 贵溪市| 渑池县| 修水县| 广丰县| 抚远县| 平遥县| 沁源县| 谢通门县| 太白县| 宜良县| 金山区| 望都县| 梧州市| 吕梁市| 黔江区| 辽阳市| 常宁市|