fork download
  1. #include <vector>
  2. #include <iostream>
  3. #include <functional>
  4.  
  5. enum Direction
  6. {
  7. UP,
  8. DOWN
  9. };
  10.  
  11. struct Control{
  12. char key;
  13. std::function<void()> press;
  14. std::function<void()> release;
  15. Control(char key, std::function<void()> press, std::function<void()> release):
  16. key(key),press(press),release(release){}
  17. };
  18.  
  19. struct Player
  20. {
  21. void go(Direction)
  22. {
  23. std::cout << "go\n";
  24. }
  25.  
  26. void stop(Direction)
  27. {
  28. std::cout << "stop\n";
  29. }
  30. };
  31.  
  32. struct Screen
  33. {
  34. void init(Player& player);
  35. void update();
  36.  
  37. std::vector<Control> controls;
  38. };
  39.  
  40. void Screen::init(Player& player){
  41.  
  42. /*this->controls.push_back(Control(
  43.   'W',
  44.   [&player](){player.go(UP);},
  45.   [&player](){player.stop(UP);}));*/
  46.  
  47. this->controls.emplace_back(
  48. 'W',
  49. [&player](){player.go(UP);},
  50. [&player](){player.stop(UP);});
  51.  
  52.  
  53. }
  54.  
  55. void Screen::update(){
  56. for (auto control : controls){
  57. control.press();
  58. control.release();
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. Player p;
  65. Screen s;
  66. s.init(p);
  67. s.update();
  68. }
  69.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
go
stop