diff options
| author | XaviDCR92 <xavi.dcr@gmail.com> | 2018-07-09 22:22:26 +0200 |
|---|---|---|
| committer | XaviDCR92 <xavi.dcr@gmail.com> | 2018-07-09 22:22:26 +0200 |
| commit | cd78b97bceaf6ecbbab6f89631c15ed712878d4d (patch) | |
| tree | c08c69a651ce91c834aef7b588e1074106315df6 | |
| parent | f0b654b9bf3bc2a93c1f89d4cc3edcf77b948555 (diff) | |
| download | pocketempires-cd78b97bceaf6ecbbab6f89631c15ed712878d4d.tar.gz | |
Calls to HumanPlayer and Camera button pressed event handlers are now automatically handled by two const arrays of member functions pointers.
| -rw-r--r-- | Camera.cpp | 12 | ||||
| -rw-r--r-- | Camera.h | 1 | ||||
| -rwxr-xr-x | Exe/POCKET.ELF | bin | 28084 -> 28184 bytes | |||
| -rw-r--r-- | Exe/POCKET.MAP | 7 | ||||
| -rw-r--r-- | HumanPlayer.cpp | 107 | ||||
| -rw-r--r-- | HumanPlayer.h | 10 |
6 files changed, 121 insertions, 16 deletions
@@ -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) +{ +} @@ -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 Binary files differindex 2366d55..85108c7 100755 --- a/Exe/POCKET.ELF +++ b/Exe/POCKET.ELF 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,23 +3,35 @@ * **************************************/ #include "HumanPlayer.h" +#include "System.h" +#include <Buttons.h> +#include <Gamebuino.h> -/* ************************************** - * 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) { @@ -31,12 +43,83 @@ Player(strPlayerName) * 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 + * left arrow button. + * + *********************************************************************/ void HumanPlayer::onLeftBtnPressed(void) { /* Also, send the event to Camera object. */ _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__ */ |
