您好,登錄后才能下訂單哦!
依然是賀歲篇!!親,不要不耐煩哈!在上節中和大家糊里糊涂地分析下了HelloWorld運行的邏輯,細心的朋友肯定會發現,我提到過顯示窗口、消息循環,發現問題所在了吧,就是為啥沒有注冊窗口類,創建窗口和窗口處理函數??
我想你肯定再說我一點也不專業,實不相瞞,確實是很不專業,來來,繼續分析,我們回到 CCApplication::run():
- int CCApplication::run()
- {
- PVRFrameEnableControlWindow(false);
- // Main message loop:
- MSG msg;
- LARGE_INTEGER nFreq;
- LARGE_INTEGER nLast;
- LARGE_INTEGER nNow;
- QueryPerformanceFrequency(&nFreq);
- QueryPerformanceCounter(&nLast);
- // Initialize instance and cocos2d.
- //看到了吧,這里初始化實例,再找到其定義
- if (!applicationDidFinishLaunching())
- {
- return 0;
- }
- //關鍵在這里啊,親,進來看看唄
- CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView();
- pMainWnd->centerWindow();
- //顯示窗口
- ShowWindow(pMainWnd->getHWnd(), SW_SHOW);
- //windows程序的消息循環,哈哈,找到你了
- while (1)
- {
- if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- // Get current time tick.
- QueryPerformanceCounter(&nNow);
- // If it's the time to draw next frame, draw it, else sleep a while.
- if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)
- {
- nLast.QuadPart = nNow.QuadPart;
- CCDirector::sharedDirector()->mainLoop();
- }
- else
- {
- Sleep(0);
- }
- continue;
- }
- if (WM_QUIT == msg.message)
- {
- // Quit message loop.
- break;
- }
- // Deal with windows message.
- if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int) msg.wParam;
- }
請看22行代碼,老規矩,進去看看,不進虎穴,焉得虎子?
跳轉到sharedOpenGLView的定義:
- CCEGLView* CCEGLView::sharedOpenGLView()
- {
- //如果空就new一個
- static CCEGLView* s_pEglView = NULL;
- if (s_pEglView == NULL)
- {
- //新建
- s_pEglView = new CCEGLView();
- }
- return s_pEglView;
- }
仔細看看此時的文件,你會發現你想要的都在這里,我截取部分吧:
- //注冊窗口類并創建窗口
- bool CCEGLView::Create(LPCTSTR pTitle, int w, int h)
- {
- bool bRet = false;
- do
- {
- CC_BREAK_IF(m_hWnd);
- HINSTANCE hInstance = GetModuleHandle( NULL );
- WNDCLASS wc; // Windows Class Structure
- // Redraw On Size, And Own DC For Window.
- wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
- wc.lpfnWndProc = _WindowProc; // WndProc Handles Messages
- wc.cbClsExtra = 0; // No Extra Window Data
- wc.cbWndExtra = 0; // No Extra Window Data
- wc.hInstance = hInstance; // Set The Instance
- wc.hIcon = LoadIcon( NULL, IDI_WINLOGO ); // Load The Default Icon
- wc.hCursor = LoadCursor( NULL, IDC_ARROW ); // Load The Arrow Pointer
- wc.hbrBackground = NULL; // No Background Required For GL
- wc.lpszMenuName = m_menu; //
- wc.lpszClassName = kWindowClassName; // Set The Class Name
- CC_BREAK_IF(! RegisterClass(&wc) && 1410 != GetLastError());
- // center window position
- RECT rcDesktop;
- GetWindowRect(GetDesktopWindow(), &rcDesktop);
- WCHAR wszBuf[50] = {0};
- MultiByteToWideChar(CP_UTF8, 0, m_szViewName, -1, wszBuf, sizeof(wszBuf));
- // create window
- m_hWnd = CreateWindowEx(
- WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, // Extended Style For The Window
- kWindowClassName, // Class Name
- wszBuf, // Window Title
- WS_CAPTION | WS_POPUPWINDOW | WS_MINIMIZEBOX, // Defined Window Style
- 0, 0, // Window Position
- 0, // Window Width
- 0, // Window Height
- NULL, // No Parent Window
- NULL, // No Menu
- hInstance, // Instance
- NULL );
- CC_BREAK_IF(! m_hWnd);
- resize(w, h);
- bRet = initGL();
- CC_BREAK_IF(!bRet);
- s_pMainWindow = this;
- bRet = true;
- } while (0);
- return bRet;
- }
- //窗口處理函數
- LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- BOOL bProcessed = FALSE;
- switch (message)
- {
- case WM_LBUTTONDOWN:
- if (m_pDelegate && MK_LBUTTON == wParam)
- {
- POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
- CCPoint pt(point.x/CC_CONTENT_SCALE_FACTOR(), point.y/CC_CONTENT_SCALE_FACTOR());
- CCPoint tmp = ccp(pt.x, m_obScreenSize.height - pt.y);
- if (m_obViewPortRect.equals(CCRectZero) || m_obViewPortRect.containsPoint(tmp))
- {
- m_bCaptured = true;
- SetCapture(m_hWnd);
- int id = 0;
- pt.x *= m_windowTouchScaleX;
- pt.y *= m_windowTouchScaleY;
- handleTouchesBegin(1, &id, &pt.x, &pt.y);
- }
- }
- break;
- case WM_MOUSEMOVE:
- if (MK_LBUTTON == wParam && m_bCaptured)
- {
- POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
- CCPoint pt(point.x/CC_CONTENT_SCALE_FACTOR(), point.y/CC_CONTENT_SCALE_FACTOR());
- int id = 0;
- pt.x *= m_windowTouchScaleX;
- pt.y *= m_windowTouchScaleY;
- handleTouchesMove(1, &id, &pt.x, &pt.y);
- }
- break;
- case WM_LBUTTONUP:
- if (m_bCaptured)
- {
- POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
- CCPoint pt(point.x/CC_CONTENT_SCALE_FACTOR(), point.y/CC_CONTENT_SCALE_FACTOR());
- int id = 0;
- pt.x *= m_windowTouchScaleX;
- pt.y *= m_windowTouchScaleY;
- handleTouchesEnd(1, &id, &pt.x, &pt.y);
- ReleaseCapture();
- m_bCaptured = false;
- }
- break;
- case WM_SIZE:
- switch (wParam)
- {
- case SIZE_RESTORED:
- CCApplication::sharedApplication()->applicationWillEnterForeground();
- break;
- case SIZE_MINIMIZED:
- CCApplication::sharedApplication()->applicationDidEnterBackground();
- break;
- }
- break;
- case WM_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;
- case WM_KEYUP:
- if ( m_lpfnAccelerometerKeyHook!=NULL )
- {
- (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );
- }
- break;
- case WM_CHAR:
- {
- if (wParam < 0x20)
- {
- if (VK_BACK == wParam)
- {
- CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
- }
- else if (VK_RETURN == wParam)
- {
- CCIMEDispatcher::sharedDispatcher()->dispatchInsertText("\n", 1);
- }
- else if (VK_TAB == wParam)
- {
- // tab input
- }
- else if (VK_ESCAPE == wParam)
- {
- // ESC input
- //CCDirector::sharedDirector()->end();
- }
- }
- else if (wParam < 128)
- {
- // ascii char
- CCIMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&wParam, 1);
- }
- else
- {
- char szUtf8[8] = {0};
- int nLen = 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;
- case WM_PAINT:
- PAINTSTRUCT ps;
- BeginPaint(m_hWnd, &ps);
- EndPaint(m_hWnd, &ps);
- break;
- case WM_CLOSE:
- CCDirector::sharedDirector()->end();
- break;
- case WM_DESTROY:
- destroyGL();
- PostQuitMessage(0);
- break;
- default:
- if (m_wndproc)
- {
- m_wndproc(message, wParam, lParam, &bProcessed);
- if (bProcessed) break;
- }
- return DefWindowProc(m_hWnd, message, wParam, lParam);
- }
- if (m_wndproc && !bProcessed)
- {
- m_wndproc(message, wParam, lParam, &bProcessed);
- }
- return 0;
- }
到目前為此,windows程序的那三把斧頭都找到了。。。。也不早了,想睡覺覺去了。
第一次寫學習博文,不足之處請原諒,由于水平有限,此博文僅是為了拋磚引玉,如有錯誤之處,忘勘正,勘正熱線QQ947491240,謝謝!!我們下節見!!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。