WindowManagerTests.c 2.3 KB

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