PocketEmpires/Game.cpp

133 lines
4.3 KiB
C++
Raw Normal View History

/* *******************************************************************
* Includes
* ******************************************************************/
#include "Game.h"
#include "Sprite.h"
#include "MouseSpr.i"
#include "System.h"
#include <Gamebuino.h>
#include <Display.h>
/* *******************************************************************
* Defines
* ******************************************************************/
/* *******************************************************************
* Types definition
* ******************************************************************/
2018-07-10 00:04:16 +02:00
/*****************************************************************//**
*
* \brief This enum holds different options to be selected
* under pause menu.
*
*********************************************************************/
enum tPauseMenuChoice
{
2018-07-10 00:04:16 +02:00
PAUSE_MENU_CHOICE_RESUME, /**< Resumes the game. */
PAUSE_MENU_CHOICE_QUIT, /**< Exits the game. */
MAX_PAUSE_MENU_CHOICES,
};
/* *******************************************************************
* Global variables definition
* ******************************************************************/
/* *******************************************************************
* Local variables definition
* ******************************************************************/
/* *******************************************************************
* Local prototypes declaration
* ******************************************************************/
2018-07-10 00:04:16 +02:00
static void GameNextFrame(const struct tGameConfig& sGameConfig);
static enum tPauseMenuChoice GamePause(void);
/* *******************************************************************
* Functions definition
* ******************************************************************/
/*****************************************************************//**
*
* \brief Entry point for gameplay logic.
*
2018-07-10 00:04:16 +02:00
* \param sGameConfig
* Game configuration structure.
*
*********************************************************************/
2018-07-10 00:04:16 +02:00
void Game(const struct tGameConfig& sGameConfig)
{
do
{
2018-07-10 00:04:16 +02:00
/* Calculate next frame. */
GameNextFrame(sGameConfig);
/* Do not calculate a new frame
* until refresh flag is set. */
while (not gb.update());
} while (GamePause() != PAUSE_MENU_CHOICE_QUIT);
}
2018-07-10 00:04:16 +02:00
/*****************************************************************//**
*
* \brief This function calculates a new frame by calling
* all handlers.
*
* \param sGameConfig
* Game configuration structure.
*
*********************************************************************/
static void GameNextFrame(const struct tGameConfig& sGameConfig)
{
for (size_t szHumanPlayer = 0; szHumanPlayer < sGameConfig.u8NHumanPlayers; szHumanPlayer++)
2018-07-10 00:04:16 +02:00
{
HumanPlayer* const pHumanPlayerData = &sGameConfig.pHumanPlayerData[szHumanPlayer];
2018-07-10 00:04:16 +02:00
if (pHumanPlayerData != NULL)
{
/* Execute handler for selected HumanPlayer. */
2018-07-10 00:04:16 +02:00
pHumanPlayerData->handler();
}
else
{
/* HumanPlayer handler callback not available. */
}
2018-07-10 00:04:16 +02:00
}
}
/*****************************************************************//**
*
* \brief When C button is pressed, this function evaluates
* user actions to determine whether game must be exited.
*
*********************************************************************/
static enum tPauseMenuChoice GamePause(void)
{
if (gb.buttons.released(BTN_C) != false)
{
2018-07-10 00:04:16 +02:00
/* Strings must be individually allocated into
* PROGMEM so they can be read correctly by gb.menu(). */
static const char strPauseMenuOption_0[] PROGMEM = "Resume";
static const char strPauseMenuOption_1[] PROGMEM = "Quit";
static const char* const astrPauseMenuOptions[MAX_PAUSE_MENU_CHOICES] PROGMEM =
{
2018-07-10 00:04:16 +02:00
[PAUSE_MENU_CHOICE_RESUME] = strPauseMenuOption_0,
[PAUSE_MENU_CHOICE_QUIT] = strPauseMenuOption_1
};
return static_cast<enum tPauseMenuChoice>(gb.menu(astrPauseMenuOptions, MAX_PAUSE_MENU_CHOICES));
}
else
{
/* C button has not been pressed. Exit. */
}
/* Return false since no
* actions need to be done yet. */
return PAUSE_MENU_CHOICE_RESUME;
}