Actualizadas todas las librerias de sprites

This commit is contained in:
2022-08-08 19:37:55 +02:00
parent c5acab7828
commit a3c846c137
11 changed files with 391 additions and 478 deletions

View File

@@ -1,110 +1,90 @@
//#include "const.h"
#include "const.h"
#include "animatedsprite.h"
//#include <iostream>
// Constructor
AnimatedSprite::AnimatedSprite()
AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer)
{
MovingSprite::init(0, 0, 0, 0, 0, 0, 0, 0, nullptr, nullptr);
init(nullptr, nullptr);
// Copia los punteros
setTexture(texture);
setRenderer(renderer);
init();
}
// Destructor
AnimatedSprite::~AnimatedSprite()
{
MovingSprite::init(0, 0, 0, 0, 0, 0, 0, 0, nullptr, nullptr);
init(nullptr, nullptr);
}
// Iniciador
void AnimatedSprite::init(LTexture *texture, SDL_Renderer *renderer)
void AnimatedSprite::init()
{
mRenderer = renderer;
mTexture = texture;
for (Uint8 i = 0; i < 20; i++)
for (int i = 0; i < 20; i++)
{
mAnimation[i].numFrames = 0;
mAnimation[i].speed = 0;
mAnimation[i].loop = true;
mAnimation[i].currentFrame = 0;
mAnimation[i].counter = 0;
for (Uint8 j = 0; i < 50; i++)
mAnimation[i].completed = false;
for (int j = 0; i < 20; i++)
{
mAnimation[i].frame[j].x = 0;
mAnimation[i].frame[j].y = 0;
mAnimation[i].frame[j].w = 0;
mAnimation[i].frame[j].h = 0;
mAnimation[i].frames[j].x = 0;
mAnimation[i].frames[j].y = 0;
mAnimation[i].frames[j].w = 0;
mAnimation[i].frames[j].h = 0;
}
}
mCurrentAnimation = 0;
mCurrentFrame = 0;
mAnimationCounter = 0;
}
// Calcula el frame correspondiente a la animación
void AnimatedSprite::animate(int index)
{
// Calculamos el frame actual a partir del contador
mAnimation[index].currentFrame = mAnimation[index].counter / mAnimation[index].speed;
// Final de la animación
if (mAnimation[index].currentFrame >= mAnimation[index].numFrames)
if (mEnabled)
{
// Si se reproduce en bucle
if (mAnimation[index].loop)
// Calculamos el frame actual a partir del contador
mCurrentFrame = mAnimationCounter / mAnimation[index].speed;
// Si alcanzamos el final de la animación, reiniciamos el contador de la animación
// en función de la variable loop
if (mCurrentFrame >= mAnimation[index].numFrames)
{
// Reiniciamos el contador de la animación
mAnimation[index].counter = 0;
if (mAnimation[index].loop)
mAnimationCounter = 0;
else
mCurrentFrame = mAnimation[index].numFrames;
}
// En caso contrario
else
{
// Mantenemos el ultimo frame
mAnimation[index].currentFrame = mAnimation[index].numFrames;
// Escogemos el frame correspondiente de la animación
setSpriteClip(mAnimation[index].frames[mCurrentFrame]);
// Incrementamos el contador de la animacion
mAnimationCounter++;
}
}
// La animación no ha llegado a su fin
else
{
// Establece el frame correspondiente de la animación
setSpriteClip(mAnimation[index].frame[mAnimation[index].currentFrame]);
// Incrementa el contador de la animacion
mAnimation[index].counter++;
}
}
// Actualiza todas las variables del objeto: posición, velocidad y animación
void AnimatedSprite::update()
{
MovingSprite::update();
animate(mCurrentAnimation);
}
// Establece el frame actual de la animación
void AnimatedSprite::setCurrentFrame(Uint8 index, Uint8 num)
void AnimatedSprite::setCurrentFrame(Uint8 num)
{
mAnimation[index].currentFrame = num;
mCurrentFrame = num;
}
// Establece el valor del contador
void AnimatedSprite::setAnimationCounter(Uint8 index, Uint16 num)
void AnimatedSprite::setAnimationCounter(Uint16 num)
{
mAnimation[index].counter = num;
mAnimationCounter = num;
}
// Establece el rectangulo para un frame de una animación
void AnimatedSprite::setAnimationFrames(Uint8 index, Uint8 frame, int x, int y, int w, int h)
void AnimatedSprite::setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h)
{
mAnimation[index].frame[frame].y = y;
mAnimation[index].frame[frame].w = w;
mAnimation[index].frame[frame].h = h;
mAnimation[index].frame[frame].x = x;
}
// Establece el rectangulo para un frame de una animación
void AnimatedSprite::setAnimationFrames(Uint8 index, Uint8 frame, SDL_Rect rect)
{
mAnimation[index].frame[frame] = rect;
mAnimation[index_animation].frames[index_frame].x = x;
mAnimation[index_animation].frames[index_frame].y = y;
mAnimation[index_animation].frames[index_frame].w = w;
mAnimation[index_animation].frames[index_frame].h = h;
}
// Establece la velocidad de una animación
@@ -116,8 +96,7 @@ void AnimatedSprite::setAnimationSpeed(Uint8 index, Uint8 speed)
// Establece el numero de frames de una animación
void AnimatedSprite::setAnimationNumFrames(Uint8 index, Uint8 num)
{
if (num < MAX_FRAMES)
mAnimation[index].numFrames = num;
mAnimation[index].numFrames = num;
}
// Establece si la animación se reproduce en bucle
@@ -126,21 +105,20 @@ void AnimatedSprite::setAnimationLoop(Uint8 index, bool loop)
mAnimation[index].loop = loop;
}
// Establece el valor de la variable
void AnimatedSprite::setCompleted(Uint8 index, bool value)
{
mAnimation[index].completed = value;
}
// Comprueba si ha terminado la animación
bool AnimatedSprite::isCompleted(Uint8 index)
{
return mAnimation[index].completed;
}
// Devuelve el rectangulo de una animación y frame concreto
SDL_Rect AnimatedSprite::getAnimationClip(Uint8 index, Uint8 frame)
SDL_Rect AnimatedSprite::getAnimationClip(Uint8 index_animation, Uint8 index_frame)
{
return mAnimation[index].frame[frame];
}
// Establece la animación actual
void AnimatedSprite::setCurrentAnimation(Uint8 index)
{
if (index < MAX_ANIMATIONS)
mCurrentAnimation = index;
}
// Obtiene la animación actual
Uint8 AnimatedSprite::getCurrentAnimation()
{
return mCurrentAnimation;
return mAnimation[index_animation].frames[index_frame];
}

