依然是贺岁篇!!亲,不要不耐烦哈!在上节中和大家糊里糊涂地分析下了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。