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