home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-STLport.exe / {app} / Samples / common / src / Win32AppHelper.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-08-21  |  10.2 KB  |  334 lines

  1. /************************************************************************
  2.     filename:   Win32AppHelper.cpp
  3.     created:    17/10/2004
  4.     author:     Paul D Turner
  5. *************************************************************************/
  6. /*************************************************************************
  7.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  8.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  9.  
  10.     This library is free software; you can redistribute it and/or
  11.     modify it under the terms of the GNU Lesser General Public
  12.     License as published by the Free Software Foundation; either
  13.     version 2.1 of the License, or (at your option) any later version.
  14.  
  15.     This library is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.     Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public
  21.     License along with this library; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. *************************************************************************/
  24. // this controls conditional compile of file for MSVC
  25. #include "CEGUIConfig.h"
  26. #if defined(CEGUI_SAMPLES_USE_DIRECTX_8) || defined(CEGUI_SAMPLES_USE_DIRECTX_9)
  27.  
  28. #include "Win32AppHelper.h"
  29. #include "CEGUI.h"
  30. #include <TCHAR.H>
  31.  
  32.  
  33. /*************************************************************************
  34.     Static Data Definitions
  35. *************************************************************************/
  36. // name of the application, used for class and window creation
  37. const TCHAR Win32AppHelper::APPLICATION_NAME[]      = _TEXT("Crazy Eddie's GUI Mk-2 - Sample Application");
  38.  
  39. // error strings displayed during initialisation
  40. const TCHAR Win32AppHelper::REGISTER_CLASS_ERROR[]  = _TEXT("Failed to register window class.");
  41. const TCHAR Win32AppHelper::CREATE_WINDOW_ERROR[]   = _TEXT("Failed to create window.");
  42.  
  43. // other error strings used
  44. const TCHAR Win32AppHelper::CREATE_D3D_ERROR[]      = _TEXT("Failed to create main Direct3D object.");
  45. const TCHAR Win32AppHelper::CREATE_DEVICE_ERROR[]   = _TEXT("Failed to create Direct3D Device object.");
  46.  
  47. // variable for tracking Win32 cursor
  48. bool Win32AppHelper::d_mouseInWindow = false;
  49.  
  50.  
  51. /*************************************************************************
  52.     Create main application window.
  53. *************************************************************************/
  54. HWND Win32AppHelper::createApplicationWindow(int width, int height)
  55. {
  56.     WNDCLASS    wndClass;       // structure used to register window class
  57.  
  58.     // Initialise WNDCLASS structure.
  59.     wndClass.style          = 0;
  60.     wndClass.lpfnWndProc    = wndProc;
  61.     wndClass.cbClsExtra     = 0;
  62.     wndClass.cbWndExtra     = 0;
  63.     wndClass.hInstance      = GetModuleHandle(0);
  64.     wndClass.hIcon          = LoadIcon(0, IDI_WINLOGO);
  65.     wndClass.hCursor        = LoadCursor(0, IDC_ARROW);
  66.     wndClass.hbrBackground  = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
  67.     wndClass.lpszMenuName   = 0;
  68.     wndClass.lpszClassName  = APPLICATION_NAME;
  69.  
  70.     HWND window = 0;
  71.  
  72.     // register class.  Report error & exit upon failure
  73.     if (RegisterClass(&wndClass))
  74.     {
  75.         // create new window
  76.         window = CreateWindow(APPLICATION_NAME, APPLICATION_NAME, WS_OVERLAPPEDWINDOW,
  77.                               0, 0, width, height, 0, 0, GetModuleHandle(0), 0);
  78.     }
  79.     else
  80.     {
  81.         MessageBox(0, REGISTER_CLASS_ERROR, APPLICATION_NAME, MB_ICONERROR|MB_OK);
  82.     }
  83.  
  84.     return window;
  85. }
  86.  
  87.  
  88. /*************************************************************************
  89.     Win32 'Window Procedure' function
  90. *************************************************************************/
  91. LRESULT CALLBACK Win32AppHelper::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  92. {
  93.     switch(message)
  94.     {
  95.     case WM_CHAR:
  96.         CEGUI::System::getSingleton().injectChar((CEGUI::utf32)wParam);
  97.         break;
  98.  
  99.     case WM_MOUSELEAVE:
  100.         mouseLeaves();
  101.         break;
  102.  
  103.     case WM_NCMOUSEMOVE:
  104.         mouseLeaves();
  105.         break;
  106.  
  107.     case WM_MOUSEMOVE:
  108.         mouseEnters();
  109.  
  110.         CEGUI::System::getSingleton().injectMousePosition((float)(LOWORD(lParam)), (float)(HIWORD(lParam)));
  111.         break;
  112.  
  113.     case WM_LBUTTONDOWN:
  114.         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);
  115.         break;
  116.  
  117.     case WM_LBUTTONUP:
  118.         CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);
  119.         break;
  120.  
  121.     case WM_RBUTTONDOWN:
  122.         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);
  123.         break;
  124.  
  125.     case WM_RBUTTONUP:
  126.         CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);
  127.         break;
  128.  
  129.     case WM_MBUTTONDOWN:
  130.         CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);
  131.         break;
  132.  
  133.     case WM_MBUTTONUP:
  134.         CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton);
  135.         break;
  136.  
  137.     case 0x020A: // WM_MOUSEWHEEL:
  138.         CEGUI::System::getSingleton().injectMouseWheelChange(static_cast<float>((short)HIWORD(wParam)) / static_cast<float>(120));
  139.         break;
  140.  
  141.     case WM_DESTROY:
  142.         PostQuitMessage(0);
  143.         break;
  144.  
  145.     case WM_SIZE:
  146.         // TODO: Notify about new size
  147.         break;
  148.  
  149.     case WM_PAINT:
  150.         {
  151.             HDC         hDC;
  152.             PAINTSTRUCT ps;
  153.  
  154.             hDC = BeginPaint(hWnd, &ps);
  155.             EndPaint(hWnd, &ps);
  156.             break;
  157.         }
  158.  
  159.     default:
  160.         return(DefWindowProc(hWnd, message, wParam, lParam));
  161.         break;
  162.     }
  163.  
  164.     return 0;
  165. }
  166.  
  167.  
  168. void Win32AppHelper::mouseEnters(void)
  169. {
  170.     if (!d_mouseInWindow)
  171.     {
  172.         d_mouseInWindow = true;
  173.         ShowCursor(false);
  174.     }
  175. }
  176.  
  177. void Win32AppHelper::mouseLeaves(void)
  178. {
  179.     if (d_mouseInWindow)
  180.     {
  181.         d_mouseInWindow = false;
  182.         ShowCursor(true);
  183.     }
  184. }
  185.  
  186.  
  187. bool Win32AppHelper::initialiseDirectInput(HWND window, Win32AppHelper::DirectInputState& dis)
  188. {
  189.     if (SUCCEEDED(DirectInput8Create(GetModuleHandle(0), DIRECTINPUT_VERSION, IID_IDirectInput8, (LPVOID*)&dis.directInput, 0)))
  190.     {
  191.         if (SUCCEEDED(dis.directInput->CreateDevice(GUID_SysKeyboard, &dis.keyboardDevice, 0)))
  192.         {
  193.             if (SUCCEEDED(dis.keyboardDevice->SetDataFormat(&c_dfDIKeyboard)))
  194.             {
  195.                 if (SUCCEEDED(dis.keyboardDevice->SetCooperativeLevel(window, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE)))
  196.                 {
  197.                     DIPROPDWORD inputProp;
  198.                     // the header
  199.                     inputProp.diph.dwSize       = sizeof(DIPROPDWORD);
  200.                     inputProp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  201.                     inputProp.diph.dwObj        = 0;
  202.                     inputProp.diph.dwHow        = DIPH_DEVICE;
  203.                     inputProp.dwData            = 16;
  204.  
  205.                     if (SUCCEEDED(dis.keyboardDevice->SetProperty(DIPROP_BUFFERSIZE, &inputProp.diph)))
  206.                     {
  207.                         dis.keyboardDevice->Acquire();
  208.                         return true;
  209.                     }
  210.                     else
  211.                     {
  212.                         MessageBox(0, _TEXT("Failed to set buffer size for keyboard device."), APPLICATION_NAME, MB_ICONERROR|MB_OK);
  213.                     }
  214.  
  215.                 }
  216.                 else
  217.                 {
  218.                     MessageBox(0, _TEXT("Failed to set co-operative level for keyboard device."), APPLICATION_NAME, MB_ICONERROR|MB_OK);
  219.                 }
  220.  
  221.             }
  222.             else
  223.             {
  224.                 MessageBox(0, _TEXT("Failed to set data format for keyboard device."), APPLICATION_NAME, MB_ICONERROR|MB_OK);
  225.             }
  226.  
  227.             dis.keyboardDevice->Release();
  228.             dis.keyboardDevice = 0;
  229.         }
  230.         else
  231.         {
  232.             MessageBox(0, _TEXT("Failed to create DirectInput keyboard device."), APPLICATION_NAME, MB_ICONERROR|MB_OK);
  233.         }
  234.  
  235.         dis.directInput->Release();
  236.         dis.directInput = 0;
  237.     }
  238.     else
  239.     {
  240.         MessageBox(0, _TEXT("Failed to create main DirectInput object."), APPLICATION_NAME, MB_ICONERROR|MB_OK);
  241.     }
  242.  
  243.     return false;
  244. }
  245.  
  246.  
  247. void Win32AppHelper::cleanupDirectInput(Win32AppHelper::DirectInputState& dis)
  248. {
  249.     if (dis.keyboardDevice)
  250.     {
  251.         dis.keyboardDevice->Unacquire();
  252.         dis.keyboardDevice->Release();
  253.         dis.keyboardDevice = 0;
  254.     }
  255.  
  256.     if (dis.directInput)
  257.     {
  258.         dis.directInput->Release();
  259.         dis.directInput = 0;
  260.     }
  261. }
  262.  
  263.  
  264. void Win32AppHelper::doDirectInputEvents(const Win32AppHelper::DirectInputState& dis)
  265. {
  266.     // handle direct input based inputs
  267.     DIDEVICEOBJECTDATA devDat;
  268.     DWORD itemCount = 1;
  269.  
  270.     HRESULT res = dis.keyboardDevice->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), &devDat, &itemCount, 0);
  271.  
  272.     if (SUCCEEDED(res))
  273.     {
  274.         if (itemCount > 0)
  275.         {
  276.             if (LOBYTE(devDat.dwData) & 0x80)
  277.             {
  278.                 // force quit on ESCAPE key
  279.                 if (devDat.dwOfs == CEGUI::Key::Escape)
  280.                 {
  281.                     PostQuitMessage(0);
  282.                 }
  283.                 else
  284.                 {
  285.                     CEGUI::System::getSingleton().injectKeyDown(devDat.dwOfs);
  286.                 }
  287.  
  288.             }
  289.             else
  290.             {
  291.                 CEGUI::System::getSingleton().injectKeyUp(devDat.dwOfs);
  292.             }
  293.  
  294.         }
  295.     }
  296.     else
  297.     {
  298.         // try to re-acquire device if that was the cause of the error.
  299.         if ((res == DIERR_NOTACQUIRED) || (res == DIERR_INPUTLOST))
  300.         {
  301.             dis.keyboardDevice->Acquire();
  302.         }
  303.  
  304.     }
  305.  
  306. }
  307.  
  308. bool Win32AppHelper::doWin32Events(bool& idle)
  309. {
  310.     MSG msg;
  311.  
  312.     if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  313.     {
  314.  
  315.         if (msg.message == WM_QUIT)
  316.         {
  317.             return false;
  318.         }
  319.  
  320.         TranslateMessage(&msg);
  321.         DispatchMessage(&msg);
  322.  
  323.         idle = false;
  324.     }
  325.     else
  326.     {
  327.         idle = true;
  328.     }
  329.  
  330.     return true;
  331. }
  332.  
  333. #endif
  334.