window.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. void pika::PikaWindow::create()
  10. {
  11. context.wind = glfwCreateWindow(640, 480, "Pika", NULL, NULL);
  12. windowState.hasFocus = true;
  13. PIKA_PERMA_ASSERT(context.wind, "problem initializing window");
  14. glfwMakeContextCurrent(context.wind);
  15. glfwSetWindowUserPointer(context.wind, this);
  16. glfwSetMouseButtonCallback(context.wind, mouseCallback);
  17. glfwSetWindowFocusCallback(context.wind, windowFocusCallback);
  18. glfwSetKeyCallback(context.wind, keyCallback);
  19. //HWND hwnd = glfwGetWin32Window(context.wind);
  20. //LONG exStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
  21. //exStyle &= ~WS_EX_APPWINDOW;
  22. //exStyle |= WS_EX_TOOLWINDOW;
  23. //exStyle |= WS_EX_CONTEXTHELP;
  24. //exStyle &= ~WS_MAXIMIZEBOX;
  25. //exStyle &= ~WS_MINIMIZEBOX;
  26. //SetWindowLongPtr(hwnd, GWL_EXSTYLE, exStyle);
  27. //LONG style = GetWindowLongPtr(hwnd, GWL_STYLE);
  28. //style &= ~WS_MAXIMIZEBOX;
  29. //style &= ~WS_MINIMIZEBOX;
  30. //style &= ~WS_CAPTION;
  31. //style |= WS_DLGFRAME;
  32. //SetWindowLongPtr(hwnd, GWL_STYLE, style);
  33. timer = std::chrono::high_resolution_clock::now();
  34. }
  35. bool pika::PikaWindow::shouldClose()
  36. {
  37. return glfwWindowShouldClose(context.wind);
  38. }
  39. void pika::PikaWindow::update()
  40. {
  41. #pragma region deltaTime
  42. auto end = std::chrono::high_resolution_clock::now();
  43. deltaTime = (std::chrono::duration_cast<std::chrono::microseconds>(end - timer)).count() / 1000000.0f;
  44. timer = end;
  45. if (deltaTime > 1.f / 10) { deltaTime = 1.f / 10; }
  46. #pragma endregion
  47. #pragma region input
  48. auto processInputBefore = [](pika::Button &b)
  49. {
  50. b.setTyped(false);
  51. };
  52. processInputBefore(input.lMouse);
  53. processInputBefore(input.rMouse);
  54. for (int i = 0; i < Button::BUTTONS_COUNT; i++)
  55. {
  56. processInputBefore(input.buttons[i]);
  57. }
  58. #pragma endregion
  59. glfwPollEvents();
  60. glfwSwapBuffers(context.wind);
  61. #pragma region window state
  62. {
  63. int w = 0;
  64. int h = 0;
  65. glfwGetWindowSize(context.wind, &w, &h);
  66. windowState.w = w;
  67. windowState.h = h;
  68. }
  69. #pragma endregion
  70. #pragma region input
  71. double mouseX = 0;
  72. double mouseY = 0;
  73. glfwGetCursorPos(context.wind, &mouseX, &mouseY);
  74. input.mouseX = (int)mouseX;
  75. input.mouseY = (int)mouseY;
  76. auto processInput = [](pika::Button &b)
  77. {
  78. if (!b.lastState() && b.held())
  79. {
  80. b.setPressed(true);
  81. }
  82. else
  83. {
  84. b.setPressed(false);
  85. }
  86. if (b.lastState() && !b.held())
  87. {
  88. b.setReleased(true);
  89. }
  90. else
  91. {
  92. b.setReleased(false);
  93. }
  94. b.setLastState(b.held());
  95. };
  96. processInput(input.lMouse);
  97. processInput(input.rMouse);
  98. for (int i = 0; i < Button::BUTTONS_COUNT; i++)
  99. {
  100. processInput(input.buttons[i]);
  101. }
  102. #pragma endregion
  103. }