Callback_14.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <array>
  2. #include <vector>
  3. #include <map>
  4. #include <iostream>
  5. #include <functional>
  6. #include "Header.h"
  7. /*
  8. * https://stackoverflow.com/questions/2298242/callback-functions-in-c
  9. * https://codereview.stackexchange.com/questions/259525/c-generic-callback-class-with-removable-listeners-by-unique-id
  10. */
  11. /************************************************************************************
  12. *
  13. */
  14. void player_jump(cEvents& e) {
  15. std::cout << "player jump - " << "x: " << e.x << " / y: " << e.y << std::endl;
  16. e.x = 36;
  17. e.y = 47;
  18. };
  19. void player_crouch(cEvents& e) {
  20. std::cout << "player crouch - " << "x: " << e.x << " / y: " << e.y << std::endl;
  21. }
  22. /************************************************************************************
  23. *
  24. */
  25. class game_core : public cEvents
  26. {
  27. // empty eventhandler (default)
  28. static void emptyEvent(cEvents& e) { std::cout << "empty event handler" << std::endl; }
  29. public:
  30. std::array<void(*)(cEvents&), 6> actions;
  31. template<class T, class S>
  32. std::array<void(*)(T&, S&), 6> actions_c;
  33. /************************************************************************************
  34. *
  35. */
  36. game_core() { actions.fill(&emptyEvent); }
  37. /************************************************************************************
  38. *
  39. */
  40. void execEvent(cEvents& e)
  41. {
  42. if (actions[e.code]) actions[e.code](e);
  43. }
  44. /************************************************************************************
  45. * update keybind from menu
  46. */
  47. void addEventCb(enum e_event_code key_id, void(*new_action)(cEvents&))
  48. {
  49. actions[key_id] = new_action;
  50. }
  51. /************************************************************************************
  52. *
  53. */
  54. template<class T, class S>
  55. void addEventCb1(enum e_event_code key_id, void (*new_action) (T*, S*))
  56. {
  57. actions[key_id] = new_action;
  58. }
  59. /************************************************************************************
  60. * update keybind from menu
  61. */
  62. void delEventCb(enum e_event_code key_id)
  63. {
  64. actions[key_id] = &emptyEvent;
  65. }
  66. };
  67. /************************************************************************************
  68. *
  69. */
  70. // template<class T, class S>
  71. void player_gen(game_core * src, myModel* dest) {
  72. std::cout << "player generic" << std::endl;
  73. return;
  74. }
  75. // template <class T, class S>
  76. std::function< void(game_core*, myModel*) > callback;
  77. /************************************************************************************
  78. *
  79. */
  80. int main() {
  81. game_core gc;
  82. myModel mym;
  83. cEvents e;
  84. gc.addEventCb(REDRAW, &player_jump);
  85. gc.addEventCb(EVENT_PRESSED, &player_crouch);
  86. auto cb = &player_gen;
  87. callback = &player_gen;
  88. gc.addEventCb1(EVENT_PRESSED, cb);
  89. #if 0
  90. gc.addEventCb1(EVENT_PRESSED, &player_gen);
  91. #else
  92. // player_gen(&gc, &mym);
  93. #endif
  94. e.code = EVENT_PRESSED;
  95. e.x = 12;
  96. e.y = 24;
  97. gc.execEvent(e);
  98. e.code = REDRAW;
  99. e.x = 39;
  100. e.y = 41;
  101. gc.execEvent(e);
  102. return 0;
  103. }