Trabajando en la reescritura del código

This commit is contained in:
2022-09-26 14:06:44 +02:00
parent 837bcbd1da
commit b092d3f86a
10 changed files with 244 additions and 210 deletions

View File

@@ -103,30 +103,42 @@ void AnimatedSprite::setAnimationCounter(std::string name, int num)
animation[getIndex(name)].counter = num; animation[getIndex(name)].counter = num;
} }
// Establece el valor del contador
void AnimatedSprite::setAnimationCounter(int index, int num)
{
animation[index].counter = num;
}
// Establece la velocidad de una animación // Establece la velocidad de una animación
void AnimatedSprite::setAnimationSpeed(std::string name, int speed) void AnimatedSprite::setAnimationSpeed(std::string name, int speed)
{ {
animation[getIndex(name)].counter = speed; animation[getIndex(name)].counter = speed;
} }
// Establece la velocidad de una animación
void AnimatedSprite::setAnimationSpeed(int index, int speed)
{
animation[index].counter = speed;
}
// Establece si la animación se reproduce en bucle // Establece si la animación se reproduce en bucle
void AnimatedSprite::setAnimationLoop(std::string name, int loop) void AnimatedSprite::setAnimationLoop(std::string name, int loop)
{ {
animation[getIndex(name)].loop = loop; animation[getIndex(name)].loop = loop;
} }
// Establece si la animación se reproduce en bucle
void AnimatedSprite::setAnimationLoop(int index, int loop)
{
animation[index].loop = loop;
}
// Establece el valor de la variable // Establece el valor de la variable
void AnimatedSprite::setAnimationCompleted(std::string name, bool value) void AnimatedSprite::setAnimationCompleted(std::string name, bool value)
{ {
animation[getIndex(name)].completed = value; animation[getIndex(name)].completed = value;
} }
// OLD - Establece el valor de la variable
void AnimatedSprite::setAnimationCompleted(int index, bool value)
{
animation[index].completed = value;
}
// Comprueba si ha terminado la animación // Comprueba si ha terminado la animación
bool AnimatedSprite::animationIsCompleted() bool AnimatedSprite::animationIsCompleted()
{ {
@@ -285,6 +297,19 @@ void AnimatedSprite::setCurrentAnimation(std::string name)
} }
} }
// Establece la animacion actual
void AnimatedSprite::setCurrentAnimation(int index)
{
const int newAnimation = index;
if (currentAnimation != newAnimation)
{
currentAnimation = newAnimation;
animation[currentAnimation].currentFrame = 0;
animation[currentAnimation].counter = 0;
animation[currentAnimation].completed = false;
}
}
// Actualiza las variables del objeto // Actualiza las variables del objeto
void AnimatedSprite::update() void AnimatedSprite::update()
{ {
@@ -295,14 +320,14 @@ void AnimatedSprite::update()
// Establece el rectangulo para un frame de una animación // Establece el rectangulo para un frame de una animación
void AnimatedSprite::setAnimationFrames(Uint8 index_animation, Uint8 index_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)
{ {
animation[index_animation].frames[index_frame].x = x; animation[index_animation].frames.push_back({x, y, w, h});
animation[index_animation].frames[index_frame].y = y;
animation[index_animation].frames[index_frame].w = w;
animation[index_animation].frames[index_frame].h = h;
} }
// Establece el numero de frames de una animación // OLD - Establece el contador para todas las animaciones
void AnimatedSprite::setAnimationNumFrames(Uint8 index, Uint8 num) void AnimatedSprite::setAnimationCounter(int value)
{ {
animation[index].numFrames = num; for (auto &a:animation)
{
a.counter = value;
}
} }

View File

@@ -42,16 +42,18 @@ public:
// Establece el valor del contador // Establece el valor del contador
void setAnimationCounter(std::string name, int num); void setAnimationCounter(std::string name, int num);
void setAnimationCounter(int index, int num);
// Establece la velocidad de una animación // Establece la velocidad de una animación
void setAnimationSpeed(std::string name, int speed); void setAnimationSpeed(std::string name, int speed);
void setAnimationSpeed(int index, int speed);
// Establece el frame al que vuelve la animación al finalizar // Establece el frame al que vuelve la animación al finalizar
void setAnimationLoop(std::string name, int loop); void setAnimationLoop(std::string name, int loop);
void setAnimationLoop(int index, int loop);
// Establece el valor de la variable // Establece el valor de la variable
void setAnimationCompleted(std::string name, bool value); void setAnimationCompleted(std::string name, bool value);
void setAnimationCompleted(int index, bool value);
// Comprueba si ha terminado la animación // Comprueba si ha terminado la animación
bool animationIsCompleted(); bool animationIsCompleted();
@@ -67,15 +69,16 @@ public:
// Establece la animacion actual // Establece la animacion actual
void setCurrentAnimation(std::string name = "default"); void setCurrentAnimation(std::string name = "default");
void setCurrentAnimation(int index = 0);
// Actualiza las variables del objeto // Actualiza las variables del objeto
void update(); void update();
// Establece el rectangulo para un frame de una animación // OLD - Establece el rectangulo para un frame de una animación
void setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h); void setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h);
// Establece el numero de frames de una animación // OLD - Establece el contador para todas las animaciones
void setAnimationNumFrames(Uint8 index, Uint8 num) void setAnimationCounter(int value);
}; };
#endif #endif

