window.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "window.h"
  2. #include <logs/assert.h>
  3. #include "callbacks.h"
  4. #ifdef PIKA_WINDOWS
  5. #define GLFW_EXPOSE_NATIVE_WIN32
  6. #include <Windows.h>
  7. #include <GLFW/glfw3native.h>
  8. #endif
  9. #include <pikaSizes.h>
  10. #include <safeSave/safeSave.h>
  11. struct WindowRect
  12. {
  13. int x = 100, y = 100, z = 640, w = 480;
  14. };
  15. void pika::PikaWindow::create()
  16. {
  17. WindowRect wr = {};
  18. #if PIKA_DEVELOPMENT
  19. if (sfs::safeLoad(&wr, sizeof(wr), PIKA_ENGINE_SAVES_PATH "windowPos", false) != sfs::noError)
  20. {
  21. wr = {};
  22. }
  23. #endif
  24. if (wr.x < 0 || wr.y < 0 || wr.z <= 0 || wr.w <= 0)
  25. {
  26. wr = {};
  27. }
  28. //todo debug from engine
  29. //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
  30. //glfwWindowHint(GLFW_SAMPLES, 1);
  31. //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  32. //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  33. //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  34. context.wind = glfwCreateWindow(wr.z, wr.w, "Pika", NULL, NULL);
  35. glfwSetWindowPos(context.wind, wr.x, wr.y);
  36. input.hasFocus = true;
  37. PIKA_PERMA_ASSERT(context.wind, "problem initializing window");
  38. glfwMakeContextCurrent(context.wind);
  39. glfwSetWindowUserPointer(context.wind, this);
  40. glfwSetMouseButtonCallback(context.wind, mouseCallback);
  41. glfwSetWindowFocusCallback(context.wind, windowFocusCallback);
  42. glfwSetCharCallback(context.wind, characterCallback);
  43. glfwSetKeyCallback(context.wind, keyCallback);
  44. #if PIKA_SHOULD_REMOVE_IMGUI == 0
  45. //todo macro
  46. context.imguiAllocator.init(malloc(pika::MB(20)), pika::MB(20));
  47. #endif
  48. //HWND hwnd = glfwGetWin32Window(context.wind);
  49. //LONG exStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  50. //exStyle &= ~WS_EX_APPWINDOW;
  51. //exStyle |= WS_EX_TOOLWINDOW;
  52. //exStyle |= WS_EX_CONTEXTHELP;
  53. //exStyle &= ~WS_MAXIMIZEBOX;
  54. //exStyle &= ~WS_MINIMIZEBOX;
  55. //SetWindowLongPtr(hwnd, GWL_EXSTYLE, exStyle);
  56. //LONG style = GetWindowLongPtr(hwnd, GWL_STYLE);
  57. //style &= ~WS_MAXIMIZEBOX;
  58. //style &= ~WS_MINIMIZEBOX;
  59. //style &= ~WS_CAPTION;
  60. //style |= WS_DLGFRAME;
  61. //SetWindowLongPtr(hwnd, GWL_STYLE, style);
  62. timer = std::chrono::high_resolution_clock::now();
  63. }
  64. void pika::PikaWindow::saveWindowPositions()
  65. {
  66. #if PIKA_DEVELOPMENT
  67. WindowRect wr = {};
  68. glfwGetWindowPos(context.wind, &wr.x, &wr.y);
  69. wr.z = windowState.windowW;
  70. wr.w = windowState.windowH;
  71. sfs::safeSave(&wr, sizeof(wr), PIKA_ENGINE_SAVES_PATH "windowPos", false);
  72. #endif
  73. }
  74. bool pika::PikaWindow::shouldClose()
  75. {
  76. return glfwWindowShouldClose(context.wind);
  77. }
  78. void pika::PikaWindow::update()
  79. {
  80. #pragma region deltaTime
  81. auto end = std::chrono::high_resolution_clock::now();
  82. input.deltaTime = (std::chrono::duration_cast<std::chrono::microseconds>(end - timer)).count() / 1000000.0f;
  83. timer = end;
  84. if (input.deltaTime > 1.f / 10) { input.deltaTime = 1.f / 10; }
  85. #pragma endregion
  86. #pragma region input
  87. auto processInputBefore = [](pika::Button &b)
  88. {
  89. b.setTyped(false);
  90. };
  91. processInputBefore(input.lMouse);
  92. processInputBefore(input.rMouse);
  93. for (int i = 0; i < Button::BUTTONS_COUNT; i++)
  94. {
  95. processInputBefore(input.buttons[i]);
  96. }
  97. memset(input.typedInput, 0, sizeof(input.typedInput));
  98. #pragma endregion
  99. glfwPollEvents();
  100. glfwSwapBuffers(context.wind);
  101. #pragma region window state
  102. {
  103. int w = 0;
  104. int h = 0;
  105. glfwGetWindowSize(context.wind, &w, &h);
  106. windowState.windowW = w;
  107. windowState.windowH = h;
  108. }
  109. #pragma endregion
  110. #pragma region input
  111. double mouseX = 0;
  112. double mouseY = 0;
  113. glfwGetCursorPos(context.wind, &mouseX, &mouseY);
  114. input.mouseX = (int)mouseX;
  115. input.mouseY = (int)mouseY;
  116. auto processInput = [](pika::Button &b)
  117. {
  118. if (!b.lastState() && b.held())
  119. {
  120. b.setPressed(true);
  121. b.setTyped(true);
  122. }
  123. else
  124. {
  125. b.setPressed(false);
  126. }
  127. if (b.lastState() && !b.held())
  128. {
  129. b.setReleased(true);
  130. }
  131. else
  132. {
  133. b.setReleased(false);
  134. }
  135. b.setLastState(b.held());
  136. };
  137. processInput(input.lMouse);
  138. processInput(input.rMouse);
  139. for (int i = 0; i < Button::BUTTONS_COUNT; i++)
  140. {
  141. processInput(input.buttons[i]);
  142. }
  143. #pragma endregion
  144. }