From cd78b97bceaf6ecbbab6f89631c15ed712878d4d Mon Sep 17 00:00:00 2001 From: XaviDCR92 Date: Mon, 9 Jul 2018 22:22:26 +0200 Subject: Calls to HumanPlayer and Camera button pressed event handlers are now automatically handled by two const arrays of member functions pointers. --- Camera.cpp | 12 ++++++- Camera.h | 1 + Exe/POCKET.ELF | Bin 28084 -> 28184 bytes Exe/POCKET.MAP | 7 +++- HumanPlayer.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++++++------- HumanPlayer.h | 10 ++++-- 6 files changed, 121 insertions(+), 16 deletions(-) diff --git a/Camera.cpp b/Camera.cpp index 5e096ed..95b334b 100644 --- a/Camera.cpp +++ b/Camera.cpp @@ -59,9 +59,19 @@ void Camera::getCoordinates(int16_t* const x, int16_t* const y) /*****************************************************************//** * * \brief Event handler executed when human player presses - * left button. + * left arrow button. * *********************************************************************/ void Camera::onLeftBtnPressed(void) { } + +/*****************************************************************//** + * + * \brief Event handler executed when human player presses + * right arrow button. + * + *********************************************************************/ +void Camera::onRightBtnPressed(void) +{ +} diff --git a/Camera.h b/Camera.h index 4e9bae9..ff18894 100644 --- a/Camera.h +++ b/Camera.h @@ -31,6 +31,7 @@ class Camera /* Event handlers. */ void onLeftBtnPressed(void); + void onRightBtnPressed(void); private: bool _bLocked; diff --git a/Exe/POCKET.ELF b/Exe/POCKET.ELF index 2366d55..85108c7 100755 Binary files a/Exe/POCKET.ELF and b/Exe/POCKET.ELF differ diff --git a/Exe/POCKET.MAP b/Exe/POCKET.MAP index 7054ed5..79825c3 100644 --- a/Exe/POCKET.MAP +++ b/Exe/POCKET.MAP @@ -1,7 +1,7 @@ Archive member included to satisfy reference by file (symbol) ../lib/libgamebuino.a(Buttons.o) - Obj/Game.o (_ZN7Buttons8releasedEh) + Obj/HumanPlayer.o (_ZN7Buttons7pressedEh) ../lib/libgamebuino.a(Display.o) Obj/System.o (_ZTV7Display) ../lib/libgamebuino.a(font3x5.o) @@ -181,6 +181,8 @@ Discarded input sections .text 0x0000000000000000 0x0 Obj/HumanPlayer.o .data 0x0000000000000000 0x0 Obj/HumanPlayer.o .bss 0x0000000000000000 0x0 Obj/HumanPlayer.o + .text._ZN11HumanPlayer13buttonHandlerEv + 0x0000000000000000 0x152 Obj/HumanPlayer.o .text._ZN11HumanPlayer16onLeftBtnPressedEv 0x0000000000000000 0x6 Obj/HumanPlayer.o .text 0x0000000000000000 0x0 Obj/Sprite.o @@ -200,6 +202,8 @@ Discarded input sections 0x0000000000000000 0x2c Obj/Camera.o .text._ZN6Camera16onLeftBtnPressedEv 0x0000000000000000 0x2 Obj/Camera.o + .text._ZN6Camera17onRightBtnPressedEv + 0x0000000000000000 0x2 Obj/Camera.o .text 0x0000000000000000 0x0 Obj/Unit.o .data 0x0000000000000000 0x0 Obj/Unit.o .bss 0x0000000000000000 0x0 Obj/Unit.o @@ -1323,6 +1327,7 @@ END GROUP 0x000000000000048e 0x16 Obj/System.o .text._ZN11HumanPlayer11drawHandlerEv 0x00000000000004a4 0x2 Obj/HumanPlayer.o + 0x00000000000004a4 _ZN11HumanPlayer13onABtnPressedEv 0x00000000000004a4 _ZN11HumanPlayer11drawHandlerEv .text._ZN11HumanPlayerC2EPKc 0x00000000000004a6 0x1e Obj/HumanPlayer.o diff --git a/HumanPlayer.cpp b/HumanPlayer.cpp index eadebcf..766bf7d 100644 --- a/HumanPlayer.cpp +++ b/HumanPlayer.cpp @@ -3,28 +3,107 @@ * **************************************/ #include "HumanPlayer.h" +#include "System.h" +#include +#include -/* ************************************** - * Defines * - * **************************************/ +/* ******************************************************************* + * Defines + * ******************************************************************/ -/* ************************************** - * Structs and enums * - * **************************************/ +/* ******************************************************************* + * Types definition + * ******************************************************************/ -/* ************************************** - * Local variables * - * **************************************/ +/* ******************************************************************* + * Global variables + * ******************************************************************/ -/* ************************************** - * Functions definition * - * **************************************/ +/* ******************************************************************* + * Local variables + * ******************************************************************/ +/* ******************************************************************* + * Functions definition + * ******************************************************************/ + +/*****************************************************************//** + * + * \brief Constructor for HumanPlayer class. + * + *********************************************************************/ HumanPlayer::HumanPlayer(const char* const strPlayerName) : Player(strPlayerName) { } +/*****************************************************************//** + * + * \brief Event handler executed when human player presses + * left arrow button. + * + *********************************************************************/ +void HumanPlayer::buttonHandler(void) +{ + for (uint8_t btn = 0; btn < NUM_BTN; btn++) + { + /* This array of member functions lists + * button pressed event handlers for each button. */ + void (HumanPlayer::*const apBtnHandlerTable[NUM_BTN])(void) = + { + [BTN_LEFT] = NULL, + [BTN_UP] = NULL, + [BTN_RIGHT] = NULL, + [BTN_DOWN] = NULL, + [BTN_A] = &HumanPlayer::onABtnPressed, + [BTN_B] = &HumanPlayer::onBBtnPressed + }; + + void (Camera::*const apBtnCameraHandlerTable[NUM_BTN])(void) = + { + [BTN_LEFT] = &Camera::onLeftBtnPressed, + [BTN_UP] = NULL, + [BTN_RIGHT] = &Camera::onRightBtnPressed, + [BTN_DOWN] = NULL + }; + + /* Member function pointer is valid. */ + if (gb.buttons.pressed(btn)) + { + /* Key has been pressed. Execute both + * HumanPlayer and Camera handlers, if available. */ + + /* Get pointer to HumanPlayer member function for selected button. */ + void (HumanPlayer::*const pBtnHandler)(void) = apBtnHandlerTable[btn]; + + if (pBtnHandler != NULL) + { + /* HumanPlayer member function + * pointer is available. Execute. */ + (this->*pBtnHandler)(); + } + else + { + /* Undefined callback for selected button. */ + } + + /* Get pointer to Camera member function for selected button. */ + void (Camera::*const pCameraBtnHandler)(void) = apBtnCameraHandlerTable[btn]; + + if (pCameraBtnHandler != NULL) + { + /* Camera member function + * pointer is available. Execute. */ + (_cam.*pCameraBtnHandler)(); + } + else + { + /* Undefined callback for selected button. */ + } + } + } +} + /*****************************************************************//** * * \brief Event handler executed when human player presses @@ -37,6 +116,10 @@ void HumanPlayer::onLeftBtnPressed(void) _cam.onLeftBtnPressed(); } +void HumanPlayer::onABtnPressed(void) +{ +} + void HumanPlayer::drawHandler(void) { diff --git a/HumanPlayer.h b/HumanPlayer.h index 0ed983a..17986ba 100644 --- a/HumanPlayer.h +++ b/HumanPlayer.h @@ -23,12 +23,18 @@ class HumanPlayer : public Player { public: - HumanPlayer(const char* const strPlayerName); - void onLeftBtnPressed(void); + explicit HumanPlayer(const char* const strPlayerName); void drawHandler(void); private: Camera _cam; + void buttonHandler(void); + + /* Event handlers. */ + void onLeftBtnPressed(void); + void onRightBtnPressed(void); + void onABtnPressed(void); + void onBBtnPressed(void); }; #endif /* HUMAN_PLAYER_H__ */ -- cgit v1.2.3