Callback_13.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <array>
  2. #include <vector>
  3. #include <map>
  4. #include <iostream>
  5. /*
  6. * https://stackoverflow.com/questions/2298242/callback-functions-in-c
  7. */
  8. enum e_event_code {NONE, EVENT_PRESSED, EVENT_LONG_PRESSED, LV_EVENT_RELEASED, REDRAW };
  9. class cEvents {
  10. public:
  11. void* user_data = nullptr;
  12. void* param = nullptr;
  13. cEvents* prev = nullptr;
  14. enum e_event_code code;
  15. uint16_t x;
  16. uint16_t y;
  17. cEvents* event_get_code(cEvents* e) {
  18. std::cout << e->code << std::endl;
  19. return this;
  20. }
  21. bool event_send_code(cEvents* e) {
  22. return true;
  23. }
  24. };
  25. class cBaseClass {
  26. uint8_t age;
  27. enum { MALE, FEMALE, DIVERS } sex;
  28. virtual void init() {};
  29. virtual void run() {};
  30. virtual void update() {};
  31. };
  32. class myModel : public cBaseClass {
  33. const char* pClassName = "myModel";
  34. };
  35. void player_jump(cEvents& e) {
  36. std::cout << "player jump" << std::endl;
  37. std::cout << "x: " << e.x << " / y: " << e.y << std::endl;
  38. e.x = 36;
  39. e.y = 47;
  40. };
  41. void player_crouch(cEvents& e) {
  42. std::cout << "player crouch" << std::endl;
  43. std::cout << "x: " << e.x << " / y: " << e.y << std::endl;
  44. }
  45. class game_core : public cEvents
  46. {
  47. // empty eventhandler (default)
  48. static void emptyEvent(cEvents& e) {
  49. std::cout << "empty event handler" << std::endl;
  50. }
  51. public:
  52. std::array<void(*)(cEvents&), 6> actions;
  53. game_core() {
  54. #if 0
  55. for (uint8_t i = 0; i < actions.size(); i++)
  56. actions[i] = &emptyEvent;
  57. #else
  58. actions.fill(&emptyEvent);
  59. #endif
  60. }
  61. //
  62. void execEvent(cEvents& e)
  63. {
  64. #if 0
  65. for (const auto act : actions)
  66. if (act) actions[e.code](e);
  67. // act (e);
  68. #else
  69. if (actions[e.code]) actions[e.code](e);
  70. #endif
  71. }
  72. // update keybind from menu
  73. void addEventCb(enum e_event_code key_id, void(*new_action)(cEvents&))
  74. {
  75. actions[key_id] = new_action;
  76. }
  77. // update keybind from menu
  78. void delEventCb(enum e_event_code key_id)
  79. {
  80. actions[key_id] = &emptyEvent;
  81. }
  82. };
  83. int main() {
  84. game_core gc;
  85. myModel mym;
  86. cEvents e;
  87. gc.addEventCb(REDRAW, &player_jump);
  88. gc.addEventCb(EVENT_PRESSED, &player_crouch);
  89. e.code = EVENT_PRESSED;
  90. e.x = 12;
  91. e.y = 24;
  92. gc.execEvent(e);
  93. e.code = REDRAW;
  94. e.x = 39;
  95. e.y = 41;
  96. gc.execEvent(e);
  97. gc.delEventCb(EVENT_PRESSED);
  98. e.code = EVENT_PRESSED;
  99. e.x = 39;
  100. e.y = 41;
  101. gc.execEvent(e);
  102. return 0;
  103. }