home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / bloodshed / devcpp-4.9.9.2_setup.exe / Templates / OpenGL.txt < prev    next >
Text File  |  2003-02-12  |  4KB  |  191 lines

  1. /**************************
  2.  * Includes
  3.  *
  4.  **************************/
  5.  
  6. #include <windows.h>
  7. #include <gl/gl.h>
  8.  
  9.  
  10. /**************************
  11.  * Function Declarations
  12.  *
  13.  **************************/
  14.  
  15. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  16. WPARAM wParam, LPARAM lParam);
  17. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
  18. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
  19.  
  20.  
  21. /**************************
  22.  * WinMain
  23.  *
  24.  **************************/
  25.  
  26. int WINAPI WinMain (HINSTANCE hInstance,
  27.                     HINSTANCE hPrevInstance,
  28.                     LPSTR lpCmdLine,
  29.                     int iCmdShow)
  30. {
  31.     WNDCLASS wc;
  32.     HWND hWnd;
  33.     HDC hDC;
  34.     HGLRC hRC;        
  35.     MSG msg;
  36.     BOOL bQuit = FALSE;
  37.     float theta = 0.0f;
  38.  
  39.     /* register window class */
  40.     wc.style = CS_OWNDC;
  41.     wc.lpfnWndProc = WndProc;
  42.     wc.cbClsExtra = 0;
  43.     wc.cbWndExtra = 0;
  44.     wc.hInstance = hInstance;
  45.     wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  46.     wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  47.     wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
  48.     wc.lpszMenuName = NULL;
  49.     wc.lpszClassName = "GLSample";
  50.     RegisterClass (&wc);
  51.  
  52.     /* create main window */
  53.     hWnd = CreateWindow (
  54.       "GLSample", "OpenGL Sample", 
  55.       WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
  56.       0, 0, 256, 256,
  57.       NULL, NULL, hInstance, NULL);
  58.  
  59.     /* enable OpenGL for the window */
  60.     EnableOpenGL (hWnd, &hDC, &hRC);
  61.  
  62.     /* program main loop */
  63.     while (!bQuit)
  64.     {
  65.         /* check for messages */
  66.         if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  67.         {
  68.             /* handle or dispatch messages */
  69.             if (msg.message == WM_QUIT)
  70.             {
  71.                 bQuit = TRUE;
  72.             }
  73.             else
  74.             {
  75.                 TranslateMessage (&msg);
  76.                 DispatchMessage (&msg);
  77.             }
  78.         }
  79.         else
  80.         {
  81.             /* OpenGL animation code goes here */
  82.  
  83.             glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
  84.             glClear (GL_COLOR_BUFFER_BIT);
  85.  
  86.             glPushMatrix ();
  87.             glRotatef (theta, 0.0f, 0.0f, 1.0f);
  88.             glBegin (GL_TRIANGLES);
  89.             glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
  90.             glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.87f, -0.5f);
  91.             glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.87f, -0.5f);
  92.             glEnd ();
  93.             glPopMatrix ();
  94.  
  95.             SwapBuffers (hDC);
  96.  
  97.             theta += 1.0f;
  98.             Sleep (1);
  99.         }
  100.     }
  101.  
  102.     /* shutdown OpenGL */
  103.     DisableOpenGL (hWnd, hDC, hRC);
  104.  
  105.     /* destroy the window explicitly */
  106.     DestroyWindow (hWnd);
  107.  
  108.     return msg.wParam;
  109. }
  110.  
  111.  
  112. /********************
  113.  * Window Procedure
  114.  *
  115.  ********************/
  116.  
  117. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  118.                           WPARAM wParam, LPARAM lParam)
  119. {
  120.  
  121.     switch (message)
  122.     {
  123.     case WM_CREATE:
  124.         return 0;
  125.     case WM_CLOSE:
  126.         PostQuitMessage (0);
  127.         return 0;
  128.  
  129.     case WM_DESTROY:
  130.         return 0;
  131.  
  132.     case WM_KEYDOWN:
  133.         switch (wParam)
  134.         {
  135.         case VK_ESCAPE:
  136.             PostQuitMessage(0);
  137.             return 0;
  138.         }
  139.         return 0;
  140.  
  141.     default:
  142.         return DefWindowProc (hWnd, message, wParam, lParam);
  143.     }
  144. }
  145.  
  146.  
  147. /*******************
  148.  * Enable OpenGL
  149.  *
  150.  *******************/
  151.  
  152. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
  153. {
  154.     PIXELFORMATDESCRIPTOR pfd;
  155.     int iFormat;
  156.  
  157.     /* get the device context (DC) */
  158.     *hDC = GetDC (hWnd);
  159.  
  160.     /* set the pixel format for the DC */
  161.     ZeroMemory (&pfd, sizeof (pfd));
  162.     pfd.nSize = sizeof (pfd);
  163.     pfd.nVersion = 1;
  164.     pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
  165.       PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  166.     pfd.iPixelType = PFD_TYPE_RGBA;
  167.     pfd.cColorBits = 24;
  168.     pfd.cDepthBits = 16;
  169.     pfd.iLayerType = PFD_MAIN_PLANE;
  170.     iFormat = ChoosePixelFormat (*hDC, &pfd);
  171.     SetPixelFormat (*hDC, iFormat, &pfd);
  172.  
  173.     /* create and enable the render context (RC) */
  174.     *hRC = wglCreateContext( *hDC );
  175.     wglMakeCurrent( *hDC, *hRC );
  176.  
  177. }
  178.  
  179.  
  180. /******************
  181.  * Disable OpenGL
  182.  *
  183.  ******************/
  184.  
  185. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
  186. {
  187.     wglMakeCurrent (NULL, NULL);
  188.     wglDeleteContext (hRC);
  189.     ReleaseDC (hWnd, hDC);
  190. }
  191.