window.cpp 2.8 KB

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