代码
#include <iostream>
#include <windows.h>
#include <GL/gl.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
WNDCLASSEX wc;
memset(&wc, 0, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hInstance = hInstance;
wc.lpfnWndProc = DefWindowProc;
wc.lpszClassName = "WindowClass";
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
RegisterClassEx(&wc);
HWND window = CreateWindowEx(WS_EX_WINDOWEDGE, "WindowClass", "OpenGL", WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 640, nullptr, nullptr, hInstance, nullptr);
HDC hdc = GetDC(window);
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.cColorBits = 32;
pfd.cDepthBits = 8;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.iPixelType = PFD_TYPE_RGBA;
int format = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, format, &pfd);
HGLRC hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
bool quit = false;
MSG msg;
while (!quit)
{
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
quit = true;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor4f(1, 0, 0, 1);
glVertex2f(0, 0);
glVertex2f(-1, -1);
glVertex2f(0, 1);
glColor4f(0, 1, 0, 1);
glVertex2f(0, 0);
glVertex2f(1, -1);
glVertex2f(0, 1);
glColor4f(0, 0, 1, 1);
glVertex2f(0, 0);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glEnd();
SwapBuffers(hdc);
}
}
wglMakeCurrent(hdc, hrc);
wglDeleteContext(hrc);
ReleaseDC(window, hdc);
DestroyWindow(window);
return msg.wParam;
}
运行结果
|