1 | //
|
---|
2 | // GLSAMPLE.CPP
|
---|
3 | // by Blaine Hodge
|
---|
4 | //
|
---|
5 |
|
---|
6 | // Includes
|
---|
7 |
|
---|
8 | #include <windows.h>
|
---|
9 | #include <gl/gl.h>
|
---|
10 | #include <stdio.h>
|
---|
11 |
|
---|
12 | // Function Declarations
|
---|
13 |
|
---|
14 | LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
|
---|
15 | void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC);
|
---|
16 | void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);
|
---|
17 |
|
---|
18 | // WinMain
|
---|
19 |
|
---|
20 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
---|
21 | LPSTR lpCmdLine, int iCmdShow)
|
---|
22 | {
|
---|
23 | WNDCLASS wc;
|
---|
24 | HWND hWnd;
|
---|
25 | HDC hDC;
|
---|
26 | HGLRC hRC;
|
---|
27 | MSG msg;
|
---|
28 | BOOL quit = FALSE;
|
---|
29 | float theta = 0.0f;
|
---|
30 |
|
---|
31 | // register window class
|
---|
32 | wc.style = CS_OWNDC;
|
---|
33 | wc.lpfnWndProc = WndProc;
|
---|
34 | wc.cbClsExtra = 0;
|
---|
35 | wc.cbWndExtra = 0;
|
---|
36 | wc.hInstance = hInstance;
|
---|
37 | wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
|
---|
38 | wc.hCursor = LoadCursor( NULL, IDC_ARROW );
|
---|
39 | wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
|
---|
40 | wc.lpszMenuName = NULL;
|
---|
41 | wc.lpszClassName = "GLSample";
|
---|
42 | RegisterClass( &wc );
|
---|
43 |
|
---|
44 | // create main window
|
---|
45 | hWnd = CreateWindow(
|
---|
46 | "GLSample", "OpenGL Sample",
|
---|
47 | WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
|
---|
48 | 0, 0, 300, 300,
|
---|
49 | NULL, NULL, hInstance, NULL );
|
---|
50 |
|
---|
51 | // enable OpenGL for the window
|
---|
52 | EnableOpenGL( hWnd, &hDC, &hRC );
|
---|
53 |
|
---|
54 | // program main loop
|
---|
55 | while ( !quit )
|
---|
56 | {
|
---|
57 |
|
---|
58 | // check for messages
|
---|
59 | if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
|
---|
60 | {
|
---|
61 |
|
---|
62 | // handle or dispatch messages
|
---|
63 | if ( msg.message == WM_QUIT )
|
---|
64 | {
|
---|
65 | quit = TRUE;
|
---|
66 | }
|
---|
67 | else
|
---|
68 | {
|
---|
69 | TranslateMessage( &msg );
|
---|
70 | DispatchMessage( &msg );
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|
74 | else
|
---|
75 | {
|
---|
76 |
|
---|
77 | // OpenGL animation code goes here
|
---|
78 |
|
---|
79 | glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
|
---|
80 | glClear( GL_COLOR_BUFFER_BIT );
|
---|
81 |
|
---|
82 | glPushMatrix();
|
---|
83 | glRotatef( theta, 0.0f, 0.0f, 1.0f );
|
---|
84 | glBegin( GL_TRIANGLES );
|
---|
85 | glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );
|
---|
86 | glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 0.87f, -0.5f );
|
---|
87 | glColor3f( 0.0f, 0.0f, 1.0f ); glVertex2f( -0.87f, -0.5f );
|
---|
88 | glEnd();
|
---|
89 | glPopMatrix();
|
---|
90 |
|
---|
91 | SwapBuffers( hDC );
|
---|
92 |
|
---|
93 | theta += 1.0f;
|
---|
94 |
|
---|
95 | }
|
---|
96 |
|
---|
97 | }
|
---|
98 |
|
---|
99 | printf("GL_VENDOR %s\n", glGetString(GL_VENDOR));
|
---|
100 | printf("GL_RENDERER %s\n", glGetString(GL_RENDERER));
|
---|
101 | printf("GL_VERSION %s\n", glGetString(GL_VERSION));
|
---|
102 | printf("GL_EXTENSIONS %s\n", glGetString(GL_EXTENSIONS));
|
---|
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 |
|
---|
114 | // Window Procedure
|
---|
115 |
|
---|
116 | LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
|
---|
117 | {
|
---|
118 |
|
---|
119 | switch (message)
|
---|
120 | {
|
---|
121 |
|
---|
122 | case WM_CREATE:
|
---|
123 | return 0;
|
---|
124 |
|
---|
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 |
|
---|
136 | case VK_ESCAPE:
|
---|
137 | PostQuitMessage(0);
|
---|
138 | return 0;
|
---|
139 |
|
---|
140 | }
|
---|
141 | return 0;
|
---|
142 |
|
---|
143 | default:
|
---|
144 | return DefWindowProc( hWnd, message, wParam, lParam );
|
---|
145 |
|
---|
146 | }
|
---|
147 |
|
---|
148 | }
|
---|
149 |
|
---|
150 | // Enable OpenGL
|
---|
151 |
|
---|
152 | void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
|
---|
153 | {
|
---|
154 | PIXELFORMATDESCRIPTOR pfd;
|
---|
155 | int format;
|
---|
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 | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
---|
165 | pfd.iPixelType = PFD_TYPE_RGBA;
|
---|
166 | pfd.cColorBits = 24;
|
---|
167 | pfd.cDepthBits = 16;
|
---|
168 | pfd.iLayerType = PFD_MAIN_PLANE;
|
---|
169 | format = ChoosePixelFormat( *hDC, &pfd );
|
---|
170 | SetPixelFormat( *hDC, format, &pfd );
|
---|
171 |
|
---|
172 | // create and enable the render context (RC)
|
---|
173 | *hRC = wglCreateContext( *hDC );
|
---|
174 | wglMakeCurrent( *hDC, *hRC );
|
---|
175 |
|
---|
176 | }
|
---|
177 |
|
---|
178 | // Disable OpenGL
|
---|
179 |
|
---|
180 | void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
|
---|
181 | {
|
---|
182 | wglMakeCurrent( NULL, NULL );
|
---|
183 | wglDeleteContext( hRC );
|
---|
184 | ReleaseDC( hWnd, hDC );
|
---|
185 | }
|
---|