home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / opengl / openglobj.h < prev    next >
C/C++ Source or Header  |  1998-03-26  |  6KB  |  208 lines

  1. // OpenGLObj.h : Declaration of the COpenGLObj
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include "resource.h"       // main symbols
  14. #include <atlctl.h>
  15.  
  16. #pragma comment(lib, "opengl32.lib")
  17. #pragma comment(lib, "glu32.lib")
  18. #pragma comment(lib, "glaux.lib")
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // OpenGL
  22.  
  23. class COpenGLObj :
  24.     public CComObjectRoot,
  25.     public CComCoClass<COpenGLObj,&CLSID_COpenGLObj>,
  26.     public CComControl<COpenGLObj>,
  27.     public IDispatchImpl<IOpenGLObj, &IID_IOpenGLObj, &LIBID_OPENGLLib>,
  28.     public IPersistStreamInitImpl<COpenGLObj>,
  29.     public IPersistStorageImpl<COpenGLObj>,
  30.     public IOleControlImpl<COpenGLObj>,
  31.     public IOleObjectImpl<COpenGLObj>,
  32.     public IOleInPlaceActiveObjectImpl<COpenGLObj>,
  33.     public IOleInPlaceObjectWindowlessImpl<COpenGLObj>,
  34.     public IViewObjectExImpl<COpenGLObj>,
  35.     public IObjectSafetyImpl<COpenGLObj, INTERFACESAFE_FOR_UNTRUSTED_CALLER>
  36. {
  37. public:
  38.     COpenGLObj()
  39.     {
  40.         m_bActive = FALSE;
  41.         m_bMouseCaptured = FALSE;
  42.         m_bstrCaption = _T("ATL 3.0");
  43.         m_bWindowOnly = TRUE;
  44.         m_wAngleY = 10.0f;
  45.         m_wAngleX = 1.0f;
  46.         m_wAngleZ = 5.0f;
  47.         m_pPal = NULL;
  48.         m_hPal = NULL;
  49.         m_hrc = NULL;
  50.         joyposX = 0x3000;
  51.         joyposY = 0x4000;
  52.         joyposZ = 0x5000;
  53.     }
  54.     ~COpenGLObj()
  55.     {
  56.         if (m_pPal)
  57.         {
  58.             delete[] m_pPal;
  59.             m_pPal = NULL;
  60.         }
  61.         if (m_hPal)
  62.             DeleteObject(m_hPal);
  63.     }
  64.  
  65.  
  66. BEGIN_MSG_MAP(COpenGLObj)
  67.     MESSAGE_HANDLER(WM_PAINT, OnPaint)
  68.     MESSAGE_HANDLER(WM_CREATE, OnCreate)
  69.     MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
  70.     MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
  71.     MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
  72.     MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
  73.     MESSAGE_HANDLER(WM_TIMER, OnTimer)
  74.     MESSAGE_HANDLER(WM_SIZE, OnSize)
  75.     MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
  76.     MESSAGE_HANDLER(MM_JOY1MOVE, OnJoyMove)
  77.     MESSAGE_HANDLER(MM_JOY1ZMOVE, OnJoyZMove)
  78. END_MSG_MAP()
  79.  
  80. BEGIN_COM_MAP(COpenGLObj)
  81.     COM_INTERFACE_ENTRY(IDispatch)
  82.     COM_INTERFACE_ENTRY(IOpenGLObj)
  83.     COM_INTERFACE_ENTRY(IObjectSafety)
  84.     COM_INTERFACE_ENTRY_IMPL_IID(IID_IViewObject, IViewObjectEx)
  85.     COM_INTERFACE_ENTRY_IMPL_IID(IID_IViewObject2, IViewObjectEx)
  86.     COM_INTERFACE_ENTRY_IMPL(IViewObjectEx)
  87.     COM_INTERFACE_ENTRY_IMPL_IID(IID_IOleWindow, IOleInPlaceObjectWindowless)
  88.     COM_INTERFACE_ENTRY_IMPL_IID(IID_IOleInPlaceObject, IOleInPlaceObjectWindowless)
  89.     COM_INTERFACE_ENTRY_IMPL(IOleInPlaceObjectWindowless)
  90.     COM_INTERFACE_ENTRY_IMPL(IOleInPlaceActiveObject)
  91.     COM_INTERFACE_ENTRY_IMPL(IOleControl)
  92.     COM_INTERFACE_ENTRY_IMPL(IOleObject)
  93.     COM_INTERFACE_ENTRY_IMPL(IPersistStorage)
  94.     COM_INTERFACE_ENTRY_IMPL(IPersistStreamInit)
  95. END_COM_MAP()
  96.  
  97. BEGIN_CONNECTION_POINT_MAP(COpenGLObj)
  98. END_CONNECTION_POINT_MAP()
  99.  
  100. BEGIN_PROPERTY_MAP(COpenGLObj)
  101. END_PROPERTY_MAP()
  102.  
  103. //DECLARE_NOT_AGGREGATABLE(COpenGLObj)
  104. DECLARE_GET_CONTROLLING_UNKNOWN()
  105. //DECLARE_CONTROL_INFO(CLSID_COpenGLObj)
  106. DECLARE_REGISTRY_RESOURCEID(IDR_OpenGLObj)
  107.  
  108.     float m_fRadius;
  109.     BOOL m_bActive;
  110.     BOOL m_bMouseCaptured;
  111.     int m_xPos;
  112.     int m_yPos;
  113.     CComBSTR m_bstrCaption;
  114.     GLfloat  m_wAngleY;
  115.     GLfloat  m_wAngleX;
  116.     GLfloat  m_wAngleZ;
  117.     HGLRC m_hrc;
  118.     HPALETTE m_hPal;
  119.     LOGPALETTE *m_pPal;
  120.     WORD joyposX;
  121.     WORD joyposY;
  122.     WORD joyposZ;
  123.  
  124. // IOpenGLObj
  125. public:
  126.     HRESULT OnDraw(ATL_DRAWINFO& di);
  127.     BOOL bSetupPixelFormat(HDC hdc);
  128.     LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  129.     LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  130.     LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  131.     LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  132.     LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  133.     LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  134.     {
  135.         ::wglMakeCurrent(NULL,  NULL);
  136.         if (m_hrc)
  137.         {
  138.             ::wglDeleteContext(m_hrc);
  139.             m_hrc = NULL;
  140.         }
  141.         HDC hdc = GetDC();
  142.         RECT rc;
  143.         GetClientRect(&rc);
  144.         CreateContext(hdc, rc);
  145.         return 0;
  146.     }
  147.     LRESULT OnEraseBackground(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  148.     {
  149.         return 0;
  150.     }
  151.     LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  152.     {
  153.         FireViewChange();
  154.         return 1;
  155.     }
  156.     LRESULT OnJoyMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  157.     {
  158.         joyposX = LOWORD(lParam);
  159.         joyposY = HIWORD(lParam);
  160.         return 0;
  161.     }
  162.     LRESULT OnJoyZMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  163.     {
  164.         joyposZ = LOWORD(lParam);
  165.         return 0;
  166.     }
  167.  
  168.     STDMETHODIMP put_Caption(BSTR pCaption)
  169.     {
  170.         USES_CONVERSION;
  171.         ATLTRACE(_T("IOpenGL::put_Caption\n"));
  172.         m_bstrCaption = pCaption;
  173.         return S_OK;
  174.     }
  175.     STDMETHODIMP get_Caption(BSTR* ppCaption)
  176.     {
  177.         ATLTRACE(_T("IOpenGL::get_Caption\n"));
  178.         *ppCaption = m_bstrCaption.Copy();
  179.         return S_OK;
  180.     }
  181.  
  182.     STDMETHOD(GetColorSet)(DWORD dwDrawAspect,LONG lindex, void* pvAspect, DVTARGETDEVICE* ptd, HDC hicTargetDev, LOGPALETTE** ppColorSet)
  183.     {
  184.         ATLTRACE(_T("GetColorSet\n"));
  185.         if (ppColorSet == NULL)
  186.             return E_POINTER;
  187.         HRESULT hr = S_FALSE;
  188.         *ppColorSet = NULL;
  189.         if (m_pPal != NULL)
  190.         {
  191.             int nSize = sizeof(LOGPALETTE) + m_pPal->palNumEntries * sizeof(PALETTEENTRY);
  192.             *ppColorSet = (PLOGPALETTE) CoTaskMemAlloc(nSize);
  193.             if (*ppColorSet == NULL)
  194.                 hr = E_OUTOFMEMORY;
  195.             else
  196.             {
  197.                 memcpy(*ppColorSet, m_pPal, nSize);
  198.                 hr = S_OK;
  199.             }
  200.         }
  201.         return hr;
  202.     }
  203.  
  204.     void CreateContext(HDC hdc, RECT& rc);
  205.     void CreateRGBPalette(HDC hdc);
  206.     unsigned char ComponentFromIndex(int i, UINT nbits, UINT shift);
  207. };
  208.