Compare commits

3 Commits

6 changed files with 106 additions and 48 deletions

View File

@@ -84,8 +84,9 @@ void Notifier::update()
// Comprueba los estados // Comprueba los estados
if (notifications_[i].state == NotificationStatus::RISING) if (notifications_[i].state == NotificationStatus::RISING)
{ {
const float step = ((float)notifications_[i].counter / notifications_[i].travel_dist); //const float step = ((float)notifications_[i].counter / notifications_[i].travel_dist);
const int alpha = 255 * step; //const int alpha = 255 * step;
constexpr int ALPHA = 255;
if (options.notifications.getVerticalPosition() == NotificationPosition::TOP) if (options.notifications.getVerticalPosition() == NotificationPosition::TOP)
{ {
@@ -95,7 +96,7 @@ void Notifier::update()
{ {
notifications_[i].rect.y--; notifications_[i].rect.y--;
} }
notifications_[i].texture->setAlpha(alpha); notifications_[i].texture->setAlpha(ALPHA);
if (notifications_[i].rect.y == notifications_[i].y) if (notifications_[i].rect.y == notifications_[i].y)
{ {
@@ -185,7 +186,6 @@ void Notifier::show(std::vector<std::string> texts, NotificationText text_is, in
const auto padding_in_v = text_->getCharacterSize() / 2; const auto padding_in_v = text_->getCharacterSize() / 2;
const int icon_space = icon >= 0 ? ICON_SIZE_ + padding_in_h : 0; const int icon_space = icon >= 0 ? ICON_SIZE_ + padding_in_h : 0;
text_is = icon_space > 0 ? NotificationText::LEFT : text_is; text_is = icon_space > 0 ? NotificationText::LEFT : text_is;
// const int width = text_->lenght(longest) + (padding_in_h * 2) + icon_space;
const int width = options.game.width - (PADDING_OUT_ * 2); const int width = options.game.width - (PADDING_OUT_ * 2);
const int height = (text_->getCharacterSize() * texts.size()) + (padding_in_v * 2); const int height = (text_->getCharacterSize() * texts.size()) + (padding_in_v * 2);
const auto shape = NotificationShape::SQUARED; const auto shape = NotificationShape::SQUARED;

View File

@@ -9,6 +9,7 @@
#include "screen.h" // for Screen #include "screen.h" // for Screen
#include "text.h" // for Text #include "text.h" // for Text
#include "texture.h" // for Texture #include "texture.h" // for Texture
#include <iostream>
// Constructor // Constructor
Scoreboard::Scoreboard(std::shared_ptr<ScoreboardData> data) Scoreboard::Scoreboard(std::shared_ptr<ScoreboardData> data)
@@ -17,6 +18,9 @@ Scoreboard::Scoreboard(std::shared_ptr<ScoreboardData> data)
asset_(Asset::get()), asset_(Asset::get()),
data_(data) data_(data)
{ {
const int TEXTURE_WIDTH_ = options.game.width;
constexpr int TEXTURE_HEIGHT_ = 6 * BLOCK;
// Reserva memoria para los objetos // Reserva memoria para los objetos
item_texture_ = resource_->getTexture("items.png"); item_texture_ = resource_->getTexture("items.png");
auto player_texture = resource_->getTexture(options.cheats.alternate_skin == Cheat::CheatState::ENABLED ? "player2.png" : "player.png"); auto player_texture = resource_->getTexture(options.cheats.alternate_skin == Cheat::CheatState::ENABLED ? "player2.png" : "player.png");
@@ -25,6 +29,16 @@ Scoreboard::Scoreboard(std::shared_ptr<ScoreboardData> data)
player_sprite_->setCurrentAnimation("walk_menu"); player_sprite_->setCurrentAnimation("walk_menu");
text_ = resource_->getText("smb2"); text_ = resource_->getText("smb2");
texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, TEXTURE_WIDTH_, TEXTURE_HEIGHT_);
if (texture_ == nullptr)
{
if (options.console)
{
std::cout << "Scorebard::texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
texture_dest_ = {0, options.game.height - TEXTURE_HEIGHT_, TEXTURE_WIDTH_, TEXTURE_HEIGHT_};
// Inicializa las variables // Inicializa las variables
counter_ = 0; counter_ = 0;
change_color_speed_ = 4; change_color_speed_ = 4;
@@ -41,51 +55,16 @@ Scoreboard::Scoreboard(std::shared_ptr<ScoreboardData> data)
} }
} }
// Destructor
Scoreboard::~Scoreboard()
{
SDL_DestroyTexture(texture_);
}
// Pinta el objeto en pantalla // Pinta el objeto en pantalla
void Scoreboard::render() void Scoreboard::render()
{ {
// Anclas SDL_RenderCopy(renderer_, texture_, nullptr, &texture_dest_);
const int line1 = 19 * BLOCK;
const int line2 = line1 + (2 * BLOCK);
// Dibuja el fondo del marcador
const SDL_Rect rect = {0, 18 * BLOCK, PLAY_AREA_WIDTH, GAMECANVAS_HEIGHT - PLAY_AREA_HEIGHT};
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
SDL_RenderFillRect(renderer_, &rect);
// Dibuja las vidas
const int desp = (counter_ / 40) % 8;
const int frame = desp % 4;
player_sprite_->setCurrentAnimationFrame(frame);
player_sprite_->setPosY(line2);
for (int i = 0; i < data_->lives; ++i)
{
player_sprite_->setPosX(8 + (16 * i) + desp);
const int index = i % color_.size();
player_sprite_->getTexture()->setColor(color_[index].r, color_[index].g, color_[index].b);
player_sprite_->render();
}
// Muestra si suena la música
if (data_->music)
{
const Color c = data_->color;
SDL_Rect clip = {0, 8, 8, 8};
item_texture_->setColor(c.r, c.g, c.b);
item_texture_->render(20 * BLOCK, line2, &clip);
}
// Escribe los textos
const std::string timeTxt = std::to_string((clock_.minutes % 100) / 10) + std::to_string(clock_.minutes % 10) + clock_.separator + std::to_string((clock_.seconds % 60) / 10) + std::to_string(clock_.seconds % 10);
const std::string itemsTxt = std::to_string(data_->items / 100) + std::to_string((data_->items % 100) / 10) + std::to_string(data_->items % 10);
this->text_->writeColored(BLOCK, line1, "Items collected ", data_->color);
this->text_->writeColored(17 * BLOCK, line1, itemsTxt, items_color_);
this->text_->writeColored(20 * BLOCK, line1, " Time ", data_->color);
this->text_->writeColored(26 * BLOCK, line1, timeTxt, stringToColor(options.video.palette, "white"));
const std::string roomsTxt = std::to_string(data_->rooms / 100) + std::to_string((data_->rooms % 100) / 10) + std::to_string(data_->rooms % 10);
this->text_->writeColored(22 * BLOCK, line2, "Rooms", stringToColor(options.video.palette, "white"));
this->text_->writeColored(28 * BLOCK, line2, roomsTxt, stringToColor(options.video.palette, "white"));
} }
// Actualiza las variables del objeto // Actualiza las variables del objeto
@@ -97,6 +76,9 @@ void Scoreboard::update()
// Actualiza el color de la cantidad de items recogidos // Actualiza el color de la cantidad de items recogidos
updateItemsColor(); updateItemsColor();
// Dibuja la textura
fillTexture();
if (!is_paused_) if (!is_paused_)
{ {
// Si está en pausa no se actualiza el reloj // Si está en pausa no se actualiza el reloj
@@ -175,4 +157,57 @@ void Scoreboard::updateItemsColor()
int Scoreboard::getMinutes() int Scoreboard::getMinutes()
{ {
return getTime().minutes; return getTime().minutes;
}
// Dibuja los elementos del marcador en la textura
void Scoreboard::fillTexture()
{
// Empieza a dibujar en la textura
auto temp = SDL_GetRenderTarget(renderer_);
SDL_SetRenderTarget(renderer_, texture_);
// Limpia la textura
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
SDL_RenderFillRect(renderer_, nullptr);
// Anclas
constexpr int LINE1 = BLOCK;
constexpr int LINE2 = 3 * BLOCK;
// Dibuja las vidas
const int desp = (counter_ / 40) % 8;
const int frame = desp % 4;
player_sprite_->setCurrentAnimationFrame(frame);
player_sprite_->setPosY(LINE2);
for (int i = 0; i < data_->lives; ++i)
{
player_sprite_->setPosX(8 + (16 * i) + desp);
const int index = i % color_.size();
player_sprite_->getTexture()->setColor(color_[index].r, color_[index].g, color_[index].b);
player_sprite_->render();
}
// Muestra si suena la música
if (data_->music)
{
const Color c = data_->color;
SDL_Rect clip = {0, 8, 8, 8};
item_texture_->setColor(c.r, c.g, c.b);
item_texture_->render(20 * BLOCK, LINE2, &clip);
}
// Escribe los textos
const std::string timeTxt = std::to_string((clock_.minutes % 100) / 10) + std::to_string(clock_.minutes % 10) + clock_.separator + std::to_string((clock_.seconds % 60) / 10) + std::to_string(clock_.seconds % 10);
const std::string itemsTxt = std::to_string(data_->items / 100) + std::to_string((data_->items % 100) / 10) + std::to_string(data_->items % 10);
text_->writeColored(BLOCK, LINE1, "Items collected ", data_->color);
text_->writeColored(17 * BLOCK, LINE1, itemsTxt, items_color_);
text_->writeColored(20 * BLOCK, LINE1, " Time ", data_->color);
text_->writeColored(26 * BLOCK, LINE1, timeTxt, stringToColor(options.video.palette, "white"));
const std::string roomsTxt = std::to_string(data_->rooms / 100) + std::to_string((data_->rooms % 100) / 10) + std::to_string(data_->rooms % 10);
text_->writeColored(22 * BLOCK, LINE2, "Rooms", stringToColor(options.video.palette, "white"));
text_->writeColored(28 * BLOCK, LINE2, roomsTxt, stringToColor(options.video.palette, "white"));
// Deja el renderizador como estaba
SDL_SetRenderTarget(renderer_, temp);
} }

View File

@@ -46,6 +46,7 @@ private:
std::shared_ptr<AnimatedSprite> player_sprite_; // Sprite para mostrar las vidas en el marcador std::shared_ptr<AnimatedSprite> player_sprite_; // Sprite para mostrar las vidas en el marcador
std::shared_ptr<Texture> item_texture_; // Textura con los graficos para las vidas std::shared_ptr<Texture> item_texture_; // Textura con los graficos para las vidas
std::shared_ptr<ScoreboardData> data_; // Contiene las variables a mostrar en el marcador std::shared_ptr<ScoreboardData> data_; // Contiene las variables a mostrar en el marcador
SDL_Texture *texture_; // Textura donde dibujar el marcador;
// Variables // Variables
std::vector<Color> color_; // Vector con los colores del objeto std::vector<Color> color_; // Vector con los colores del objeto
@@ -56,6 +57,7 @@ private:
Uint32 paused_time_elapsed_; // Tiempo acumulado en pausa Uint32 paused_time_elapsed_; // Tiempo acumulado en pausa
ClockData clock_; // Contiene las horas, minutos y segundos transcurridos desde el inicio de la partida ClockData clock_; // Contiene las horas, minutos y segundos transcurridos desde el inicio de la partida
Color items_color_; // Color de la cantidad de items recogidos Color items_color_; // Color de la cantidad de items recogidos
SDL_Rect texture_dest_; // Rectangulo donde dibujar la textura del marcador
// Obtiene el tiempo transcurrido de partida // Obtiene el tiempo transcurrido de partida
ClockData getTime(); ClockData getTime();
@@ -63,12 +65,15 @@ private:
// Actualiza el color de la cantidad de items recogidos // Actualiza el color de la cantidad de items recogidos
void updateItemsColor(); void updateItemsColor();
// Dibuja los elementos del marcador en la textura
void fillTexture();
public: public:
// Constructor // Constructor
Scoreboard(std::shared_ptr<ScoreboardData> data); Scoreboard(std::shared_ptr<ScoreboardData> data);
// Destructor // Destructor
~Scoreboard() = default; ~Scoreboard();
// Pinta el objeto en pantalla // Pinta el objeto en pantalla
void render(); void render();

View File

@@ -71,6 +71,9 @@ Title::Title()
// Rellena la textura de fondo con todos los gráficos // Rellena la textura de fondo con todos los gráficos
fillTexture(); fillTexture();
// Inicia la musica
playMusic("title.ogg");
} }
// Destructor // Destructor

View File

@@ -8,6 +8,8 @@
#include <iostream> // for basic_ostream, cout, basic_ios, ios, endl #include <iostream> // for basic_ostream, cout, basic_ios, ios, endl
#include <unordered_map> // for unordered_map #include <unordered_map> // for unordered_map
#include <string> // for string #include <string> // for string
#include "jail_audio.h"
#include "resource.h"
// Calcula el cuadrado de la distancia entre dos puntos // Calcula el cuadrado de la distancia entre dos puntos
double distanceSquared(int x1, int y1, int x2, int y2) double distanceSquared(int x1, int y1, int x2, int y2)
@@ -524,4 +526,14 @@ void printWithDots(const std::string &text1, const std::string &text2, const std
bool stringInVector(const std::vector<std::string> &vec, const std::string &str) bool stringInVector(const std::vector<std::string> &vec, const std::string &str)
{ {
return std::find(vec.begin(), vec.end(), str) != vec.end(); return std::find(vec.begin(), vec.end(), str) != vec.end();
}
// Hace sonar la música
void playMusic(const std::string &music_path)
{
// Si la música no está sonando
if (JA_GetMusicState() == JA_MUSIC_INVALID || JA_GetMusicState() == JA_MUSIC_STOPPED)
{
JA_PlayMusic(Resource::get()->getMusic(music_path));
}
} }

View File

@@ -126,4 +126,7 @@ std::string getPath(const std::string &full_path);
void printWithDots(const std::string &text1, const std::string &text2, const std::string &text3); void printWithDots(const std::string &text1, const std::string &text2, const std::string &text3);
// Comprueba si una vector contiene una cadena // Comprueba si una vector contiene una cadena
bool stringInVector(const std::vector<std::string> &vec, const std::string &str); bool stringInVector(const std::vector<std::string> &vec, const std::string &str);
// Hace sonar la música
void playMusic(const std::string &music_path);