192 lines
3.0 KiB
C++
192 lines
3.0 KiB
C++
#include "sprite.h"
|
|
#include "texture.h" // for Texture
|
|
|
|
// Constructor
|
|
Sprite::Sprite(int x, int y, int w, int h, Texture *texture)
|
|
{
|
|
// Establece la posición X,Y del sprite
|
|
this->x = x;
|
|
this->y = y;
|
|
|
|
// Establece el alto y el ancho del sprite
|
|
this->w = w;
|
|
this->h = h;
|
|
|
|
// Establece la textura donde están los gráficos para el sprite
|
|
this->texture = texture;
|
|
|
|
// Establece el rectangulo de donde coger la imagen
|
|
spriteClip = {0, 0, w, h};
|
|
|
|
// Inicializa variables
|
|
enabled = true;
|
|
}
|
|
|
|
Sprite::Sprite(SDL_Rect rect, Texture *texture)
|
|
{
|
|
// Establece la posición X,Y del sprite
|
|
x = rect.x;
|
|
y = rect.y;
|
|
|
|
// Establece el alto y el ancho del sprite
|
|
w = rect.w;
|
|
h = rect.h;
|
|
|
|
// Establece la textura donde están los gráficos para el sprite
|
|
this->texture = texture;
|
|
|
|
// Establece el rectangulo de donde coger la imagen
|
|
spriteClip = {0, 0, w, h};
|
|
|
|
// Inicializa variables
|
|
enabled = true;
|
|
}
|
|
|
|
// Destructor
|
|
Sprite::~Sprite()
|
|
{
|
|
texture = nullptr;
|
|
}
|
|
|
|
// Muestra el sprite por pantalla
|
|
void Sprite::render()
|
|
{
|
|
if (enabled)
|
|
{
|
|
texture->render(x, y, &spriteClip);
|
|
}
|
|
}
|
|
|
|
// Obten el valor de la variable
|
|
int Sprite::getPosX()
|
|
{
|
|
return x;
|
|
}
|
|
|
|
// Obten el valor de la variable
|
|
int Sprite::getPosY()
|
|
{
|
|
return y;
|
|
}
|
|
|
|
// Obten el valor de la variable
|
|
int Sprite::getWidth()
|
|
{
|
|
return w;
|
|
}
|
|
|
|
// Obten el valor de la variable
|
|
int Sprite::getHeight()
|
|
{
|
|
return h;
|
|
}
|
|
|
|
// Establece la posición del objeto
|
|
void Sprite::setPos(SDL_Point p)
|
|
{
|
|
this->x = p.x;
|
|
this->y = p.y;
|
|
}
|
|
|
|
// Establece la posición del objeto
|
|
void Sprite::setPos(SDL_Rect r)
|
|
{
|
|
this->x = r.x;
|
|
this->y = r.y;
|
|
this->w = r.w;
|
|
this->h = r.h;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Sprite::setPosX(int x)
|
|
{
|
|
this->x = x;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Sprite::setPosY(int y)
|
|
{
|
|
this->y = y;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Sprite::setWidth(int w)
|
|
{
|
|
this->w = w;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Sprite::setHeight(int h)
|
|
{
|
|
this->h = h;
|
|
}
|
|
|
|
// Obten el valor de la variable
|
|
SDL_Rect Sprite::getSpriteClip()
|
|
{
|
|
return spriteClip;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Sprite::setSpriteClip(SDL_Rect rect)
|
|
{
|
|
spriteClip = rect;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Sprite::setSpriteClip(int x, int y, int w, int h)
|
|
{
|
|
spriteClip = {x, y, w, h};
|
|
}
|
|
|
|
// Obten el valor de la variable
|
|
Texture *Sprite::getTexture()
|
|
{
|
|
return texture;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Sprite::setTexture(Texture *texture)
|
|
{
|
|
this->texture = texture;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Sprite::setEnabled(bool value)
|
|
{
|
|
enabled = value;
|
|
}
|
|
|
|
// Comprueba si el objeto está habilitado
|
|
bool Sprite::isEnabled()
|
|
{
|
|
return enabled;
|
|
}
|
|
|
|
// Devuelve el rectangulo donde está el sprite
|
|
SDL_Rect Sprite::getRect()
|
|
{
|
|
SDL_Rect rect = {x, y, w, h};
|
|
return rect;
|
|
}
|
|
|
|
// Establece los valores de posición y tamaño del sprite
|
|
void Sprite::setRect(SDL_Rect rect)
|
|
{
|
|
x = rect.x;
|
|
y = rect.y;
|
|
w = rect.w;
|
|
h = rect.h;
|
|
}
|
|
|
|
// Incrementa el valor de la variable
|
|
void Sprite::incPosX(int value)
|
|
{
|
|
x += value;
|
|
}
|
|
|
|
// Incrementa el valor de la variable
|
|
void Sprite::incPosY(int value)
|
|
{
|
|
y += value;
|
|
} |