From 0e560c2eef8a2871c612f1feab230c353fb50f80 Mon Sep 17 00:00:00 2001 From: Anselme Date: Fri, 22 Jul 2016 13:06:15 +0200 Subject: [PATCH] fixed font xadvance, added color to text, updated test scene --- src/test/main.cpp | 16 ++++++++++++++-- src/tools/font.cpp | 34 +++++++++++++++++++++++----------- src/tools/font.h | 20 ++++++++++++-------- src/tools/loader.cpp | 13 ++++++------- src/tools/loader.h | 1 + 5 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/test/main.cpp b/src/test/main.cpp index 7f17909..90f87a2 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -50,8 +51,19 @@ int main(){ RESOURCE_ADD(fonte_des_neiges,Font,"shellfont"); - MeshNode* mnode = new MeshNode(fonte_des_neiges->getTextMesh("Hello World!")); - + MeshNode* mnode/* = new MeshNode(fonte_des_neiges->getTextMesh("Hello World!")); + scene.addObject(scene.getRootObject(),mnode)*/; + + mnode = new MeshNode(fonte_des_neiges->getTextMesh("WOW!", glm::vec3(0.5, 0.7, 0.2))); + mnode->setTransform(glm::rotate(glm::translate(glm::mat4(), glm::vec3(70, 30, 0)), 0.4f, glm::vec3(0, 0, 1))); + scene.addObject(scene.getRootObject(),mnode); + + mnode = new MeshNode(fonte_des_neiges->getTextMesh("Such Text!", glm::vec3(0.7, 0.4, 0.2))); + mnode->setTransform(glm::rotate(glm::translate(glm::mat4(), glm::vec3(200, 170, 0)), -0.5f, glm::vec3(0, 0, 1))); + scene.addObject(scene.getRootObject(),mnode); + + mnode = new MeshNode(fonte_des_neiges->getTextMesh("Very font!", glm::vec3(0.7, 0.2, 0.8))); + mnode->setTransform(glm::rotate(glm::translate(glm::mat4(), glm::vec3(260, 300, 0)), 0.1f, glm::vec3(0, 0, 1))); scene.addObject(scene.getRootObject(),mnode); engine.setScene(&scene); diff --git a/src/tools/font.cpp b/src/tools/font.cpp index b881041..35567d9 100644 --- a/src/tools/font.cpp +++ b/src/tools/font.cpp @@ -1,24 +1,36 @@ #include "font.h" -#include +#include Font::Font() { } -Mesh* Font::getTextMesh(std::string s) +Mesh* Font::getTextMesh(std::string s, glm::vec3 color) { Mesh* text = new Mesh(); - glm::vec2 current_pos(50,50); - for(auto carac : s){ - CharInfo chinf = m_charTable[carac]; - current_pos += chinf.offset; - text->addRectangle2D(current_pos,chinf.dim,chinf.pos,chinf.dim); - current_pos -= chinf.offset; - current_pos.x += (chinf.dim.x + chinf.offset.x); + glm::ivec2 current_pos; + for(char c : s){ + if(c == '\n') + { + current_pos.x = 0; // left alignment -> TODO : be able to center or align right + current_pos.y += m_lineHeight; + } + else + { + CharInfo charInfo = m_charTable[c]; + text->addRectangle2D(current_pos + charInfo.offset, + charInfo.dim, + glm::vec2(charInfo.pos)/m_scale, + glm::vec2(charInfo.dim)/m_scale); + current_pos.x += charInfo.xadvance; + } } - text->setMaterial((Material*)m_mat); - text->setDepth(0.5); + PhongMaterial *mat = new PhongMaterial(); + // TODO : delete this material somewhere (garbage collector ?) + mat->setTexture(PhongMaterial::ALPHA_SLOT, m_tex, "font_texture"); + mat->diffuse = color; + text->setMaterial((Material*)mat); text->initGL(); return text; } diff --git a/src/tools/font.h b/src/tools/font.h index 1ea842c..f81449f 100644 --- a/src/tools/font.h +++ b/src/tools/font.h @@ -4,9 +4,8 @@ #include #include #include "mesh.h" -//#include "scenetree.h" -class PhongMaterial; +class Texture; class Font { @@ -19,22 +18,27 @@ public: int xadvance; }; Font(); + + // setters void setName(std::string name){m_name = name;} - void addCharInfo(wchar_t character,CharInfo character_info){ m_charTable[character] = character_info; } + 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_lineHeight = line_h;} void setBase(int base){m_base = base;} void setAntiAnliasing(bool antialiasing){m_antiAliasing = antialiasing;} void setScale(glm::vec2 scale){m_scale = scale;} - void setMaterial(PhongMaterial *mat){m_mat = mat;} - Mesh* getTextMesh(std::string s); + void setTexture(Texture *tex){m_tex = tex;} + + Mesh* getTextMesh(std::string s, glm::vec3 color = glm::vec3(1)); private: std::string m_name; - PhongMaterial *m_mat; - std::map m_charTable; + Texture *m_tex; + std::map m_charTable; glm::vec2 m_scale; bool m_antiAliasing; - int m_nbChar, m_lineHeight,m_base; + int m_nbChar; + int m_lineHeight; + int m_base; }; #endif // FONT_H diff --git a/src/tools/loader.cpp b/src/tools/loader.cpp index 9409421..5348e35 100644 --- a/src/tools/loader.cpp +++ b/src/tools/loader.cpp @@ -34,7 +34,9 @@ std::string* Loader::loadTextFile(const std::string &filename) Image* Loader::loadImage(const std::string &filename, bool hasAlpha) { sf::Image sfImg; - sfImg.loadFromFile(tex_directory+filename); + bool ok = sfImg.loadFromFile(tex_directory+filename); + if(!ok) + return NULL; Image* img = new Image(); img->depth = hasAlpha ? 32 : 24; img->width = sfImg.getSize().x; @@ -74,12 +76,12 @@ Font* Loader::loadFont(const std::string &description_file, const std::string &t std::getline(file, line); int line_h, base; - glm::vec2 scale; + glm::ivec2 scale; std::sscanf(line.c_str(),"common lineHeight=%d base=%d scaleW=%d scaleH=%d", &line_h,&base,&(scale.x),&(scale.y)); font->setLineHeight(line_h); font->setBase(base); - font->setScale(scale); + font->setScale(glm::vec2(scale)); std::getline(file, line);//ignore 3rd line for now => only use 1 page. std::getline(file, line); @@ -99,10 +101,7 @@ Font* Loader::loadFont(const std::string &description_file, const std::string &t Image* fucking_image_of_doom = loadImage(texture_file); Texture* texture = new Texture(fucking_image_of_doom); delete fucking_image_of_doom; - PhongMaterial* mat = new PhongMaterial(); - mat->setTexture(PhongMaterial::DIFFUSE_SLOT,texture,font_name); -// mat->diffuse = glm::vec3(1,1,1); - font->setMaterial(mat); + font->setTexture(texture); return font; } diff --git a/src/tools/loader.h b/src/tools/loader.h index 34401c6..04b0523 100644 --- a/src/tools/loader.h +++ b/src/tools/loader.h @@ -3,6 +3,7 @@ #include #include +#include class Image; class Mesh;