home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 36 / #36-1.iso / quake / Files / md2 / src / wingl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-25  |  5.1 KB  |  192 lines

  1. /*****************************************************************
  2. Copyright (c) 1998 Jawed Karim <jkarim@students.uiuc.edu>
  3. All rights reserved.
  4.  
  5. This source code has been provided to you under the condition
  6. that you adhere to the following terms:
  7.  
  8. 1. YOU MAY USE THIS SOURCE CODE PRIVATELY WITHOUT RESTRICTIONS.
  9.  
  10. 2. REDISTRIBUTIONS OF MODIFICATIONS OF THIS PROGRAM IN BINARY OR
  11.    IN SOURCE CODE FORM ARE NOT PERMITTED.
  12.  
  13. 3. REDISTRIBUTIONS OF THIS SOURCE CODE ARE ONLY PERMITTED IF
  14.    THE SOURCE CODE REMAINS COMPLETELY UNCHANGED AND ALL THE
  15.    FILES WHICH WERE IN THE ORIGINAL DISTRIBUTION ARE INCLUDED.
  16.  
  17. 4. ALL SOFTWARE USING SECTIONS OF THIS SOURCE CODE MUST GIVE
  18.    EXPLICIT ACKNOWLEDGMENT TO JAWED KARIM IN THE PROGRAM
  19.    ITSELF AS WELL AS IN THE DOCUMENTATION.
  20.  
  21. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  22. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  25. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  27. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. INTERRUPTION).
  29. *****************************************************************/
  30.  
  31. #include <stdio.h>
  32. #include <iostream.h>
  33.  
  34. #include <windows.h>
  35. #include <windowsx.h>
  36. #include <GL/gl.h>
  37. #include <GL/glu.h>
  38.  
  39. #include "wingl.h"
  40.  
  41. void COpenGLWindow::Create (HWND hWnd)
  42. {
  43.     m_hdc = ::GetDC(hWnd);
  44.     SetupPixelFormat ();
  45.     SetupPalette ();
  46.     m_hGLRC = wglCreateContext (m_hdc);
  47.     wglMakeCurrent(m_hdc, m_hGLRC);
  48. }
  49.  
  50. void COpenGLWindow::SetSize (int iX, int iY)
  51. {
  52.     m_sizeX = iX;
  53.     m_sizeY = iY;
  54. }
  55.  
  56. void COpenGLWindow::GetSize (int *iX, int *iY)
  57. {
  58.     if (iX != NULL && iY != NULL)
  59.     {
  60.         m_sizeX = *iX;
  61.         m_sizeY = *iY;
  62.     }
  63. }
  64.  
  65. void COpenGLWindow::Destroy (HWND hWnd)
  66. {
  67.     if (m_hGLRC) {
  68.         wglMakeCurrent (NULL, NULL);
  69.         wglDeleteContext(m_hGLRC);
  70.     }
  71.  
  72.     if (m_hPalette) {
  73.         DeleteObject (m_hPalette);
  74.     }
  75.  
  76.     ReleaseDC(hWnd, m_hdc);
  77. }
  78.  
  79. BOOL COpenGLWindow::GLRCValid ()
  80. {
  81.     if (m_hGLRC)
  82.         return TRUE;
  83.     else
  84.         return FALSE;
  85. }
  86.  
  87. BOOL COpenGLWindow::PaletteValid ()
  88. {
  89.     if (m_hPalette)
  90.         return TRUE;
  91.     else
  92.         return FALSE;
  93. }
  94.  
  95. HDC COpenGLWindow::GetDC ()
  96. {
  97.     return m_hdc;
  98. }
  99.  
  100. void COpenGLWindow::RedoPalette ()
  101. {
  102.     UnrealizeObject (m_hPalette);
  103.     SelectPalette (m_hdc, m_hPalette, FALSE);
  104.     RealizePalette (m_hdc);
  105. }
  106.  
  107. void COpenGLWindow::SetupPixelFormat ()
  108. {
  109.     PIXELFORMATDESCRIPTOR pfd = {
  110.         sizeof(PIXELFORMATDESCRIPTOR),  /* size */
  111.         1,                              /* version */
  112.         PFD_SUPPORT_OPENGL |
  113.         PFD_DRAW_TO_WINDOW |
  114.         PFD_DOUBLEBUFFER,               /* support double-buffering */
  115.         PFD_TYPE_RGBA,                  /* color type */
  116.         16,                             /* prefered color depth */
  117.         0, 0, 0, 0, 0, 0,               /* color bits (ignored) */
  118.         0,                              /* no alpha buffer */
  119.         0,                              /* alpha bits (ignored) */
  120.         0,                              /* no accumulation buffer */
  121.         0, 0, 0, 0,                     /* accum bits (ignored) */
  122.         16,                             /* depth buffer */
  123.         0,                              /* no stencil buffer */
  124.         0,                              /* no auxiliary buffers */
  125.         PFD_MAIN_PLANE,                 /* main layer */
  126.         0,                              /* reserved */
  127.         0, 0, 0,                        /* no layer, visible, damage masks */
  128.     };
  129.     int pixelFormat;
  130.  
  131.     pixelFormat = ChoosePixelFormat(m_hdc, &pfd);
  132.     if (pixelFormat == 0) {
  133.         MessageBox(WindowFromDC(m_hdc), "ChoosePixelFormat failed.", "Error",
  134.             MB_ICONERROR | MB_OK);
  135.         exit(1);
  136.     }
  137.  
  138.     if (SetPixelFormat(m_hdc, pixelFormat, &pfd) != TRUE) {
  139.         MessageBox(WindowFromDC(m_hdc), "SetPixelFormat failed.", "Error",
  140.             MB_ICONERROR | MB_OK);
  141.         exit(1);
  142.     }
  143. }
  144.  
  145. void COpenGLWindow::SetupPalette ()
  146. {
  147.     int pixelFormat = GetPixelFormat(m_hdc);
  148.     PIXELFORMATDESCRIPTOR pfd;
  149.     LOGPALETTE* pPal;
  150.     int paletteSize;
  151.  
  152.     DescribePixelFormat(m_hdc, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  153.  
  154.     if (pfd.dwFlags & PFD_NEED_PALETTE) {
  155.         paletteSize = 1 << pfd.cColorBits;
  156.     } else {
  157.         return;
  158.     }
  159.  
  160.     pPal = (LOGPALETTE*)
  161.         malloc(sizeof(LOGPALETTE) + paletteSize * sizeof(PALETTEENTRY));
  162.     pPal->palVersion = 0x300;
  163.     pPal->palNumEntries = paletteSize;
  164.  
  165.     /* build a simple RGB color palette */
  166.     {
  167.         int redMask = (1 << pfd.cRedBits) - 1;
  168.         int greenMask = (1 << pfd.cGreenBits) - 1;
  169.         int blueMask = (1 << pfd.cBlueBits) - 1;
  170.         int i;
  171.  
  172.         for (i=0; i<paletteSize; ++i)
  173.         {
  174.             pPal->palPalEntry[i].peRed =
  175.                 (((i >> pfd.cRedShift) & redMask) * 255) / redMask;
  176.             pPal->palPalEntry[i].peGreen =
  177.                 (((i >> pfd.cGreenShift) & greenMask) * 255) / greenMask;
  178.             pPal->palPalEntry[i].peBlue =
  179.                 (((i >> pfd.cBlueShift) & blueMask) * 255) / blueMask;
  180.             pPal->palPalEntry[i].peFlags = 0;
  181.         }
  182.     }
  183.  
  184.     m_hPalette = (HPALETTE)CreatePalette(pPal);
  185.     free(pPal);
  186.  
  187.     if (m_hPalette) {
  188.         SelectPalette(m_hdc, m_hPalette, FALSE);
  189.         RealizePalette(m_hdc);
  190.     }
  191. }
  192.