Advertisement
Guest User

eLL

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