main.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 
  2. // An integration test application to quickly go through the most essential features to test in new implementations inheriting BackendWindow in the windowManagers folder.
  3. // Instead of reading documentation with risk of missunderstanding something, this integration test should guide the developer through the stages and give hints on what is wrong and how to fix it.
  4. // It should be somewhat difficult to pass the test by accident without having integrated the media layer correctly with the operating system.
  5. // Planned tests:
  6. // * Update the image one frame at a time while showing digits, then move on to the next digit after having pressed the correct key on the keyboard.
  7. // By randomizing the order, one can see if the keys are mapped wrong or if the canvas upload is delayed to show old images.
  8. // One can also show a frame counter for easy debugging.
  9. // Might need to use a boolean flag to manually say when it is okay to update the canvas.
  10. // Any repaint or resize events during the time may prove problematic, because the canvas should be possible to update once without getting duplicate requests.
  11. // * Before the real tests begin, have a mode where one can freely move the mouse and get ripple animations from pressing, scrolling and hovering.
  12. // See the names of buttons being pressed, so that one can quickly try things out before starting the test.
  13. // * Press and hold a keyboard button for ten seconds without getting any up or down events in between.
  14. // Features for repeated key presses have to be filtered out, so that up and down is only triggered by physical up and down events.
  15. // * Press all the keys on the keyboard and see them light up on a picture of a keyboard as you type.
  16. // Create a reusable component for handling keyboard input, which can also be used to bind keys in a game.
  17. // * Ask if relative input (mouse/ball/trackpad) is available and skip tests if not available.
  18. // Enter full screen and rotate a 3D camera in a cube map sky by moving the cursor to the center of the window.
  19. // Is there any clean way to allocate temporary resources for 3D graphics without creating an error-prone mess of pointers and inheritance?
  20. #include "../../DFPSR/includeFramework.h"
  21. #include "tests/inputTest.h"
  22. using namespace dsr;
  23. Window window;
  24. bool running = true;
  25. TestContext context;
  26. DSR_MAIN_CALLER(dsrMain)
  27. void dsrMain(List<String> args) {
  28. // Create a window
  29. window = window_create(U"Integration test", 800, 600);
  30. // Create tests.
  31. // TODO: Ask the user which kinds of inputs are available and use that information to select the correct tests.
  32. inputTests_populate(context.tests, 3, true, true);
  33. // Create finishing screen showing results.
  34. context.tests.pushConstruct(
  35. U"Summary"
  36. ,
  37. [](AlignedImageRgbaU8 &canvas, TestContext &context) {
  38. image_fill(canvas, ColorRgbaI32(255, 255, 255, 255));
  39. font_printLine(canvas, font_getDefault(), U"Test summary:", IVector2D(40, 40), ColorRgbaI32(0, 0, 0, 255));
  40. for (int t = 0; t < context.tests.length() - 1; t++) {
  41. font_printLine(canvas, font_getDefault(), string_combine(context.tests[t].result, U" - ", context.tests[t].name), IVector2D(60, t * 20 + 60), ColorRgbaI32(0, 0, 0, 255));
  42. }
  43. // TODO: Allow scrolling through the results if it gets too much to fit inside the window.
  44. // TODO: Also print this information to the terminal, in case that it is difficult to see.
  45. }
  46. ,
  47. [](const MouseEvent& event, TestContext &context) {}
  48. ,
  49. [](const KeyboardEvent& event, TestContext &context) {}
  50. ,
  51. false
  52. );
  53. // TODO: Move to a method taking window as the argument in Test.cpp.
  54. window_setMouseEvent(window, [](const MouseEvent& event) {
  55. if (event.mouseEventType == MouseEventType::MouseDown) {
  56. if (context.lastPosition != event.position) {
  57. sendWarning(U"A mouse (button) down event changed the cursor location! Unless the window moved while the mouse was standing still, something might be wrong. Check if mouse move events are missing, off or arriving too late.\n");
  58. }
  59. if (event.key == MouseKeyEnum::Left) {
  60. context.leftMouseDown = true;
  61. } else if (event.key == MouseKeyEnum::Middle) {
  62. context.middleMouseDown = true;
  63. } else if (event.key == MouseKeyEnum::Right) {
  64. context.rightMouseDown = true;
  65. }
  66. } else if (event.mouseEventType == MouseEventType::MouseUp) {
  67. if (context.lastPosition != event.position) {
  68. sendWarning(U"A mouse (button) up event changed the cursor location! Unless the window moved while the mouse was standing still, something might be wrong. Check if mouse move events are missing, off or arriving too late.\n");
  69. }
  70. if (event.key == MouseKeyEnum::Left) {
  71. context.leftMouseDown = false;
  72. } else if (event.key == MouseKeyEnum::Middle) {
  73. context.middleMouseDown = false;
  74. } else if (event.key == MouseKeyEnum::Right) {
  75. context.rightMouseDown = false;
  76. }
  77. }
  78. context.lastPosition = event.position;
  79. if (context.testIndex >= 0 && context.testIndex < context.tests.length()) {
  80. context.tests[context.testIndex].mouseCallback(event, context);
  81. }
  82. });
  83. window_setKeyboardEvent(window, [](const KeyboardEvent& event) {
  84. if (context.testIndex >= 0 && context.testIndex < context.tests.length()) {
  85. if (event.keyboardEventType == KeyboardEventType::KeyDown && event.dsrKey == DsrKey::DsrKey_Escape) {
  86. if (context.testIndex >= context.tests.length() - 1) {
  87. running = false;
  88. } else {
  89. context.finishTest(Grade::Skipped);
  90. }
  91. } else {
  92. context.tests[context.testIndex].keyboardCallback(event, context);
  93. }
  94. }
  95. });
  96. window_setCloseEvent(window, []() {
  97. running = false;
  98. });
  99. // Execute
  100. while(running) {
  101. if (context.tests[context.testIndex].activeDrawing) {
  102. window_executeEvents(window);
  103. } else {
  104. // Wait for actions
  105. while (!window_executeEvents(window)) {
  106. time_sleepSeconds(0.01);
  107. }
  108. }
  109. // Get the current canvas from the swap chain.
  110. AlignedImageRgbaU8 canvas = window_getCanvas(window);
  111. // Draw things to the canvas.
  112. if (context.testIndex >= 0 && context.testIndex < context.tests.length()) {
  113. context.tests[context.testIndex].drawEvent(canvas, context);
  114. }
  115. // Show the canvas.
  116. window_showCanvas(window);
  117. }
  118. }