From b092d3f86aff1e41b0986df0b87f0c861cd39d74 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 26 Sep 2022 14:06:44 +0200 Subject: [PATCH] =?UTF-8?q?Trabajando=20en=20la=20reescritura=20del=20c?= =?UTF-8?q?=C3=B3digo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/animatedsprite.cpp | 51 ++++++--- source/animatedsprite.h | 11 +- source/balloon.cpp | 39 +++++-- source/game.cpp | 35 ++---- source/instructions.cpp | 13 +-- source/item.cpp | 29 ++--- source/item.h | 5 +- source/logo.cpp | 226 ++++++++++++++++++++------------------ source/logo.h | 43 +++++--- source/player.cpp | 2 +- 10 files changed, 244 insertions(+), 210 deletions(-) diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index 132ab02..3e22cda 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -103,30 +103,42 @@ void AnimatedSprite::setAnimationCounter(std::string name, int 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 void AnimatedSprite::setAnimationSpeed(std::string name, int 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 void AnimatedSprite::setAnimationLoop(std::string name, int 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 void AnimatedSprite::setAnimationCompleted(std::string name, bool 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 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 void AnimatedSprite::update() { @@ -295,14 +320,14 @@ void AnimatedSprite::update() // 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) { - animation[index_animation].frames[index_frame].x = x; - animation[index_animation].frames[index_frame].y = y; - animation[index_animation].frames[index_frame].w = w; - animation[index_animation].frames[index_frame].h = h; + animation[index_animation].frames.push_back({x, y, w, h}); } -// Establece el numero de frames de una animación -void AnimatedSprite::setAnimationNumFrames(Uint8 index, Uint8 num) +// OLD - Establece el contador para todas las animaciones +void AnimatedSprite::setAnimationCounter(int value) { - animation[index].numFrames = num; + for (auto &a:animation) + { + a.counter = value; + } } \ No newline at end of file diff --git a/source/animatedsprite.h b/source/animatedsprite.h index a88fe16..4d843a3 100644 --- a/source/animatedsprite.h +++ b/source/animatedsprite.h @@ -42,16 +42,18 @@ public: // Establece el valor del contador void setAnimationCounter(std::string name, int num); - void setAnimationCounter(int index, int num); // Establece la velocidad de una animación void setAnimationSpeed(std::string name, int speed); + void setAnimationSpeed(int index, int speed); // Establece el frame al que vuelve la animación al finalizar void setAnimationLoop(std::string name, int loop); + void setAnimationLoop(int index, int loop); // Establece el valor de la variable void setAnimationCompleted(std::string name, bool value); + void setAnimationCompleted(int index, bool value); // Comprueba si ha terminado la animación bool animationIsCompleted(); @@ -67,15 +69,16 @@ public: // Establece la animacion actual void setCurrentAnimation(std::string name = "default"); + void setCurrentAnimation(int index = 0); // Actualiza las variables del objeto 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); - // Establece el numero de frames de una animación - void setAnimationNumFrames(Uint8 index, Uint8 num) + // OLD - Establece el contador para todas las animaciones + void setAnimationCounter(int value); }; #endif \ No newline at end of file diff --git a/source/balloon.cpp b/source/balloon.cpp index ea3cc8f..b00564a 100644 --- a/source/balloon.cpp +++ b/source/balloon.cpp @@ -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 for (int i = 0; i < NUM_FRAMES_BALLON; i++) + { 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++) + { 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++) + { mSprite->setAnimationFrames(BALLOON_POP_ANIMATION, i, 50 + OFFSET_EXPLOSIONS, 21 + (37 * i), getWidth(), getHeight()); + } break; @@ -407,13 +413,13 @@ void Balloon::init(float x, float y, Uint8 kind, float velx, float speed, Uint16 mKind = kind; // Inicializa las variables para la animación - mSprite->setCurrentFrame(0); - mSprite->setAnimationCounter(0); + // mSprite->setCurrentFrame(0); + // mSprite->setAnimationCounter(0); // Establece el numero de frames de cada animacion - mSprite->setAnimationNumFrames(BALLOON_MOVING_ANIMATION, NUM_FRAMES_BALLON); - mSprite->setAnimationNumFrames(BALLOON_POP_ANIMATION, NUM_FRAMES_BALLON_POP); - mSprite->setAnimationNumFrames(BALLOON_BORN_ANIMATION, NUM_FRAMES_BALLON_BORN); + // mSprite->setAnimationNumFrames(BALLOON_MOVING_ANIMATION, NUM_FRAMES_BALLON); + // mSprite->setAnimationNumFrames(BALLOON_POP_ANIMATION, NUM_FRAMES_BALLON_POP); + // mSprite->setAnimationNumFrames(BALLOON_BORN_ANIMATION, NUM_FRAMES_BALLON_BORN); // Establece la velocidad de cada animación mSprite->setAnimationSpeed(BALLOON_MOVING_ANIMATION, 10); @@ -631,9 +637,9 @@ void Balloon::updateState() { setInvulnerable(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; disable(); } @@ -699,11 +705,14 @@ void Balloon::updateState() { // Si está detenido, reduce el contador if (mStoppedCounter > 0) + { mStoppedCounter--; + } // Si el contador ha llegado a cero, ya no está detenido else if (!isPopping()) - // Quitarles el estado "detenido" si no estan explosionando + { // Quitarles el estado "detenido" si no estan explosionando setStop(false); + } } } @@ -712,11 +721,19 @@ void Balloon::updateAnimation() { // Establece el frame de animación if (isPopping()) - mSprite->animate(BALLOON_POP_ANIMATION); + { + mSprite->setCurrentAnimation(BALLOON_POP_ANIMATION); + } else if (isBeingCreated()) - mSprite->animate(BALLOON_BORN_ANIMATION); + { + mSprite->setCurrentAnimation(BALLOON_BORN_ANIMATION); + } else - mSprite->animate(BALLOON_MOVING_ANIMATION); + { + mSprite->setCurrentAnimation(BALLOON_MOVING_ANIMATION); + } + + mSprite->animate(); } // Comprueba si el globo está habilitado diff --git a/source/game.cpp b/source/game.cpp index 8739f2b..eca8500 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -75,19 +75,19 @@ Game::Game(int numPlayers, int currentStage, SDL_Renderer *renderer, Screen *scr mFade = new Fade(mRenderer); mEventHandler = new SDL_Event(); - mClouds1a = new MovingSprite(); - mClouds1b = new MovingSprite(); - mClouds2a = new MovingSprite(); - mClouds2b = new MovingSprite(); + mClouds1a = new MovingSprite(0, 0, 256, 52, -0.4f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer); + mClouds1b = new MovingSprite(256, 0, 256, 52, -0.4f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer); + mClouds2a = new MovingSprite(0, 52, 256, 32, -0.2f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer); + mClouds2b = new MovingSprite(256, 52, 256, 32, -0.2f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer); m1000Bitmap = new SmartSprite(); m2500Bitmap = new SmartSprite(); m5000Bitmap = new SmartSprite(); - mSpriteBackground = new Sprite(); - mSpriteGetReady = new Sprite(); - mSpriteGradient = new Sprite(); - mSpriteGrass = new Sprite(); - mSpritePowerMeter = new Sprite(); - mSpriteScoreBoard = new Sprite(); + mSpriteBackground = new Sprite(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mTextureGameBG, mRenderer); + mSpriteGetReady = new Sprite(0, PLAY_AREA_CENTER_Y - 10, 109, 20, mTextureGameText, mRenderer); + mSpriteGradient = new Sprite(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mTextureGameBG, mRenderer); + mSpriteGrass = new Sprite(0, 85, SCREEN_WIDTH, 6, mTextureGameBG, mRenderer); + mSpritePowerMeter = new Sprite(PLAY_AREA_CENTER_X - 20, 170, 40, 8, mTextureGameBG, mRenderer); + mSpriteScoreBoard = new Sprite(0, 160, SCREEN_WIDTH, 32, mTextureGameBG, mRenderer); } Game::~Game() @@ -417,25 +417,13 @@ void Game::init() mMenuGameOver->centerMenuElementsOnX(); // Sprites - mClouds1a->init(0, 0, 256, 52, -0.4f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer); 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); - - mClouds2a->init(0, 52, 256, 32, -0.2f, 0.0f, 0.0f, 0.0f, mTextureGameBG, mRenderer); 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); - mSpriteGrass->init(0, 85, SCREEN_WIDTH, 6, mTextureGameBG, mRenderer); mSpriteGrass->setPosY(154); - - mSpriteScoreBoard->init(0, 160, SCREEN_WIDTH, 32, mTextureGameBG, mRenderer); 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); // Vector de jugadores @@ -466,7 +454,6 @@ void Game::init() resetSmartSprites(); // Inicializa el bitmap de GetReady! - mSpriteGetReady->init(0, PLAY_AREA_CENTER_Y - 10, 109, 20, mTextureGameText, mRenderer); mSpriteGetReady->setSpriteClip(0, 0, 109, 20); // Inicializa el bitmap de 1000 puntos @@ -518,8 +505,6 @@ void Game::init() m5000Bitmap->setDestY(0); // 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[1] = {256, 192, SCREEN_WIDTH, SCREEN_HEIGHT}; mGradientRect[2] = {0, 384, SCREEN_WIDTH, SCREEN_HEIGHT}; diff --git a/source/instructions.cpp b/source/instructions.cpp index a6e7cd6..269e599 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -14,7 +14,7 @@ Instructions::Instructions(SDL_Renderer *renderer, Screen *screen, Asset *mAsset // Reserva memoria para los punteros mEventHandler = new SDL_Event(); 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); // Crea un backbuffer para el renderizador @@ -68,7 +68,6 @@ void Instructions::init() // Inicializa variables mSection.name = SELF; - mSprite->init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mItemTexture, mRenderer); mTicks = 0; mTicksSpeed = 15; 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); // Disquito - mSprite->init(destRect1, mItemTexture, mRenderer); + mSprite->setPos(destRect1); srcRect.x = 0; srcRect.y = 16 * (((mCounter + 12) / 36) % 2); mSprite->setSpriteClip(srcRect); mSprite->render(); // Gavineixon - mSprite->init(destRect2, mItemTexture, mRenderer); + mSprite->setPos(destRect2); srcRect.x += srcRect.w; srcRect.y = 16 * (((mCounter + 9) / 36) % 2); mSprite->setSpriteClip(srcRect); mSprite->render(); // Pacmar - mSprite->init(destRect3, mItemTexture, mRenderer); + mSprite->setPos(destRect3); srcRect.x += srcRect.w; srcRect.y = 16 * (((mCounter + 6) / 36) % 2); mSprite->setSpriteClip(srcRect); mSprite->render(); // Time Stopper - mSprite->init(destRect4, mItemTexture, mRenderer); + mSprite->setPos(destRect4); srcRect.x += srcRect.w; srcRect.y = 16 * (((mCounter + 3) / 36) % 2); mSprite->setSpriteClip(srcRect); mSprite->render(); // Coffee - mSprite->init(destRect5, mItemTexture, mRenderer); + mSprite->setPos(destRect5); srcRect.x += (srcRect.w * 2); // Se salta el icono del TNT srcRect.y = 16 * (((mCounter + 0) / 36) % 2); mSprite->setSpriteClip(srcRect); diff --git a/source/item.cpp b/source/item.cpp index 73d78dd..f311c1f 100644 --- a/source/item.cpp +++ b/source/item.cpp @@ -2,22 +2,11 @@ #include "item.h" // 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; -} -// Destructor -Item::~Item() -{ - delete mSprite; - mSprite = nullptr; -} - -// Iniciador -void Item::init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *renderer) -{ mClass = value; mEnabled = true; mTimeToLive = 600; @@ -33,12 +22,10 @@ void Item::init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer * mCollider.r = mWidth / 2; shiftColliders(); - mSprite->init(texture, renderer); mSprite->setAnimationFrames(0, 0, 0, 48, mWidth, mHeight); mSprite->setAnimationFrames(0, 1, 0, 64, mWidth, mHeight); mSprite->setCurrentFrame(0); mSprite->setAnimationCounter(0); - mSprite->setAnimationNumFrames(0, 2); mSprite->setAnimationSpeed(0, 10); mSprite->setAnimationLoop(0, true); 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; mVelY = -0.1f; mAccelY = 0.1f; - mSprite->setAnimationNumFrames(0, 4); mSprite->setAnimationFrames(0, 0, 32 * 0, 16 * 2, mWidth, mHeight); mSprite->setAnimationFrames(0, 1, 32 * 1, 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 void Item::allignTo(int x) { @@ -202,7 +195,7 @@ void Item::move() // Pone a cero todos los valores del objeto 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 @@ -285,8 +278,6 @@ circle_t &Item::getCollider() // Alinea el circulo de colisión con la posición del objeto void Item::shiftColliders() { - //mCollider.x = int(mPosX + mCollider.r); - //mCollider.y = int(mPosY + mCollider.r); mCollider.x = int(mPosX + (mWidth / 2)); mCollider.y = int(mPosY + (mHeight / 2)); } diff --git a/source/item.h b/source/item.h index 8707dcc..5c1e566 100644 --- a/source/item.h +++ b/source/item.h @@ -43,14 +43,11 @@ public: Uint16 mTimeToLive; // Temporizador con el tiempo que el objeto está presente // Constructor - Item(); + Item(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *renderer); // Destructor ~Item(); - // Iniciador - void init(Uint8 value, float x, float y, LTexture *texture, SDL_Renderer *renderer); - // Centra el objeto en la posición X void allignTo(int x); diff --git a/source/logo.cpp b/source/logo.cpp index 0a4cbcb..631dc5b 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -4,146 +4,154 @@ #define END_LOGO 200 // 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 - mRenderer = renderer; - mScreen = screen; - this->mAsset = mAsset; + this->renderer = renderer; + this->screen = screen; + this->asset = asset; // Reserva memoria para los punteros - mEventHandler = new SDL_Event(); - mTexture = new LTexture(mRenderer); - mSprite = new Sprite(); + eventHandler = new SDL_Event(); + texture = new LTexture(renderer,asset->get("logo.png"); + sprite = new Sprite(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, texture, renderer); // Crea un backbuffer para el renderizador - mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT); - if (mBackbuffer == nullptr) + backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT); + if (backbuffer == nullptr) { 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 Logo::~Logo() { - mRenderer = nullptr; - mScreen = nullptr; - mAsset = nullptr; + SDL_DestroyTexture(backbuffer); - mTexture->unload(); - delete mTexture; - mTexture = nullptr; + texture->unload(); + delete texture; - delete mSprite; - mSprite = nullptr; - - delete mEventHandler; - mEventHandler = nullptr; - - SDL_DestroyTexture(mBackbuffer); - mBackbuffer = nullptr; + delete sprite; + delete eventHandler; } -// Inicializa las variables necesarias para la sección 'Logo' -void Logo::init() +// Comprueba si ha terminado el logo +void Logo::checkLogoEnd() { - // Carga los recursos - loadMedia(); + if (counter == 0) + { + if (JA_GetMusicState() == JA_MUSIC_PLAYING) + { + JA_StopMusic(); + } + } - // Inicializa variables - mCounter = 0; - mSection.name = PROG_SECTION_LOGO; - mSection.subsection = 0; - mSprite->init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mTexture, mRenderer); - mTicks = 0; - mTicksSpeed = 15; + if (counter == END_LOGO + 20) + { + counter = 0; + section.name = PROG_SECTION_INTRO; + section.subsection = 0; + } + else + { + counter++; + } } -// Carga los recursos necesarios para la sección 'Logo' -bool Logo::loadMedia() +// Comprueba los eventos +void Logo::checkEventHandler() { - bool success = true; + // 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; + } - success &= loadTextureFromFile(mTexture, mAsset->get("logo.png"), mRenderer); + // Cualquier tecla pulsada + if ((eventHandler->type == SDL_KEYDOWN) || (eventHandler->type == SDL_JOYBUTTONDOWN)) + { + section.name = PROG_SECTION_TITLE; + section.subsection = TITLE_SECTION_1; + } + } +} - return success; +// 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 section_t Logo::run() { - init(); - const SDL_Rect rect = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT}; - const int fadeLenght = END_LOGO - INIT_FADE; - - while (mSection.name == PROG_SECTION_LOGO) + while (section.name == PROG_SECTION_LOGO) { - // Comprueba los eventos que hay en la cola - while (SDL_PollEvent(mEventHandler) != 0) - { - // Evento de salida de la aplicación - if (mEventHandler->type == SDL_QUIT) - { - mSection.name = PROG_SECTION_QUIT; - break; - } - - // Cualquier tecla pulsada - 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++; - } - } + update(); + render(); } - return mSection; + return section; } diff --git a/source/logo.h b/source/logo.h index 5196cde..05abaa8 100644 --- a/source/logo.h +++ b/source/logo.h @@ -15,17 +15,32 @@ class Logo { private: - SDL_Renderer *mRenderer; // El renderizador de la ventana - Screen *mScreen; // Objeto encargado de dibujar en pantalla - Asset *mAsset; // Objeto que gestiona todos los ficheros de recursos - LTexture *mTexture; // Textura con los graficos - SDL_Event *mEventHandler; // Manejador de eventos - SDL_Texture *mBackbuffer; // Textura para usar como backbuffer - Sprite *mSprite; // Sprite con la textura del logo - Uint16 mCounter; // Contador - section_t mSection; // Estado del bucle principal para saber si continua o se sale - Uint32 mTicks; // Contador de ticks para ajustar la velocidad del programa - Uint8 mTicksSpeed; // Velocidad a la que se repiten los bucles del programa + SDL_Renderer *renderer; // El renderizador de la ventana + Screen *screen; // Objeto encargado de dibujar en pantalla + Asset *asset; // Objeto que gestiona todos los ficheros de recursos + LTexture *texture; // Textura con los graficos + SDL_Event *eventHandler; // Manejador de eventos + SDL_Texture *backbuffer; // Textura para usar como backbuffer + Sprite *sprite; // Sprite con la textura del logo + int counter; // Contador + section_t section; // Estado del bucle principal para saber si continua o se sale + Uint32 ticks; // Contador de ticks para ajustar la velocidad 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: // Constructor @@ -34,12 +49,6 @@ public: // Destructor ~Logo(); - // Inicializa las variables - void init(); - - // Carga los recursos - bool loadMedia(); - // Bucle principal section_t run(); }; diff --git a/source/player.cpp b/source/player.cpp index 0dccb37..ce6e65b 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -74,7 +74,7 @@ void Player::init(float x, int y, LTexture *textureLegs, LTexture *textureBody, mCooldown = 10; // Inicia el sprite - mSpriteLegs->init(textureLegs, renderer); + mSpriteLegs->init(); mSpriteBody->init(textureBody, renderer); mSpriteHead->init(textureHead, renderer);