WindowManagerTests.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <core/Logger.hpp>
  2. #include <cstdio>
  3. #include <GLFW/glfw3.h>
  4. #include "../Tests.hpp"
  5. #include "core/WindowManager.hpp"
  6. namespace W = Core::Window;
  7. static W::Button closeButton = 0;
  8. static W::Button testButton = 0;
  9. static W::Button textButton = 0;
  10. static bool isRunning(void*) {
  11. return !W::shouldClose() && !W::isButtonDown(closeButton);
  12. }
  13. static void tick(void*) {
  14. if(!W::isInputEnabled()) {
  15. LOG_INFO(
  16. "TPS: #\nFPS: #\n", W::getTicksPerSecond(),
  17. W::getFramesPerSecond());
  18. printf(
  19. "%12s | Down: %d | DownTime: %3d | Released: %d\n",
  20. W::getButtonName(closeButton), W::isButtonDown(closeButton),
  21. W::getButtonDownTime(closeButton),
  22. W::wasButtonReleased(closeButton));
  23. printf(
  24. "%12s | Down: %d | DownTime: %3d | Released: %d\n",
  25. W::getButtonName(testButton), W::isButtonDown(testButton),
  26. W::getButtonDownTime(testButton), W::wasButtonReleased(testButton));
  27. Core::Vector2 mouse = W::getLastMousePosition();
  28. LOG_INFO("Mouse: # #\n", mouse[0], mouse[1]);
  29. }
  30. if(W::getButtonDownTime(textButton) == 1) {
  31. if(W::isInputEnabled()) {
  32. W::disableInput();
  33. } else {
  34. W::enableInput();
  35. }
  36. }
  37. if(W::isInputEnabled()) {
  38. printf("Input: '%s'\n", W::getInputString());
  39. printf("Cursor: %zu\n", W::getInputCursor());
  40. }
  41. }
  42. static void render(void*, float) {
  43. }
  44. static void printReport(
  45. Core::LogLevel l, const char* file, int line, void*, const char* message) {
  46. printLog(l, file, line, TERMINAL_RED, "#", message);
  47. }
  48. void testWindow() {
  49. setReportHandler(printReport, nullptr);
  50. W::Options options = {{800, 480}, false, "Test"};
  51. if(W::open(options)) {
  52. return;
  53. }
  54. closeButton = W::addButton("Close Button");
  55. W::bindKeyToButton(closeButton, GLFW_KEY_Q);
  56. testButton = W::addButton("Test Button");
  57. W::bindKeyToButton(testButton, GLFW_KEY_T);
  58. textButton = W::addButton("Text Button");
  59. W::bindKeyToButton(textButton, GLFW_KEY_C);
  60. W::show();
  61. W::setRunHandler(isRunning, nullptr);
  62. W::setTickHandler(tick, nullptr);
  63. W::setRenderHandler(render, nullptr);
  64. W::setNanosPerTick(50'000'000);
  65. W::run();
  66. W::close();
  67. }