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.

115 lines
2.9 KiB

4 years ago
  1. #include "ggl.h"
  2. #include "scene.h"
  3. #pragma comment(lib,"opengl32.lib")
  4. #pragma comment(lib, "glew32.lib")
  5. #pragma comment(lib, "winmm.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. float GetFrameTime() {
  25. static unsigned long lastTime = 0, timeSinceComputerStart = 0;
  26. timeSinceComputerStart = timeGetTime();
  27. unsigned long frameTime = lastTime == 0 ? 0 : timeSinceComputerStart - lastTime;
  28. lastTime = timeSinceComputerStart;
  29. return float(frameTime) / 1000.0f;
  30. }
  31. LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  32. {
  33. switch (msg)
  34. {
  35. case WM_CLOSE:
  36. PostQuitMessage(0);
  37. return 0;
  38. }
  39. return DefWindowProc(hwnd, msg, wParam, lParam);
  40. }
  41. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  42. {
  43. WNDCLASSEX wndclass;
  44. wndclass.cbClsExtra = 0;
  45. wndclass.cbSize = sizeof(WNDCLASSEX);
  46. wndclass.cbWndExtra = 0;
  47. wndclass.hbrBackground = NULL;
  48. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  49. wndclass.hIcon = NULL;
  50. wndclass.hIconSm = NULL;
  51. wndclass.hInstance = hInstance;
  52. wndclass.lpfnWndProc = GLWindowProc;
  53. wndclass.lpszClassName = L"GLWindow";
  54. wndclass.lpszMenuName = NULL;
  55. wndclass.style = CS_VREDRAW | CS_HREDRAW;
  56. ATOM atom = RegisterClassEx(&wndclass);
  57. if (!atom) {
  58. MessageBox(NULL, L"Register Fail", L"Error", MB_OK);
  59. return 0;
  60. }
  61. RECT rect;
  62. rect.left = 0;
  63. rect.right = 800;
  64. rect.top = 0;
  65. rect.bottom = 600;
  66. AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, NULL);
  67. int windowWidth = rect.right - rect.left;
  68. int windowHeight = rect.bottom - rect.top;
  69. HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW,
  70. 100, 100, windowWidth, windowHeight,
  71. NULL, NULL, hInstance, NULL);
  72. HDC dc = GetDC(hwnd);
  73. PIXELFORMATDESCRIPTOR pfd;
  74. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  75. pfd.nVersion = 1;
  76. pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  77. pfd.cColorBits = 32;
  78. pfd.cDepthBits = 24;
  79. pfd.cStencilBits = 8;
  80. pfd.iPixelType = PFD_TYPE_RGBA;
  81. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  82. int pixelFormat = ChoosePixelFormat(dc, &pfd);
  83. SetPixelFormat(dc, pixelFormat, &pfd);
  84. HGLRC rc = wglCreateContext(dc);
  85. wglMakeCurrent(dc, rc);
  86. glewInit();
  87. Init();
  88. SetViewPortSize(800.0f, 600.0f);
  89. ShowWindow(hwnd, SW_SHOW);
  90. UpdateWindow(hwnd);
  91. MSG msg;
  92. while (true)
  93. {
  94. if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  95. {
  96. if (msg.message == WM_QUIT)
  97. {
  98. break;
  99. }
  100. TranslateMessage(&msg);
  101. DispatchMessage(&msg);
  102. }
  103. Draw();
  104. SwapBuffers(dc);
  105. }
  106. return 0;
  107. }