| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include <array>
- #include <vector>
- #include <map>
- #include <iostream>
- /*
- * https://stackoverflow.com/questions/2298242/callback-functions-in-c
- */
- enum e_event_code {NONE, 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;
- }
- };
- class cBaseClass {
- uint8_t age;
- enum { MALE, FEMALE, DIVERS } sex;
- virtual void init() {};
- virtual void run() {};
- virtual void update() {};
- };
- class myModel : public cBaseClass {
- const char* pClassName = "myModel";
- };
- 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&), 6> actions;
- game_core() {
- #if 0
- for (uint8_t i = 0; i < actions.size(); i++)
- actions[i] = &emptyEvent;
- #else
- actions.fill(&emptyEvent);
- #endif
- }
- //
- void execEvent(cEvents& e)
- {
- #if 0
- for (const auto act : actions)
- if (act) actions[e.code](e);
- // act (e);
- #else
- if (actions[e.code]) actions[e.code](e);
- #endif
- }
- // update keybind from menu
- void addEventCb(enum e_event_code key_id, void(*new_action)(cEvents&))
- {
- actions[key_id] = new_action;
- }
- // update keybind from menu
- void delEventCb(enum e_event_code key_id)
- {
- actions[key_id] = &emptyEvent;
- }
- };
- int main() {
- game_core gc;
- myModel mym;
- 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);
- gc.delEventCb(EVENT_PRESSED);
- e.code = EVENT_PRESSED;
- e.x = 39;
- e.y = 41;
- gc.execEvent(e);
- return 0;
- }
|