window.cpp 3.4 KB

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