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.

120 lines
2.7 KiB

  1. #include "ggl.h"
  2. #include "scene.h"
  3. #include "utils.h"
  4. #pragma comment(lib, "opengl32.lib")
  5. #pragma comment(lib, "glew32.lib")
  6. unsigned char * LoadFileContent(const char *path, int &filesize) {
  7. unsigned char*fileContent = nullptr;
  8. filesize = 0;
  9. FILE*pFile = fopen(path, "rb");
  10. if (pFile) {
  11. fseek(pFile, 0, SEEK_END);
  12. int nLen = ftell(pFile);
  13. if (nLen > 0) {
  14. rewind(pFile);
  15. fileContent = new unsigned char[nLen + 1];
  16. fread(fileContent, sizeof(unsigned char), nLen, pFile);
  17. fileContent[nLen] = '\0';
  18. filesize = nLen;
  19. }
  20. fclose(pFile);
  21. }
  22. return fileContent;
  23. }
  24. /**
  25. *Ϣ
  26. */
  27. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  28. {
  29. switch (msg)
  30. {
  31. case WM_CLOSE:
  32. PostQuitMessage(0);
  33. return 0;
  34. }
  35. return DefWindowProc(hwnd, msg, wParam, lParam);
  36. }
  37. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  38. {
  39. /*ע�ᴰ��*/
  40. WNDCLASSEX wndclass;
  41. wndclass.cbClsExtra = 0;
  42. wndclass.cbSize = sizeof(WNDCLASSEX);
  43. wndclass.cbWndExtra = 0;
  44. wndclass.hbrBackground = NULL;
  45. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  46. wndclass.hIcon = NULL;
  47. wndclass.hIconSm = NULL;
  48. wndclass.hInstance = hInstance;
  49. wndclass.lpfnWndProc = GLWindowProc;
  50. wndclass.lpszClassName = L"GLWindow";
  51. wndclass.lpszMenuName = NULL;
  52. wndclass.style = CS_VREDRAW | CS_HREDRAW;
  53. ATOM atom = RegisterClassEx(&wndclass);
  54. if (!atom) {
  55. MessageBox(NULL, L"Register Fail", L"Error", MB_OK);
  56. return 0;
  57. }
  58. /*��������*/
  59. RECT rect;
  60. rect.left = 0;
  61. rect.right = 800;
  62. rect.top = 0;
  63. rect.bottom = 600;
  64. AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, NULL);
  65. int windowWidth = rect.right - rect.left;
  66. int windowHeight = rect.bottom - rect.top;
  67. HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW,
  68. 100, 100, windowWidth, windowHeight,
  69. NULL, NULL, hInstance, NULL);
  70. /*ѡ�����ظ�ʽ*/
  71. HDC dc = GetDC(hwnd);
  72. PIXELFORMATDESCRIPTOR pfd;
  73. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  74. pfd.nVersion = 1;
  75. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  76. pfd.cColorBits = 32;
  77. pfd.cDepthBits = 24;
  78. pfd.cStencilBits = 8;
  79. pfd.iPixelType = PFD_TYPE_RGBA;
  80. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  81. int pixelFormat = ChoosePixelFormat(dc, &pfd);
  82. SetPixelFormat(dc, pixelFormat, &pfd);
  83. /*����OpenGL��Ⱦ����*/
  84. HGLRC rc = wglCreateContext(dc);
  85. wglMakeCurrent(dc, rc);
  86. /*����OpenGL�߼�api*/
  87. glewInit();
  88. /*��ʼ������*/
  89. Init();
  90. SetViewPortSize(800.0f, 600.0f);
  91. ShowWindow(hwnd, SW_SHOW);
  92. UpdateWindow(hwnd);
  93. MSG msg;
  94. while (true)
  95. {
  96. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  97. {
  98. if (msg.message == WM_QUIT)
  99. {
  100. break;
  101. }
  102. TranslateMessage(&msg);
  103. DispatchMessage(&msg);
  104. }
  105. Draw();
  106. SwapBuffers(dc);
  107. }
  108. return 0;
  109. }