From 01e901a0c8031f64f36f850f193b18018130c1e9 Mon Sep 17 00:00:00 2001 From: Anselme Date: Sun, 4 Dec 2016 16:15:03 +0100 Subject: [PATCH] fixed font resize ugly appearance --- src/sparrowshell.cpp | 2 +- src/sparrowshell.h | 2 ++ src/test/main.cpp | 2 +- src/tools/font.cpp | 15 ++++++--------- src/tools/font.h | 12 ++++++------ src/tools/loader.cpp | 12 ++++++------ 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/sparrowshell.cpp b/src/sparrowshell.cpp index 2ade0b7..91782b2 100644 --- a/src/sparrowshell.cpp +++ b/src/sparrowshell.cpp @@ -24,7 +24,7 @@ SparrowShell::SparrowShell(sf::Window* window, Input* input): sf::Vector2u size = m_window->getSize(); m_dimension = glm::ivec2(size.x,size.y/2); Mesh* mesh = new Mesh(); - m_buffer.setFontSize(12.f); + m_buffer.setFontSize(16.f); mesh->addRectangle2D(m_position,m_dimension); PhongMaterial *mat = new PhongMaterial(); mat->diffuse = glm::vec3(0.1,0.1,0.1); diff --git a/src/sparrowshell.h b/src/sparrowshell.h index 1eff5d3..bba1bd3 100644 --- a/src/sparrowshell.h +++ b/src/sparrowshell.h @@ -40,6 +40,8 @@ private: SceneNode* operator[](int i){return m_children[(m_zero_offset+i)%m_max_size];} void push(TextNode*); unsigned int size(){return m_children.size();} + + // if this font_size is scaling down, sizes which are power of 2 render better. void setFontSize(float font_size){m_font_size = font_size;} float getFontSize(){return m_font_size;} }; diff --git a/src/test/main.cpp b/src/test/main.cpp index 53ad32a..3205927 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -62,7 +62,7 @@ int main(){ // utils::rotate2D(mnode, glm::vec2(10,10),0.5); // scene.addObject(scene.getRootObject(),mnode); - tnode = fonte_des_neiges->getTextNode("Such Text", glm::vec3(0.7, 0.4, 0.2),12.f); + tnode = fonte_des_neiges->getTextNode("Such Text", glm::vec3(0.7, 0.4, 0.2), 32.f); utils::setPosition2D((MeshNode*)tnode,glm::vec2(200, 170)); utils::rotate2D((MeshNode*)tnode, glm::vec2(0),-0.5); diff --git a/src/tools/font.cpp b/src/tools/font.cpp index eeb0af7..cef7ce9 100644 --- a/src/tools/font.cpp +++ b/src/tools/font.cpp @@ -12,27 +12,24 @@ TextNode* Font::getTextNode(std::string s, glm::vec3 color, float font_size) { Mesh* textmesh = new Mesh(); - glm::vec2 current_pos; + glm::vec2 current_pos(0.f); float sizeRatio = font_size / m_defaultLineHeight; for(char c : s){ if(c == '\n') { - current_pos.x = 0; // left alignment -> TODO : be able to center or align right + current_pos.x = 0.f; // left alignment -> TODO : be able to center or align right current_pos.y += m_defaultLineHeight; } else { CharInfo charInfo = m_charTable[c]; - - glm::vec2 font_offset = glm::vec2(charInfo.offset) * sizeRatio; - - textmesh->addRectangle2D((current_pos + font_offset)* sizeRatio, + textmesh->addRectangle2D((current_pos + charInfo.offset)* sizeRatio, charInfo.dim * sizeRatio, - charInfo.pos/m_scale, - charInfo.dim/m_scale + charInfo.pos * m_scale, + charInfo.dim * m_scale ); - current_pos.x += (charInfo.xadvance); + current_pos.x += charInfo.xadvance; } } PhongMaterial *mat = new PhongMaterial(); diff --git a/src/tools/font.h b/src/tools/font.h index a6190e5..a03e842 100644 --- a/src/tools/font.h +++ b/src/tools/font.h @@ -24,14 +24,14 @@ public: void setName(std::string name){m_name = name;} void addCharInfo(wchar_t character, CharInfo character_info){ m_charTable[character] = character_info; } void setNbChar(int nbchar){m_nbChar =nbchar;} - void setLineHeight(int line_h){m_defaultLineHeight = line_h;} - int getLineHeight(){return m_defaultLineHeight;} - void setBase(int base){m_base = base;} + void setLineHeight(float line_h){m_defaultLineHeight = line_h;} + float getLineHeight(){return m_defaultLineHeight;} + void setBase(float base){m_base = base;} void setAntiAnliasing(bool antialiasing){m_antiAliasing = antialiasing;} void setScale(glm::vec2 scale){m_scale = scale;} void setTexture(Texture *tex){m_tex = tex;} - TextNode* getTextNode(std::string s, glm::vec3 color = glm::vec3(1),float font_size = 0); + TextNode* getTextNode(std::string s, glm::vec3 color = glm::vec3(1), float font_size = 64.f); private: std::string m_name; Texture *m_tex; @@ -39,8 +39,8 @@ private: glm::vec2 m_scale; bool m_antiAliasing; int m_nbChar; - int m_defaultLineHeight; - int m_base; + float m_defaultLineHeight; + float m_base; }; #endif // FONT_H diff --git a/src/tools/loader.cpp b/src/tools/loader.cpp index 004cc15..c38b425 100644 --- a/src/tools/loader.cpp +++ b/src/tools/loader.cpp @@ -75,13 +75,13 @@ Font* Loader::loadFont(const std::string &description_file, const std::string &t font->setName(font_name); std::getline(file, line); - int line_h, base; - glm::ivec2 scale; - std::sscanf(line.c_str(),"common lineHeight=%d base=%d scaleW=%d scaleH=%d", + float line_h, base; + glm::vec2 scale; + std::sscanf(line.c_str(),"common lineHeight=%f base=%f scaleW=%f scaleH=%f", &line_h,&base,&(scale.x),&(scale.y)); font->setLineHeight(line_h); font->setBase(base); - font->setScale(glm::vec2(scale)); + font->setScale(1.f/scale); std::getline(file, line);//ignore 3rd line for now => only use 1 page. std::getline(file, line); @@ -92,7 +92,7 @@ Font* Loader::loadFont(const std::string &description_file, const std::string &t std::getline(file, line); Font::CharInfo char_info; int id; - sscanf(line.c_str(),"char id=%f x=%f y=%f width=%f height=%f xoffset=%f yoffset=%f xadvance=%f", + sscanf(line.c_str(),"char id=%d x=%f y=%f width=%f height=%f xoffset=%f yoffset=%f xadvance=%f", &id, &(char_info.pos.x),&(char_info.pos.y),&(char_info.dim.x), &(char_info.dim.y), &(char_info.offset.x), &(char_info.offset.y), &(char_info.xadvance)); font->addCharInfo(id,char_info); @@ -101,7 +101,7 @@ Font* Loader::loadFont(const std::string &description_file, const std::string &t Image* fucking_image_of_doom = loadImage(texture_file); if(fucking_image_of_doom == NULL) printf("can't load \"%s\".\n", texture_file.c_str()); - Texture* texture = new Texture(fucking_image_of_doom); + Texture* texture = new Texture(fucking_image_of_doom, false); // mipmaps are doing a very bad job at interpolating alpha component delete fucking_image_of_doom; font->setTexture(texture); return font;