View File

@@ -55,13 +55,19 @@ void Balloon::init(float x, float y, Uint8 kind, float velx, float speed, Uint16
// Establece los frames de cada animación // Establece los frames de cada animación
for (int i = 0; i < NUM_FRAMES_BALLON; i++) for (int i = 0; i < NUM_FRAMES_BALLON; i++)
{
mSprite->setAnimationFrames(BALLOON_MOVING_ANIMATION, i, 50 + OFFSET_ORANGE_BALLOONS, 21 + (37 * i), getWidth(), getHeight()); mSprite->setAnimationFrames(BALLOON_MOVING_ANIMATION, i, 50 + OFFSET_ORANGE_BALLOONS, 21 + (37 * i), getWidth(), getHeight());
}
for (int i = 0; i < NUM_FRAMES_BALLON_BORN; i++) for (int i = 0; i < NUM_FRAMES_BALLON_BORN; i++)
{
mSprite->setAnimationFrames(BALLOON_BORN_ANIMATION, i, 50 + OFFSET_BLUE_BALLOONS, 21 + (37 * i), getWidth(), getHeight()); mSprite->setAnimationFrames(BALLOON_BORN_ANIMATION, i, 50 + OFFSET_BLUE_BALLOONS, 21 + (37 * i), getWidth(), getHeight());
}
for (int i = 0; i < NUM_FRAMES_BALLON_POP; i++) for (int i = 0; i < NUM_FRAMES_BALLON_POP; i++)
{
mSprite->setAnimationFrames(BALLOON_POP_ANIMATION, i, 50 + OFFSET_EXPLOSIONS, 21 + (37 * i), getWidth(), getHeight()); mSprite->setAnimationFrames(BALLOON_POP_ANIMATION, i, 50 + OFFSET_EXPLOSIONS, 21 + (37 * i), getWidth(), getHeight());
}
break; break;
@@ -407,13 +413,13 @@ void Balloon::init(float x, float y, Uint8 kind, float velx, float speed, Uint16
mKind = kind; mKind = kind;
// Inicializa las variables para la animación // Inicializa las variables para la animación
mSprite->setCurrentFrame(0); // mSprite->setCurrentFrame(0);
mSprite->setAnimationCounter(0); // mSprite->setAnimationCounter(0);
// Establece el numero de frames de cada animacion // Establece el numero de frames de cada animacion
mSprite->setAnimationNumFrames(BALLOON_MOVING_ANIMATION, NUM_FRAMES_BALLON); // mSprite->setAnimationNumFrames(BALLOON_MOVING_ANIMATION, NUM_FRAMES_BALLON);
mSprite->setAnimationNumFrames(BALLOON_POP_ANIMATION, NUM_FRAMES_BALLON_POP); // mSprite->setAnimationNumFrames(BALLOON_POP_ANIMATION, NUM_FRAMES_BALLON_POP);
mSprite->setAnimationNumFrames(BALLOON_BORN_ANIMATION, NUM_FRAMES_BALLON_BORN); // mSprite->setAnimationNumFrames(BALLOON_BORN_ANIMATION, NUM_FRAMES_BALLON_BORN);
// Establece la velocidad de cada animación // Establece la velocidad de cada animación
mSprite->setAnimationSpeed(BALLOON_MOVING_ANIMATION, 10); mSprite->setAnimationSpeed(BALLOON_MOVING_ANIMATION, 10);
@@ -631,9 +637,9 @@ void Balloon::updateState()
{ {
setInvulnerable(true); setInvulnerable(true);
setStop(true); setStop(true);
if (mSprite->isCompleted(BALLOON_POP_ANIMATION)) if (mSprite->animationIsCompleted())
{ {
mSprite->setCompleted(BALLOON_POP_ANIMATION, false); mSprite->setAnimationCompleted(BALLOON_POP_ANIMATION, false);
mTimeToLive = 0; mTimeToLive = 0;
disable(); disable();
} }
@@ -699,24 +705,35 @@ void Balloon::updateState()
{ {
// Si está detenido, reduce el contador // Si está detenido, reduce el contador
if (mStoppedCounter > 0) if (mStoppedCounter > 0)
{
mStoppedCounter--; mStoppedCounter--;
}
// Si el contador ha llegado a cero, ya no está detenido // Si el contador ha llegado a cero, ya no está detenido
else if (!isPopping()) else if (!isPopping())
// Quitarles el estado "detenido" si no estan explosionando { // Quitarles el estado "detenido" si no estan explosionando
setStop(false); setStop(false);
} }
} }
}
// Establece la animación correspondiente al estado // Establece la animación correspondiente al estado
void Balloon::updateAnimation() void Balloon::updateAnimation()
{ {
// Establece el frame de animación // Establece el frame de animación
if (isPopping()) if (isPopping())
mSprite->animate(BALLOON_POP_ANIMATION); {
mSprite->setCurrentAnimation(BALLOON_POP_ANIMATION);
}
else if (isBeingCreated()) else if (isBeingCreated())
mSprite->animate(BALLOON_BORN_ANIMATION); {
mSprite->setCurrentAnimation(BALLOON_BORN_ANIMATION);
}
else else
mSprite->animate(BALLOON_MOVING_ANIMATION); {
mSprite->setCurrentAnimation(BALLOON_MOVING_ANIMATION);
}
mSprite->animate();
} }
// Comprueba si el globo está habilitado // Comprueba si el globo está habilitado

View File

@@ -75,19 +75,19 @@ Game::Game(int numPlayers, int currentStage, SDL_Renderer *renderer, Screen *scr
mFade = new Fade(mRenderer); mFade = new Fade(mRenderer);
mEventHandler = new SDL_Event(); mEventHandler = new SDL_Event();
mClouds1a = new MovingSprite(); mClouds1a = new MovingSprite(0, 0, 256, 52, -0.4f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer);
mClouds1b = new MovingSprite(); mClouds1b = new MovingSprite(256, 0, 256, 52, -0.4f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer);
mClouds2a = new MovingSprite(); mClouds2a = new MovingSprite(0, 52, 256, 32, -0.2f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer);
mClouds2b = new MovingSprite(); mClouds2b = new MovingSprite(256, 52, 256, 32, -0.2f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer);
m1000Bitmap = new SmartSprite(); m1000Bitmap = new SmartSprite();
m2500Bitmap = new SmartSprite(); m2500Bitmap = new SmartSprite();
m5000Bitmap = new SmartSprite(); m5000Bitmap = new SmartSprite();
mSpriteBackground = new Sprite(); mSpriteBackground = new Sprite(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mTextureGameBG, mRenderer);
mSpriteGetReady = new Sprite(); mSpriteGetReady = new Sprite(0, PLAY_AREA_CENTER_Y - 10, 109, 20, mTextureGameText, mRenderer);
mSpriteGradient = new Sprite(); mSpriteGradient = new Sprite(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mTextureGameBG, mRenderer);
mSpriteGrass = new Sprite(); mSpriteGrass = new Sprite(0, 85, SCREEN_WIDTH, 6, mTextureGameBG, mRenderer);
mSpritePowerMeter = new Sprite(); mSpritePowerMeter = new Sprite(PLAY_AREA_CENTER_X - 20, 170, 40, 8, mTextureGameBG, mRenderer);
mSpriteScoreBoard = new Sprite(); mSpriteScoreBoard = new Sprite(0, 160, SCREEN_WIDTH, 32, mTextureGameBG, mRenderer);
} }
Game::~Game() Game::~Game()
@@ -417,25 +417,13 @@ void Game::init()
mMenuGameOver->centerMenuElementsOnX(); mMenuGameOver->centerMenuElementsOnX();
// Sprites // Sprites
mClouds1a->init(0, 0, 256, 52, -0.4f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer);
mClouds1a->setSpriteClip(256, 0, 256, 52); mClouds1a->setSpriteClip(256, 0, 256, 52);
mClouds1b->init(256, 0, 256, 52, -0.4f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer);
mClouds1b->setSpriteClip(256, 0, 256, 52); mClouds1b->setSpriteClip(256, 0, 256, 52);
mClouds2a->init(0, 52, 256, 32, -0.2f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer);
mClouds2a->setSpriteClip(256, 52, 256, 32); mClouds2a->setSpriteClip(256, 52, 256, 32);
mClouds2b->init(256, 52, 256, 32, -0.2f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer);
mClouds2b->setSpriteClip(256, 52, 256, 32); mClouds2b->setSpriteClip(256, 52, 256, 32);
mSpriteGrass->init(0, 85, SCREEN_WIDTH, 6, mTextureGameBG, mRenderer);
mSpriteGrass->setPosY(154); mSpriteGrass->setPosY(154);
mSpriteScoreBoard->init(0, 160, SCREEN_WIDTH, 32, mTextureGameBG, mRenderer);
mSpriteScoreBoard->setSpriteClip(0, 160, 256, 32); mSpriteScoreBoard->setSpriteClip(0, 160, 256, 32);
mSpritePowerMeter->init(PLAY_AREA_CENTER_X - 20, 170, 40, 8, mTextureGameBG, mRenderer);
mSpritePowerMeter->setSpriteClip(256, 192 - 8, 40, 8); mSpritePowerMeter->setSpriteClip(256, 192 - 8, 40, 8);
// Vector de jugadores // Vector de jugadores
@@ -466,7 +454,6 @@ void Game::init()
resetSmartSprites(); resetSmartSprites();
// Inicializa el bitmap de GetReady! // Inicializa el bitmap de GetReady!
mSpriteGetReady->init(0, PLAY_AREA_CENTER_Y - 10, 109, 20, mTextureGameText, mRenderer);
mSpriteGetReady->setSpriteClip(0, 0, 109, 20); mSpriteGetReady->setSpriteClip(0, 0, 109, 20);
// Inicializa el bitmap de 1000 puntos // Inicializa el bitmap de 1000 puntos
@@ -518,8 +505,6 @@ void Game::init()
m5000Bitmap->setDestY(0); m5000Bitmap->setDestY(0);
// Los fondos // Los fondos
mSpriteBackground->init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mTextureGameBG, mRenderer);
mSpriteGradient->init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mTextureGameBG, mRenderer);
mGradientRect[0] = {0, 192, SCREEN_WIDTH, SCREEN_HEIGHT}; mGradientRect[0] = {0, 192, SCREEN_WIDTH, SCREEN_HEIGHT};
mGradientRect[1] = {256, 192, SCREEN_WIDTH, SCREEN_HEIGHT}; mGradientRect[1] = {256, 192, SCREEN_WIDTH, SCREEN_HEIGHT};
mGradientRect[2] = {0, 384, SCREEN_WIDTH, SCREEN_HEIGHT}; mGradientRect[2] = {0, 384, SCREEN_WIDTH, SCREEN_HEIGHT};

