249 lines
8.8 KiB
C++
249 lines
8.8 KiB
C++
#include "credits.h"
|
|
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
|
|
#include <SDL2/SDL_events.h> // Para SDL_PollEvent, SDL_Event, SDL_QUIT
|
|
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
|
|
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
|
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
|
|
#include <string> // Para basic_string, string
|
|
#include <vector> // 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<BalloonManager>()),
|
|
text_texture_(SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)),
|
|
tiled_bg_(std::make_unique<TiledBG>(param.game.game_area.rect, TiledBGMode::DIAGONAL))
|
|
{
|
|
section::name = section::Name::CREDITS;
|
|
// balloon_manager_->setPlayArea(param.game.game_area.rect);
|
|
const auto r = param.game.game_area.rect;
|
|
const int bars = 30;
|
|
balloon_manager_->setPlayArea({r.x, r.y + bars, r.w, r.h - bars});
|
|
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();
|
|
throwBalloons();
|
|
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_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 255);
|
|
SDL_Rect r1 = {0, 0, 320, 30};
|
|
SDL_Rect r2 = {0, 255 - 30, 320, 30};
|
|
SDL_RenderFillRect(Screen::get()->getRenderer(), &r1);
|
|
SDL_RenderFillRect(Screen::get()->getRenderer(), &r2);
|
|
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<std::string> 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<Sprite>(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_);
|
|
}
|
|
|
|
// Tira globos al escenario
|
|
void Credits::throwBalloons()
|
|
{
|
|
constexpr int speed = 200;
|
|
const std::vector<int> sets = {0, 63, 25, 67, 17, 75, 13, 50};
|
|
|
|
if (counter_ > ((sets.size() - 1) * speed) * 3)
|
|
return;
|
|
|
|
if (counter_ % speed == 0)
|
|
{
|
|
const int index = (counter_ / speed) % sets.size();
|
|
balloon_manager_->deploySet(sets.at(index), -50);
|
|
}
|
|
|
|
if (counter_ % (speed * 4) == 0)
|
|
{
|
|
balloon_manager_->createPowerBall();
|
|
}
|
|
} |