01_mouse_around_capture.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. require_mouse_capture(true);
  26. float2 cursor_position{};
  27. bool run = true;
  28. while(run)
  29. {
  30. while(auto e = next_event()) std::visit( simple::support::overload{
  31. [&cursor_position, &screen_size](const mouse_motion& event)
  32. {
  33. cursor_position = screen_size * event.screen_normalized_position().value();
  34. },
  35. [&run](mouse_up)
  36. {
  37. run = false;
  38. },
  39. [&run](key_pressed)
  40. {
  41. run = false;
  42. },
  43. [](auto) { }
  44. }, *e);
  45. std::puts("\nPress any key or click to quit.");
  46. render_screen(int2(screen_size), int2(cursor_position), ' ', '*', break_lines);
  47. std::this_thread::sleep_for(frametime);
  48. }
  49. std::puts("");
  50. return 0;
  51. }
  52. catch(...)
  53. {
  54. if(errno)
  55. std::perror("ERROR");
  56. const char* sdl_error = SDL_GetError();
  57. if(*sdl_error)
  58. std::puts(sdl_error);
  59. throw;
  60. }
  61. void render_screen(int2 size, int2 cursor_position, char bg, char cursor, bool break_lines)
  62. {
  63. static std::string buffer;
  64. int linear_size = size.y() * size.x();
  65. if(break_lines)
  66. linear_size += size.y() - 1;
  67. buffer.resize(linear_size);
  68. auto row = buffer.begin();
  69. for(int y = 0; y < size.y(); ++y)
  70. {
  71. row = std::fill_n(row, size.x(), bg);
  72. if(break_lines && y != (size.y() - 1))
  73. *row++ = '\n';
  74. }
  75. int pitch = break_lines ? size.x() + 1 : size.x();
  76. int linear_cursor_position = cursor_position.y() * pitch + cursor_position.x();
  77. buffer[linear_cursor_position] = cursor;
  78. std::fputs(buffer.c_str(), stdout);
  79. std::fflush(stdout);
  80. }