PocketEmpires/Camera.cpp

217 lines
6.0 KiB
C++

/* *******************************************************************
* Includes
* ******************************************************************/
#include "Camera.h"
#include "Cursor.h"
#include <stdint.h>
#include <stdbool.h>
/* *******************************************************************
* Defines
* ******************************************************************/
/*****************************************************************//**
*
* \brief This macro defines camera speed, in pixels per frame.
*
*********************************************************************/
#define CAMERA_SPEED (static_cast<int8_t>(4))
/* *******************************************************************
* Types definition
* ******************************************************************/
/* *******************************************************************
* Global variables definition
* ******************************************************************/
/* *******************************************************************
* Local variables definition
* ******************************************************************/
/* *******************************************************************
* Local prototypes declaration
* ******************************************************************/
/* *******************************************************************
* Functions definition
* ******************************************************************/
/*****************************************************************//**
*
* \brief Constructor for Camera class.
*
*********************************************************************/
Camera::Camera(void) :
_bLocked(false),
_xOffset(0),
_yOffset(0),
_speedTimer(0)
{
}
uint16_t Camera::getRealX(const int16_t x) const
{
return static_cast<uint16_t>(x + _xOffset);
}
uint16_t Camera::getRealY(const int16_t y) const
{
return static_cast<uint16_t>(y + _yOffset);
}
/*****************************************************************//**
*
* \brief This function transforms X coordinates for a given
* object to camera coordinates.
*
*********************************************************************/
int16_t Camera::getX(const uint16_t x) const
{
return static_cast<int16_t>(x - _xOffset);
}
/*****************************************************************//**
*
* \brief This function transforms Y coordinates for a given
* object to camera coordinates.
*
*********************************************************************/
int16_t Camera::getY(const uint16_t y) const
{
return static_cast<int16_t>(y - _yOffset);
}
/*****************************************************************//**
*
* \brief Event handler executed when human player presses
* left arrow button.
*
*********************************************************************/
void Camera::onLeftBtnPressed(Cursor& cursor)
{
if (not cursor.isXCentered())
{
/* Move cursor to initial position. */
cursor.move(-CAMERA_SPEED);
}
else if (_xOffset >= CAMERA_SPEED)
{
/* Move camera to the left. */
_xOffset -= CAMERA_SPEED;
}
else
{
/* Left screen margin reached.
* Cursor has to be moved. */
cursor.move(-CAMERA_SPEED);
}
}
/*****************************************************************//**
*
* \brief Event handler executed when human player presses
* right arrow button.
*
*********************************************************************/
void Camera::onRightBtnPressed(Cursor& cursor)
{
if (not cursor.isXCentered())
{
/* Move cursor to initial position. */
cursor.move(CAMERA_SPEED);
}
else if (_xOffset < 512)
{
/* Move camera to the right. */
_xOffset += CAMERA_SPEED;
}
else
{
/* Right screen margin reached.
* Cursor has to be moved. */
cursor.move(CAMERA_SPEED);
}
}
/*****************************************************************//**
*
* \brief Event handler executed when human player presses
* up arrow button.
*
*********************************************************************/
void Camera::onUpBtnPressed(Cursor& cursor)
{
if (not cursor.isYCentered())
{
/* Move cursor to initial position. */
cursor.move(0, -CAMERA_SPEED);
}
else if (_yOffset > 0)
{
/* Move camera to the right. */
_yOffset -= CAMERA_SPEED;
}
else
{
/* Upper screen margin reached.
* Cursor has to be moved. */
cursor.move(0, -CAMERA_SPEED);
}
}
/*****************************************************************//**
*
* \brief Event handler executed when human player presses
* down arrow button.
*
*********************************************************************/
void Camera::onDownBtnPressed(Cursor& cursor)
{
if (not cursor.isYCentered())
{
/* Move cursor to initial position. */
cursor.move(0, CAMERA_SPEED);
}
else if (_yOffset < 512)
{
/* Move camera to the right. */
_yOffset += CAMERA_SPEED;
}
else
{
/* Lower screen margin reached.
* Cursor has to be moved. */
cursor.move(0, CAMERA_SPEED);
}
}
/*****************************************************************//**
*
* \brief As its name suggests, this function adjusts camera lock.
*
* When camera is locked, it cannot be moved when pressing
* direction keys.
*
* \param bLock
* True if camera must be locked, false otherwise.
*
*********************************************************************/
void Camera::adjustLock(const bool bLock)
{
_bLocked = bLock;
}
/*****************************************************************//**
*
* \brief Returns camera lock state.
*
* \return Returns true if camera is locked, false otherwise.
*
*********************************************************************/
bool Camera::isLocked(void) const
{
return _bLocked;
}