Actualizada la clase Text

This commit is contained in:
2022-08-29 17:47:06 +02:00
parent 21c844a925
commit 822386c269
4 changed files with 62 additions and 81 deletions

View File

@@ -20,9 +20,15 @@ Game::Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang,
mRoom = new Room(mAsset->get(mCurrentRoom), mRenderer, mAsset, mItemTracker);
mPlayer = new Player(mSpawnPoint, mAsset->get("player01.png"), mRenderer, mAsset, mInput, mRoom);
mEventHandler = new SDL_Event();
mTextureText = new LTexture();
mText = new Text(mAsset->get("smb2.txt"), mTextureText, renderer);
mText = new Text(mAsset->get("smb2.png"), mAsset->get("smb2.txt"), renderer);
mFade = new Fade(renderer);
// Inicializa variables
mTicks = 0;
mTicksSpeed = 15;
mSection.name = SECTION_PROG_GAME;
mSection.subsection = SECTION_GAME_PLAY;
}
Game::~Game()
@@ -49,10 +55,6 @@ Game::~Game()
delete mEventHandler;
mEventHandler = nullptr;
mTextureText->unload();
delete mTextureText;
mTextureText = nullptr;
delete mText;
mText = nullptr;
@@ -60,35 +62,9 @@ Game::~Game()
mFade = nullptr;
}
// Inicializa las variables necesarias para la sección 'Game'
void Game::init()
{
// Carga los recursos
loadMedia();
mTicks = 0;
mTicksSpeed = 15;
mSection.name = SECTION_PROG_GAME;
mSection.subsection = SECTION_GAME_PLAY;
}
// Carga los recursos necesarios para la sección 'Game'
bool Game::loadMedia()
{
bool success = true;
// Texturas
success &= loadTextureFromFile(mTextureText, mAsset->get("smb2.png"), mRenderer);
return success;
}
// Bucle para el juego
section_t Game::run()
{
init();
while (mSection.name == SECTION_PROG_GAME)
{
// Sección juego jugando

View File

@@ -35,20 +35,13 @@ private:
Input *mInput; // Objeto pata gestionar la entrada
Text *mText; // Objeto para los textos del juego
Fade *mFade; // Objeto para renderizar fades
LTexture *mTextureText; // Textura para la fuente de texto
Uint32 mTicks; // Contador de ticks para ajustar la velocidad del programa
Uint8 mTicksSpeed; // Velocidad a la que se repiten los bucles del programa
int mTicks; // Contador de ticks para ajustar la velocidad del programa
int mTicksSpeed; // Velocidad a la que se repiten los bucles del programa
section_t mSection; // Seccion actual dentro del juego
std::string mCurrentRoom; // Fichero de la habitación actual
player_t mSpawnPoint; // Lugar de la habitación donde aparece el jugador
bool mDebug; // Indica si el modo debug está activo
// Inicializa las variables
void init();
// Carga los recursos
bool loadMedia();
// Actualiza el juego, las variables, comprueba la entrada, etc.
void update();

View File

@@ -4,13 +4,15 @@
#include <fstream>
// Constructor
Text::Text(std::string file, LTexture *texture, SDL_Renderer *renderer)
Text::Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer)
{
SDL_Rect rect = {0,0,0,0};
mSprite = new Sprite(rect, texture, renderer);
texture = new LTexture();
loadTextureFromFile(texture, bitmapFile, renderer);
mSprite = new Sprite({0, 0, 0, 0}, texture, renderer);
mSprite->setTexture(texture);
mSprite->setRenderer(renderer);
mFile = file;
file = textFile;
init();
}
@@ -18,6 +20,10 @@ Text::Text(std::string file, LTexture *texture, SDL_Renderer *renderer)
// Destructor
Text::~Text()
{
texture->unload();
delete texture;
texture = nullptr;
delete mSprite;
mSprite = nullptr;
}
@@ -28,17 +34,17 @@ void Text::init()
// Inicializa a cero el vector con las coordenadas
for (int i = 0; i < 128; i++)
{
mOffset[i].x = 0;
mOffset[i].y = 0;
mOffset[i].w = 0;
offset[i].x = 0;
offset[i].y = 0;
offset[i].w = 0;
}
// Carga los offsets desde el fichero
initOffsetFromFile();
// Inicia los valores del sprite que dibuja las letras
mSprite->setWidth(mBoxWidth);
mSprite->setHeight(mBoxHeight);
mSprite->setWidth(boxWidth);
mSprite->setHeight(boxHeight);
mSprite->setPosX(0);
mSprite->setPosY(0);
mSprite->setSpriteClip(0, 0, mSprite->getWidth(), mSprite->getHeight());
@@ -46,26 +52,30 @@ void Text::init()
// Establece las coordenadas para cada caracter ascii de la cadena y su ancho
for (int i = 32; i < 128; i++)
{
mOffset[i].x = ((i - 32) % 15) * mBoxWidth;
mOffset[i].y = ((i - 32) / 15) * mBoxHeight;
offset[i].x = ((i - 32) % 15) * boxWidth;
offset[i].y = ((i - 32) / 15) * boxHeight;
}
// printf("Cargando %s\n", file.c_str());
// const std::string texto = "w = "+ std::to_string(boxWidth) + ", h = " + std::to_string(boxHeight);
// printf("%s\n",texto.c_str());
}
// Escribe texto en pantalla
void Text::write(int x, int y, std::string text, int kerning, int lenght)
{
Uint16 shift = 0;
int shift = 0;
if (lenght == -1)
lenght = text.length();
for (int i = 0; i < lenght; ++i)
{
mSprite->setSpriteClip(mOffset[int(text[i])].x, mOffset[int(text[i])].y, mSprite->getWidth(), mSprite->getHeight());
mSprite->setSpriteClip(offset[int(text[i])].x, offset[int(text[i])].y, mSprite->getWidth(), mSprite->getHeight());
mSprite->setPosX(x + shift);
mSprite->setPosY(y);
mSprite->render();
shift += (mOffset[int(text[i])].w + kerning);
shift += (offset[int(text[i])].w + kerning);
}
}
@@ -127,33 +137,33 @@ void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, col
}
// Obtiene la longitud en pixels de una cadena
Uint16 Text::lenght(std::string text, int kerning)
int Text::lenght(std::string text, int kerning)
{
Uint16 shift = 0;
int shift = 0;
for (int i = 0; i < (int)text.length(); ++i)
shift += (mOffset[int(text[i])].w + kerning);
for (int i = 0; i < (int)text.length(); i++)
shift += (offset[int(text[i])].w + kerning);
return shift;
// Descuenta el kerning del último caracter
return shift - kerning;
}
// Inicializa el vector de offsets desde un fichero
void Text::initOffsetFromFile()
{
std::ifstream rfile(mFile);
std::ifstream rfile(file);
if (rfile.is_open() && rfile.good())
{
std::string buffer;
//printf("Reading %s file\n", mFile.c_str());
// Lee los dos primeros valores del fichero
std::getline(rfile, buffer);
std::getline(rfile, buffer);
mBoxWidth = std::stoi(buffer);
boxWidth = std::stoi(buffer);
std::getline(rfile, buffer);
std::getline(rfile, buffer);
mBoxHeight = std::stoi(buffer);
boxHeight = std::stoi(buffer);
// lee el resto de datos del fichero
int index = 32;
@@ -162,7 +172,7 @@ void Text::initOffsetFromFile()
{
// Almacena solo las lineas impares
if (line_read % 2 == 1)
mOffset[index++].w = std::stoi(buffer);
offset[index++].w = std::stoi(buffer);
// Limpia el buffer
buffer.clear();
@@ -172,7 +182,7 @@ void Text::initOffsetFromFile()
}
// Devuelve el valor de la variable
Uint8 Text::getCharacterWidth()
int Text::getCharacterWidth()
{
return mBoxWidth;
return boxWidth;
}

View File

@@ -1,4 +1,5 @@
#pragma once
#include "sprite.h"
#include "utils.h"
@@ -20,27 +21,28 @@ private:
{
int x;
int y;
Uint8 w;
int w;
};
Offset mOffset[128]; // Vector con las posiciones y ancho de cada letra
Offset offset[128]; // Vector con las posiciones y ancho de cada letra
Uint8 mBoxWidth; // Anchura de la caja de cada caracter en el png
Uint8 mBoxHeight; // Altura de la caja de cada caracter en el png
std::string mFile; // Fichero con los descriptores de la fuente
int boxWidth; // Anchura de la caja de cada caracter en el png
int boxHeight; // Altura de la caja de cada caracter en el png
std::string file; // Fichero con los descriptores de la fuente
LTexture *texture; // Textura con los bitmaps del texto
// Inicializador
void init();
// Inicializa el vector de offsets desde un fichero
void initOffsetFromFile();
public:
// Constructor
Text(std::string file, LTexture *texture, SDL_Renderer *renderer);
Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer);
// Destructor
~Text();
// Inicializador
void init();
// Escribe el texto en pantalla
void write(int x, int y, std::string text, int kerning = 1, int lenght = -1);
@@ -50,17 +52,17 @@ public:
// Escribe el texto con sombra
void writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance = 1, int kerning = 1, int lenght = -1);
// Escribe el texto centrado en un punto x y con kerning
// Escribe el texto centrado en un punto x
void writeCentered(int x, int y, std::string text, int kerning = 1, int lenght = -1);
// Escribe texto con extras
void writeDX(Uint8 flags, int x, int y, std::string text, int kerning = 1, color_t textColor = {255, 255, 255}, Uint8 shadowDistance = 1, color_t shadowColor = {0, 0, 0}, int lenght = -1);
// Obtiene la longitud en pixels de una cadena
Uint16 lenght(std::string text, int kerning = 1);
int lenght(std::string text, int kerning = 1);
// Devuelve el valor de la variable
Uint8 getCharacterWidth();
int getCharacterWidth();
};
#endif