| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #include <array>
- #include <vector>
- #include <map>
- #include <iostream>
- #include <functional>
- #include "Header.h"
- /*
- * https://stackoverflow.com/questions/2298242/callback-functions-in-c
- * https://codereview.stackexchange.com/questions/259525/c-generic-callback-class-with-removable-listeners-by-unique-id
- */
- /************************************************************************************
- *
- */
- void player_jump(cEvents& e) {
- std::cout << "player jump - " << "x: " << e.x << " / y: " << e.y << std::endl;
- e.x = 36;
- e.y = 47;
- };
- void player_crouch(cEvents& e) {
- std::cout << "player crouch - " << "x: " << e.x << " / y: " << e.y << std::endl;
- }
- /************************************************************************************
- *
- */
- class game_core : public cEvents
- {
- // empty eventhandler (default)
- static void emptyEvent(cEvents& e) { std::cout << "empty event handler" << std::endl; }
- public:
- std::array<void(*)(cEvents&), 6> actions;
- template<class T, class S>
- std::array<void(*)(T&, S&), 6> actions_c;
- /************************************************************************************
- *
- */
- game_core() { actions.fill(&emptyEvent); }
-
- /************************************************************************************
- *
- */
- void execEvent(cEvents& e)
- {
- if (actions[e.code]) actions[e.code](e);
- }
- /************************************************************************************
- * update keybind from menu
- */
- void addEventCb(enum e_event_code key_id, void(*new_action)(cEvents&))
- {
- actions[key_id] = new_action;
- }
- /************************************************************************************
- *
- */
- template<class T, class S>
- void addEventCb1(enum e_event_code key_id, void (*new_action) (T*, S*))
- {
- actions[key_id] = new_action;
- }
- /************************************************************************************
- * update keybind from menu
- */
- void delEventCb(enum e_event_code key_id)
- {
- actions[key_id] = &emptyEvent;
- }
- };
- /************************************************************************************
- *
- */
- // template<class T, class S>
- void player_gen(game_core * src, myModel* dest) {
- std::cout << "player generic" << std::endl;
- return;
- }
- // template <class T, class S>
- std::function< void(game_core*, myModel*) > callback;
- /************************************************************************************
- *
- */
- int main() {
- game_core gc;
- myModel mym;
- cEvents e;
- gc.addEventCb(REDRAW, &player_jump);
- gc.addEventCb(EVENT_PRESSED, &player_crouch);
- auto cb = &player_gen;
- callback = &player_gen;
- gc.addEventCb1(EVENT_PRESSED, cb);
- #if 0
- gc.addEventCb1(EVENT_PRESSED, &player_gen);
- #else
- // player_gen(&gc, &mym);
- #endif
- e.code = EVENT_PRESSED;
- e.x = 12;
- e.y = 24;
- gc.execEvent(e);
- e.code = REDRAW;
- e.x = 39;
- e.y = 41;
- gc.execEvent(e);
- return 0;
- }
|