window.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. //todo macro
  45. context.imguiAllocator.init(malloc(pika::MB(20)), pika::MB(20));
  46. //HWND hwnd = glfwGetWin32Window(context.wind);
  47. //LONG exStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  48. //exStyle &= ~WS_EX_APPWINDOW;
  49. //exStyle |= WS_EX_TOOLWINDOW;
  50. //exStyle |= WS_EX_CONTEXTHELP;
  51. //exStyle &= ~WS_MAXIMIZEBOX;
  52. //exStyle &= ~WS_MINIMIZEBOX;
  53. //SetWindowLongPtr(hwnd, GWL_EXSTYLE, exStyle);
  54. //LONG style = GetWindowLongPtr(hwnd, GWL_STYLE);
  55. //style &= ~WS_MAXIMIZEBOX;
  56. //style &= ~WS_MINIMIZEBOX;
  57. //style &= ~WS_CAPTION;
  58. //style |= WS_DLGFRAME;
  59. //SetWindowLongPtr(hwnd, GWL_STYLE, style);
  60. timer = std::chrono::high_resolution_clock::now();
  61. }
  62. void pika::PikaWindow::saveWindowPositions()
  63. {
  64. #if PIKA_DEVELOPMENT
  65. WindowRect wr = {};
  66. glfwGetWindowPos(context.wind, &wr.x, &wr.y);
  67. wr.z = windowState.w;
  68. wr.w = windowState.h;
  69. sfs::safeSave(&wr, sizeof(wr), PIKA_ENGINE_SAVES_PATH "windowPos", false);
  70. #endif
  71. }
  72. bool pika::PikaWindow::shouldClose()
  73. {
  74. return glfwWindowShouldClose(context.wind);
  75. }
  76. void pika::PikaWindow::update()
  77. {
  78. #pragma region deltaTime
  79. auto end = std::chrono::high_resolution_clock::now();
  80. input.deltaTime = (std::chrono::duration_cast<std::chrono::microseconds>(end - timer)).count() / 1000000.0f;
  81. timer = end;
  82. if (input.deltaTime > 1.f / 10) { input.deltaTime = 1.f / 10; }
  83. #pragma endregion
  84. #pragma region input
  85. auto processInputBefore = [](pika::Button &b)
  86. {
  87. b.setTyped(false);
  88. };
  89. processInputBefore(input.lMouse);
  90. processInputBefore(input.rMouse);
  91. for (int i = 0; i < Button::BUTTONS_COUNT; i++)
  92. {
  93. processInputBefore(input.buttons[i]);
  94. }
  95. memset(input.typedInput, 0, sizeof(input.typedInput));
  96. #pragma endregion
  97. glfwPollEvents();
  98. glfwSwapBuffers(context.wind);
  99. #pragma region window state
  100. {
  101. int w = 0;
  102. int h = 0;
  103. glfwGetWindowSize(context.wind, &w, &h);
  104. windowState.w = w;
  105. windowState.h = h;
  106. }
  107. #pragma endregion
  108. #pragma region input
  109. double mouseX = 0;
  110. double mouseY = 0;
  111. glfwGetCursorPos(context.wind, &mouseX, &mouseY);
  112. input.mouseX = (int)mouseX;
  113. input.mouseY = (int)mouseY;
  114. auto processInput = [](pika::Button &b)
  115. {
  116. if (!b.lastState() && b.held())
  117. {
  118. b.setPressed(true);
  119. b.setTyped(true);
  120. }
  121. else
  122. {
  123. b.setPressed(false);
  124. }
  125. if (b.lastState() && !b.held())
  126. {
  127. b.setReleased(true);
  128. }
  129. else
  130. {
  131. b.setReleased(false);
  132. }
  133. b.setLastState(b.held());
  134. };
  135. processInput(input.lMouse);
  136. processInput(input.rMouse);
  137. for (int i = 0; i < Button::BUTTONS_COUNT; i++)
  138. {
  139. processInput(input.buttons[i]);
  140. }
  141. #pragma endregion
  142. }