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.

103 lines
3.0 KiB

4 years ago
  1. #include <windows.h>
  2. #include <gl/GL.h>
  3. #include <stdio.h>
  4. #pragma comment(lib,"opengl32.lib")
  5. /**
  6. * @hwnd ϢĴ
  7. * @msg Ϣ
  8. */
  9. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  10. switch (msg) {
  11. case WM_CLOSE:
  12. PostQuitMessage(0);
  13. return 0;
  14. }
  15. return DefWindowProc(hwnd, msg, wParam, lParam);//������Ϣ����windowĬ�ϴ�������
  16. }
  17. /**
  18. * @hinstance Ӧóʵ
  19. * @hPrevInstance һӦóʽʵ
  20. * @IpCmdLine еIJ
  21. * @ShowCmd ôʾ
  22. */
  23. INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  24. {
  25. /*ע�ᴰ��*/
  26. WNDCLASSEX wndclass;
  27. wndclass.cbClsExtra = 0; //�������͵Ķ����ռ䣬���ﲻ��Ҫ
  28. wndclass.cbSize = sizeof(WNDCLASSEX); //����ʵ��ռ�õ��ڴ�
  29. wndclass.cbWndExtra = 0; //���ڵĶ����ռ䣬���ﲻ��Ҫ
  30. wndclass.hbrBackground = NULL; //���ڱ���
  31. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //������������
  32. wndclass.hIcon = NULL; //Ӧ�ó���exe�ļ���ʾͼ��
  33. wndclass.hIconSm = NULL; //Ӧ�ó�������ʱ���Ͻ�ͼ��
  34. wndclass.hInstance = hinstance; //��Ӧ�ó���ʵ��
  35. wndclass.lpfnWndProc = GLWindowProc; //�����û������˴��ڣ��������ᱻ����
  36. wndclass.lpszClassName = L"GLWindow";//��������
  37. wndclass.lpszMenuName = NULL;//�˵�����
  38. wndclass.style = CS_VREDRAW | CS_HREDRAW;//���ڸ���ʱ���ػ淽ʽ������ʹ�ô�ֱ�ػ���ˮƽ�ػ�
  39. ATOM atom = RegisterClassEx(&wndclass);
  40. if (!atom) {
  41. MessageBox(NULL, L"Register failed", L"Error", MB_OK);
  42. return 0;
  43. }
  44. /*��������*/
  45. //�������ڴ�С
  46. RECT rect;
  47. rect.left = 0;
  48. rect.right = 800;
  49. rect.top = 0;
  50. rect.bottom = 600;
  51. AdjustWindowRect(&rect, WS_EX_OVERLAPPEDWINDOW, NULL);
  52. int windowWidth = rect.right - rect.left;
  53. int windowHeight = rect.bottom - rect.top;
  54. //��������һ��Ҫ�͸ղ�ע�ᴰ�ڵı���һ��
  55. HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
  56. //������Ⱦ����
  57. HDC dc = GetDC(hwnd);//��ȡ�豸������
  58. PIXELFORMATDESCRIPTOR pfd;
  59. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  60. pfd.nVersion = 1;
  61. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  62. pfd.cColorBits = 32; //��ɫ������ÿ������Ϊ32����4ͨ��RGBA
  63. pfd.cDepthBits = 24; //���Ȼ�����ÿ�����ش�С��24���ر�ʾһ��������
  64. pfd.cStencilBits = 8; //�ɰ建����ÿ����Ϊ8����
  65. pfd.iPixelType = PFD_TYPE_RGBA; //������������ΪRGBA
  66. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; //��ʾ������������������
  67. //�������ظ�ʽ
  68. int pixelFormat = ChoosePixelFormat(dc, &pfd);
  69. SetPixelFormat(dc, pixelFormat, &pfd);
  70. //����OpenGL��Ⱦ����
  71. HGLRC rc = wglCreateContext(dc);
  72. wglMakeCurrent(dc, rc);//����OpenGL��Ⱦ������Ч
  73. glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
  74. /*��ʾ����*/
  75. ShowWindow(hwnd, SW_SHOW);
  76. UpdateWindow(hwnd);
  77. /*�����������û�����*/
  78. MSG msg;
  79. while (true) {
  80. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
  81. if (msg.message == WM_QUIT) {
  82. break;
  83. }
  84. TranslateMessage(&msg);
  85. DispatchMessage(&msg);
  86. }
  87. glClear(GL_COLOR_BUFFER_BIT);
  88. SwapBuffers(dc);
  89. }
  90. return 0;
  91. }