You are on page 1of 4

/**************************

* Includes
*
**************************/
#include <windows.h>
#include <gl/gl.h>

/**************************
* Function Declarations
*
**************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);

/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);
/* create main window */
hWnd = CreateWindow (
"GLSample", "OpenGL Sample",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 256, 256,
NULL, NULL, hInstance, NULL);
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);

0. 1. } } else { /* OpenGL animation code goes here */ glClearColor (0. glPopMatrix ().0f). 0. NULL.0f. 0.0f. -0.5f).0f. glColor3f (0. 0.0f). UINT message. 0.0f. /* program main loop */ while (!bQuit) { /* check for messages */ if (PeekMessage (&msg. } } /* shutdown OpenGL */ DisableOpenGL (hWnd. glVertex2f (0.0f. 1. glColor3f (0.wParam. -0. } /******************** * Window Procedure * ********************/ LRESULT CALLBACK WndProc (HWND hWnd.0f.0f. DispatchMessage (&msg). glEnd ().0f). LPARAM lParam) { . glVertex2f (0. 1. 0. glClear (GL_COLOR_BUFFER_BIT). glBegin (GL_TRIANGLES). 0. WPARAM wParam. glColor3f (1.0f). SwapBuffers (hDC).0f. /* destroy the window explicitly */ DestroyWindow (hWnd).0f). glRotatef (theta. return msg.0f. glVertex2f (-0. 1.0f.5f). 0. 0.0f.0f. glPushMatrix (). 0. hDC. hRC). PM_REMOVE)) { /* handle or dispatch messages */ if (msg.87f.message == WM_QUIT) { bQuit = TRUE.0f. } else { TranslateMessage (&msg).0f). theta += 1. 0. Sleep (1).87f.

switch (message) { case WM_CREATE: return 0. iFormat = ChoosePixelFormat (*hDC. pfd. pfd. *hRC ). } /****************** . &pfd). HDC *hDC. case WM_KEYDOWN: switch (wParam) { case VK_ESCAPE: PostQuitMessage(0). message. wParam. pfd.cColorBits = 24. SetPixelFormat (*hDC.nVersion = 1. default: return DefWindowProc (hWnd. case WM_CLOSE: PostQuitMessage (0). pfd.iLayerType = PFD_MAIN_PLANE. iFormat. /* set the pixel format for the DC */ ZeroMemory (&pfd. pfd. } return 0.cDepthBits = 16. case WM_DESTROY: return 0. HGLRC *hRC) { PIXELFORMATDESCRIPTOR pfd. &pfd). } } /******************* * Enable OpenGL * *******************/ void EnableOpenGL (HWND hWnd. return 0. int iFormat.nSize = sizeof (pfd). sizeof (pfd)). lParam). pfd. return 0. wglMakeCurrent( *hDC. /* create and enable the render context (RC) */ *hRC = wglCreateContext( *hDC ). /* get the device context (DC) */ *hDC = GetDC (hWnd).dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER. pfd.iPixelType = PFD_TYPE_RGBA.

} . NULL). HGLRC hRC) { wglMakeCurrent (NULL. hDC). ReleaseDC (hWnd. HDC hDC. * Disable OpenGL * ******************/ void DisableOpenGL (HWND hWnd. wglDeleteContext (hRC).