| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #include <array>
- #include <vector>
- #include <map>
- #include <iostream>
- /*
- * https://stackoverflow.com/questions/2298242/callback-functions-in-c
- */
- enum e_event_code { ALL, EVENT_PRESSED, EVENT_LONG_PRESSED, LV_EVENT_RELEASED, REDRAW } ;
- class cEvents {
- public:
-
- void* user_data = nullptr;
- void* param = nullptr;
- cEvents* prev = nullptr;
- enum e_event_code code;
- uint16_t x;
- uint16_t y;
- cEvents* event_get_code(cEvents* e) {
- std::cout << e->code << std::endl;
- return this;
- }
- bool event_send_code(cEvents* e) {
- return true;
- }
- };
- void player_jump(cEvents &e) {
- std::cout << "player jump" << std::endl;
- std::cout << "x: " << e.x << " / y: " << e.y << std::endl;
- e.x = 36;
- e.y = 47;
- };
- void player_crouch(cEvents& e) {
- std::cout << "player crouch" << std::endl;
- std::cout << "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&), 5> actions;
- 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;
- }
- };
- int main() {
- game_core gc;
- cEvents e;
- gc.addEventCb(REDRAW, &player_jump);
- gc.addEventCb(EVENT_PRESSED, &player_crouch);
- 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);
- e.code = ALL;
- e.x = 39;
- e.y = 41;
- gc.execEvent(e);
- return 1;
- }
|