* Sprite data width and height is now extracted from Sprite class.
This commit is contained in:
parent
c501d0e4e7
commit
4efe5ad6f9
33
BaseUnit.cpp
33
BaseUnit.cpp
|
@ -3,7 +3,9 @@
|
||||||
* ******************************************************************/
|
* ******************************************************************/
|
||||||
|
|
||||||
#include "BaseUnit.h"
|
#include "BaseUnit.h"
|
||||||
|
#include "Camera.h"
|
||||||
#include "System.h"
|
#include "System.h"
|
||||||
|
#include "Sprite.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <Gamebuino.h>
|
#include <Gamebuino.h>
|
||||||
|
@ -24,6 +26,8 @@
|
||||||
* Local variables definition
|
* Local variables definition
|
||||||
* ******************************************************************/
|
* ******************************************************************/
|
||||||
|
|
||||||
|
static const Camera* cam;
|
||||||
|
|
||||||
/* *******************************************************************
|
/* *******************************************************************
|
||||||
* Local prototypes declaration
|
* Local prototypes declaration
|
||||||
* ******************************************************************/
|
* ******************************************************************/
|
||||||
|
@ -69,6 +73,17 @@ void BaseUnit::handler(void)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*****************************************************************//**
|
||||||
|
*
|
||||||
|
* \brief This function sets global camera for all BaseUnit
|
||||||
|
* instances.
|
||||||
|
*
|
||||||
|
*********************************************************************/
|
||||||
|
void BaseUnit::setCamera(const Camera* const c)
|
||||||
|
{
|
||||||
|
cam = c;
|
||||||
|
}
|
||||||
|
|
||||||
/*****************************************************************//**
|
/*****************************************************************//**
|
||||||
*
|
*
|
||||||
* \brief Periodical draw handler for BaseUnit class.
|
* \brief Periodical draw handler for BaseUnit class.
|
||||||
|
@ -78,19 +93,19 @@ void BaseUnit::drawHandler(const uint8_t* const pu8SprData)
|
||||||
{
|
{
|
||||||
if (isSelected())
|
if (isSelected())
|
||||||
{
|
{
|
||||||
enum
|
|
||||||
{
|
|
||||||
WIDTH_DATA_INDEX,
|
|
||||||
HEIGHT_DATA_INDEX
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Retrieve unit width from sprite data. */
|
/* Retrieve unit width from sprite data. */
|
||||||
const uint8_t w = pgm_read_byte(&pu8SprData[WIDTH_DATA_INDEX]);
|
const uint8_t w = Sprite::getWidth(pu8SprData);
|
||||||
|
|
||||||
/* Retrieve unit height from sprite data. */
|
/* Retrieve unit height from sprite data. */
|
||||||
const uint8_t h = pgm_read_byte(&pu8SprData[HEIGHT_DATA_INDEX]);
|
const uint8_t h = Sprite::getHeight(pu8SprData);
|
||||||
|
|
||||||
gb.display.drawRect(_x, _y, w, h);
|
if (cam != NULL)
|
||||||
|
{
|
||||||
|
const uint8_t x = cam->getX(_x - w);
|
||||||
|
const uint8_t y = cam->getY(_y - h);
|
||||||
|
|
||||||
|
gb.display.drawRoundRect(x, y, w << 1, h << 1, 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include "Camera.h"
|
||||||
|
|
||||||
/* *******************************************************************
|
/* *******************************************************************
|
||||||
* Defines
|
* Defines
|
||||||
|
@ -35,6 +36,7 @@ class BaseUnit
|
||||||
void handler(void);
|
void handler(void);
|
||||||
void drawHandler(const uint8_t* const pu8SprData);
|
void drawHandler(const uint8_t* const pu8SprData);
|
||||||
void create(const uint16_t x, const uint16_t y);
|
void create(const uint16_t x, const uint16_t y);
|
||||||
|
static void setCamera(const Camera* const c);
|
||||||
bool isAlive(void);
|
bool isAlive(void);
|
||||||
bool isSelected(void);
|
bool isSelected(void);
|
||||||
void setSelected(const bool bSelect);
|
void setSelected(const bool bSelect);
|
||||||
|
|
18
Camera.cpp
18
Camera.cpp
|
@ -51,15 +51,25 @@ Camera::Camera(void) :
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
* \brief This function transforms X coordinates for a given
|
||||||
* object to camera coordinates.
|
* object to camera coordinates.
|
||||||
*
|
*
|
||||||
*********************************************************************/
|
*********************************************************************/
|
||||||
uint8_t Camera::getX(const uint8_t x) const
|
int16_t Camera::getX(const uint16_t x) const
|
||||||
{
|
{
|
||||||
return x - _xOffset;
|
return static_cast<int16_t>(x - _xOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************************************************************//**
|
/*****************************************************************//**
|
||||||
|
@ -68,9 +78,9 @@ uint8_t Camera::getX(const uint8_t x) const
|
||||||
* object to camera coordinates.
|
* object to camera coordinates.
|
||||||
*
|
*
|
||||||
*********************************************************************/
|
*********************************************************************/
|
||||||
uint8_t Camera::getY(const uint8_t y) const
|
int16_t Camera::getY(const uint16_t y) const
|
||||||
{
|
{
|
||||||
return y - _yOffset;
|
return static_cast<int16_t>(y - _yOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************************************************************//**
|
/*****************************************************************//**
|
||||||
|
|
35
Camera.h
35
Camera.h
|
@ -1,25 +1,36 @@
|
||||||
#ifndef CAMERA_H__
|
#ifndef CAMERA_H__
|
||||||
#define CAMERA_H__
|
#define CAMERA_H__
|
||||||
|
|
||||||
/* *************************************
|
/* *******************************************************************
|
||||||
* Includes
|
* Includes
|
||||||
* *************************************/
|
* ******************************************************************/
|
||||||
|
|
||||||
#include "Cursor.h"
|
#include "Cursor.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
/* *************************************
|
/* *******************************************************************
|
||||||
* Defines
|
* Defines
|
||||||
* *************************************/
|
* ******************************************************************/
|
||||||
|
|
||||||
/* *************************************
|
/* *******************************************************************
|
||||||
* Structs and enums
|
* Global types definition
|
||||||
* *************************************/
|
* ******************************************************************/
|
||||||
|
|
||||||
/* *************************************
|
/* Forward declaration. */
|
||||||
|
class Cursor;
|
||||||
|
|
||||||
|
/* *******************************************************************
|
||||||
|
* Global variables declaration
|
||||||
|
* ******************************************************************/
|
||||||
|
|
||||||
|
/* *******************************************************************
|
||||||
|
* Global functions declaration
|
||||||
|
* ******************************************************************/
|
||||||
|
|
||||||
|
/* *******************************************************************
|
||||||
* Class definition
|
* Class definition
|
||||||
* *************************************/
|
* ******************************************************************/
|
||||||
|
|
||||||
class Camera
|
class Camera
|
||||||
{
|
{
|
||||||
|
@ -27,8 +38,10 @@ class Camera
|
||||||
Camera(void);
|
Camera(void);
|
||||||
void adjustLock(const bool bLock);
|
void adjustLock(const bool bLock);
|
||||||
bool isLocked(void) const;
|
bool isLocked(void) const;
|
||||||
uint8_t getX(const uint8_t x) const;
|
int16_t getX(const uint16_t x) const;
|
||||||
uint8_t getY(const uint8_t y) const;
|
int16_t getY(const uint16_t y) const;
|
||||||
|
uint16_t getRealX(const int16_t x) const;
|
||||||
|
uint16_t getRealY(const int16_t y) const;
|
||||||
|
|
||||||
/* Event handlers. */
|
/* Event handlers. */
|
||||||
void onLeftBtnPressed(Cursor& cursor);
|
void onLeftBtnPressed(Cursor& cursor);
|
||||||
|
|
34
Sprite.cpp
34
Sprite.cpp
|
@ -72,12 +72,12 @@ void Sprite::draw(void)
|
||||||
|
|
||||||
if (_cam != NULL)
|
if (_cam != NULL)
|
||||||
{
|
{
|
||||||
const uint8_t x = _followCam ? _cam->getX(_x) : _x;
|
const int16_t x = _followCam ? _cam->getX(_x) : (int16_t)_x;
|
||||||
const uint8_t y = _followCam ? _cam->getY(_y) : _y;
|
const int16_t y = _followCam ? _cam->getY(_y) : (int16_t)_y;
|
||||||
|
|
||||||
if (_pu8SprData != NULL)
|
if (_pu8SprData != NULL)
|
||||||
{
|
{
|
||||||
gb.display.drawBitmap(x, y, _pu8SprData);
|
gb.display.drawBitmap((int8_t)x, (int8_t)y, _pu8SprData);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -90,6 +90,34 @@ void Sprite::draw(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t Sprite::getWidth(const uint8_t* const pu8SprData)
|
||||||
|
{
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
WIDTH_DATA_INDEX = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Retrieve unit width from sprite data. */
|
||||||
|
const uint8_t w = pgm_read_byte(&pu8SprData[WIDTH_DATA_INDEX]);
|
||||||
|
|
||||||
|
/* Return retrieved width. */
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Sprite::getHeight(const uint8_t* const pu8SprData)
|
||||||
|
{
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
HEIGHT_DATA_INDEX = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Retrieve unit height from sprite data. */
|
||||||
|
const uint8_t h = pgm_read_byte(&pu8SprData[HEIGHT_DATA_INDEX]);
|
||||||
|
|
||||||
|
/* Return retrieved height. */
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
/*****************************************************************//**
|
/*****************************************************************//**
|
||||||
*
|
*
|
||||||
* \brief Reportedly, this function updates X/Y coordinates for
|
* \brief Reportedly, this function updates X/Y coordinates for
|
||||||
|
|
Loading…
Reference in New Issue