Browse Source

Change Character postiion/size to uint, add Font size

master
Riyyi 4 years ago
parent
commit
a962f7bc31
  1. 26
      inferno/src/inferno/render/font.cpp
  2. 11
      inferno/src/inferno/render/font.h

26
inferno/src/inferno/render/font.cpp

@ -5,6 +5,7 @@
#include "inferno/io/file.h" #include "inferno/io/file.h"
#include "inferno/render/font.h" #include "inferno/render/font.h"
#include "inferno/render/texture.h" #include "inferno/render/texture.h"
#include "inferno/util/string.h"
namespace Inferno { namespace Inferno {
@ -24,21 +25,28 @@ namespace Inferno {
std::istringstream iss(font); std::istringstream iss(font);
for (std::string line; std::getline(iss, line);) { for (std::string line; std::getline(iss, line);) {
if (findAction(line).compare("char") != 0) { if (findAction(line).compare("info") != 0 &&
findAction(line).compare("char") != 0) {
continue; continue;
} }
const std::vector<std::string> data = findColumns(line); const std::vector<std::string> columns = findColumns(line);
unsigned char id = std::stoi(findValue("id", data)); // Info
unsigned long advance = std::stoul(findValue("xadvance", data));
ASSERT(advance <= std::numeric_limits<uint32_t>::max(), "Font file xadvance is not in uint32_t range on line \n'{}'", line);
if (findAction(line).compare("info") == 0) {
m_size = std::stou(findValue("size", columns));
continue;
}
// Character
unsigned char id = std::stoi(findValue("id", columns));
Character character = { Character character = {
{ std::stoi(findValue("x", data)), std::stoi(findValue("y", data)) }, { std::stou(findValue("x", columns)), std::stou(findValue("y", columns)) },
{ std::stoi(findValue("width", data)), std::stoi(findValue("height", data)) }, { std::stou(findValue("width", columns)), std::stou(findValue("height", columns)) },
{ std::stoi(findValue("xoffset", data)), std::stoi(findValue("yoffset", data)) }, { std::stoi(findValue("xoffset", columns)), std::stoi(findValue("yoffset", columns)) },
static_cast<uint32_t>(advance) std::stou(findValue("xadvance", columns))
}; };
m_characterList.emplace(id, std::make_shared<Character>(character)); m_characterList.emplace(id, std::make_shared<Character>(character));
} }

11
inferno/src/inferno/render/font.h

@ -7,7 +7,8 @@
#include <unordered_map> // std::unordered_map #include <unordered_map> // std::unordered_map
#include <vector> // std::vector #include <vector> // std::vector
#include "glm/ext/vector_int2.hpp" // glm::ivec2 #include "glm/ext/vector_uint2.hpp" // glm::uvec2
#include "glm/ext/vector_int2.hpp" // glm::ivec2
#include "inferno/io/log.h" #include "inferno/io/log.h"
@ -16,8 +17,8 @@ namespace Inferno {
class Texture; class Texture;
struct Character { struct Character {
glm::ivec2 position; // Position glm::uvec2 position; // Position
glm::ivec2 size; // Width/height glm::uvec2 size; // Width/height
glm::ivec2 offset; // Offset from baseline to left / top of glyph glm::ivec2 offset; // Offset from baseline to left / top of glyph
uint32_t advance; // Amount to advance to next glyph uint32_t advance; // Amount to advance to next glyph
}; };
@ -29,6 +30,7 @@ class Texture;
Font(const std::string& name); Font(const std::string& name);
virtual ~Font() {} virtual ~Font() {}
inline uint32_t size() const { return m_size; }
inline const std::shared_ptr<Character>& get(unsigned char c) const { return m_characterList.at(c); } inline const std::shared_ptr<Character>& get(unsigned char c) const { return m_characterList.at(c); }
inline const std::shared_ptr<Character>& operator[](unsigned char c) const { return m_characterList.at(c); } inline const std::shared_ptr<Character>& operator[](unsigned char c) const { return m_characterList.at(c); }
@ -41,9 +43,10 @@ class Texture;
const std::vector<std::string> findColumns(const std::string& line); const std::vector<std::string> findColumns(const std::string& line);
const std::string findValue(const std::string& key, const std::vector<std::string>& columns); const std::string findValue(const std::string& key, const std::vector<std::string>& columns);
uint32_t m_size;
std::unordered_map<unsigned char, std::shared_ptr<Character>> m_characterList;
std::string m_name; std::string m_name;
std::shared_ptr<Texture> m_texture; std::shared_ptr<Texture> m_texture;
std::unordered_map<unsigned char, std::shared_ptr<Character>> m_characterList;
}; };
// ----------------------------------------- // -----------------------------------------

Loading…
Cancel
Save