forked from jaildesigner-jailgames/jaildoctors_dilemma
Actualizada la clase Text
This commit is contained in:
@@ -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);
|
mRoom = new Room(mAsset->get(mCurrentRoom), mRenderer, mAsset, mItemTracker);
|
||||||
mPlayer = new Player(mSpawnPoint, mAsset->get("player01.png"), mRenderer, mAsset, mInput, mRoom);
|
mPlayer = new Player(mSpawnPoint, mAsset->get("player01.png"), mRenderer, mAsset, mInput, mRoom);
|
||||||
mEventHandler = new SDL_Event();
|
mEventHandler = new SDL_Event();
|
||||||
mTextureText = new LTexture();
|
mText = new Text(mAsset->get("smb2.png"), mAsset->get("smb2.txt"), renderer);
|
||||||
mText = new Text(mAsset->get("smb2.txt"), mTextureText, renderer);
|
|
||||||
mFade = new Fade(renderer);
|
mFade = new Fade(renderer);
|
||||||
|
|
||||||
|
// Inicializa variables
|
||||||
|
mTicks = 0;
|
||||||
|
mTicksSpeed = 15;
|
||||||
|
|
||||||
|
mSection.name = SECTION_PROG_GAME;
|
||||||
|
mSection.subsection = SECTION_GAME_PLAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::~Game()
|
Game::~Game()
|
||||||
@@ -49,10 +55,6 @@ Game::~Game()
|
|||||||
delete mEventHandler;
|
delete mEventHandler;
|
||||||
mEventHandler = nullptr;
|
mEventHandler = nullptr;
|
||||||
|
|
||||||
mTextureText->unload();
|
|
||||||
delete mTextureText;
|
|
||||||
mTextureText = nullptr;
|
|
||||||
|
|
||||||
delete mText;
|
delete mText;
|
||||||
mText = nullptr;
|
mText = nullptr;
|
||||||
|
|
||||||
@@ -60,35 +62,9 @@ Game::~Game()
|
|||||||
mFade = nullptr;
|
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
|
// Bucle para el juego
|
||||||
section_t Game::run()
|
section_t Game::run()
|
||||||
{
|
{
|
||||||
init();
|
|
||||||
|
|
||||||
while (mSection.name == SECTION_PROG_GAME)
|
while (mSection.name == SECTION_PROG_GAME)
|
||||||
{
|
{
|
||||||
// Sección juego jugando
|
// Sección juego jugando
|
||||||
|
|||||||
@@ -35,20 +35,13 @@ private:
|
|||||||
Input *mInput; // Objeto pata gestionar la entrada
|
Input *mInput; // Objeto pata gestionar la entrada
|
||||||
Text *mText; // Objeto para los textos del juego
|
Text *mText; // Objeto para los textos del juego
|
||||||
Fade *mFade; // Objeto para renderizar fades
|
Fade *mFade; // Objeto para renderizar fades
|
||||||
LTexture *mTextureText; // Textura para la fuente de texto
|
int mTicks; // Contador de ticks para ajustar la velocidad del programa
|
||||||
Uint32 mTicks; // Contador de ticks para ajustar la velocidad del programa
|
int mTicksSpeed; // Velocidad a la que se repiten los bucles del programa
|
||||||
Uint8 mTicksSpeed; // Velocidad a la que se repiten los bucles del programa
|
|
||||||
section_t mSection; // Seccion actual dentro del juego
|
section_t mSection; // Seccion actual dentro del juego
|
||||||
std::string mCurrentRoom; // Fichero de la habitación actual
|
std::string mCurrentRoom; // Fichero de la habitación actual
|
||||||
player_t mSpawnPoint; // Lugar de la habitación donde aparece el jugador
|
player_t mSpawnPoint; // Lugar de la habitación donde aparece el jugador
|
||||||
bool mDebug; // Indica si el modo debug está activo
|
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.
|
// Actualiza el juego, las variables, comprueba la entrada, etc.
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
|
|||||||
@@ -4,13 +4,15 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
// Constructor
|
// 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};
|
texture = new LTexture();
|
||||||
mSprite = new Sprite(rect, texture, renderer);
|
loadTextureFromFile(texture, bitmapFile, renderer);
|
||||||
|
|
||||||
|
mSprite = new Sprite({0, 0, 0, 0}, texture, renderer);
|
||||||
mSprite->setTexture(texture);
|
mSprite->setTexture(texture);
|
||||||
mSprite->setRenderer(renderer);
|
mSprite->setRenderer(renderer);
|
||||||
mFile = file;
|
file = textFile;
|
||||||
|
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
@@ -18,6 +20,10 @@ Text::Text(std::string file, LTexture *texture, SDL_Renderer *renderer)
|
|||||||
// Destructor
|
// Destructor
|
||||||
Text::~Text()
|
Text::~Text()
|
||||||
{
|
{
|
||||||
|
texture->unload();
|
||||||
|
delete texture;
|
||||||
|
texture = nullptr;
|
||||||
|
|
||||||
delete mSprite;
|
delete mSprite;
|
||||||
mSprite = nullptr;
|
mSprite = nullptr;
|
||||||
}
|
}
|
||||||
@@ -28,17 +34,17 @@ void Text::init()
|
|||||||
// Inicializa a cero el vector con las coordenadas
|
// Inicializa a cero el vector con las coordenadas
|
||||||
for (int i = 0; i < 128; i++)
|
for (int i = 0; i < 128; i++)
|
||||||
{
|
{
|
||||||
mOffset[i].x = 0;
|
offset[i].x = 0;
|
||||||
mOffset[i].y = 0;
|
offset[i].y = 0;
|
||||||
mOffset[i].w = 0;
|
offset[i].w = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Carga los offsets desde el fichero
|
// Carga los offsets desde el fichero
|
||||||
initOffsetFromFile();
|
initOffsetFromFile();
|
||||||
|
|
||||||
// Inicia los valores del sprite que dibuja las letras
|
// Inicia los valores del sprite que dibuja las letras
|
||||||
mSprite->setWidth(mBoxWidth);
|
mSprite->setWidth(boxWidth);
|
||||||
mSprite->setHeight(mBoxHeight);
|
mSprite->setHeight(boxHeight);
|
||||||
mSprite->setPosX(0);
|
mSprite->setPosX(0);
|
||||||
mSprite->setPosY(0);
|
mSprite->setPosY(0);
|
||||||
mSprite->setSpriteClip(0, 0, mSprite->getWidth(), mSprite->getHeight());
|
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
|
// Establece las coordenadas para cada caracter ascii de la cadena y su ancho
|
||||||
for (int i = 32; i < 128; i++)
|
for (int i = 32; i < 128; i++)
|
||||||
{
|
{
|
||||||
mOffset[i].x = ((i - 32) % 15) * mBoxWidth;
|
offset[i].x = ((i - 32) % 15) * boxWidth;
|
||||||
mOffset[i].y = ((i - 32) / 15) * mBoxHeight;
|
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
|
// Escribe texto en pantalla
|
||||||
void Text::write(int x, int y, std::string text, int kerning, int lenght)
|
void Text::write(int x, int y, std::string text, int kerning, int lenght)
|
||||||
{
|
{
|
||||||
Uint16 shift = 0;
|
int shift = 0;
|
||||||
|
|
||||||
if (lenght == -1)
|
if (lenght == -1)
|
||||||
lenght = text.length();
|
lenght = text.length();
|
||||||
|
|
||||||
for (int i = 0; i < lenght; ++i)
|
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->setPosX(x + shift);
|
||||||
mSprite->setPosY(y);
|
mSprite->setPosY(y);
|
||||||
mSprite->render();
|
mSprite->render();
|
||||||
shift += (mOffset[int(text[i])].w + kerning);
|
shift += (offset[int(text[i])].w + kerning);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +123,7 @@ void Text::writeDX(Uint8 flags, int x, int y, std::string text, int kerning, col
|
|||||||
writeColored(x - shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
|
writeColored(x - shadowDistance, y + shadowDistance, text, shadowColor, kerning, lenght);
|
||||||
writeColored(x + shadowDistance, y - shadowDistance, text, shadowColor, kerning, lenght);
|
writeColored(x + shadowDistance, y - shadowDistance, text, shadowColor, kerning, lenght);
|
||||||
writeColored(x - shadowDistance, y - shadowDistance, text, shadowColor, kerning, lenght);
|
writeColored(x - shadowDistance, y - shadowDistance, text, shadowColor, kerning, lenght);
|
||||||
|
|
||||||
writeColored(x, y + shadowDistance, text, shadowColor, kerning, lenght);
|
writeColored(x, y + shadowDistance, text, shadowColor, kerning, lenght);
|
||||||
writeColored(x, y - shadowDistance, text, shadowColor, kerning, lenght);
|
writeColored(x, y - shadowDistance, text, shadowColor, kerning, lenght);
|
||||||
writeColored(x + shadowDistance, y, text, shadowColor, kerning, lenght);
|
writeColored(x + shadowDistance, y, text, shadowColor, kerning, lenght);
|
||||||
@@ -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
|
// 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)
|
for (int i = 0; i < (int)text.length(); i++)
|
||||||
shift += (mOffset[int(text[i])].w + kerning);
|
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
|
// Inicializa el vector de offsets desde un fichero
|
||||||
void Text::initOffsetFromFile()
|
void Text::initOffsetFromFile()
|
||||||
{
|
{
|
||||||
std::ifstream rfile(mFile);
|
std::ifstream rfile(file);
|
||||||
if (rfile.is_open() && rfile.good())
|
if (rfile.is_open() && rfile.good())
|
||||||
{
|
{
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
//printf("Reading %s file\n", mFile.c_str());
|
|
||||||
|
|
||||||
// Lee los dos primeros valores del fichero
|
// Lee los dos primeros valores del fichero
|
||||||
std::getline(rfile, buffer);
|
std::getline(rfile, buffer);
|
||||||
std::getline(rfile, buffer);
|
std::getline(rfile, buffer);
|
||||||
mBoxWidth = std::stoi(buffer);
|
boxWidth = std::stoi(buffer);
|
||||||
|
|
||||||
std::getline(rfile, buffer);
|
std::getline(rfile, buffer);
|
||||||
std::getline(rfile, buffer);
|
std::getline(rfile, buffer);
|
||||||
mBoxHeight = std::stoi(buffer);
|
boxHeight = std::stoi(buffer);
|
||||||
|
|
||||||
// lee el resto de datos del fichero
|
// lee el resto de datos del fichero
|
||||||
int index = 32;
|
int index = 32;
|
||||||
@@ -162,8 +172,8 @@ void Text::initOffsetFromFile()
|
|||||||
{
|
{
|
||||||
// Almacena solo las lineas impares
|
// Almacena solo las lineas impares
|
||||||
if (line_read % 2 == 1)
|
if (line_read % 2 == 1)
|
||||||
mOffset[index++].w = std::stoi(buffer);
|
offset[index++].w = std::stoi(buffer);
|
||||||
|
|
||||||
// Limpia el buffer
|
// Limpia el buffer
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
line_read++;
|
line_read++;
|
||||||
@@ -172,7 +182,7 @@ void Text::initOffsetFromFile()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Devuelve el valor de la variable
|
// Devuelve el valor de la variable
|
||||||
Uint8 Text::getCharacterWidth()
|
int Text::getCharacterWidth()
|
||||||
{
|
{
|
||||||
return mBoxWidth;
|
return boxWidth;
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "sprite.h"
|
#include "sprite.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
@@ -20,27 +21,28 @@ private:
|
|||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
int y;
|
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
|
int boxWidth; // Anchura de la caja de cada caracter en el png
|
||||||
Uint8 mBoxHeight; // Altura de la caja de cada caracter en el png
|
int boxHeight; // Altura de la caja de cada caracter en el png
|
||||||
std::string mFile; // Fichero con los descriptores de la fuente
|
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
|
// Inicializa el vector de offsets desde un fichero
|
||||||
void initOffsetFromFile();
|
void initOffsetFromFile();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Text(std::string file, LTexture *texture, SDL_Renderer *renderer);
|
Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer);
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~Text();
|
~Text();
|
||||||
|
|
||||||
// Inicializador
|
|
||||||
void init();
|
|
||||||
|
|
||||||
// Escribe el texto en pantalla
|
// Escribe el texto en pantalla
|
||||||
void write(int x, int y, std::string text, int kerning = 1, int lenght = -1);
|
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
|
// 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);
|
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);
|
void writeCentered(int x, int y, std::string text, int kerning = 1, int lenght = -1);
|
||||||
|
|
||||||
// Escribe texto con extras
|
// 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);
|
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
|
// 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
|
// Devuelve el valor de la variable
|
||||||
Uint8 getCharacterWidth();
|
int getCharacterWidth();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user