View File

@@ -14,7 +14,7 @@ Instructions::Instructions(SDL_Renderer *renderer, Screen *screen, Asset *mAsset
// Reserva memoria para los punteros // Reserva memoria para los punteros
mEventHandler = new SDL_Event(); mEventHandler = new SDL_Event();
mItemTexture = new LTexture(mRenderer); mItemTexture = new LTexture(mRenderer);
mSprite = new Sprite(); mSprite = new Sprite(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mItemTexture, mRenderer);
mText = new Text(mAsset->get("smb2.png"), mAsset->get("smb2.txt"), mRenderer); mText = new Text(mAsset->get("smb2.png"), mAsset->get("smb2.txt"), mRenderer);
// Crea un backbuffer para el renderizador // Crea un backbuffer para el renderizador
@@ -68,7 +68,6 @@ void Instructions::init()
// Inicializa variables // Inicializa variables
mSection.name = SELF; mSection.name = SELF;
mSprite->init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mItemTexture, mRenderer);
mTicks = 0; mTicks = 0;
mTicksSpeed = 15; mTicksSpeed = 15;
mManualQuit = false; mManualQuit = false;
@@ -170,35 +169,35 @@ void Instructions::run(Uint8 mode)
mText->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, SCREEN_CENTER_X, SCREEN_HEIGHT - 12, mLang->getText(22), 1, orangeColor, 1, shdwTxtColor); mText->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, SCREEN_CENTER_X, SCREEN_HEIGHT - 12, mLang->getText(22), 1, orangeColor, 1, shdwTxtColor);
// Disquito // Disquito
mSprite->init(destRect1, mItemTexture, mRenderer); mSprite->setPos(destRect1);
srcRect.x = 0; srcRect.x = 0;
srcRect.y = 16 * (((mCounter + 12) / 36) % 2); srcRect.y = 16 * (((mCounter + 12) / 36) % 2);
mSprite->setSpriteClip(srcRect); mSprite->setSpriteClip(srcRect);
mSprite->render(); mSprite->render();
// Gavineixon // Gavineixon
mSprite->init(destRect2, mItemTexture, mRenderer); mSprite->setPos(destRect2);
srcRect.x += srcRect.w; srcRect.x += srcRect.w;
srcRect.y = 16 * (((mCounter + 9) / 36) % 2); srcRect.y = 16 * (((mCounter + 9) / 36) % 2);
mSprite->setSpriteClip(srcRect); mSprite->setSpriteClip(srcRect);
mSprite->render(); mSprite->render();
// Pacmar // Pacmar
mSprite->init(destRect3, mItemTexture, mRenderer); mSprite->setPos(destRect3);
srcRect.x += srcRect.w; srcRect.x += srcRect.w;
srcRect.y = 16 * (((mCounter + 6) / 36) % 2); srcRect.y = 16 * (((mCounter + 6) / 36) % 2);
mSprite->setSpriteClip(srcRect); mSprite->setSpriteClip(srcRect);
mSprite->render(); mSprite->render();
// Time Stopper // Time Stopper
mSprite->init(destRect4, mItemTexture, mRenderer); mSprite->setPos(destRect4);
srcRect.x += srcRect.w; srcRect.x += srcRect.w;
srcRect.y = 16 * (((mCounter + 3) / 36) % 2); srcRect.y = 16 * (((mCounter + 3) / 36) % 2);
mSprite->setSpriteClip(srcRect); mSprite->setSpriteClip(srcRect);
mSprite->render(); mSprite->render();
// Coffee // Coffee
mSprite->init(destRect5, mItemTexture, mRenderer); mSprite->setPos(destRect5);
srcRect.x += (srcRect.w * 2); // Se salta el icono del TNT srcRect.x += (srcRect.w * 2); // Se salta el icono del TNT
srcRect.y = 16 * (((mCounter + 0) / 36) % 2); srcRect.y = 16 * (((mCounter + 0) / 36) % 2);
mSprite->setSpriteClip(srcRect); mSprite->setSpriteClip(srcRect);

