#include <windows.h> #include<GL/glut.h>
void drawScene(HDC* hdc); void EnableOpenGL(HWND hWnd, HDC* hdc, HGLRC* hrc); void DisableOpenGL(HWND hWnd, HDC hdc, HGLRC hrc);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
char lpszClassName[] = "Windows"; char lpszTitle[] = "Win32 Window"; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = NULL; wndclass.lpszMenuName = NULL; wndclass.lpszClassName = lpszClassName; if (!RegisterClass(&wndclass)) { MessageBeep(0); return FALSE; } HWND hwnd = CreateWindow( lpszClassName, lpszTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); HGLRC hRC; HDC hDC; EnableOpenGL(hwnd, &hDC, &hRC); MSG msg = { 0 }; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); drawScene(&hDC); }
DisableOpenGL(hwnd, hDC, hRC); return 0; }
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { RECT rcClient;
switch (message) { case WM_CREATE: { return 0; } break; case WM_DESTROY: { PostQuitMessage(0); } break; case WM_SIZE: { GetClientRect(hwnd, &rcClient); int w = rcClient.right - rcClient.left; int h = rcClient.bottom - rcClient.top; glViewport(0, 0, w, h); } break; case WM_TIMER: { InvalidateRect(hwnd, NULL, FALSE); } break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return DefWindowProc(hwnd, message, wParam, lParam); }
void drawScene(HDC* hdc) {
glClearColor(0.3f, 0.3f, 0.3f, 0.0f); glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1.0f, 0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS); glVertex3f(-0.6f, -0.6f, 0.0f); glVertex3f(0.6f, -0.6f, 0.0f); glVertex3f(0.6f, 0.6f, 0.0f); glVertex3f(-0.6f, 0.6f, 0.0f); glEnd();
SwapBuffers(*hdc);
Sleep(1); }
void EnableOpenGL(HWND hwnd, HDC* hdc, HGLRC* hrc) {
*hdc = GetDC(hwnd); PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 }; int iPixelFormat = ChoosePixelFormat(*hdc, &pfd); SetPixelFormat(*hdc, iPixelFormat, &pfd);
*hrc = wglCreateContext(*hdc); wglMakeCurrent(*hdc, *hrc); }
void DisableOpenGL(HWND hwnd, HDC hdc, HGLRC hrc) { wglMakeCurrent(nullptr, nullptr); wglDeleteContext(hrc); ReleaseDC(hwnd, hdc); }
|