View File

@@ -5,39 +5,47 @@
#ifndef ANIMATEDSPRITE_H
#define ANIMATEDSPRITE_H
#define MAX_FRAMES 50
#define MAX_FRAMES 30
#define MAX_ANIMATIONS 20
// Clase AnimatedSprite
class AnimatedSprite : public MovingSprite
{
private:
struct sAnimation
{
SDL_Rect frames[MAX_FRAMES]; // Cada uno de los frames que componen la animación
Uint8 numFrames; // Numero de frames que componen la animación
Uint8 speed; // Velocidad de la animación
bool loop; // Indica si la animación se reproduce en bucle
bool completed; // Indica si ha finalizado la animación
};
sAnimation mAnimation[MAX_ANIMATIONS]; // Vector con las diferentes animaciones
Uint8 mCurrentFrame; // Frame actual
Uint16 mAnimationCounter; // Contador para las animaciones
public:
// Constructor
AnimatedSprite();
AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr);
// Destructor
~AnimatedSprite();
// Iniciador
void init(LTexture *texture, SDL_Renderer *renderer);
void init();
// Calcula el frame correspondiente a la animación
void animate(int index);
// Actualiza todas las variables del objeto: posición, velocidad y animación
void update();
// Establece el frame actual de la animación
void setCurrentFrame(Uint8 index, Uint8 num);
void setCurrentFrame(Uint8 num);
// Establece el valor del contador
void setAnimationCounter(Uint8 index, Uint16 num);
void setAnimationCounter(Uint16 num);
// Establece el rectangulo para un frame de una animación
void setAnimationFrames(Uint8 index, Uint8 frame, int x, int y, int w, int h);
// Establece el rectangulo para un frame de una animación
void setAnimationFrames(Uint8 index, Uint8 frame, SDL_Rect rect);
void setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h);
// Establece la velocidad de una animación
void setAnimationSpeed(Uint8 index, Uint8 speed);
@@ -48,28 +56,14 @@ public:
// Establece si la animación se reproduce en bucle
void setAnimationLoop(Uint8 index, bool loop);
// Establece el valor de la variable
void setCompleted(Uint8 index, bool value);
// Comprueba si ha terminado la animación
bool isCompleted(Uint8 index);
// Devuelve el rectangulo de una animación y frame concreto
SDL_Rect getAnimationClip(Uint8 index, Uint8 frame);
// Establece la animación actual
void setCurrentAnimation(Uint8 index);
// Obtiene la animación actual
Uint8 getCurrentAnimation();
private:
struct sAnimation
{
SDL_Rect frame[MAX_FRAMES]; // Vector con los rectangulos de cada frame de la animación
Uint8 numFrames; // Cantidad de frames de la animacion
Uint8 speed; // Velocidad de la animación
Uint8 currentFrame; // Frame actual
Uint16 counter; // Contador
bool loop; // Indica si la animación se reproduce en bucle
};
sAnimation mAnimation[MAX_ANIMATIONS]; // Vector con las animaciones
Uint8 mCurrentAnimation; // Animación actual;
SDL_Rect getAnimationClip(Uint8 index_animation, Uint8 index_frame);
};
#endif

