neteja tidy a source/core i encamina Texture::loadFromFile pel ResourceHelper
This commit is contained in:
@@ -39,25 +39,25 @@ static void computeTextFileOffsets(textFile_t &tf) {
|
||||
}
|
||||
|
||||
// Llena una estructuta textFile_t desde un fichero
|
||||
textFile_t LoadTextFile(const std::string &file, bool verbose) {
|
||||
auto LoadTextFile(const std::string &file, bool verbose) -> textFile_t {
|
||||
textFile_t tf;
|
||||
tf.boxWidth = 0;
|
||||
tf.boxHeight = 0;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
tf.offset[i].x = 0;
|
||||
tf.offset[i].y = 0;
|
||||
tf.offset[i].w = 0;
|
||||
for (auto &i : tf.offset) {
|
||||
i.x = 0;
|
||||
i.y = 0;
|
||||
i.w = 0;
|
||||
}
|
||||
|
||||
const std::string filename = file.substr(file.find_last_of("\\/") + 1).c_str();
|
||||
const std::string filename = file.substr(file.find_last_of("\\/") + 1);
|
||||
std::ifstream rfile(file);
|
||||
if (rfile.is_open() && rfile.good()) {
|
||||
parseTextFileStream(rfile, tf);
|
||||
if (verbose) {
|
||||
std::cout << "Text loaded: " << filename.c_str() << std::endl;
|
||||
std::cout << "Text loaded: " << filename.c_str() << '\n';
|
||||
}
|
||||
} else if (verbose) {
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
|
||||
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << '\n';
|
||||
}
|
||||
|
||||
computeTextFileOffsets(tf);
|
||||
@@ -65,21 +65,21 @@ textFile_t LoadTextFile(const std::string &file, bool verbose) {
|
||||
}
|
||||
|
||||
// Llena una estructura textFile_t desde bytes en memoria
|
||||
textFile_t LoadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose) {
|
||||
auto LoadTextFileFromMemory(const std::vector<uint8_t> &bytes, bool verbose) -> textFile_t {
|
||||
textFile_t tf;
|
||||
tf.boxWidth = 0;
|
||||
tf.boxHeight = 0;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
tf.offset[i].x = 0;
|
||||
tf.offset[i].y = 0;
|
||||
tf.offset[i].w = 0;
|
||||
for (auto &i : tf.offset) {
|
||||
i.x = 0;
|
||||
i.y = 0;
|
||||
i.w = 0;
|
||||
}
|
||||
if (!bytes.empty()) {
|
||||
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
|
||||
std::stringstream ss(content);
|
||||
parseTextFileStream(ss, tf);
|
||||
if (verbose) {
|
||||
std::cout << "Text loaded from memory" << std::endl;
|
||||
std::cout << "Text loaded from memory" << '\n';
|
||||
}
|
||||
}
|
||||
computeTextFileOffsets(tf);
|
||||
@@ -170,9 +170,8 @@ Text::Text(const std::vector<uint8_t> &pngBytes, const std::vector<uint8_t> &txt
|
||||
// Destructor
|
||||
Text::~Text() {
|
||||
delete sprite;
|
||||
if (texture != nullptr) {
|
||||
|
||||
delete texture;
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe texto en pantalla
|
||||
@@ -187,11 +186,11 @@ void Text::write(int x, int y, const std::string &text, int kerning, int lenght)
|
||||
const int width = sprite->getWidth();
|
||||
const int height = sprite->getHeight();
|
||||
for (int i = 0; i < lenght; ++i) {
|
||||
const int index = text[i];
|
||||
const int index = static_cast<unsigned char>(text[i]);
|
||||
sprite->setSpriteClip(offset[index].x, offset[index].y, width, height);
|
||||
sprite->setPosX(x + shift);
|
||||
sprite->render();
|
||||
shift += fixedWidth ? boxWidth : (offset[int(text[i])].w + kerning);
|
||||
shift += fixedWidth ? boxWidth : (offset[index].w + kerning);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,18 +248,19 @@ void Text::writeDX(Uint8 flags, int x, int y, const std::string &text, int kerni
|
||||
}
|
||||
|
||||
// Obtiene la longitud en pixels de una cadena
|
||||
int Text::lenght(const std::string &text, int kerning) {
|
||||
auto Text::lenght(const std::string &text, int kerning) -> int {
|
||||
int shift = 0;
|
||||
|
||||
for (int i = 0; i < (int)text.length(); ++i)
|
||||
shift += (offset[int(text[i])].w + kerning);
|
||||
for (int i = 0; i < (int)text.length(); ++i) {
|
||||
shift += (offset[static_cast<unsigned char>(text[i])].w + kerning);
|
||||
}
|
||||
|
||||
// Descuenta el kerning del último caracter
|
||||
return shift - kerning;
|
||||
}
|
||||
|
||||
// Devuelve el valor de la variable
|
||||
int Text::getCharacterSize() {
|
||||
auto Text::getCharacterSize() const -> int {
|
||||
return boxWidth;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user