How to debug games for ipad retina on low-resolution PC¶
Desktop platforms(win32, mac, linux) in cocos2d-x are mainly for easily debugging mobile games.
Since Apple released New iPad with resolution of 2048 by 1536. It's hard for developers to debug games with New iPad resources on win32.
So CCEGLView::setFrameZoomFactor function was born to resolve this issue.
[1]The Principle¶
'CCEGLView::setFrameZoomFactor' resets the size of game window and the projection by multiply a zoom factor.
On win32:
1
2void CCEGLView::setFrameZoomFactor(float fZoomFactor)
3{
4 m_fFrameZoomFactor = fZoomFactor;
5 resize(m_obScreenSize.width * fZoomFactor, m_obScreenSize.height * fZoomFactor);
6 centerWindow();
7 CCDirector::sharedDirector()->setProjection(CCDirector::sharedDirector()->getProjection());
8}
CCEGLView on win32, mac and linux also overrides two functions, 'setViewPortInPoints' and 'setScissorInPoints'.
When the projection is reset, CCEGLView::setViewPortInPoints will be invoked.
The implementation of CCEGLView::setViewPortInPoints is different from CCEGLViewProtocol::setViewPortInPoints.
All the parameters are multiply with m_fFrameZoomFactor.
1void CCEGLView::setViewPortInPoints(float x , float y , float w , float h)
2{
3 glViewport((GLint)(x * m_fScaleX * m_fFrameZoomFactor + m_obViewPortRect.origin.x * m_fFrameZoomFactor),
4 (GLint)(y * m_fScaleY * m_fFrameZoomFactor + m_obViewPortRect.origin.y * m_fFrameZoomFactor),
5 (GLsizei)(w * m_fScaleX * m_fFrameZoomFactor),
6 (GLsizei)(h * m_fScaleY * m_fFrameZoomFactor));
7}
8
9void CCEGLView::setScissorInPoints(float x , float y , float w , float h)
10{
11 glScissor((GLint)(x * m_fScaleX * m_fFrameZoomFactor + m_obViewPortRect.origin.x * m_fFrameZoomFactor),
12 (GLint)(y * m_fScaleY * m_fFrameZoomFactor + m_obViewPortRect.origin.y * m_fFrameZoomFactor),
13 (GLsizei)(w * m_fScaleX * m_fFrameZoomFactor),
14 (GLsizei)(h * m_fScaleY * m_fFrameZoomFactor));
15}
Also, while cocos2d-x get the touch events from system, we divide x and y of postion by m_fFrameZoomFactor.
1LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
2{
3 BOOL bProcessed = FALSE;
4
5 switch (message)
6 {
7 case WM_LBUTTONDOWN:
8.....
9 if (m_pDelegate && MK_LBUTTON == wParam)
10 {
11 POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
12 CCPoint pt(point.x, point.y);
13 pt.x /= m_fFrameZoomFactor; // added this line
14 pt.y /= m_fFrameZoomFactor; // added this line
15 CCPoint tmp = ccp(pt.x, m_obScreenSize.height - pt.y);
16 if (m_obViewPortRect.equals(CCRectZero) || m_obViewPortRect.containsPoint(tmp))
17 {
18 m_bCaptured = true;
19 SetCapture(m_hWnd);
20 int id = 0;
21 handleTouchesBegin(1, &id, &pt.x, &pt.y);
22 }
23 }
24 break;
25................
[2]Where to invoke CCEGLView::setFrameZoomFactor?¶
This function can only be invoked in platform relative codes. e.g. 'proj.win32/main.cpp, proj.mac/AppController.mm and proj.linux/main.cpp'.
Just adding one line code will enable this useful feature.
For example, on win32
1int APIENTRY _tWinMain(HINSTANCE hInstance,
2 HINSTANCE hPrevInstance,
3 LPTSTR lpCmdLine,
4 int nCmdShow)
5{
6 UNREFERENCED_PARAMETER(hPrevInstance);
7 UNREFERENCED_PARAMETER(lpCmdLine);
8
9 // create the application instance
10 AppDelegate app;
11 CCEGLView* eglView = CCEGLView::sharedOpenGLView();
12 eglView->setFrameSize(2048, 1536);
13 // The resolution of ipad3 is very large. In general, PC's resolution is smaller than it.
14 // So we need to invoke 'setFrameZoomFactor'(only valid on desktop(win32, mac, linux)) to make the window smaller.
15 eglView->setFrameZoomFactor(0.4f);
16 return CCApplication::sharedApplication()->run();
17}
The result:
Now, you are able to debug large resolution game on low resolution desktop. Hope you join it. 