View File

@@ -2,7 +2,6 @@
#include "ifdefs.h"
#include "sprite.h"
#include "movingsprite.h"
#include "smartsprite.h"
#include "player.h"
#include "map.h"
#include "text.h"

View File

@@ -1,5 +1,4 @@
#include <stdio.h>
#include <string>
#include "const.h"
#include "ltexture.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
@@ -15,7 +14,7 @@ LTexture::LTexture()
LTexture::~LTexture()
{
// Deallocate
free();
unload();
}
bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
@@ -45,19 +44,23 @@ bool LTexture::loadFromFile(std::string path, SDL_Renderer *renderer)
}
// Get rid of preexisting texture
free();
unload();
// The final texture
SDL_Texture *newTexture = NULL;
// Load image at specified path
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void*)data, width, height, depth, pitch, pixel_format);
//SDL_Surface *loadedSurface = IMG_Load(path.c_str());
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
if (loadedSurface == NULL)
{
printf("Unable to load image %s!\n", path.c_str());
}
else
{
// Color key image
//SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, COLOR_KEY_R, COLOR_KEY_G, COLOR_KEY_B));
// Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == NULL)
@@ -97,7 +100,7 @@ bool LTexture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_Te
return mTexture != NULL;
}
void LTexture::free()
void LTexture::unload()
{
// Free texture if it exists
if (mTexture != NULL)
@@ -127,7 +130,7 @@ void LTexture::setAlpha(Uint8 alpha)
SDL_SetTextureAlphaMod(mTexture, alpha);
}
void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, double angle, SDL_Point *center, SDL_RendererFlip flip)
void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_RendererFlip flip)
{
// Set rendering space and render to screen
SDL_Rect renderQuad = {x, y, mWidth, mHeight};
@@ -139,6 +142,9 @@ void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, doub
renderQuad.h = clip->h;
}
renderQuad.w = renderQuad.w * zoomW;
renderQuad.h = renderQuad.h * zoomH;
// Render to screen
SDL_RenderCopyEx(renderer, mTexture, clip, &renderQuad, angle, center, flip);
}

View File

@@ -23,7 +23,7 @@ public:
bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);
// Deallocates texture
void free();
void unload();
// Set color modulation
void setColor(Uint8 red, Uint8 green, Uint8 blue);
@@ -35,7 +35,7 @@ public:
void setAlpha(Uint8 alpha);
// Renders texture at given point
void render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = NULL, double angle = 0.0, SDL_Point *center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE);
void render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = NULL, float zoomW = 1, float zoomH = 1, double angle = 0.0, SDL_Point *center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE);
// Set self as render target
void setAsRenderTarget(SDL_Renderer *renderer);

View File

