Na początku zadeklarujmy zmienne których użyjemy:
1 2 3 4 5 |
static HDC hDC; static HGLRC hRC; static HWND hstat; |
W kodzie dla zdarzenia WM_CREATE należy wpisać:
1 2 3 |
hstat=CreateWindow("static","",WS_CHILD|WS_VISIBLE, 10,10,200,200,hWnd,(HMENU)666,GetModuleHandle(NULL),NULL); |
by stworzyć obiekt.
Potem normalnie inicjujemy OpenGL’a, tyle, że kontekst graficzny przypisujemy dla hstat, o taki kod był w szablonie, tylko dół dodałem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC) { PIXELFORMATDESCRIPTOR pfd; int iFormat; /* get the device context (DC) */ *hDC = GetDC (hWnd); /* set the pixel format for the DC */ ZeroMemory (&pfd, sizeof (pfd)); pfd.nSize = sizeof (pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 16; pfd.iLayerType = PFD_MAIN_PLANE; iFormat = ChoosePixelFormat (*hDC, &pfd); SetPixelFormat (*hDC, iFormat, &pfd); /* create and enable the render context (RC) */ *hRC = wglCreateContext( *hDC ); wglMakeCurrent( *hDC, *hRC ); //tutaj może być dowolne ustawienie rzutowania, ale wybrałem takie glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,200,200,0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } |
Użyjemy tej funkcji z procedurze okienkowej po pokazaniu się okna, czyli po funkcji ShowWindow w taki sposób:
1 |
EnableOpenGL (hstat, &hDC, &hRC); |
Przed zakończeniem programu (w WM_DESTROY przed funkcją PostQuitMessage(0)) użyjemy takiej funkcji:
1 2 3 4 5 6 7 8 9 10 11 |
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC) { wglMakeCurrent (NULL, NULL); wglDeleteContext (hRC); ReleaseDC (hWnd, hDC); } |
No i jeszcze ostatecznie coś narysujmy na naszym staticu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
int DrawGLScene(GLvoid) { glLoadIdentity(); glClearColor (0.0f, 0.0f, 0.0f, 0.0f); glClear (GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glColor3f(0.2,0.8,0.4);glVertex2f(15,15); glColor3f(0.8,0.2,1);glVertex2f(90,15); glColor3f(0.9,0.1,1);glVertex2f(90,100); glColor3f(0.4,1,0.9);glVertex2f(15,100); glEnd(); } |
Użyjemy to w ten sposób:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
while( msg.message!=WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { DrawGLScene(); SwapBuffers(hDC); } } |
W razie watpliwości, przejrzyj kod przykładowy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
#include #include #include /* Declare Windows procedure */ HWND hwnd; LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); int DrawGLScene(GLvoid); void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC); void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC); static HDC hDC; static HGLRC hRC; static HWND hstat; /* Make the class name into a global variable */ char szClassName[ ] = "CodeBlocksWindowsApp"; int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { HWND hWnd; /* This is the handle for our window */ MSG msg; /* Here messages to the application are saved */ WNDCLASSEX wincl; /* Data structure for the windowclass */ /* The Window structure */ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ wincl.style = CS_DBLCLKS; /* Catch double-clicks */ wincl.cbSize = sizeof (WNDCLASSEX); /* Use default icon and mouse-pointer */ wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; /* No menu */ wincl.cbClsExtra = 0; /* No extra bytes after the window class */ wincl.cbWndExtra = 0; /* structure or the window instance */ /* Use Windows's default color as the background of the window */ wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; /* Register the window class, and if it fails quit the program */ if (!RegisterClassEx (&wincl)) return 0; /* The class is registered, let's create the program*/ hWnd = CreateWindowEx ( 0, /* Extended possibilites for variation */ szClassName, /* Classname */ "Code::Blocks Template Windows App", /* Title Text */ WS_OVERLAPPEDWINDOW, /* default window */ CW_USEDEFAULT, /* Windows decides the position */ CW_USEDEFAULT, /* where the window ends up on the screen */ 544, /* The programs width */ 375, /* and height in pixels */ HWND_DESKTOP, /* The window is a child-window to desktop */ NULL, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); /* Make the window visible on the screen */ ShowWindow (hWnd, nFunsterStil); hwnd=hWnd; EnableOpenGL (hstat, &hDC, &hRC); /* Run the message loop. It will run until GetMessage() returns 0 */ while( msg.message!=WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { DrawGLScene(); SwapBuffers(hDC); } } /* The program return-value is 0 - The value that PostQuitMessage() gave */ return msg.wParam; } /* This function is called by the Windows function DispatchMessage() */ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) /* handle the messages */ { case WM_CREATE: hstat=CreateWindow("static","",WS_CHILD|WS_VISIBLE, 10,10,200,200,hwnd,(HMENU)666,GetModuleHandle(NULL),NULL); break; case WM_DESTROY: DisableOpenGL(hstat,hDC,hRC); PostQuitMessage (0); /* send a WM_QUIT to the message queue */ break; default: /* for messages that we don't deal with */ return DefWindowProc (hwnd, message, wParam, lParam); } return 0; } int DrawGLScene(GLvoid) { glLoadIdentity(); glClearColor (0.0f, 0.0f, 0.0f, 0.0f); glClear (GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glColor3f(0.2,0.8,0.4);glVertex2f(15,15); glColor3f(0.8,0.2,1);glVertex2f(90,15); glColor3f(0.9,0.1,1);glVertex2f(90,100); glColor3f(0.4,1,0.9);glVertex2f(15,100); glEnd(); } void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC) { PIXELFORMATDESCRIPTOR pfd; int iFormat; /* get the device context (DC) */ *hDC = GetDC (hWnd); /* set the pixel format for the DC */ ZeroMemory (&pfd, sizeof (pfd)); pfd.nSize = sizeof (pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 16; pfd.iLayerType = PFD_MAIN_PLANE; iFormat = ChoosePixelFormat (*hDC, &pfd); SetPixelFormat (*hDC, iFormat, &pfd); /* create and enable the render context (RC) */ *hRC = wglCreateContext( *hDC ); wglMakeCurrent( *hDC, *hRC ); //tutaj może być dowolne ustawienie rzutowania, ale wybrałem takie glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,200,200,0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC) { wglMakeCurrent (NULL, NULL); wglDeleteContext (hRC); ReleaseDC (hWnd, hDC); } |
należy nie zapomnieć o podlinkowaniu libów opengl32 i glu32.
Autor: Spine