Calls to HumanPlayer and Camera button pressed event handlers are now automatically handled by two const arrays of member functions pointers.

This commit is contained in:
XaviDCR92 2018-07-09 22:22:26 +02:00
parent f0b654b9bf
commit cd78b97bce
6 changed files with 121 additions and 16 deletions

View File

@ -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)
{
}

View File

@ -31,6 +31,7 @@ class Camera
/* Event handlers. */
void onLeftBtnPressed(void);
void onRightBtnPressed(void);
private:
bool _bLocked;

Binary file not shown.

View File

@ -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

View File

@ -3,28 +3,107 @@
* **************************************/
#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)
{
}
/*****************************************************************//**
*
* \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)
{

View File

@ -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__ */