@@ -25,10 +25,12 @@ Map::~Map()
void Map::init(SDL_Renderer *renderer, LTexture *texture1, LTexture *texture2, LTexture *texture3, std::string file)
{
sprite_tile = new AnimatedSprite();
sprite_tile->init(texture1, renderer);
sprite_tile->setTexture(texture1);
sprite_tile->setRenderer(renderer);
sprite_actor = new AnimatedSprite();
sprite_actor->init(texture2, renderer);
sprite_actor->setTexture(texture2);
sprite_actor->setRenderer(renderer);
background = new Sprite();
background->setTexture(texture3);

View File

@@ -1,23 +1,13 @@
#include "const.h"
#include "movingsprite.h"
#include <iostream>
// Constructor
MovingSprite::MovingSprite()
MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vely, float accelx, float accely, LTexture *texture, SDL_Renderer *renderer)
{
init(0, 0, 0, 0, 0, 0, 0, 0, nullptr, nullptr);
}
// Copia los punteros
setTexture(texture);
setRenderer(renderer);
// Destructor
MovingSprite::~MovingSprite()
{
init(0, 0, 0, 0, 0, 0, 0, 0, nullptr, nullptr);
}
// Iniciador
void MovingSprite::init(float x, float y, Uint16 w, Uint16 h, float velx, float vely, float accelx, float accely,
LTexture *texture, SDL_Renderer *renderer)
{
// Establece el alto y el ancho del sprite
setWidth(w);
setHeight(h);
@@ -34,74 +24,135 @@ void MovingSprite::init(float x, float y, Uint16 w, Uint16 h, float velx, float
setAccelX(accelx);
setAccelY(accely);
// Establece la textura donde están los gráficos para el sprite
setTexture(texture);
// Establece el zoom W,H del sprite
setZoomW(1);
setZoomH(1);
// Establece el renderizador
setRenderer(renderer);
// Establece el angulo con el que se dibujará
setAngle(0.0);
// Establece los valores de rotacion
setRotate(false);
setRotateSpeed(0);
setRotateAmount(0.0);
// Contador interno
mCounter = 0;
// Establece el rectangulo de donde coger la imagen
setSpriteClip(0, 0, w, h);
// Establece el centro de rotación
mCenter = {0,0};
// Establece el tipo de volteado
mFlip = SDL_FLIP_NONE;
};
// Destructor
MovingSprite::~MovingSprite()
{
}
// Coloca el sprite en su nueva posición en funcion de la velocidad
// Reinicia todas las variables
void MovingSprite::clear()
{
mPosX = 0.0f; // Posición en el eje X
mPosY = 0.0f; // Posición en el eje Y
mVelX = 0.0f; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
mVelY = 0.0f; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
mAccelX = 0.0f; // Aceleración en el eje X. Variación de la velocidad
mAccelY = 0.0f; // Aceleración en el eje Y. Variación de la velocidad
mZoomW = 1.0f; // Zoom aplicado a la anchura
mZoomH = 1.0f; // Zoom aplicado a la altura
mAngle = 0.0; // Angulo para dibujarlo
mRotate = false; // Indica si ha de rotar
mCenter = {0, 0}; // Centro de rotación
mRotateSpeed = 0; // Velocidad de giro
mRotateAmount = 0.0; // Cantidad de grados a girar en cada iteración
mCounter = 0; // Contador interno
mFlip = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite
}
// Mueve el sprite
void MovingSprite::move()
{
mPosX += mVelX;
mPosY += mVelY;
}
if (mEnabled)
{
mPosX += mVelX;
mPosY += mVelY;
// Actualiza las variables internas del sprite: posición y velocidad
void MovingSprite::update()
{
move();
mVelX += mAccelX;
mVelY += mAccelY;
mVelX += mAccelX;
mVelY += mAccelY;
}
}
// Muestra el sprite por pantalla
void MovingSprite::render()
{
mTexture->render(mRenderer, (int)mPosX, (int)mPosY, &mSpriteClip);
if (mEnabled)
mTexture->render(mRenderer, (int)mPosX, (int)mPosY, &mSpriteClip, mZoomW, mZoomH, mAngle, &mCenter, mFlip);
}
// Establece el valor de la variable
// Obtiene el valor de la variable
float MovingSprite::getPosX()
{
return mPosX;
}
// Establece el valor de la variable
// Obtiene el valor de la variable
float MovingSprite::getPosY()
{
return mPosY;
}
// Establece el valor de la variable
// Obtiene el valor de la variable
float MovingSprite::getVelX()
{
return mVelX;
}
// Establece el valor de la variable
// Obtiene el valor de la variable
float MovingSprite::getVelY()
{
return mVelY;
}
// Establece el valor de la variable
// Obtiene el valor de la variable
float MovingSprite::getAccelX()
{
return mAccelX;
}
// Establece el valor de la variable
// Obtiene el valor de la variable
float MovingSprite::getAccelY()
{
return mAccelY;
}
// Obtiene el valor de la variable
float MovingSprite::getZoomW()
{
return mZoomW;
}
// Obtiene el valor de la variable
float MovingSprite::getZoomH()
{
return mZoomH;
}
// Obtiene el valor de la variable
double MovingSprite::getAngle()
{
return mAngle;
}
// Establece el valor de la variable
void MovingSprite::setPosX(float x)
{
@@ -137,3 +188,118 @@ void MovingSprite::setAccelY(float y)
{
mAccelY = y;
}
// Establece el valor de la variable
void MovingSprite::setZoomW(float w)
{
mZoomW = w;
}
// Establece el valor de la variable
void MovingSprite::setZoomH(float h)
{
mZoomH = h;
}
// Establece el valor de la variable
void MovingSprite::setAngle(double a)
{
mAngle = a;
}
// Incrementa el valor de la variable
void MovingSprite::incAngle(double inc)
{
mAngle += inc;
}
// Decrementa el valor de la variable
void MovingSprite::decAngle(double dec)
{
mAngle -= dec;
}
// Obtiene el valor de la variable
bool MovingSprite::getRotate()
{
return mRotate;
}
// Obtiene el valor de la variable
Uint16 MovingSprite::getRotateSpeed()
{
return mRotateSpeed;
}
// Establece la rotacion
void MovingSprite::rotate()
{
if (mEnabled)
if (mRotate)
{
if (mCounter % mRotateSpeed == 0)
{
incAngle(mRotateAmount);
}
}
}
// Establece el valor de la variable
void MovingSprite::setRotate(bool value)
{
mRotate = value;
}
// Establece el valor de la variable
void MovingSprite::setRotateSpeed(Uint16 value)
{
mRotateSpeed = value;
}
// Establece el valor de la variable
void MovingSprite::setRotateAmount(double value)
{
mRotateAmount = value;
}
// Establece el valor de la variable
void MovingSprite::disableRotate()
{
mRotate = false;
mAngle = 0;
}
// Actualiza las variables internas del objeto
void MovingSprite::update()
{
move();
rotate();
if (mEnabled)
++mCounter %= 60000;
}
// Cambia el sentido de la rotación
void MovingSprite::switchRotate()
{
mRotateAmount *= -1;
}
// Establece el valor de la variable
void MovingSprite::setFlip(SDL_RendererFlip flip)
{
mFlip = flip;
}
// Obtiene el valor de la variable
SDL_RendererFlip MovingSprite::getFlip()
{
return mFlip;
}
// Devuelve el rectangulo donde está el sprite
SDL_Rect MovingSprite::getRect()
{
SDL_Rect rect = {(int)getPosX(), (int)getPosY(), getWidth(), getHeight()};
return rect;
}

View File

@@ -8,23 +8,46 @@
// Clase MovingSprite. Añade posicion y velocidad en punto flotante
class MovingSprite : public Sprite
{
protected:
float mPosX; // Posición en el eje X
float mPosY; // Posición en el eje Y
float mVelX; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
float mVelY; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
float mAccelX; // Aceleración en el eje X. Variación de la velocidad
float mAccelY; // Aceleración en el eje Y. Variación de la velocidad
float mZoomW; // Zoom aplicado a la anchura
float mZoomH; // Zoom aplicado a la altura
double mAngle; // Angulo para dibujarlo
bool mRotate; // Indica si ha de rotar
Uint16 mRotateSpeed; // Velocidad de giro
double mRotateAmount; // Cantidad de grados a girar en cada iteración
Uint16 mCounter; // Contador interno
SDL_Point mCenter; // Centro de rotación
SDL_RendererFlip mFlip; // Indica como se voltea el sprite
public:
// Constructor
MovingSprite();
MovingSprite(float x = 0, float y = 0, int w = 0, int h = 0, float velx = 0, float vely = 0, float accelx = 0, float accely = 0, LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr);
// Destructor
~MovingSprite();
// Iniciador
void init(float x, float y, Uint16 w, Uint16 h, float velx, float vely, float accelx, float accely,
LTexture *texture, SDL_Renderer *renderer);
// Coloca el sprite en su nueva posición en funcion de la velocidad
// Mueve el sprite
void move();
// Actualiza las variables internas del sprite: posición y velocidad
// Rota el sprite
void rotate();
// Actualiza las variables internas del objeto
void update();
// Reinicia todas las variables
void clear();
// Muestra el sprite por pantalla
void render();
@@ -46,6 +69,21 @@ public:
// Obten el valor de la variable
float getAccelY();
// Obten el valor de la variable
float getZoomW();
// Obten el valor de la variable
float getZoomH();
// Obten el valor de la variable
double getAngle();
// Obtiene el valor de la variable
bool getRotate();
// Obtiene el valor de la variable
Uint16 getRotateSpeed();
// Establece el valor de la variable
void setPosX(float x);
@@ -64,15 +102,44 @@ public:
// Establece el valor de la variable
void setAccelY(float y);
private:
float mPosX; // Posición en el eje X
float mPosY; // Posición en el eje Y
// Establece el valor de la variable
void setZoomW(float w);
float mVelX; // Velocidad en el eje X
float mVelY; // Velocidad en el eje Y
// Establece el valor de la variable
void setZoomH(float h);
float mAccelX; // Aceleración en el eje X
float mAccelY; // Aceleración en el eje Y
// Establece el valor de la variable
void setAngle(double a);
// Incrementa el valor de la variable
void incAngle(double inc);
// Decrementa el valor de la variable
void decAngle(double dec);
// Establece el valor de la variable
void setRotate(bool value);
// Establece el valor de la variable
void setRotateSpeed(Uint16 value);
// Establece el valor de la variable
void setRotateAmount(double value);
// Quita el efecto de rotación y deja el sprite en su angulo inicial.
void disableRotate();
// Cambia el sentido de la rotación
void switchRotate();
// Establece el valor de la variable
void setFlip(SDL_RendererFlip flip);
// Obtiene el valor de la variable
SDL_RendererFlip getFlip();
// Devuelve el rectangulo donde está el sprite
SDL_Rect getRect();
};
#endif

View File

@@ -37,7 +37,8 @@ void Player::init(SDL_Renderer *renderer, LTexture *texture)
speed_y = 0;
sprite = new AnimatedSprite();
sprite->init(texture, renderer);
sprite->setTexture(texture);
sprite->setRenderer(renderer);
sprite->setSpriteClip(rect);
sound_coin = 0;

View File

@@ -1,228 +0,0 @@
#include "const.h"
#include "smartsprite.h"
// Constructor
SmartSprite::SmartSprite()
{
AnimatedSprite::init(nullptr, nullptr);
init(nullptr, nullptr);
}
// Destructor
SmartSprite::~SmartSprite()
{
init(nullptr, nullptr);
}
// Inicializador
void SmartSprite::init(LTexture *texture, SDL_Renderer *renderer)
{
setTexture(texture);
setRenderer(renderer);
setPosX(0);
setPosY(0);
setWidth(0);
setHeight(0);
setVelX(0);
setVelY(0);
setAccelX(0);
setAccelY(0);
setSpriteClip(0, 0, 0, 0);
setEnabled(false);
setCounter(0);
mIsOnDestination = false;
mDestX = 0;
mDestY = 0;
// El Id siempre es >=0, por lo tanto si no se le asigna Id se queda en negativo
mId = -1;
}
// Pone a cero los elementos del objeto
void SmartSprite::reset()
{
init(nullptr, nullptr);
}
// Obtiene el valor de la variable
bool SmartSprite::isEnabled()
{
return mEnabled;
}
// Establece el valor de la variable
void SmartSprite::setEnabled(bool state)
{
mEnabled = state;
}
// Obtiene el valor del contador
Uint16 SmartSprite::getCounter()
{
return mCounter;
}
// Establece el valor del contador
void SmartSprite::setCounter(Uint16 time)
{
mCounter = time;
}
// Establece el valor del contador
void SmartSprite::resetCounter(Uint16 time)
{
mCounter = mCounterIni;
}
// Establece el valor de la variable
void SmartSprite::setDestX(int value)
{
mDestX = value;
}
// Establece el valor de la variable
void SmartSprite::setDestY(int value)
{
mDestY = value;
}
// Obtiene el valor de la variable
int SmartSprite::getDestX()
{
return mDestX;
}
// Obtiene el valor de la variable
int SmartSprite::getDestY()
{
return mDestY;
}
// Actualiza la posición y comprueba si ha llegado a su destino
bool SmartSprite::update()
{
if (mEnabled)
{
// Actualiza las variables de posición, velocidad y animación
AnimatedSprite::update();
// Comprueba si se desplaza en el eje X hacia la derecha
if ((getAccelX() > 0) || ((getAccelX() == 0) && (getVelX() > 0)))
{
// Comprueba si hemos llegado al destino
if (getPosX() > mDestX)
{
// Lo coloca en posición
setPosX(mDestX);
// Lo detiene
setVelX(0.0f);
setAccelX(0.0f);
}
}
// Comprueba si se desplaza en el eje X hacia la izquierda
else if ((getAccelX() < 0) || ((getAccelX() == 0) && (getVelX() < 0)))
{
// Comprueba si hemos llegado al destino
if (getPosX() < mDestX)
{
// Lo coloca en posición
setPosX(mDestX);
// Lo detiene
setVelX(0.0f);
setAccelX(0.0f);
}
}
// Comprueba si se desplaza en el eje Y hacia abajo
if ((getAccelY() > 0) || ((getAccelY() == 0) && (getVelY() > 0)))
{
// Comprueba si hemos llegado al destino
if (getPosY() > mDestY)
{
// Lo coloca en posición
setPosY(mDestY);
// Lo detiene
setVelY(0.0f);
setAccelY(0.0f);
}
}
// Comprueba si se desplaza en el eje Y hacia arriba
else if ((getAccelY() < 0) || ((getAccelY() == 0) && (getVelY() < 0)))
{
// Comprueba si hemos llegado al destino
if (getPosY() < mDestY)
{
// Lo coloca en posición
setPosY(mDestY);
// Lo detiene
setVelY(0.0f);
setAccelY(0.0f);
}
}
// Comprueba si ha llegado a su destino
if ((getPosX() == mDestX) && (getPosY() == mDestY))
{
mIsOnDestination = true;
}
else
{
mIsOnDestination = false;
}
// Si esta en el destino comprueba su contador
if (mIsOnDestination)
{
// Si el contador es mayor que cero, lo decrementa
if (mCounter > 0)
{
mCounter--;
}
// Si ha llegado a cero, deshabilita el objeto o manda el aviso en función de si tiene Id
else if (mCounter == 0)
{
if (mId < 0)
{
mEnabled = false;
}
else
{
//return true;;
}
}
}
}
return mIsOnDestination;
}
// Obtiene el valor de la variable
bool SmartSprite::isOnDestination()
{
return mIsOnDestination;
}
// Pinta el objeto en pantalla
void SmartSprite::render()
{
if (mEnabled)
{
MovingSprite::render();
}
}
// Establece el valor de la variable
void SmartSprite::setId(int id)
{
mId = id;
}

View File

@@ -1,72 +0,0 @@
#pragma once
#include "animatedsprite.h"
#ifndef SMARTSPRITE_H
#define SMARTSPRITE_H
// Clase SmartSprite
class SmartSprite : public AnimatedSprite
{
public:
// Constructor
SmartSprite();
// Destructor
~SmartSprite();
// Inicializador
void init(LTexture *texture, SDL_Renderer *renderer);
// Pone a cero los elementos del objeto
void reset();
// Obtiene el valor de la variable
bool isEnabled();
// Establece el valor de la variable
void setEnabled(bool state);
// Obtiene el valor del contador
Uint16 getCounter();
// Establece el valor del contador
void setCounter(Uint16 time);
// Inicializa el valor del contador
void resetCounter(Uint16 time);
// Establece el valor de la variable
void setDestX(int value);
// Establece el valor de la variable
void setDestY(int value);
// Obtiene el valor de la variable
int getDestX();
// Obtiene el valor de la variable
int getDestY();
// Obtiene el valor de la variable
bool isOnDestination();
// Establece el valor de la variable
void setId(int id);
// Actualiza la posición y comprueba si ha llegado a su destino
bool update();
// Pinta el objeto en pantalla
void render();
private:
bool mEnabled; // Indica si esta habilitado
bool mIsOnDestination; // Indica si está en el destino
int mDestX; // Posicion de destino en el eje X
int mDestY; // Posicion de destino en el eje Y
int mId; // Identificador
Uint16 mCounter; // Contador
Uint16 mCounterIni; // Valor inicial del contador
};
#endif