window.cpp 3.6 KB

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