treballant en els panels del marcador
This commit is contained in:
@@ -21,8 +21,9 @@ Scoreboard::Scoreboard(SDL_Renderer *renderer, Screen *screen, Asset *asset, Lan
|
||||
hiScoreName = "";
|
||||
color = {0, 0, 0};
|
||||
rect = {0, 0, 320, 40};
|
||||
mode[0] = SCOREBOARD_MODE_PLAYING;
|
||||
mode[1] = SCOREBOARD_MODE_PLAYING;
|
||||
panel[SCOREBOARD_LEFT_PANEL].mode = SCOREBOARD_MODE_SCORE;
|
||||
panel[SCOREBOARD_RIGHT_PANEL].mode = SCOREBOARD_MODE_SCORE;
|
||||
panel[SCOREBOARD_CENTER_PANEL].mode = SCOREBOARD_MODE_STAGE_INFO;
|
||||
ticks = SDL_GetTicks();
|
||||
counter = 0;
|
||||
|
||||
@@ -34,9 +35,11 @@ Scoreboard::Scoreboard(SDL_Renderer *renderer, Screen *screen, Asset *asset, Lan
|
||||
powerMeterSprite = new Sprite(offsetPowerMeter.x - 20, offsetPowerMeter.y, 40, 7, gamePowerMeterTexture);
|
||||
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);
|
||||
// Crea la textura de fondo
|
||||
createBackgroundTexture();
|
||||
|
||||
// Crea las texturas de los paneles
|
||||
createPanelTextures();
|
||||
|
||||
// Rellena la textura de fondo
|
||||
fillBackgroundTexture();
|
||||
@@ -49,6 +52,13 @@ Scoreboard::~Scoreboard()
|
||||
delete powerMeterSprite;
|
||||
delete textScoreBoard;
|
||||
SDL_DestroyTexture(background);
|
||||
for (auto texture : panelTexture)
|
||||
{
|
||||
if (texture)
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Transforma un valor numérico en una cadena de 6 cifras
|
||||
@@ -181,19 +191,92 @@ void Scoreboard::setPos(SDL_Rect rect)
|
||||
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);
|
||||
// Crea la textura de fondo
|
||||
createBackgroundTexture();
|
||||
|
||||
// Crea las texturas de los paneles
|
||||
createPanelTextures();
|
||||
|
||||
// Rellena la textura de fondo
|
||||
fillBackgroundTexture();
|
||||
}
|
||||
|
||||
// Rellena los diferentes paneles del marcador
|
||||
void Scoreboard::fillPanelTextures()
|
||||
{
|
||||
// Guarda a donde apunta actualmente el renderizador
|
||||
SDL_Texture *temp = SDL_GetRenderTarget(renderer);
|
||||
|
||||
// Genera el contenidoi de cada panel
|
||||
for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i)
|
||||
{
|
||||
// Cambia el destino del renderizador
|
||||
SDL_SetRenderTarget(renderer, panelTexture[i]);
|
||||
|
||||
// Dibuja el fondo de la textura
|
||||
SDL_SetRenderDrawColor(renderer, i * 64, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
switch (panel[i].mode)
|
||||
{
|
||||
case SCOREBOARD_MODE_SCORE:
|
||||
// SCORE
|
||||
textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y, lang->getText(53));
|
||||
textScoreBoard->writeCentered(offsetScoreP1.x, offsetScoreP1.y, updateScoreText(score1));
|
||||
|
||||
// MULT
|
||||
textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y, lang->getText(55));
|
||||
textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y, std::to_string(mult1).substr(0, 3));
|
||||
break;
|
||||
|
||||
case SCOREBOARD_MODE_DEMO:
|
||||
textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y + 4, "Mode demostracio");
|
||||
if (counter % 10 < 8)
|
||||
{
|
||||
textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y - 2, "Pulsa START");
|
||||
textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y - 2, "per jugar");
|
||||
}
|
||||
break;
|
||||
|
||||
case SCOREBOARD_MODE_GAME_OVER:
|
||||
textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y + 4, "Game Over");
|
||||
if (counter % 10 < 8)
|
||||
{
|
||||
textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y - 2, "Pulsa START");
|
||||
textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y - 2, "per jugar");
|
||||
}
|
||||
break;
|
||||
|
||||
case SCOREBOARD_MODE_STAGE_INFO:
|
||||
// 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));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Deja el renderizador apuntando donde estaba
|
||||
SDL_SetRenderTarget(renderer, temp);
|
||||
}
|
||||
|
||||
// Rellena la textura de fondo
|
||||
void Scoreboard::fillBackgroundTexture()
|
||||
{
|
||||
// Rellena los diferentes paneles del marcador
|
||||
fillPanelTextures();
|
||||
|
||||
// Cambia el destino del renderizador
|
||||
SDL_Texture *temp = SDL_GetRenderTarget(renderer);
|
||||
SDL_SetRenderTarget(renderer, background);
|
||||
@@ -202,83 +285,12 @@ void Scoreboard::fillBackgroundTexture()
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
|
||||
SDL_RenderFillRect(renderer, nullptr);
|
||||
|
||||
// PARTE IZQUIERDA
|
||||
if (mode[SCOREBOARD_LEFT_SIDE] == SCOREBOARD_MODE_PLAYING)
|
||||
{ // 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));
|
||||
}
|
||||
|
||||
else if (mode[SCOREBOARD_LEFT_SIDE] == SCOREBOARD_MODE_DEMO)
|
||||
// Copia las texturas de los paneles
|
||||
for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i)
|
||||
{
|
||||
textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y + 4, "Mode demostracio");
|
||||
if (counter % 10 < 8)
|
||||
{
|
||||
textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y - 2, "Pulsa START");
|
||||
textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y - 2, "per jugar");
|
||||
}
|
||||
SDL_RenderCopy(renderer, panelTexture[i], nullptr, &panel[i].pos);
|
||||
}
|
||||
|
||||
else if (mode[SCOREBOARD_LEFT_SIDE] == SCOREBOARD_MODE_GAME_OVER)
|
||||
{
|
||||
textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y + 4, "Game Over");
|
||||
if (counter % 10 < 8)
|
||||
{
|
||||
textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y - 2, "Pulsa START");
|
||||
textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y - 2, "per jugar");
|
||||
}
|
||||
}
|
||||
|
||||
// PARTE DERECHA
|
||||
if (mode[SCOREBOARD_RIGHT_SIDE] == SCOREBOARD_MODE_PLAYING)
|
||||
{ // 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));
|
||||
}
|
||||
|
||||
else if (mode[SCOREBOARD_RIGHT_SIDE] == SCOREBOARD_MODE_DEMO)
|
||||
{
|
||||
textScoreBoard->writeCentered(offsetScoreP2Label.x, offsetScoreP2Label.y + 4, "Mode demostracio");
|
||||
if (counter % 10 < 8)
|
||||
{
|
||||
textScoreBoard->writeCentered(offsetMultP2Label.x, offsetMultP2Label.y - 2, "Pulsa START");
|
||||
textScoreBoard->writeCentered(offsetMultP2.x, offsetMultP2.y - 2, "per jugar");
|
||||
}
|
||||
}
|
||||
|
||||
else if (mode[SCOREBOARD_RIGHT_SIDE] == SCOREBOARD_MODE_GAME_OVER)
|
||||
{
|
||||
textScoreBoard->writeCentered(offsetScoreP2Label.x, offsetScoreP2Label.y + 4, "Game Over");
|
||||
if (counter % 10 < 8)
|
||||
{
|
||||
textScoreBoard->writeCentered(offsetMultP2Label.x, offsetMultP2Label.y - 2, "Pulsa START");
|
||||
textScoreBoard->writeCentered(offsetMultP2.x, offsetMultP2.y - 2, "per jugar");
|
||||
}
|
||||
}
|
||||
|
||||
// PARTE CENTRAL
|
||||
|
||||
// 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);
|
||||
}
|
||||
@@ -286,42 +298,87 @@ void Scoreboard::fillBackgroundTexture()
|
||||
// Recalcula las anclas de los elementos
|
||||
void Scoreboard::recalculateAnchors()
|
||||
{
|
||||
// Constantes para definir las zonas del marcador: 4 filas y 3 columnas
|
||||
const int panelWidth = rect.w / SCOREBOARD_MAX_PANELS;
|
||||
for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i)
|
||||
{
|
||||
panel[i].pos.x = panelWidth * i;
|
||||
panel[i].pos.y = 0;
|
||||
panel[i].pos.w = panelWidth;
|
||||
panel[i].pos.h = rect.h;
|
||||
}
|
||||
|
||||
// Constantes para definir las zonas del panel: 4 filas y 1 columna
|
||||
const int rowSize = rect.h / 4;
|
||||
const int textHeight = 7;
|
||||
|
||||
// Filas
|
||||
const int row1 = (rowSize * 0) + (textHeight / 2);
|
||||
const int row2 = (rowSize * 1) + (textHeight / 2) - 1;
|
||||
const int row3 = (rowSize * 2) + (textHeight / 2) - 2;
|
||||
const int row4 = (rowSize * 3) + (textHeight / 2) - 3;
|
||||
|
||||
const int halfColSize = rect.w / 6;
|
||||
const int col1 = halfColSize;
|
||||
const int col2 = halfColSize * 3;
|
||||
const int col3 = halfColSize * 5;
|
||||
// Columna
|
||||
const int col = panelWidth / 2;
|
||||
|
||||
// Primera fila
|
||||
offsetScoreP1Label = {col1, row1};
|
||||
offsetStage = {col2, row1};
|
||||
offsetScoreP2Label = {col3, row1};
|
||||
offsetScoreP1Label = {col, row1};
|
||||
offsetStage = {col, row1};
|
||||
offsetScoreP2Label = {col, row1};
|
||||
|
||||
// Segunda fila
|
||||
offsetScoreP1 = {col1, row2};
|
||||
offsetPowerMeter = {col2, row2};
|
||||
offsetScoreP2 = {col3, row2};
|
||||
offsetScoreP1 = {col, row2};
|
||||
offsetPowerMeter = {col, row2};
|
||||
offsetScoreP2 = {col, row2};
|
||||
|
||||
// Tercera fila
|
||||
offsetMultP1Label = {col1, row3};
|
||||
offsetHiScoreLabel = {col2, row3};
|
||||
offsetMultP2Label = {col3, row3};
|
||||
offsetMultP1Label = {col, row3};
|
||||
offsetHiScoreLabel = {col, row3};
|
||||
offsetMultP2Label = {col, row3};
|
||||
|
||||
// Cuarta fila
|
||||
offsetMultP1 = {col1, row4};
|
||||
offsetHiScore = {col2, row4};
|
||||
offsetMultP2 = {col3, row4};
|
||||
offsetMultP1 = {col, row4};
|
||||
offsetHiScore = {col, row4};
|
||||
offsetMultP2 = {col, row4};
|
||||
}
|
||||
|
||||
// Establece el modo del marcador
|
||||
void Scoreboard::setMode(int index, scoreboard_modes_e mode)
|
||||
{
|
||||
this->mode[index] = mode;
|
||||
panel[index].mode = mode;
|
||||
}
|
||||
|
||||
// Crea la textura de fondo
|
||||
void Scoreboard::createBackgroundTexture()
|
||||
{
|
||||
// Elimina la textura en caso de existir
|
||||
if (background)
|
||||
{
|
||||
SDL_DestroyTexture(background);
|
||||
}
|
||||
|
||||
// Recrea la textura de fondo
|
||||
background = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h);
|
||||
SDL_SetTextureBlendMode(background, SDL_BLENDMODE_BLEND);
|
||||
}
|
||||
|
||||
// Crea las texturas de los paneles
|
||||
void Scoreboard::createPanelTextures()
|
||||
{
|
||||
// Elimina las texturas en caso de existir
|
||||
for (auto texture : panelTexture)
|
||||
{
|
||||
if (texture)
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
}
|
||||
panelTexture.clear();
|
||||
|
||||
// Crea las texturas para cada panel
|
||||
for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i)
|
||||
{
|
||||
SDL_Texture *tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w / SCOREBOARD_MAX_PANELS, rect.h);
|
||||
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
|
||||
panelTexture.push_back(tex);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user