Advertisement
musicm122

CCEGLView.cpp

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