#include "credits.h" #include // Para SDL_BLENDMODE_BLEND #include // Para SDL_PollEvent, SDL_Event, SDL_QUIT #include // Para SDL_PIXELFORMAT_RGBA8888 #include // Para SDL_Rect #include // Para SDL_GetTicks #include // Para basic_string, string #include // Para vector #include "balloon_manager.h" // Para BalloonManager #include "global_inputs.h" // Para check #include "input.h" // Para Input #include "jail_audio.h" // Para JA_PlayMusic, JA_StopMusic #include "param.h" // Para Param, ParamGame, param #include "resource.h" // Para Resource #include "screen.h" // Para Screen #include "section.h" // Para Name, name, Options, options #include "text.h" // Para Text, TEXT_CENTER, TEXT_SHADOW #include "tiled_bg.h" // Para TiledBG, TiledBGMode #include "utils.h" // Para Color, no_color, shdw_txt_color, Zone // Textos constexpr const char TEXT_COPYRIGHT[] = "@2020,2024 JailDesigner"; // Constructor Credits::Credits() : balloon_manager_(std::make_unique()), text_texture_(SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)), tiled_bg_(std::make_unique(param.game.game_area.rect, TiledBGMode::DIAGONAL)) { section::name = section::Name::CREDITS; balloon_manager_->setPlayArea(param.game.game_area.rect); balloon_manager_->createTwoBigBalloons(); balloon_manager_->deployBalloonFormation(4,2); balloon_manager_->deployBalloonFormation(4,5); balloon_manager_->createPowerBall(); SDL_SetTextureBlendMode(text_texture_, SDL_BLENDMODE_BLEND); fillTextTexture(); JA_PlayMusic(Resource::get()->getMusic("credits.ogg")); } // Destructor Credits::~Credits() { SDL_DestroyTexture(text_texture_); } // Bucle principal void Credits::run() { while (section::name == section::Name::CREDITS) { checkInput(); update(); checkEvents(); // Tiene que ir antes del render render(); } } // Actualiza las variables void Credits::update() { constexpr int TICKS_SPEED = 15; if (SDL_GetTicks() - ticks_ > TICKS_SPEED) { ticks_ = SDL_GetTicks(); tiled_bg_->update(); balloon_manager_->update(); updateRects(); Screen::get()->update(); ++counter_; } } // Dibuja Credits::en patalla void Credits::render() { // Prepara para empezar a dibujar en la textura de juego Screen::get()->start(); // Limpia la pantalla Screen::get()->clean(); tiled_bg_->render(); balloon_manager_->render(); SDL_RenderCopy(Screen::get()->getRenderer(), text_texture_, &credits_rect_src_, &credits_rect_dst_); SDL_RenderCopy(Screen::get()->getRenderer(), text_texture_, &mini_logo_rect_src_, &mini_logo_rect_dst_); // Vuelca el contenido del renderizador en pantalla Screen::get()->blit(); } // Comprueba el manejador de eventos void Credits::checkEvents() { SDL_Event event; // Comprueba los eventos que hay en la cola while (SDL_PollEvent(&event)) { // Evento de salida de la aplicación if (event.type == SDL_QUIT) { section::name = section::Name::QUIT; section::options = section::Options::QUIT_FROM_EVENT; break; } } } // Comprueba las entradas void Credits::checkInput() { // Comprueba si se ha pulsado cualquier botón (de los usados para jugar) if (Input::get()->checkAnyButtonPressed()) { JA_StopMusic(); section::name = section::Name::LOGO; return; } // Comprueba los inputs que se pueden introducir en cualquier sección del juego globalInputs::check(); } // Crea la textura con el texto void Credits::fillTextTexture() { auto text = Resource::get()->getText("smb2"); SDL_SetRenderTarget(Screen::get()->getRenderer(), text_texture_); SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0); SDL_RenderClear(Screen::get()->getRenderer()); std::vector texts = { "PROGRAMACIO I DISSENY", "GRAFICS", "MUSICA", "SONS", "JAILDESIGNER", "JAILDOCTOR (INTRO)", "ERIC MATYAS (SOUNDIMAGE.ORG)", "WWW.THEMOTIONMONKEY.CO.UK", "WWW.KENNEY.NL", "JAILDOCTOR"}; const int space_post_title = 3 + text->getCharacterSize(); const int space_pre_title = text->getCharacterSize() * 3; const int texts_height = 1 * text->getCharacterSize() + 7 * space_post_title + 3 * space_pre_title; credits_rect_dst_.h = credits_rect_src_.h = texts_height; int y = (param.game.height - texts_height) / 2; y = 0; text->setPalette(1); text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, texts.at(0), 1, no_color, 1, shdw_txt_color); text->setPalette(0); y += space_post_title; text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, texts.at(4), 1, no_color, 1, shdw_txt_color); y += space_pre_title; text->setPalette(1); text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, texts.at(1), 1, no_color, 1, shdw_txt_color); text->setPalette(0); y += space_post_title; text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, texts.at(4), 1, no_color, 1, shdw_txt_color); y += space_pre_title; text->setPalette(1); text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, texts.at(2), 1, no_color, 1, shdw_txt_color); text->setPalette(0); y += space_post_title; text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, texts.at(5), 1, no_color, 1, shdw_txt_color); y += space_post_title; text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, texts.at(6), 1, no_color, 1, shdw_txt_color); y += space_pre_title; text->setPalette(1); text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, texts.at(3), 1, no_color, 1, shdw_txt_color); text->setPalette(0); y += space_post_title; text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, texts.at(7), 1, no_color, 1, shdw_txt_color); y += space_post_title; text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, texts.at(8), 1, no_color, 1, shdw_txt_color); y += space_post_title; text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, texts.at(9), 1, no_color, 1, shdw_txt_color); // Mini logo y += space_pre_title; mini_logo_rect_src_.y = y; auto mini_logo_sprite = std::make_unique(Resource::get()->getTexture("logo_jailgames_mini.png")); mini_logo_sprite->setPosition(1 + param.game.game_area.center_x - mini_logo_sprite->getWidth() / 2, 1 + y); Resource::get()->getTexture("logo_jailgames_mini.png")->setColor(shdw_txt_color.r, shdw_txt_color.g, shdw_txt_color.b); mini_logo_sprite->render(); mini_logo_sprite->setPosition(param.game.game_area.center_x - mini_logo_sprite->getWidth() / 2, y); Resource::get()->getTexture("logo_jailgames_mini.png")->setColor(255, 255, 255); mini_logo_sprite->render(); // Texto con el copyright y += mini_logo_sprite->getHeight() + 3; text->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, y, TEXT_COPYRIGHT, 1, no_color, 1, shdw_txt_color); mini_logo_rect_dst_.h = mini_logo_rect_src_.h = mini_logo_sprite->getHeight() + 3 + text->getCharacterSize(); SDL_SetRenderTarget(Screen::get()->getRenderer(), nullptr); credits_rect_dst_.y = param.game.game_area.rect.h; mini_logo_rect_dst_.y = credits_rect_dst_.y + credits_rect_dst_.h + 30; mini_logo_final_pos_ = param.game.game_area.center_y - mini_logo_rect_src_.h / 2; } // Actualiza el destino de los rectangulos de las texturas void Credits::updateRects() { if (counter_ % 10 == 0) { --credits_rect_dst_.y; --mini_logo_rect_dst_.y; } mini_logo_rect_dst_.y = std::max(mini_logo_rect_dst_.y, mini_logo_final_pos_); }