View File

@@ -2,22 +2,11 @@
#include "item.h" #include "item.h"
// Constructor // Constructor
Item::Item() Item::Item(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *renderer)
{ {
mSprite = new AnimatedSprite(); mSprite = new AnimatedSprite(texture, renderer);
mClass = NO_KIND; mClass = NO_KIND;
}
// Destructor
Item::~Item()
{
delete mSprite;
mSprite = nullptr;
}
// Iniciador
void Item::init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *renderer)
{
mClass = value; mClass = value;
mEnabled = true; mEnabled = true;
mTimeToLive = 600; mTimeToLive = 600;
@@ -33,12 +22,10 @@ void Item::init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *
mCollider.r = mWidth / 2; mCollider.r = mWidth / 2;
shiftColliders(); shiftColliders();
mSprite->init(texture, renderer);
mSprite->setAnimationFrames(0, 0, 0, 48, mWidth, mHeight); mSprite->setAnimationFrames(0, 0, 0, 48, mWidth, mHeight);
mSprite->setAnimationFrames(0, 1, 0, 64, mWidth, mHeight); mSprite->setAnimationFrames(0, 1, 0, 64, mWidth, mHeight);
mSprite->setCurrentFrame(0); mSprite->setCurrentFrame(0);
mSprite->setAnimationCounter(0); mSprite->setAnimationCounter(0);
mSprite->setAnimationNumFrames(0, 2);
mSprite->setAnimationSpeed(0, 10); mSprite->setAnimationSpeed(0, 10);
mSprite->setAnimationLoop(0, true); mSprite->setAnimationLoop(0, true);
mSprite->setSpriteClip(mSprite->getAnimationClip(0, 0)); mSprite->setSpriteClip(mSprite->getAnimationClip(0, 0));
@@ -91,7 +78,6 @@ void Item::init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *
mVelX = 0.0f; mVelX = 0.0f;
mVelY = -0.1f; mVelY = -0.1f;
mAccelY = 0.1f; mAccelY = 0.1f;
mSprite->setAnimationNumFrames(0, 4);
mSprite->setAnimationFrames(0, 0, 32 * 0, 16 * 2, mWidth, mHeight); mSprite->setAnimationFrames(0, 0, 32 * 0, 16 * 2, mWidth, mHeight);
mSprite->setAnimationFrames(0, 1, 32 * 1, 16 * 2, mWidth, mHeight); mSprite->setAnimationFrames(0, 1, 32 * 1, 16 * 2, mWidth, mHeight);
mSprite->setAnimationFrames(0, 2, 32 * 2, 16 * 2, mWidth, mHeight); mSprite->setAnimationFrames(0, 2, 32 * 2, 16 * 2, mWidth, mHeight);
@@ -107,6 +93,13 @@ void Item::init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *
} }
} }
// Destructor
Item::~Item()
{
delete mSprite;
mSprite = nullptr;
}
// Centra el objeto en la posición X // Centra el objeto en la posición X
void Item::allignTo(int x) void Item::allignTo(int x)
{ {
@@ -202,7 +195,7 @@ void Item::move()
// Pone a cero todos los valores del objeto // Pone a cero todos los valores del objeto
void Item::erase() void Item::erase()
{ {
init(NO_KIND, 0, 0, nullptr, nullptr); //init(NO_KIND, 0, 0, nullptr, nullptr);
} }
// Actualiza el objeto a su posicion, animación y controla los contadores // Actualiza el objeto a su posicion, animación y controla los contadores
@@ -285,8 +278,6 @@ circle_t &Item::getCollider()
// Alinea el circulo de colisión con la posición del objeto // Alinea el circulo de colisión con la posición del objeto
void Item::shiftColliders() void Item::shiftColliders()
{ {
//mCollider.x = int(mPosX + mCollider.r);
//mCollider.y = int(mPosY + mCollider.r);
mCollider.x = int(mPosX + (mWidth / 2)); mCollider.x = int(mPosX + (mWidth / 2));
mCollider.y = int(mPosY + (mHeight / 2)); mCollider.y = int(mPosY + (mHeight / 2));
} }

View File

@@ -43,14 +43,11 @@ public:
Uint16 mTimeToLive; // Temporizador con el tiempo que el objeto está presente Uint16 mTimeToLive; // Temporizador con el tiempo que el objeto está presente
// Constructor // Constructor
Item(); Item(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *renderer);
// Destructor // Destructor
~Item(); ~Item();
// Iniciador
void init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *renderer);
// Centra el objeto en la posición X // Centra el objeto en la posición X
void allignTo(int x); void allignTo(int x);

View File

@@ -4,146 +4,154 @@
#define END_LOGO 200 #define END_LOGO 200
// Constructor // Constructor
Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *mAsset) Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset)
{ {
// Copia la dirección de los objetos // Copia la dirección de los objetos
mRenderer = renderer; this->renderer = renderer;
mScreen = screen; this->screen = screen;
this->mAsset = mAsset; this->asset = asset;
// Reserva memoria para los punteros // Reserva memoria para los punteros
mEventHandler = new SDL_Event(); eventHandler = new SDL_Event();
mTexture = new LTexture(mRenderer); texture = new LTexture(renderer,asset->get("logo.png");
mSprite = new Sprite(); sprite = new Sprite(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, texture, renderer);
// Crea un backbuffer para el renderizador // Crea un backbuffer para el renderizador
mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT); backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
if (mBackbuffer == nullptr) if (backbuffer == nullptr)
{ {
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError()); printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError());
} }
// Inicializa variables
counter = 0;
section.name = PROG_SECTION_LOGO;
section.subsection = 0;
ticks = 0;
ticksSpeed = 15;
} }
// Destructor // Destructor
Logo::~Logo() Logo::~Logo()
{ {
mRenderer = nullptr; SDL_DestroyTexture(backbuffer);
mScreen = nullptr;
mAsset = nullptr;
mTexture->unload(); texture->unload();
delete mTexture; delete texture;
mTexture = nullptr;
delete mSprite; delete sprite;
mSprite = nullptr; delete eventHandler;
delete mEventHandler;
mEventHandler = nullptr;
SDL_DestroyTexture(mBackbuffer);
mBackbuffer = nullptr;
} }
// Inicializa las variables necesarias para la sección 'Logo' // Comprueba si ha terminado el logo
void Logo::init() void Logo::checkLogoEnd()
{ {
// Carga los recursos if (counter == 0)
loadMedia(); {
if (JA_GetMusicState() == JA_MUSIC_PLAYING)
// Inicializa variables {
mCounter = 0; JA_StopMusic();
mSection.name = PROG_SECTION_LOGO; }
mSection.subsection = 0;
mSprite->init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mTexture, mRenderer);
mTicks = 0;
mTicksSpeed = 15;
} }
// Carga los recursos necesarios para la sección 'Logo' if (counter == END_LOGO + 20)
bool Logo::loadMedia()
{ {
bool success = true; counter = 0;
section.name = PROG_SECTION_INTRO;
section.subsection = 0;
}
else
{
counter++;
}
}
success &= loadTextureFromFile(mTexture, mAsset->get("logo.png"), mRenderer); // Comprueba los eventos
void Logo::checkEventHandler()
{
// Comprueba los eventos que hay en la cola
while (SDL_PollEvent(eventHandler) != 0)
{
// Evento de salida de la aplicación
if (eventHandler->type == SDL_QUIT)
{
section.name = PROG_SECTION_QUIT;
break;
}
return success; // Cualquier tecla pulsada
if ((eventHandler->type == SDL_KEYDOWN) || (eventHandler->type == SDL_JOYBUTTONDOWN))
{
section.name = PROG_SECTION_TITLE;
section.subsection = TITLE_SECTION_1;
}
}
}
// Dibuja el fade
void Logo::renderFade()
{
const SDL_Rect rect = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
const int fadeLenght = END_LOGO - INIT_FADE;
// Dibuja el fade
if (counter >= INIT_FADE)
{
const Uint16 alpha = (255 * (counter - INIT_FADE)) / fadeLenght;
if (alpha < 256)
{
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, alpha);
}
else
{
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
}
SDL_RenderFillRect(renderer, &rect);
}
}
// Actualiza las variables del objeto
void Logo::update()
{
checkEventHandler();
if (SDL_GetTicks() - ticks > ticksSpeed)
{
// Actualiza el contador de ticks
ticks = SDL_GetTicks();
// Comprueba si ha terminado el logo
checkLogoEnd();
}
}
// Dibuja el objeto en pantalla
void Logo::render()
{
// Prepara para empezar a dibujar en la textura de juego
screen->start();
// Limpia la pantalla
screen->clean(bgColor);
// Dibuja los objetos
sprite->render();
// Dibuja el fade
renderFade();
// Vuelca el contenido del renderizador en pantalla
screen->blit();
} }
// Bucle para el logo del juego // Bucle para el logo del juego
section_t Logo::run() section_t Logo::run()
{ {
init(); while (section.name == PROG_SECTION_LOGO)
const SDL_Rect rect = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
const int fadeLenght = END_LOGO - INIT_FADE;
while (mSection.name == PROG_SECTION_LOGO)
{ {
// Comprueba los eventos que hay en la cola update();
while (SDL_PollEvent(mEventHandler) != 0) render();
{
// Evento de salida de la aplicación
if (mEventHandler->type == SDL_QUIT)
{
mSection.name = PROG_SECTION_QUIT;
break;
} }
// Cualquier tecla pulsada return section;
if ((mEventHandler->type == SDL_KEYDOWN) || (mEventHandler->type == SDL_JOYBUTTONDOWN))
{
mSection.name = PROG_SECTION_TITLE;
mSection.subsection = TITLE_SECTION_1;
}
}
// Prepara para empezar a dibujar en la textura de juego
mScreen->start();
// Limpia la pantalla
mScreen->clean(bgColor);
// Dibuja los objetos
mSprite->render();
// Dibuja el fade
if (mCounter >= INIT_FADE)
{
Uint16 alpha = (255 * (mCounter - INIT_FADE)) / fadeLenght;
if (alpha < 256)
SDL_SetRenderDrawColor(mRenderer, bgColor.r, bgColor.g, bgColor.b, alpha);
else
SDL_SetRenderDrawColor(mRenderer, bgColor.r, bgColor.g, bgColor.b, 255);
SDL_RenderFillRect(mRenderer, &rect);
}
// Vuelca el contenido del renderizador en pantalla
mScreen->blit();
// Comprueba si ha terminado el logo
if (SDL_GetTicks() - mTicks > mTicksSpeed)
{
// Actualiza el contador de ticks
mTicks = SDL_GetTicks();
if (mCounter == 0)
{
if (JA_GetMusicState() == JA_MUSIC_PLAYING)
JA_StopMusic();
}
if (mCounter == END_LOGO + 20)
{
mCounter = 0;
mSection.name = PROG_SECTION_INTRO;
mSection.subsection = 0;
}
else
{
mCounter++;
}
}
}
return mSection;
} }

