You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.1 KiB

  1. #include <windows.h>
  2. #include <gl/GL.h>
  3. #pragma comment(lib, "opengl32.lib")
  4. /**
  5. *Ϣ
  6. */
  7. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  8. {
  9. switch (msg)
  10. {
  11. case WM_CLOSE:
  12. PostQuitMessage(0);
  13. return 0;
  14. }
  15. return DefWindowProc(hwnd, msg, wParam, lParam);
  16. }
  17. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  18. {
  19. /*ע�ᴰ��*/
  20. WNDCLASSEX wndclass;
  21. wndclass.cbClsExtra = 0;
  22. wndclass.cbSize = sizeof(WNDCLASSEX);
  23. wndclass.cbWndExtra = 0;
  24. wndclass.hbrBackground = NULL;
  25. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  26. wndclass.hIcon = NULL;
  27. wndclass.hIconSm = NULL;
  28. wndclass.hInstance = hInstance;
  29. wndclass.lpfnWndProc = GLWindowProc;
  30. wndclass.lpszClassName = L"GLWindow";
  31. wndclass.lpszMenuName = NULL;
  32. wndclass.style = CS_VREDRAW | CS_HREDRAW;
  33. ATOM atom = RegisterClassEx(&wndclass);
  34. if (!atom) {
  35. MessageBox(NULL, L"Register Fail", L"Error", MB_OK);
  36. return 0;
  37. }
  38. /*��������*/
  39. RECT rect;
  40. rect.left = 0;
  41. rect.right = 800;
  42. rect.top = 0;
  43. rect.bottom = 600;
  44. AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, NULL);
  45. int windowWidth = rect.right - rect.left;
  46. int windowHeight = rect.bottom - rect.top;
  47. HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW,
  48. 100, 100, windowWidth, windowHeight,
  49. NULL, NULL, hInstance, NULL);
  50. /*ѡ�����ظ�ʽ*/
  51. HDC dc = GetDC(hwnd);
  52. PIXELFORMATDESCRIPTOR pfd;
  53. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  54. pfd.nVersion = 1;
  55. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  56. pfd.cColorBits = 32;
  57. pfd.cDepthBits = 24;
  58. pfd.cStencilBits = 8;
  59. pfd.iPixelType = PFD_TYPE_RGBA;
  60. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  61. int pixelFormat = ChoosePixelFormat(dc, &pfd);
  62. SetPixelFormat(dc, pixelFormat, &pfd);
  63. /*����OpenGL��Ⱦ����*/
  64. HGLRC rc = wglCreateContext(dc);
  65. wglMakeCurrent(dc, rc);
  66. ShowWindow(hwnd, SW_SHOW);
  67. UpdateWindow(hwnd);
  68. MSG msg;
  69. while (true)
  70. {
  71. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  72. {
  73. if (msg.message == WM_QUIT)
  74. {
  75. break;
  76. }
  77. TranslateMessage(&msg);
  78. DispatchMessage(&msg);
  79. }
  80. }
  81. return 0;
  82. }