Skip to content

Commit

Permalink
[Font] Class added in order to remove Text::updateCharWidth.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Mar 3, 2020
1 parent a8542b8 commit 896460c
Show file tree
Hide file tree
Showing 6 changed files with 392 additions and 65 deletions.
6 changes: 5 additions & 1 deletion client/source/core/ClientApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "ClientApplication.hpp"
#include "Config.hpp"
#include "Font.hpp"
#include "TextureAtlas.hpp"
#include "TextureLoader.hpp"

Expand Down Expand Up @@ -66,9 +67,12 @@ void ClientApplication::init() {
initOpenGL();

m_resourceHandler.loadConfigFile<TextureLoader>("resources/config/textures.xml");
m_resourceHandler.add<gk::Font>("font-default", "resources/fonts/default.ttf");
m_resourceHandler.add<Font>("font-ascii", "texture-font", "resources/textures/font.properties");
m_resourceHandler.add<TextureAtlas>("atlas-blocks");

// FIXME: Remove this after replacing gk::TextInput in ServerConnectState
m_resourceHandler.add<gk::Font>("font-default", "resources/fonts/default.ttf");

Registry::setInstance(m_registry);

// m_stateStack.push<TitleScreenState>();
Expand Down
64 changes: 64 additions & 0 deletions client/source/gui/Font.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#include <fstream>

#include <gk/gl/Texture.hpp>
#include <gk/resource/ResourceHandler.hpp>

#include "Font.hpp"

Font::Font(const std::string &textureName, const std::string &configPath)
: m_texture(gk::ResourceHandler::getInstance().get<gk::Texture>(textureName))
{
m_textureName = textureName;

std::memset(m_charWidth, 0, sizeof(u8) * 256);

parseConfig(configPath);
}

void Font::parseConfig(const std::string &configPath) {
std::ifstream file(configPath);

std::string line;
while (std::getline(file, line, '\n')) {
if (line.empty()) continue;

u16 dot = line.find_first_of('.');
u16 equal = line.find_first_of('=');

std::string propertyName = line.substr(0, dot);
if (propertyName != "width")
throw EXCEPTION("Unexpected property for font:", propertyName);

int propertyKey = std::stoi(line.substr(dot + 1, equal - dot - 1));
u8 propertyValue = std::stoi(line.substr(equal + 1));

m_charWidth[propertyKey] = propertyValue;
}
}

56 changes: 56 additions & 0 deletions client/source/gui/Font.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef FONT_HPP_
#define FONT_HPP_

#include <string>

#include <gk/core/IntTypes.hpp>

namespace gk {
class Texture;
}

class Font {
public:
Font(const std::string &textureName, const std::string &configPath);

u8 getCharWidth(u8 c) { return m_charWidth[c]; }

const std::string &textureName() const { return m_textureName; } // FIXME: Will be removed later
const gk::Texture &texture() const { return m_texture; }

private:
void parseConfig(const std::string &configPath);

std::string m_textureName; // FIXME: Will be removed later
gk::Texture &m_texture;

u8 m_charWidth[256];
};

#endif // FONT_HPP_
67 changes: 8 additions & 59 deletions client/source/gui/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@
*
* =====================================================================================
*/
#include <gk/gl/Texture.hpp>
#include <gk/resource/ResourceHandler.hpp>

#include "Color.hpp"
#include "Font.hpp"
#include "Text.hpp"

Text::Text() : m_texture(gk::ResourceHandler::getInstance().get<gk::Texture>("texture-font")) {
Text::Text() : m_font(gk::ResourceHandler::getInstance().get<Font>("font-ascii")) {
m_background.setFillColor(gk::Color::Transparent);

updateCharWidth();
}

void Text::setText(const std::string &text) {
Expand Down Expand Up @@ -71,38 +69,38 @@ void Text::updateTextSprites() {
unsigned int maxX = 0;
gk::Color color = gk::Color{70, 70, 70, 255};
for(char c : m_text) {
if (c == '\n' || (m_maxLineLength && x + m_charWidth[(u8)c] >= m_maxLineLength)) {
if (c == '\n' || (m_maxLineLength && x + m_font.getCharWidth(c) >= m_maxLineLength)) {
y += 9;
x = 0;
continue;
}

gk::Sprite sprite{"texture-font", 8, 8};
gk::Sprite sprite{m_font.textureName(), 8, 8};
sprite.setCurrentFrame(c);
sprite.setPosition(x + 1, y + 1, 0);
sprite.setColor(color);
m_textSprites.emplace_back(std::move(sprite));
x += m_charWidth[(u8)c];
x += m_font.getCharWidth(c);
}
x = 0;
y = 0;
color = m_color;
for(char c : m_text) {
if (c == '\n' || (m_maxLineLength && x + m_charWidth[(u8)c] >= m_maxLineLength)) {
if (c == '\n' || (m_maxLineLength && x + m_font.getCharWidth(c) >= m_maxLineLength)) {
maxX = std::max(x, maxX);
y += 9;
x = 0;
continue;
}

gk::Sprite sprite{"texture-font", 8, 8};
gk::Sprite sprite{m_font.textureName(), 8, 8};
sprite.setCurrentFrame(c);
sprite.setPosition(x, y, 0);
if (c == '[')
color = Color::Blue;
sprite.setColor(color);
m_textSprites.emplace_back(std::move(sprite));
x += m_charWidth[(u8)c];
x += m_font.getCharWidth(c);
}

m_size.x = std::max(x, maxX);
Expand All @@ -114,52 +112,3 @@ void Text::updateTextSprites() {
m_background.setSize(backgroundX, backgroundY);
}

// FIXME: Since I use the font from Minecraft assets, I needed to use
// this piece of code to make it look good
// I'll remove it later anyway
void Text::updateCharWidth() {
const int width = m_texture.getSize().x;
const int height = m_texture.getSize().y;
unsigned int *data = new unsigned int[width * height];

gk::Texture::bind(&m_texture);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, data);
gk::Texture::bind(nullptr);

const int charMaxHeight = height / 16;
const int charMaxWidth = width / 16;

for (int i = 0 ; i < 256 ; ++i) {
if (i == ' ') {
m_charWidth[i] = 4;
continue;
}

int charX = i % 16;
int charY = i / 16;

if (i == 32)
m_charWidth[i] = 4;

int l1;
for (l1 = charMaxWidth - 1 ; l1 >= 0 ; --l1) {
int i2 = charX * charMaxWidth + l1;
bool flag1 = true;

for (int j2 = 0 ; j2 < charMaxHeight && flag1 ; ++j2) {
int k2 = (charY * charMaxWidth + j2) * width;

if ((data[i2 + k2] & 255) != 0)
flag1 = false;
}

if (!flag1) break;
}

++l1;
m_charWidth[i] = 0.5f + l1 * (8.0f / charMaxWidth) + 1;
}

delete[] data;
}

8 changes: 3 additions & 5 deletions client/source/gui/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <gk/graphics/RectangleShape.hpp>
#include <gk/graphics/Sprite.hpp>

class Font;

class Text : public gk::Drawable, public gk::Transformable {
public:
Text();
Expand All @@ -55,15 +57,11 @@ class Text : public gk::Drawable, public gk::Transformable {
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

void updateTextSprites();
void updateCharWidth();

std::string m_text;
std::vector<gk::Sprite> m_textSprites;

int m_charWidth[256];

gk::Texture &m_texture;
gk::VertexBuffer m_vbo;
Font &m_font;

gk::Vector2i m_size;
gk::Vector2i m_padding{0, 0};
Expand Down
Loading

0 comments on commit 896460c

Please sign in to comment.