View File

@@ -15,17 +15,32 @@
class Logo class Logo
{ {
private: private:
SDL_Renderer *mRenderer; // El renderizador de la ventana SDL_Renderer *renderer; // El renderizador de la ventana
Screen *mScreen; // Objeto encargado de dibujar en pantalla Screen *screen; // Objeto encargado de dibujar en pantalla
Asset *mAsset; // Objeto que gestiona todos los ficheros de recursos Asset *asset; // Objeto que gestiona todos los ficheros de recursos
LTexture *mTexture; // Textura con los graficos LTexture *texture; // Textura con los graficos
SDL_Event *mEventHandler; // Manejador de eventos SDL_Event *eventHandler; // Manejador de eventos
SDL_Texture *mBackbuffer; // Textura para usar como backbuffer SDL_Texture *backbuffer; // Textura para usar como backbuffer
Sprite *mSprite; // Sprite con la textura del logo Sprite *sprite; // Sprite con la textura del logo
Uint16 mCounter; // Contador int counter; // Contador
section_t mSection; // Estado del bucle principal para saber si continua o se sale section_t section; // Estado del bucle principal para saber si continua o se sale
Uint32 mTicks; // Contador de ticks para ajustar la velocidad del programa Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
Uint8 mTicksSpeed; // Velocidad a la que se repiten los bucles del programa Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
// Actualiza las variables del objeto
void update();
// Dibuja el objeto en pantalla
void render();
// Comprueba si ha terminado el logo
void checkLogoEnd();
// Comprueba los eventos
void checkEventHandler();
// Dibuja el fade
void renderFade();
public: public:
// Constructor // Constructor
@@ -34,12 +49,6 @@ public:
// Destructor // Destructor
~Logo(); ~Logo();
// Inicializa las variables
void init();
// Carga los recursos
bool loadMedia();
// Bucle principal // Bucle principal
section_t run(); section_t run();
}; };

View File

@@ -74,7 +74,7 @@ void Player::init(float x, int y, LTexture *textureLegs, LTexture *textureBody,
mCooldown = 10; mCooldown = 10;
// Inicia el sprite // Inicia el sprite
mSpriteLegs->init(textureLegs, renderer); mSpriteLegs->init();
mSpriteBody->init(textureBody, renderer); mSpriteBody->init(textureBody, renderer);
mSpriteHead->init(textureHead, renderer); mSpriteHead->init(textureHead, renderer);