01_mouse_around_grab.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <cstdio>
  2. #include <thread>
  3. #include <chrono>
  4. #include <string>
  5. #include "simple/interactive/initializer.h"
  6. #include "simple/interactive/event.h"
  7. #include "simple/support/function_utils.hpp"
  8. #include "simple/support/misc.hpp"
  9. #include "../common/sdl_input_grabber.h"
  10. #include "../common/sdl_input_grabber.cpp"
  11. using namespace simple::interactive;
  12. using namespace std::chrono_literals;
  13. using namespace std::string_literals;
  14. void render_screen(int2 size, int2 cursor_position, char bg, char cursor, bool break_lines = false);
  15. int main(int argc, char const* argv[]) try
  16. {
  17. using simple::support::ston;
  18. const int screen_size_x = argc > 1 ? ston<int>(argv[1]) : 79;
  19. const int screen_size_y = argc > 2 ? ston<int>(argv[2]) : 23;
  20. const std::chrono::milliseconds frametime(argc > 3 ? ston<int>(argv[3]) : 16);
  21. const bool break_lines = argc > 4 ? "false"s != argv[4] : true;
  22. const float2 screen_size{float(screen_size_x), float(screen_size_y)};
  23. initializer init;
  24. sdl_input_grabber input_grabber;
  25. input_grabber.grab();
  26. relative_mouse_mode(true);
  27. float2 cursor_position{};
  28. bool run = true;
  29. while(run)
  30. {
  31. while(auto e = next_event()) std::visit( simple::support::overload{
  32. [&cursor_position, &screen_size](const mouse_motion& event)
  33. {
  34. // using value_or here since SDL sends motion events with invalid window id until window gains focus for the first time,
  35. // and screen_normalized_motion selects the screen/display based on the window id.
  36. const float2 motion = event.screen_normalized_motion().value_or(float2{});
  37. cursor_position += screen_size * motion;
  38. cursor_position.clamp(float2::zero(), screen_size - 1);
  39. },
  40. [&run](mouse_up)
  41. {
  42. run = false;
  43. },
  44. [&run](key_pressed)
  45. {
  46. run = false;
  47. },
  48. [](auto) { }
  49. }, *e);
  50. std::puts("\nPress any key or click to quit.");
  51. render_screen(int2(screen_size), int2(cursor_position), ' ', '*', break_lines);
  52. std::this_thread::sleep_for(frametime);
  53. }
  54. std::puts("");
  55. return 0;
  56. }
  57. catch(...)
  58. {
  59. if(errno)
  60. std::perror("ERROR");
  61. const char* sdl_error = SDL_GetError();
  62. if(*sdl_error)
  63. std::puts(sdl_error);
  64. throw;
  65. }
  66. void render_screen(int2 size, int2 cursor_position, char bg, char cursor, bool break_lines)
  67. {
  68. static std::string buffer;
  69. int linear_size = size.y() * size.x();
  70. if(break_lines)
  71. linear_size += size.y() - 1;
  72. buffer.resize(linear_size);
  73. auto row = buffer.begin();
  74. for(int y = 0; y < size.y(); ++y)
  75. {
  76. row = std::fill_n(row, size.x(), bg);
  77. if(break_lines && y != (size.y() - 1))
  78. *row++ = '\n';
  79. }
  80. int pitch = break_lines ? size.x() + 1 : size.x();
  81. int linear_cursor_position = cursor_position.y() * pitch + cursor_position.x();
  82. buffer[linear_cursor_position] = cursor;
  83. std::fputs(buffer.c_str(), stdout);
  84. std::fflush(stdout);
  85. }