Files
coffee_crisis_arcade_edition/source/scoreboard.cpp

250 lines
6.1 KiB
C++

#include "scoreboard.h"
// Constructor
Scoreboard::Scoreboard(SDL_Renderer *renderer, Screen *screen, Asset *asset, Lang *lang, options_t *options)
{
// Copia los punteros
this->renderer = renderer;
this->screen = screen;
this->asset = asset;
this->lang = lang;
this->options = options;
// Inicializa variables
stage = 1;
score1 = 0;
score2 = 0;
mult1 = 1;
mult2 = 1;
hiScore = 0;
power = 0;
hiScoreName = "";
color = {0, 0, 0};
rect = {0, 0, 256, 32};
const int left = 45;
const int right = rect.w - left;
const int center = rect.w / 2;
const int desp = 7;
const int line1 = 2;
const int line2 = line1 + desp;
const int line3 = line2 + desp;
const int line4 = line3 + desp;
offsetScoreP1Label = {left, line1};
offsetScoreP1 = {left, line2};
offsetScoreP2Label = {right, line1};
offsetScoreP2 = {right, line2};
offsetHiScoreLabel = {center, line3};
offsetHiScore = {center, line4};
offsetMultP1Label = {left, line3};
offsetMultP1 = {left, line4};
offsetMultP2Label = {right, line3};
offsetMultP2 = {right, line4};
offsetStage = {center, line1};
offsetPowerMeter = {center, line2};
// Crea objetos
gamePowerMeterTexture = new Texture(renderer, asset->get("game_power_meter.png"));
powerMeterSprite = new Sprite(offsetPowerMeter.x - 20, offsetPowerMeter.y, 40, 7, gamePowerMeterTexture, renderer);
textScoreBoard = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer);
// Crea la textura para dibujar el marcador
background = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h);
SDL_SetTextureBlendMode(background, SDL_BLENDMODE_BLEND);
// Rellena la textura de fondo
fillBackgroundTexture();
}
Scoreboard::~Scoreboard()
{
gamePowerMeterTexture->unload();
delete gamePowerMeterTexture;
delete powerMeterSprite;
delete textScoreBoard;
SDL_DestroyTexture(background);
}
// Transforma un valor numérico en una cadena de 6 cifras
std::string Scoreboard::updateScoreText(Uint32 num)
{
if ((num >= 0) && (num <= 9))
{
return ("000000" + std::to_string(num));
}
if ((num >= 10) && (num <= 99))
{
return ("00000" + std::to_string(num));
}
if ((num >= 100) && (num <= 999))
{
return ("0000" + std::to_string(num));
}
if ((num >= 1000) && (num <= 9999))
{
return ("000" + std::to_string(num));
}
if ((num >= 010000) && (num <= 99999))
{
return ("00" + std::to_string(num));
}
if ((num >= 100000) && (num <= 999999))
{
return ("0" + std::to_string(num));
}
if ((num >= 1000000) && (num <= 9999999))
{
return (std::to_string(num));
}
return (std::to_string(num));
}
// Pinta el marcador
void Scoreboard::render()
{
fillBackgroundTexture();
SDL_RenderCopy(renderer, background, nullptr, &rect);
}
void Scoreboard::setScore1(int score)
{
score1 = score;
}
void Scoreboard::setScore2(int score)
{
score2 = score;
}
void Scoreboard::setMult1(float mult)
{
mult1 = mult;
}
void Scoreboard::setMult2(float mult)
{
mult2 = mult;
}
void Scoreboard::setStage(int stage)
{
this->stage = stage;
}
void Scoreboard::setHiScore(int hiScore)
{
this->hiScore = hiScore;
}
void Scoreboard::setPower(float power)
{
this->power = power;
}
void Scoreboard::setHiScoreName(std::string name)
{
hiScoreName = name;
}
void Scoreboard::setColor(color_t color)
{
this->color = color;
fillBackgroundTexture();
}
void Scoreboard::setPos(SDL_Rect rect)
{
this->rect = rect;
const int left = rect.w / 6;
const int right = rect.w - left;
const int center = rect.w / 2;
const int desp = 7;
const int line1 = 2;
const int line2 = line1 + desp;
const int line3 = line2 + desp;
const int line4 = line3 + desp;
offsetScoreP1Label = {left, line1};
offsetScoreP1 = {left, line2};
offsetScoreP2Label = {right, line1};
offsetScoreP2 = {right, line2};
offsetHiScoreLabel = {center, line3};
offsetHiScore = {center, line4};
offsetMultP1Label = {left, line3};
offsetMultP1 = {left, line4};
offsetMultP2Label = {right, line3};
offsetMultP2 = {right, line4};
offsetStage = {center, line1};
offsetPowerMeter = {center, line2};
powerMeterSprite->setPosX(offsetPowerMeter.x - 20);
powerMeterSprite->setPosY(offsetPowerMeter.y);
// Recrea la textura de fondo
if (background)
{
SDL_DestroyTexture(background);
}
background = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h);
SDL_SetTextureBlendMode(background, SDL_BLENDMODE_BLEND);
fillBackgroundTexture();
}
// Rellena la textura de fondo
void Scoreboard::fillBackgroundTexture()
{
// Cambia el destino del renderizador
SDL_Texture *temp = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, background);
// Dibuja el fondo del marcador
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
SDL_RenderFillRect(renderer, nullptr);
// PLAYER1 - SCORE
textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y, lang->getText(53));
textScoreBoard->writeCentered(offsetScoreP1.x, offsetScoreP1.y, updateScoreText(score1));
// PLAYER1 - MULT
textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y, lang->getText(55));
textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y, std::to_string(mult1).substr(0, 3));
// PLAYER2 - SCORE
textScoreBoard->writeCentered(offsetScoreP2Label.x, offsetScoreP2Label.y, lang->getText(54));
textScoreBoard->writeCentered(offsetScoreP2.x, offsetScoreP2.y, updateScoreText(score2));
// PLAYER2 - MULT
textScoreBoard->writeCentered(offsetMultP2Label.x, offsetMultP2Label.y, lang->getText(55));
textScoreBoard->writeCentered(offsetMultP2.x, offsetMultP2.y, std::to_string(mult2).substr(0, 3));
// STAGE
textScoreBoard->writeCentered(offsetStage.x, offsetStage.y, lang->getText(57) + std::to_string(stage));
// POWERMETER
powerMeterSprite->setSpriteClip(0, 0, 40, 7);
powerMeterSprite->render();
powerMeterSprite->setSpriteClip(40, 0, int(power * 40.0f), 7);
powerMeterSprite->render();
// HI-SCORE
textScoreBoard->writeCentered(offsetHiScoreLabel.x, offsetHiScoreLabel.y, lang->getText(56));
textScoreBoard->writeCentered(offsetHiScore.x, offsetHiScore.y, hiScoreName + updateScoreText(hiScore));
// Deja el renderizador apuntando donde estaba
SDL_SetRenderTarget(renderer, temp);
}