Acabat el nou motor per a textos en pantalla

This commit is contained in:
2024-10-30 09:25:28 +01:00
parent b43782786a
commit 20c51d0796
11 changed files with 269 additions and 274 deletions

View File

@@ -1,39 +1,40 @@
#include "game.h"
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
#include <SDL2/SDL_events.h> // Para SDL_PollEvent, SDL_Event, SDL_KEYDOWN
#include <SDL2/SDL_keycode.h> // Para SDLK_1, SDLK_2, SDLK_3, SDLK_4
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
#include <SDL2/SDL_video.h> // Para SDL_WINDOWEVENT_FOCUS_GAINED, SDL_...
#include <stdlib.h> // Para rand, size_t
#include <algorithm> // Para find_if, clamp, min, remove_if
#include <iterator> // Para distance, size
#include <numeric> // Para accumulate
#include "asset.h" // Para Asset
#include "background.h" // Para Background
#include "balloon.h" // Para Balloon, BALLOON_SCORE, BALLOON_VE...
#include "balloon_formations.h" // Para BalloonFormations, BalloonFormatio...
#include "bullet.h" // Para Bullet, BulletType, BulletMoveStatus
#include "explosions.h" // Para Explosions
#include "fade.h" // Para Fade, FadeType
#include "global_inputs.h" // Para check
#include "input.h" // Para InputType, Input, INPUT_DO_NOT_ALL...
#include "item.h" // Para Item, ItemType
#include "jail_audio.h" // Para JA_PlaySound, JA_GetMusicState
#include "lang.h" // Para getText
#include "manage_hiscore_table.h" // Para ManageHiScoreTable
#include "notifier.h" // Para Notifier
#include "param.h" // Para Param, param, ParamGame, ParamFade
#include "path_sprite.h" // Para PathSprite, PathType
#include "player.h" // Para Player, PlayerStatus
#include "resource.h" // Para Resource
#include "scoreboard.h" // Para Scoreboard, ScoreboardMode, SCOREB...
#include "screen.h" // Para Screen
#include "section.h" // Para Name, name, Options, options
#include "smart_sprite.h" // Para SmartSprite
#include "text.h" // Para Text, TEXT_CENTER
#include "texture.h" // Para Texture
struct JA_Sound_t; // lines 40-40
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_events.h> // for SDL_PollEvent, SDL_Event, SDL_KEYDOWN
#include <SDL2/SDL_keycode.h> // for SDLK_1, SDLK_2, SDLK_3, SDLK_4
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
#include <SDL2/SDL_video.h> // for SDL_WINDOWEVENT_FOCUS_GAINED, SDL_...
#include <stdlib.h> // for rand, size_t
#include <algorithm> // for find_if, clamp, min, remove_if
#include <functional> // for function
#include <iterator> // for distance, size
#include <numeric> // for accumulate
#include "asset.h" // for Asset
#include "background.h" // for Background
#include "balloon.h" // for Balloon, BALLOON_SCORE, BALLOON_VE...
#include "balloon_formations.h" // for BalloonFormations, BalloonFormatio...
#include "bullet.h" // for Bullet, BulletType, BulletMoveStatus
#include "explosions.h" // for Explosions
#include "fade.h" // for Fade, FadeType
#include "global_inputs.h" // for check
#include "input.h" // for InputType, Input, INPUT_DO_NOT_ALL...
#include "item.h" // for Item, ItemType
#include "jail_audio.h" // for JA_PlaySound, JA_GetMusicState
#include "lang.h" // for getText
#include "manage_hiscore_table.h" // for ManageHiScoreTable, HiScoreEntry
#include "notifier.h" // for Notifier
#include "param.h" // for Param, param, ParamGame, ParamFade
#include "path_sprite.h" // for Path, PathSprite, createPath, Path...
#include "player.h" // for Player, PlayerStatus
#include "resource.h" // for Resource
#include "scoreboard.h" // for Scoreboard, ScoreboardMode, SCOREB...
#include "screen.h" // for Screen
#include "section.h" // for Name, name, Options, options
#include "smart_sprite.h" // for SmartSprite
#include "text.h" // for Text
#include "texture.h" // for Texture
struct JA_Sound_t; // lines 36-36
// Constructor
Game::Game(int player_id, int current_stage, bool demo)
@@ -46,8 +47,7 @@ Game::Game(int player_id, int current_stage, bool demo)
balloon_formations_(std::make_unique<BalloonFormations>()),
canvas_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.play_area.rect.w, param.game.play_area.rect.h)),
fade_(std::make_unique<Fade>()),
current_stage_(current_stage),
last_stage_reached_(current_stage)
current_stage_(current_stage)
{
// Pasa variables
demo_.enabled = demo;
@@ -89,6 +89,7 @@ Game::Game(int player_id, int current_stage, bool demo)
{
createTwoBigBalloons();
evaluateAndSetMenace();
createMessage({paths_.at(0), paths_.at(1)}, Resource::get()->getTexture("get_ready"));
}
}
@@ -325,46 +326,50 @@ void Game::updateStage()
if (current_power_ >= balloon_formations_->getStage(current_stage_).power_to_complete)
{
// Cambio de fase
current_stage_++;
++current_stage_;
current_power_ = 0;
last_stage_reached_ = current_stage_;
// Ha llegado al final el juego
if (current_stage_ == 10)
{ // Ha llegado al final el juego
{
game_completed_ = true; // Marca el juego como completado
current_stage_ = 9; // Deja el valor dentro de los limites
destroyAllBalloons(); // Destruye a todos los enemigos
current_power_ = 0; // Vuelve a dejar el poder a cero, por lo que hubiera podido subir al destruir todos lo globos
menace_current_ = 255; // Sube el nivel de amenaza para que no cree mas globos
// Añade un millon de puntos a los jugadores que queden vivos
for (auto &player : players_)
{ // Añade un millon de puntos a los jugadores que queden vivos
if (player->isPlaying())
{
player->addScore(1000000);
}
}
updateHiScore();
JA_StopMusic();
}
JA_PlaySound(Resource::get()->getSound("stage_change.wav"));
stage_bitmap_counter_ = 0;
balloon_speed_ = default_balloon_speed_;
setBalloonSpeed(balloon_speed_);
screen_->flash(flash_color, 5);
screen_->flash(flash_color, 10);
screen_->shake();
}
// Incrementa el contador del bitmap que aparece mostrando el cambio de fase
if (stage_bitmap_counter_ < STAGE_COUNTER_)
{
stage_bitmap_counter_++;
}
// Si el juego se ha completado, el bitmap se detiene en el centro de la pantalla
if (game_completed_)
{
if (stage_bitmap_counter_ > 100)
// Escribe el texto por pantalla
const auto stage_number = balloon_formations_->getStage(current_stage_).number;
if (!game_completed_)
{
stage_bitmap_counter_ = 100;
std::vector<Path> paths = {paths_.at(2), paths_.at(3)};
if (stage_number == 10)
createMessage(paths, Resource::get()->getTexture("last_stage"));
else
{
auto text = std::make_unique<Text>(Resource::get()->getTexture("04b_25.png"), Resource::get()->getTextFile("04b_25.txt"));
const std::string caption = std::to_string(10 - current_stage_) + lang::getText(38);
createMessage(paths, text->writeToTexture(caption, 2, -2));
}
}
else
{
createMessage({paths_.at(4), paths_.at(5)}, Resource::get()->getTexture("congratulations"));
createMessage({paths_.at(6), paths_.at(7)}, Resource::get()->getTexture("1000000_points"));
}
}
}
@@ -377,6 +382,9 @@ void Game::updateGameOver()
{
if (game_over_counter_ > 0)
{
if (game_over_counter_ == GAME_OVER_COUNTER_)
createMessage({paths_.at(2), paths_.at(3)}, Resource::get()->getTexture("game_over"));
game_over_counter_--;
if ((game_over_counter_ == 250) || (game_over_counter_ == 200) || (game_over_counter_ == 180) || (game_over_counter_ == 120) || (game_over_counter_ == 60))
@@ -913,13 +921,13 @@ void Game::createItemText(int x, std::shared_ptr<Texture> texture)
}
// Crea un objeto PathSprite
void Game::createMessage(std::vector<Path> paths, std::shared_ptr<Texture> texture)
void Game::createMessage(const std::vector<Path> &paths, std::shared_ptr<Texture> texture)
{
path_sprites_.emplace_back(std::make_unique<PathSprite>(texture));
// Inicializa
for (const auto &path : paths)
path_sprites_.back()->addPath(path);
path_sprites_.back()->addPath(path, true);
path_sprites_.back()->enable();
}
@@ -1205,15 +1213,15 @@ void Game::updateBackground()
balloons_popped_ = (balloons_popped_ > 400) ? (balloons_popped_ - 25) : 200;
// Calcula la velocidad en función de los globos explotados y el total de globos a explotar para acabar el juego
constexpr auto clouds_initial_speed = 0.05f;
constexpr auto clouds_final_speed = 2.00f - clouds_initial_speed;
const float cloudsSpeed = (-clouds_initial_speed) + (-clouds_final_speed * ((float)balloons_popped_ / (float)total_power_to_complete_game_));
constexpr float clouds_initial_speed = 0.05f;
constexpr float clouds_final_speed = 2.00f - clouds_initial_speed;
const float cloudsSpeed = (-clouds_initial_speed) + (-clouds_final_speed * (static_cast<float>(balloons_popped_) / total_power_to_complete_game_));
background_->setCloudsSpeed(cloudsSpeed);
// Calcula la transición de los diferentes fondos
const float gradient_number = std::min(((float)balloons_popped_ / 1250.0f), 3.0f);
const float percent = gradient_number - (int)gradient_number;
background_->setGradientNumber((int)gradient_number);
const float gradient_number = std::min(balloons_popped_ / 1250.0f, 3.0f);
const float percent = gradient_number - static_cast<int>(gradient_number);
background_->setGradientNumber(static_cast<int>(gradient_number));
background_->setTransition(percent);
// Actualiza el objeto
@@ -1231,11 +1239,10 @@ void Game::fillCanvas()
background_->render();
renderItems();
renderSmartSprites();
renderPathSprites();
explosions_->render();
renderBalloons();
renderBullets();
renderMessages();
renderPathSprites();
renderPlayers();
// Deja el renderizador apuntando donde estaba
@@ -1251,6 +1258,12 @@ void Game::render()
// Copia la textura con la zona de juego a la pantalla
SDL_RenderCopy(renderer_, canvas_, nullptr, &param.game.play_area.rect);
// SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 255, 255);
// const int y = param.game.play_area.center_y;
// SDL_RenderDrawLine(Screen::get()->getRenderer(), 0, y, param.game.play_area.rect.w, y);
// const int x = param.game.play_area.center_x;
// SDL_RenderDrawLine(Screen::get()->getRenderer(), x, 0, x, param.game.play_area.rect.h);
// Dibuja el marcador
scoreboard_->render();
@@ -1285,48 +1298,6 @@ void Game::updateMenace()
}
}
// Pinta diferentes mensajes en la pantalla
void Game::renderMessages()
{
// GetReady
if (counter_ == 10 && !demo_.enabled)
{ // text_04b_25_->write2X((int)get_ready_bitmap_path_[counter_], param.game.play_area.center_y - 8, lang::getText(75), -2);
std::vector<Path> paths = {paths_.at(0), paths_.at(1)};
createMessage(paths, Resource::get()->getTexture("get_ready"));
}
// STAGE NUMBER
if (stage_bitmap_counter_ < STAGE_COUNTER_)
{
const auto stage_number = balloon_formations_->getStage(current_stage_).number;
std::string text;
if (stage_number == 10)
{
// Ultima fase
text = lang::getText(79);
}
else
{
// X fases restantes
text = std::to_string(11 - stage_number) + lang::getText(38);
}
if (!game_completed_)
{
// Escribe el número de fases restantes
//text_04b_25_->write2X(param.game.play_area.center_x, stage_bitmap_path_[stage_bitmap_counter_], text, -2);
}
else
{
// Escribe el texto de juego completado
text = lang::getText(50);
//text_nokia2_big_->writeDX(TEXT_CENTER, param.game.play_area.center_x, stage_bitmap_path_[stage_bitmap_counter_], text, -2, no_color, 1, shdw_txt_color);
//text_nokia2_->writeDX(TEXT_CENTER, param.game.play_area.center_x, stage_bitmap_path_[stage_bitmap_counter_] + text_nokia2_big_->getCharacterSize() + 2, lang::getText(76), -1, no_color, 1, shdw_txt_color);
}
}
}
// Habilita el efecto del item de detener el tiempo
void Game::enableTimeStopItem()
{
@@ -1385,42 +1356,54 @@ int Game::calculateScreenPower()
// Inicializa las variables que contienen puntos de ruta para mover objetos
void Game::initPaths()
{
// Recorrido para el texto de "Get Ready!"
const auto &texture = Resource::get()->getTexture("get_ready");
const auto w = texture->getWidth();
const auto h = texture->getHeight();
const int x0 = -w;
const int x1 = param.game.play_area.center_x - w / 2;
const int x2 = param.game.play_area.rect.w;
const int y = param.game.play_area.center_y - h / 2;
paths_.emplace_back(Path(createPath(x0, x1, PathType::HORIZONTAL, y, 80, easeOutQuint), 10));
paths_.emplace_back(Path(createPath(x1, x2, PathType::HORIZONTAL, y, 80, easeInQuint), 0));
// Vector con los valores del seno para 360 grados
float sin[360];
for (int i = 0; i < 360; ++i)
sin[i] = SDL_sinf((float)i * 3.14f / 180.0f);
// Letrero de STAGE #
constexpr auto first_part = STAGE_COUNTER_ / 4; // 50
constexpr auto second_part = first_part * 3; // 150
const auto center_point = param.game.play_area.center_y - (BLOCK * 2);
const auto distance = (param.game.play_area.rect.h) - (param.game.play_area.center_y - 16);
for (int i = 0; i < first_part; ++i)
// Recorrido para el texto de "Get Ready!" (0,1)
{
int index = static_cast<int>((i * 1.8f) + 90) % 360;
stage_bitmap_path_[i] = sin[index] * distance + center_point;
const auto &texture = Resource::get()->getTexture("get_ready");
const auto w = texture->getWidth();
const int x0 = -w;
const int x1 = param.game.play_area.center_x - w / 2;
const int x2 = param.game.play_area.rect.w;
const int y = param.game.play_area.center_y;
paths_.emplace_back(Path(createPath(x0, x1, PathType::HORIZONTAL, y, 80, easeOutQuint), 20));
paths_.emplace_back(Path(createPath(x1, x2, PathType::HORIZONTAL, y, 80, easeInQuint), 0));
}
for (int i = first_part; i < second_part; ++i)
stage_bitmap_path_[i] = center_point;
for (int i = second_part; i < STAGE_COUNTER_; ++i)
// Recorrido para el texto de "Last Stage!" o de "X stages left" o "Game Over" (2,3)
{
int index = static_cast<int>(((i - 149) * 1.8f) + 90) % 360;
stage_bitmap_path_[i] = sin[index] * (center_point + 17) - 17;
const auto &texture = Resource::get()->getTexture("last_stage");
const auto h = texture->getHeight();
const int y0 = param.game.play_area.rect.h - h;
const int y1 = param.game.play_area.center_y - h / 2;
const int y2 = -h;
const int x = param.game.play_area.center_x;
paths_.emplace_back(Path(createPath(y0, y1, PathType::VERTICAL, x, 80, easeOutQuint), 20));
paths_.emplace_back(Path(createPath(y1, y2, PathType::VERTICAL, x, 80, easeInQuint), 0));
}
// Recorrido para el texto de "Congratulations!!" (3,4)
{
const auto &texture = Resource::get()->getTexture("congratulations");
const auto w = texture->getWidth();
const auto h = texture->getHeight();
const int x0 = -w;
const int x1 = param.game.play_area.center_x - w / 2;
const int x2 = param.game.play_area.rect.w;
const int y = param.game.play_area.center_y - h / 2 - 20;
paths_.emplace_back(Path(createPath(x0, x1, PathType::HORIZONTAL, y, 80, easeOutQuint), 400));
paths_.emplace_back(Path(createPath(x1, x2, PathType::HORIZONTAL, y, 80, easeInQuint), 0));
}
// Recorrido para el texto de "1.000.000 points!" (5,6)
{
const auto &texture = Resource::get()->getTexture("1000000_points");
const auto w = texture->getWidth();
const auto h = texture->getHeight();
const int x0 = param.game.play_area.rect.w;
const int x1 = param.game.play_area.center_x - w / 2;
const int x2 = -w;
const int y = param.game.play_area.center_y + h / 2 - 20;
paths_.emplace_back(Path(createPath(x0, x1, PathType::HORIZONTAL, y, 80, easeOutQuint), 400));
paths_.emplace_back(Path(createPath(x1, x2, PathType::HORIZONTAL, y, 80, easeInQuint), 0));
}
}
@@ -1559,6 +1542,11 @@ void Game::checkEvents()
createItemText(x, game_text_textures_.at(3));
break;
}
case SDLK_6: // Crea un mensaje
{
createMessage({paths_.at(4), paths_.at(5)}, Resource::get()->getTexture("congratulations"));
createMessage({paths_.at(6), paths_.at(7)}, Resource::get()->getTexture("1000000_points"));
}
default:
break;
}
@@ -1610,9 +1598,7 @@ void Game::updateScoreboard()
void Game::pause(bool value)
{
paused_ = value;
#ifndef DEBUG
screen_->attenuate(paused_);
#endif
}
// Añade una puntuación a la tabla de records