Advertisement
Guest User

CCEGLView.cpp

a guest
May 30th, 2013
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 28.26 KB | None | 0 0
  1. /****************************************************************************
  2. Copyright (c) 2010 cocos2d-x.org
  3.  
  4. http://www.cocos2d-x.org
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24.  
  25. #include "CCEGLView.h"
  26. #include "cocoa/CCSet.h"
  27. #include "ccMacros.h"
  28. #include "CCDirector.h"
  29. #include "touch_dispatcher/CCTouch.h"
  30. #include "touch_dispatcher/CCTouchDispatcher.h"
  31. #include "text_input_node/CCIMEDispatcher.h"
  32. #include "keypad_dispatcher/CCKeypadDispatcher.h"
  33. #include "support/CCPointExtension.h"
  34. #include "CCApplication.h"
  35.  
  36. NS_CC_BEGIN
  37.  
  38. #if(_MSC_VER >= 1600) // Visual Studio 2010 or higher version.
  39. // Windows Touch define
  40. #define MOUSEEVENTF_FROMTOUCH 0xFF515700
  41.  
  42. // Windows Touch functions
  43. // Workaround to be able tu run app on Windows XP
  44. typedef WINUSERAPI BOOL (WINAPI *RegisterTouchWindowFn)(_In_ HWND hwnd, _In_ ULONG ulFlags);
  45. typedef WINUSERAPI BOOL (WINAPI *UnregisterTouchWindowFn)(_In_ HWND hwnd);
  46. typedef WINUSERAPI LPARAM (WINAPI *GetMessageExtraInfoFn)(VOID);
  47. typedef WINUSERAPI BOOL (WINAPI *GetTouchInputInfoFn)(_In_ HTOUCHINPUT hTouchInput, _In_ UINT cInputs, __out_ecount(cInputs) PTOUCHINPUT pInputs, _In_ int cbSize);
  48. typedef WINUSERAPI BOOL (WINAPI *CloseTouchInputHandleFn)(_In_ HTOUCHINPUT hTouchInput);
  49.  
  50. static RegisterTouchWindowFn s_pfRegisterTouchWindowFunction = NULL;
  51. static UnregisterTouchWindowFn s_pfUnregisterTouchWindowFunction = NULL;
  52. static GetMessageExtraInfoFn s_pfGetMessageExtraInfoFunction = NULL;
  53. static GetTouchInputInfoFn s_pfGetTouchInputInfoFunction = NULL;
  54. static CloseTouchInputHandleFn s_pfCloseTouchInputHandleFunction = NULL;
  55.  
  56. static bool CheckTouchSupport()
  57. {
  58.     s_pfRegisterTouchWindowFunction = (RegisterTouchWindowFn)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), "RegisterTouchWindow");
  59.     s_pfUnregisterTouchWindowFunction = (UnregisterTouchWindowFn)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), "UnregisterTouchWindow");
  60.     s_pfGetMessageExtraInfoFunction = (GetMessageExtraInfoFn)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), "GetMessageExtraInfo");
  61.     s_pfGetTouchInputInfoFunction = (GetTouchInputInfoFn)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), "GetTouchInputInfo");
  62.     s_pfCloseTouchInputHandleFunction = (CloseTouchInputHandleFn)GetProcAddress(GetModuleHandle(TEXT("user32.dll")), "CloseTouchInputHandle");
  63.  
  64.     return (s_pfRegisterTouchWindowFunction && s_pfUnregisterTouchWindowFunction && s_pfGetMessageExtraInfoFunction && s_pfGetTouchInputInfoFunction && s_pfCloseTouchInputHandleFunction);
  65. }
  66.  
  67. #endif /* #if(_MSC_VER >= 1600) */
  68.  
  69. static void SetupPixelFormat(HDC hDC)
  70. {
  71.     int pixelFormat;
  72.  
  73.     PIXELFORMATDESCRIPTOR pfd =
  74.     {
  75.         sizeof(PIXELFORMATDESCRIPTOR),  // size
  76.         1,                          // version
  77.         PFD_SUPPORT_OPENGL |        // OpenGL window
  78.         PFD_DRAW_TO_WINDOW |        // render to window
  79.         PFD_DOUBLEBUFFER,           // support double-buffering
  80.         PFD_TYPE_RGBA,              // color type
  81.         32,                         // preferred color depth
  82.         0, 0, 0, 0, 0, 0,           // color bits (ignored)
  83.         0,                          // no alpha buffer
  84.         0,                          // alpha bits (ignored)
  85.         0,                          // no accumulation buffer
  86.         0, 0, 0, 0,                 // accum bits (ignored)
  87.         24,                         // depth buffer
  88.         8,                          // no stencil buffer
  89.         0,                          // no auxiliary buffers
  90.         PFD_MAIN_PLANE,             // main layer
  91.         0,                          // reserved
  92.         0, 0, 0,                    // no layer, visible, damage masks
  93.     };
  94.  
  95.     pixelFormat = ChoosePixelFormat(hDC, &pfd);
  96.     SetPixelFormat(hDC, pixelFormat, &pfd);
  97. }
  98.  
  99. static bool glew_dynamic_binding()
  100. {
  101.     const char *gl_extensions = (const char*)glGetString(GL_EXTENSIONS);
  102.  
  103.     // If the current opengl driver doesn't have framebuffers methods, check if an extension exists
  104.     if (glGenFramebuffers == NULL)
  105.     {
  106.         CCLog("OpenGL: glGenFramebuffers is NULL, try to detect an extension\n");
  107.         if (strstr(gl_extensions, "ARB_framebuffer_object"))
  108.         {
  109.             CCLog("OpenGL: ARB_framebuffer_object is supported\n");
  110.  
  111.             glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbuffer");
  112.             glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC) wglGetProcAddress("glBindRenderbuffer");
  113.             glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC) wglGetProcAddress("glDeleteRenderbuffers");
  114.             glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC) wglGetProcAddress("glGenRenderbuffers");
  115.             glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC) wglGetProcAddress("glRenderbufferStorage");
  116.             glGetRenderbufferParameteriv = (PFNGLGETRENDERBUFFERPARAMETERIVPROC) wglGetProcAddress("glGetRenderbufferParameteriv");
  117.             glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC) wglGetProcAddress("glIsFramebuffer");
  118.             glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC) wglGetProcAddress("glBindFramebuffer");
  119.             glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC) wglGetProcAddress("glDeleteFramebuffers");
  120.             glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC) wglGetProcAddress("glGenFramebuffers");
  121.             glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC) wglGetProcAddress("glCheckFramebufferStatus");
  122.             glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DPROC) wglGetProcAddress("glFramebufferTexture1D");
  123.             glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) wglGetProcAddress("glFramebufferTexture2D");
  124.             glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC) wglGetProcAddress("glFramebufferTexture3D");
  125.             glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC) wglGetProcAddress("glFramebufferRenderbuffer");
  126.             glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC) wglGetProcAddress("glGetFramebufferAttachmentParameteriv");
  127.             glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmap");
  128.         }
  129.         else
  130.         if (strstr(gl_extensions, "EXT_framebuffer_object"))
  131.         {
  132.             CCLog("OpenGL: EXT_framebuffer_object is supported\n");
  133.             glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbufferEXT");
  134.             glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC) wglGetProcAddress("glBindRenderbufferEXT");
  135.             glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC) wglGetProcAddress("glDeleteRenderbuffersEXT");
  136.             glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC) wglGetProcAddress("glGenRenderbuffersEXT");
  137.             glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC) wglGetProcAddress("glRenderbufferStorageEXT");
  138.             glGetRenderbufferParameteriv = (PFNGLGETRENDERBUFFERPARAMETERIVPROC) wglGetProcAddress("glGetRenderbufferParameterivEXT");
  139.             glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC) wglGetProcAddress("glIsFramebufferEXT");
  140.             glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC) wglGetProcAddress("glBindFramebufferEXT");
  141.             glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC) wglGetProcAddress("glDeleteFramebuffersEXT");
  142.             glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC) wglGetProcAddress("glGenFramebuffersEXT");
  143.             glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC) wglGetProcAddress("glCheckFramebufferStatusEXT");
  144.             glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DPROC) wglGetProcAddress("glFramebufferTexture1DEXT");
  145.             glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) wglGetProcAddress("glFramebufferTexture2DEXT");
  146.             glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC) wglGetProcAddress("glFramebufferTexture3DEXT");
  147.             glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC) wglGetProcAddress("glFramebufferRenderbufferEXT");
  148.             glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC) wglGetProcAddress("glGetFramebufferAttachmentParameterivEXT");
  149.             glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmapEXT");
  150.         }
  151.         else
  152.         {
  153.             CCLog("OpenGL: No framebuffers extension is supported\n");
  154.             CCLog("OpenGL: Any call to Fbo will crash!\n");
  155.             return false;
  156.         }
  157.     }
  158.     return true;
  159. }
  160.  
  161. //////////////////////////////////////////////////////////////////////////
  162. // impliment CCEGLView
  163. //////////////////////////////////////////////////////////////////////////
  164. static CCEGLView* s_pMainWindow = NULL;
  165. static const WCHAR* kWindowClassName = L"Cocos2dxWin32";
  166.  
  167. static LRESULT CALLBACK _WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  168. {
  169.     if (s_pMainWindow && s_pMainWindow->getHWnd() == hWnd)
  170.     {
  171.         return s_pMainWindow->WindowProc(uMsg, wParam, lParam);
  172.     }
  173.     else
  174.     {
  175.         return DefWindowProc(hWnd, uMsg, wParam, lParam);
  176.     }
  177. }
  178.  
  179. CCEGLView::CCEGLView()
  180. : m_bCaptured(false)
  181. , m_hWnd(NULL)
  182. , m_hDC(NULL)
  183. , m_hRC(NULL)
  184. , m_lpfnAccelerometerKeyHook(NULL)
  185. , m_menu(NULL)
  186. , m_wndproc(NULL)
  187. , m_fFrameZoomFactor(1.0f)
  188. , m_bSupportTouch(false)
  189. {
  190.     strcpy(m_szViewName, "Cocos2dxWin32");
  191. }
  192.  
  193. CCEGLView::~CCEGLView()
  194. {
  195.  
  196. }
  197.  
  198. bool CCEGLView::initGL()
  199. {
  200.     m_hDC = GetDC(m_hWnd);
  201.     SetupPixelFormat(m_hDC);
  202.     //SetupPalette();
  203.     m_hRC = wglCreateContext(m_hDC);
  204.     wglMakeCurrent(m_hDC, m_hRC);
  205.  
  206.     // check OpenGL version at first
  207.     const GLubyte* glVersion = glGetString(GL_VERSION);
  208.     CCLOG("OpenGL version = %s", glVersion);
  209.  
  210.     if ( atof((const char*)glVersion) < 1.5 )
  211.     {
  212.         char strComplain[256] = {0};
  213.         sprintf(strComplain,
  214.         "OpenGL 1.5 or higher is required (your version is %s). Please upgrade the driver of your video card.",
  215.         glVersion);
  216.         CCMessageBox(strComplain, "OpenGL version too old");
  217.         return false;
  218.     }
  219.  
  220.     GLenum GlewInitResult = glewInit();
  221.     if (GLEW_OK != GlewInitResult)
  222.     {
  223.         CCMessageBox((char *)glewGetErrorString(GlewInitResult), "OpenGL error");
  224.         return false;
  225.     }
  226.  
  227.     if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
  228.     {
  229.         CCLog("Ready for GLSL");
  230.     }
  231.     else
  232.     {
  233.         CCLog("Not totally ready :(");
  234.     }
  235.  
  236.     if (glewIsSupported("GL_VERSION_2_0"))
  237.     {
  238.         CCLog("Ready for OpenGL 2.0");
  239.     }
  240.     else
  241.     {
  242.         CCLog("OpenGL 2.0 not supported");
  243.     }
  244.  
  245.     if(glew_dynamic_binding() == false)
  246.     {
  247.         CCMessageBox("No OpenGL framebuffer support. Please upgrade the driver of your video card.", "OpenGL error");
  248.         return false;
  249.     }
  250.  
  251.     // Enable point size by default on windows.
  252.     glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
  253.  
  254.     return true;
  255. }
  256.  
  257. void CCEGLView::destroyGL()
  258. {
  259.     if (m_hDC != NULL && m_hRC != NULL)
  260.     {
  261.         // deselect rendering context and delete it
  262.         wglMakeCurrent(m_hDC, NULL);
  263.         wglDeleteContext(m_hRC);
  264.     }
  265. }
  266.  
  267. bool CCEGLView::Create()
  268. {
  269.     bool bRet = false;
  270.     do
  271.     {
  272.         CC_BREAK_IF(m_hWnd);
  273.  
  274.         HINSTANCE hInstance = GetModuleHandle( NULL );
  275.         WNDCLASS  wc;        // Windows Class Structure
  276.  
  277.         // Redraw On Size, And Own DC For Window.
  278.         wc.style          = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  279.         wc.lpfnWndProc    = _WindowProc;                    // WndProc Handles Messages
  280.         wc.cbClsExtra     = 0;                              // No Extra Window Data
  281.         wc.cbWndExtra     = 0;                                // No Extra Window Data
  282.         wc.hInstance      = hInstance;                        // Set The Instance
  283.         wc.hIcon          = LoadIcon( NULL, IDI_WINLOGO );    // Load The Default Icon
  284.         wc.hCursor        = LoadCursor( NULL, IDC_ARROW );    // Load The Arrow Pointer
  285.         wc.hbrBackground  = NULL;                           // No Background Required For GL
  286.         wc.lpszMenuName   = m_menu;                         //
  287.         wc.lpszClassName  = kWindowClassName;               // Set The Class Name
  288.  
  289.         CC_BREAK_IF(! RegisterClass(&wc) && 1410 != GetLastError());
  290.  
  291.         // center window position
  292.         RECT rcDesktop;
  293.         GetWindowRect(GetDesktopWindow(), &rcDesktop);
  294.  
  295.         WCHAR wszBuf[50] = {0};
  296.         MultiByteToWideChar(CP_UTF8, 0, m_szViewName, -1, wszBuf, sizeof(wszBuf));
  297.  
  298.         // create window
  299.         m_hWnd = CreateWindowEx(
  300.             WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,    // Extended Style For The Window
  301.             kWindowClassName,                                    // Class Name
  302.             wszBuf,                                                // Window Title
  303.             WS_CAPTION | WS_POPUPWINDOW | WS_MINIMIZEBOX,        // Defined Window Style
  304.             0, 0,                                                // Window Position
  305.             //TODO: Initializing width with a large value to avoid getting a wrong client area by 'GetClientRect' function.
  306.             1000,                                               // Window Width
  307.             1000,                                               // Window Height
  308.             NULL,                                                // No Parent Window
  309.             NULL,                                                // No Menu
  310.             hInstance,                                            // Instance
  311.             NULL );
  312.  
  313.         CC_BREAK_IF(! m_hWnd);
  314.  
  315.         bRet = initGL();
  316.         if(!bRet) destroyGL();
  317.         CC_BREAK_IF(!bRet);
  318.  
  319.         s_pMainWindow = this;
  320.         bRet = true;
  321.     } while (0);
  322.  
  323. #if(_MSC_VER >= 1600)
  324.     m_bSupportTouch = CheckTouchSupport();
  325.     if(m_bSupportTouch)
  326.     {
  327.         m_bSupportTouch = (s_pfRegisterTouchWindowFunction(m_hWnd, 0) != 0);
  328.     }
  329. #endif /* #if(_MSC_VER >= 1600) */
  330.  
  331.     return bRet;
  332. }
  333.  
  334. LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  335. {
  336.     BOOL bProcessed = FALSE;
  337.  
  338.     switch (message)
  339.     {
  340.     case WM_LBUTTONDOWN:
  341. #if(_MSC_VER >= 1600)
  342.         // Don't process message generated by Windows Touch
  343.         if (m_bSupportTouch && (s_pfGetMessageExtraInfoFunction() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) break;
  344. #endif /* #if(_MSC_VER >= 1600) */
  345.  
  346.         if (m_pDelegate && MK_LBUTTON == wParam)
  347.         {
  348.             POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
  349.             CCPoint pt(point.x, point.y);
  350.             pt.x /= m_fFrameZoomFactor;
  351.             pt.y /= m_fFrameZoomFactor;
  352.             CCPoint tmp = ccp(pt.x, m_obScreenSize.height - pt.y);
  353.             if (m_obViewPortRect.equals(CCRectZero) || m_obViewPortRect.containsPoint(tmp))
  354.             {
  355.                 m_bCaptured = true;
  356.                 SetCapture(m_hWnd);
  357.                 int id = 0;
  358.                 handleTouchesBegin(1, &id, &pt.x, &pt.y);
  359.             }
  360.         }
  361.         break;
  362.  
  363.     case WM_MOUSEMOVE:
  364. #if(_MSC_VER >= 1600)
  365.         // Don't process message generated by Windows Touch
  366.         if (m_bSupportTouch && (s_pfGetMessageExtraInfoFunction() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) break;
  367. #endif /* #if(_MSC_VER >= 1600) */
  368.         if (MK_LBUTTON == wParam && m_bCaptured)
  369.         {
  370.             POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
  371.             CCPoint pt(point.x, point.y);
  372.             int id = 0;
  373.             pt.x /= m_fFrameZoomFactor;
  374.             pt.y /= m_fFrameZoomFactor;
  375.             handleTouchesMove(1, &id, &pt.x, &pt.y);
  376.         }
  377.         break;
  378.  
  379.     case WM_LBUTTONUP:
  380. #if(_MSC_VER >= 1600)
  381.         // Don't process message generated by Windows Touch
  382.         if (m_bSupportTouch && (s_pfGetMessageExtraInfoFunction() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) break;
  383. #endif /* #if(_MSC_VER >= 1600) */
  384.         if (m_bCaptured)
  385.         {
  386.             POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
  387.             CCPoint pt(point.x, point.y);
  388.             int id = 0;
  389.             pt.x /= m_fFrameZoomFactor;
  390.             pt.y /= m_fFrameZoomFactor;
  391.             handleTouchesEnd(1, &id, &pt.x, &pt.y);
  392.  
  393.             ReleaseCapture();
  394.             m_bCaptured = false;
  395.         }
  396.         break;
  397. #if(_MSC_VER >= 1600)
  398.     case WM_TOUCH:
  399.         {
  400.             BOOL bHandled = FALSE;
  401.             UINT cInputs = LOWORD(wParam);
  402.             PTOUCHINPUT pInputs = new TOUCHINPUT[cInputs];
  403.             if (pInputs)
  404.             {
  405.                 if (s_pfGetTouchInputInfoFunction((HTOUCHINPUT)lParam, cInputs, pInputs, sizeof(TOUCHINPUT)))
  406.                 {
  407.                     for (UINT i=0; i < cInputs; i++)
  408.                     {
  409.                         TOUCHINPUT ti = pInputs[i];
  410.                         POINT input;
  411.                         input.x = TOUCH_COORD_TO_PIXEL(ti.x);
  412.                         input.y = TOUCH_COORD_TO_PIXEL(ti.y);
  413.                         ScreenToClient(m_hWnd, &input);
  414.                         CCPoint pt(input.x, input.y);
  415.                         CCPoint tmp = ccp(pt.x, m_obScreenSize.height - pt.y);
  416.                         if (m_obViewPortRect.equals(CCRectZero) || m_obViewPortRect.containsPoint(tmp))
  417.                         {
  418.                             pt.x /= m_fFrameZoomFactor;
  419.                             pt.y /= m_fFrameZoomFactor;
  420.  
  421.                             if (ti.dwFlags & TOUCHEVENTF_DOWN)
  422.                                 handleTouchesBegin(1, reinterpret_cast<int*>(&ti.dwID), &pt.x, &pt.y);
  423.                             else if (ti.dwFlags & TOUCHEVENTF_MOVE)
  424.                                 handleTouchesMove(1, reinterpret_cast<int*>(&ti.dwID), &pt.x, &pt.y);
  425.                             else if (ti.dwFlags & TOUCHEVENTF_UP)
  426.                                 handleTouchesEnd(1, reinterpret_cast<int*>(&ti.dwID), &pt.x, &pt.y);
  427.                          }
  428.                      }
  429.                      bHandled = TRUE;
  430.                  }
  431.                  delete [] pInputs;
  432.              }
  433.              if (bHandled)
  434.              {
  435.                  s_pfCloseTouchInputHandleFunction((HTOUCHINPUT)lParam);
  436.              }
  437.         }
  438.       break;
  439. #endif /* #if(_MSC_VER >= 1600) */
  440.     case WM_SIZE:
  441.         switch (wParam)
  442.         {
  443.         case SIZE_RESTORED:
  444.             CCApplication::sharedApplication()->applicationWillEnterForeground();
  445.             break;
  446.         case SIZE_MINIMIZED:
  447.             CCApplication::sharedApplication()->applicationDidEnterBackground();
  448.             break;
  449.         }
  450.         break;
  451.     case WM_KEYDOWN:
  452.         if (wParam == VK_F1 || wParam == VK_F2)
  453.         {
  454.             CCDirector* pDirector = CCDirector::sharedDirector();
  455.             if (GetKeyState(VK_LSHIFT) < 0 ||  GetKeyState(VK_RSHIFT) < 0 || GetKeyState(VK_SHIFT) < 0)
  456.                 pDirector->getKeypadDispatcher()->dispatchKeypadMSG(wParam == VK_F1 ? kTypeBackClicked : kTypeMenuClicked);
  457.         }
  458.         if ( m_lpfnAccelerometerKeyHook!=NULL )
  459.         {
  460.             (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );
  461.         }
  462.  
  463.  
  464.         switch (wParam) {
  465.         case '1': CCDirector::sharedDirector()->numberOneDown = true; break;
  466.         case '2': CCDirector::sharedDirector()->numberTwoDown = true; break;
  467.         case '3': CCDirector::sharedDirector()->numberThreeDown = true; break;
  468.         case '4': CCDirector::sharedDirector()->numberFourDown = true; break;
  469.         case '5': CCDirector::sharedDirector()->numberFiveDown = true; break;
  470.         case VK_UP: CCDirector::sharedDirector()->upArrowKeyDown = true; break;
  471.         case VK_DOWN: CCDirector::sharedDirector()->downArrowKeyDown = true; break;
  472.         case VK_LEFT: CCDirector::sharedDirector()->leftArrowKeyDown = true; break;
  473.         case VK_RIGHT: CCDirector::sharedDirector()->rightArrowKeyDown = true; break;
  474.         };
  475.        
  476.         break;
  477.     case WM_KEYUP:
  478.         if ( m_lpfnAccelerometerKeyHook!=NULL )
  479.         {
  480.             (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );
  481.         }
  482.  
  483.        
  484.         switch (wParam) {
  485.         case '1': CCDirector::sharedDirector()->numberOneDown = false; CCDirector::sharedDirector()->numberOnePressed = true; break;
  486.         case '2': CCDirector::sharedDirector()->numberTwoDown = false; CCDirector::sharedDirector()->numberTwoPressed = true;  break;
  487.         case '3': CCDirector::sharedDirector()->numberThreeDown = false; CCDirector::sharedDirector()->numberThreePressed = true;  break;
  488.         case '4': CCDirector::sharedDirector()->numberFourDown = false; CCDirector::sharedDirector()->numberFourPressed = true; break;
  489.         case '5': CCDirector::sharedDirector()->numberFiveDown = false; CCDirector::sharedDirector()->numberFivePressed = true;  break;
  490.         case VK_UP: CCDirector::sharedDirector()->upArrowKeyDown = false;        break;
  491.         case VK_DOWN: CCDirector::sharedDirector()->downArrowKeyDown = false;    break;
  492.         case VK_LEFT: CCDirector::sharedDirector()->leftArrowKeyDown = false;    break;
  493.         case VK_RIGHT: CCDirector::sharedDirector()->rightArrowKeyDown = false;  break;
  494.         };
  495.  
  496.         break;
  497.     case WM_CHAR:
  498.         {
  499.             if (wParam < 0x20)
  500.             {
  501.                 if (VK_BACK == wParam)
  502.                 {
  503.                     CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
  504.                 }
  505.                 else if (VK_RETURN == wParam)
  506.                 {
  507.                     CCIMEDispatcher::sharedDispatcher()->dispatchInsertText("\n", 1);
  508.                 }
  509.                 else if (VK_TAB == wParam)
  510.                 {
  511.                     // tab input
  512.                 }
  513.                 else if (VK_ESCAPE == wParam)
  514.                 {
  515.                     // ESC input
  516.                     //CCDirector::sharedDirector()->end();
  517.                 }
  518.             }
  519.             else if (wParam < 128)
  520.             {
  521.                 // ascii char
  522.                 CCIMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&wParam, 1);
  523.             }
  524.             else
  525.             {
  526.                 char szUtf8[8] = {0};
  527.                 int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)&wParam, 1, szUtf8, sizeof(szUtf8), NULL, NULL);
  528.                 CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen);
  529.             }
  530.             if ( m_lpfnAccelerometerKeyHook!=NULL )
  531.             {
  532.                 (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );
  533.             }
  534.         }
  535.         break;
  536.     case WM_PAINT:
  537.         PAINTSTRUCT ps;
  538.         BeginPaint(m_hWnd, &ps);
  539.         EndPaint(m_hWnd, &ps);
  540.         break;
  541.  
  542.     case WM_CLOSE:
  543.         CCDirector::sharedDirector()->end();
  544.         break;
  545.  
  546.     case WM_DESTROY:
  547.         destroyGL();
  548.         PostQuitMessage(0);
  549.         break;
  550.  
  551.     default:
  552.         if (m_wndproc)
  553.         {
  554.            
  555.             m_wndproc(message, wParam, lParam, &bProcessed);
  556.             if (bProcessed) break;
  557.         }
  558.         return DefWindowProc(m_hWnd, message, wParam, lParam);
  559.     }
  560.  
  561.     if (m_wndproc && !bProcessed)
  562.     {
  563.         m_wndproc(message, wParam, lParam, &bProcessed);
  564.     }
  565.     return 0;
  566. }
  567.  
  568. void CCEGLView::setAccelerometerKeyHook( LPFN_ACCELEROMETER_KEYHOOK lpfnAccelerometerKeyHook )
  569. {
  570.     m_lpfnAccelerometerKeyHook=lpfnAccelerometerKeyHook;
  571. }
  572.  
  573.  
  574. bool CCEGLView::isOpenGLReady()
  575. {
  576.     return (m_hDC != NULL && m_hRC != NULL);
  577. }
  578.  
  579. void CCEGLView::end()
  580. {
  581.     if (m_hWnd)
  582.     {
  583. #if(_MSC_VER >= 1600)
  584.         if(m_bSupportTouch)
  585.         {
  586.             s_pfUnregisterTouchWindowFunction(m_hWnd);
  587.         }
  588. #endif /* #if(_MSC_VER >= 1600) */
  589.         DestroyWindow(m_hWnd);
  590.         m_hWnd = NULL;
  591.     }
  592.     s_pMainWindow = NULL;
  593.     UnregisterClass(kWindowClassName, GetModuleHandle(NULL));
  594.     delete this;
  595. }
  596.  
  597. void CCEGLView::swapBuffers()
  598. {
  599.     if (m_hDC != NULL)
  600.     {
  601.         ::SwapBuffers(m_hDC);
  602.     }
  603. }
  604.  
  605.  
  606. void CCEGLView::setIMEKeyboardState(bool /*bOpen*/)
  607. {
  608.  
  609. }
  610.  
  611. void CCEGLView::setMenuResource(LPCWSTR menu)
  612. {
  613.     m_menu = menu;
  614.     if (m_hWnd != NULL)
  615.     {
  616.         HMENU hMenu = LoadMenu(GetModuleHandle(NULL), menu);
  617.         SetMenu(m_hWnd, hMenu);
  618.     }
  619. }
  620.  
  621. void CCEGLView::setWndProc(CUSTOM_WND_PROC proc)
  622. {
  623.     m_wndproc = proc;
  624. }
  625.  
  626. HWND CCEGLView::getHWnd()
  627. {
  628.     return m_hWnd;
  629. }
  630.  
  631. void CCEGLView::resize(int width, int height)
  632. {
  633.     if (! m_hWnd)
  634.     {
  635.         return;
  636.     }
  637.  
  638.     RECT rcWindow;
  639.     GetWindowRect(m_hWnd, &rcWindow);
  640.  
  641.     RECT rcClient;
  642.     GetClientRect(m_hWnd, &rcClient);
  643.  
  644.     // calculate new window width and height
  645.     POINT ptDiff;
  646.     ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
  647.     ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
  648.     rcClient.right = rcClient.left + width;
  649.     rcClient.bottom = rcClient.top + height;
  650.  
  651.     const CCSize& frameSize = getFrameSize();
  652.     if (frameSize.width > 0)
  653.     {
  654. #ifdef _DEBUG
  655.         TCHAR buff[MAX_PATH + 1];
  656.         memset(buff, 0, sizeof(buff));
  657.         swprintf_s(buff, MAX_PATH, L"%s - %0.0fx%0.0f - %0.2f",
  658.                    kWindowClassName, frameSize.width, frameSize.height, m_fFrameZoomFactor);
  659.         SetWindowText(m_hWnd, buff);
  660. #endif
  661.     }
  662.  
  663.     AdjustWindowRectEx(&rcClient, GetWindowLong(m_hWnd, GWL_STYLE), FALSE, GetWindowLong(m_hWnd, GWL_EXSTYLE));
  664.  
  665.     // change width and height
  666.     SetWindowPos(m_hWnd, 0, 0, 0, width + ptDiff.x, height + ptDiff.y,
  667.                  SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
  668. }
  669.  
  670. void CCEGLView::setFrameZoomFactor(float fZoomFactor)
  671. {
  672.     m_fFrameZoomFactor = fZoomFactor;
  673.     resize(m_obScreenSize.width * fZoomFactor, m_obScreenSize.height * fZoomFactor);
  674.     centerWindow();
  675.     CCDirector::sharedDirector()->setProjection(CCDirector::sharedDirector()->getProjection());
  676. }
  677.  
  678. float CCEGLView::getFrameZoomFactor()
  679. {
  680.     return m_fFrameZoomFactor;
  681. }
  682.  
  683. void CCEGLView::setFrameSize(float width, float height)
  684. {
  685.     CCEGLViewProtocol::setFrameSize(width, height);
  686.  
  687.     resize(width, height); // adjust window size for menubar
  688.     centerWindow();
  689. }
  690.  
  691. void CCEGLView::centerWindow()
  692. {
  693.     if (! m_hWnd)
  694.     {
  695.         return;
  696.     }
  697.  
  698.     RECT rcDesktop, rcWindow;
  699.     GetWindowRect(GetDesktopWindow(), &rcDesktop);
  700.  
  701.     // substract the task bar
  702.     HWND hTaskBar = FindWindow(TEXT("Shell_TrayWnd"), NULL);
  703.     if (hTaskBar != NULL)
  704.     {
  705.         APPBARDATA abd;
  706.  
  707.         abd.cbSize = sizeof(APPBARDATA);
  708.         abd.hWnd = hTaskBar;
  709.  
  710.         SHAppBarMessage(ABM_GETTASKBARPOS, &abd);
  711.         SubtractRect(&rcDesktop, &rcDesktop, &abd.rc);
  712.     }
  713.     GetWindowRect(m_hWnd, &rcWindow);
  714.  
  715.     int offsetX = (rcDesktop.right - rcDesktop.left - (rcWindow.right - rcWindow.left)) / 2;
  716.     offsetX = (offsetX > 0) ? offsetX : rcDesktop.left;
  717.     int offsetY = (rcDesktop.bottom - rcDesktop.top - (rcWindow.bottom - rcWindow.top)) / 2;
  718.     offsetY = (offsetY > 0) ? offsetY : rcDesktop.top;
  719.  
  720.     SetWindowPos(m_hWnd, 0, offsetX, offsetY, 0, 0, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOOWNERZORDER | SWP_NOZORDER);
  721. }
  722.  
  723. void CCEGLView::setViewPortInPoints(float x , float y , float w , float h)
  724. {
  725.     glViewport((GLint)(x * m_fScaleX * m_fFrameZoomFactor + m_obViewPortRect.origin.x * m_fFrameZoomFactor),
  726.         (GLint)(y * m_fScaleY  * m_fFrameZoomFactor + m_obViewPortRect.origin.y * m_fFrameZoomFactor),
  727.         (GLsizei)(w * m_fScaleX * m_fFrameZoomFactor),
  728.         (GLsizei)(h * m_fScaleY * m_fFrameZoomFactor));
  729. }
  730.  
  731. void CCEGLView::setScissorInPoints(float x , float y , float w , float h)
  732. {
  733.     glScissor((GLint)(x * m_fScaleX * m_fFrameZoomFactor + m_obViewPortRect.origin.x * m_fFrameZoomFactor),
  734.               (GLint)(y * m_fScaleY * m_fFrameZoomFactor + m_obViewPortRect.origin.y * m_fFrameZoomFactor),
  735.               (GLsizei)(w * m_fScaleX * m_fFrameZoomFactor),
  736.               (GLsizei)(h * m_fScaleY * m_fFrameZoomFactor));
  737. }
  738.  
  739. CCEGLView* CCEGLView::sharedOpenGLView()
  740. {
  741.     static CCEGLView* s_pEglView = NULL;
  742.     if (s_pEglView == NULL)
  743.     {
  744.         s_pEglView = new CCEGLView();
  745.         if(!s_pEglView->Create())
  746.         {
  747.             delete s_pEglView;
  748.             s_pEglView = NULL;
  749.         }
  750.     }
  751.  
  752.     return s_pEglView;
  753. }
  754.  
  755. NS_CC_END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement