window.cpp 3.5 KB

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