From 77bfe51607d4e52ed062b32325be4631a259e393 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Thu, 14 Jul 2016 23:26:27 +0200 Subject: [PATCH] added parsing of fnt file for loading font --- src/main.cpp | 26 ++++++++++++---- src/scenetree.cpp | 3 +- src/tools/font.cpp | 6 ++++ src/tools/font.h | 37 +++++++++++++++++++++++ src/tools/loader.cpp | 70 +++++++++++++++++++++++++++++++++++--------- src/tools/loader.h | 3 ++ src/tools/utils.cpp | 12 ++++++++ src/tools/utils.h | 12 ++++++++ 8 files changed, 148 insertions(+), 21 deletions(-) create mode 100644 src/tools/font.cpp create mode 100644 src/tools/font.h create mode 100644 src/tools/utils.cpp create mode 100644 src/tools/utils.h diff --git a/src/main.cpp b/src/main.cpp index bdf5c9f..42941de 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,8 @@ #include "tools/pathfinder.h" #include "tools/loader.h" +#include "tools/font.h" + #include "sparrowshell.h" int main(){ @@ -22,20 +24,30 @@ int main(){ engine.createWindow("test"); SceneTree scene; + /* Exemple creation mesh 2D + * + Mesh* mesh = new Mesh(); mesh->addRectangle2D(50,50,100,100); PhongMaterial *mat = new PhongMaterial(); mat->diffuse = glm::vec3(1, 0.5, 0); mesh->setMaterial(mat); mesh->initGL(); +*/ -// SparrowShell *shell = new SparrowShell(engine.getWindow(),engine.getInput()); -// scene.addObject(scene.getRootObject(),shell); +/* Exemple ajout d'un objet a la scene + * + SparrowShell *shell = new SparrowShell(engine.getWindow(),engine.getInput()); + scene.addObject(scene.getRootObject(),shell); +*/ // the pipeline needs to updates his shaders because the scene changed // this should be handled somewhere else in the future - DeferredPipeline* pipeline = (DeferredPipeline*)scene.getPipeline(); - pipeline->refreshScene(&scene); + //DeferredPipeline* pipeline = (DeferredPipeline*)scene.getPipeline(); + //pipeline->refreshScene(&scene); + + Font* fonte_des_neiges = Loader::loadFont("../data/consolas.fnt","../data/consolas.png"); + RESOURCE_ADD(fonte_des_neiges,Font,"shellfont"); engine.setScene(&scene); engine.start(); @@ -67,5 +79,9 @@ int main(){ Loader::setObjDirectory("../data/"); Loader::setMtlDirectory("../data/"); Loader::setTexDirectory("../data/"); - std::vector meshes = Loader::loadMesh("sword.obj");*/ + std::vector meshes = Loader::loadMesh("sword.obj"); +*/ + + + } diff --git a/src/scenetree.cpp b/src/scenetree.cpp index 08038eb..d822a27 100644 --- a/src/scenetree.cpp +++ b/src/scenetree.cpp @@ -45,8 +45,7 @@ void SceneTree::addObject(ContainerNode *parent, SceneNode *node) parent->addChild(node); addToIndex(node); node->addedToSceneTree(this); - GuiPipeline* pipeline = (GuiPipeline*)this->getPipeline(); - pipeline->refreshScene(this); + ((DeferredPipeline*) m_pipeline)->refreshScene(this); } void SceneTree::addToIndex(SceneNode* node){ diff --git a/src/tools/font.cpp b/src/tools/font.cpp new file mode 100644 index 0000000..e0573c9 --- /dev/null +++ b/src/tools/font.cpp @@ -0,0 +1,6 @@ +#include "font.h" + +Font::Font() +{ + +} diff --git a/src/tools/font.h b/src/tools/font.h new file mode 100644 index 0000000..57f1184 --- /dev/null +++ b/src/tools/font.h @@ -0,0 +1,37 @@ +#ifndef FONT_H +#define FONT_H + +#include +#include + +class PhongMaterial; + +class Font +{ +public: + struct CharInfo + { + glm::vec2 pos; + glm::vec2 dim; + glm::vec2 offset; + int xadvance; + }; + Font(); + 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_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;} +private: + std::string m_name; + PhongMaterial* m_mat; + std::map m_charTable; + glm::vec2 m_scale; + bool m_antiAliasing; + int m_nbChar, m_lineHeight,m_base; +}; + +#endif // FONT_H diff --git a/src/tools/loader.cpp b/src/tools/loader.cpp index 6ce7fd0..a0d5a94 100644 --- a/src/tools/loader.cpp +++ b/src/tools/loader.cpp @@ -8,6 +8,8 @@ #include #include #include "texture.h" +#include "utils.h" +#include "font.h" #include @@ -51,6 +53,59 @@ Image* Loader::loadImage(const std::string &filename, bool hasAlpha) return img; } +Font* Loader::loadFont(const std::string &description_file, const std::string &texture_file) +{ + std::string line; + std::ifstream file(description_file); + + if(!file) + { + fprintf(stderr, "can't load '%s'.\n", description_file.c_str()); + std::cerr << "Error code: " << strerror(errno) << std::endl; + return nullptr; + } + + Font* font = new Font(); + std::getline(file, line); + char font_n[256]; + std::sscanf(line.c_str(),"info face=\"%[^\"]",font_n); + std::string font_name(font_n); + font->setName(font_name); + + std::getline(file, line); + int line_h, base; + glm::vec2 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); + + std::getline(file, line);//ignore 3rd line for now => only use 1 page. + std::getline(file, line); + int nbchar; + std::sscanf(line.c_str(),"chars count=%d",&nbchar); + font->setNbChar(nbchar); + while(!file.eof()){ + std::getline(file, line); + Font::CharInfo char_info; + int id; + sscanf(line.c_str(),"char id=%d x=%d y=%d width=%d height=%d xoffset=%d yoffset=%d xadvance=%d", + &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); + } + + std::cout << "Parsing finished" << std::endl; + 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); + font->setMaterial(mat); + return font; +} + std::vector Loader::loadMesh(const std::string &filename){ std::vector meshes; @@ -194,19 +249,6 @@ std::vector Loader::loadMesh(const std::string &filename){ return meshes; } - -//move this to an utils file ? -std::vector split(const std::string &line, char sep){ - std::vector tokens; - std::size_t start=0, end=0; - while((end = line.find(sep,start)) != std::string::npos){ - tokens.push_back(line.substr(start,end-start)); - start=end+1; - } - tokens.push_back(line.substr(start)); - return tokens; -} - //load MTL bool Loader::loadMTL(const std::string &filename) { @@ -232,7 +274,7 @@ bool Loader::loadMTL(const std::string &filename) continue; } //QStringList tokens = line.split(' '); - std::vector tokens = split(line,' '); + std::vector tokens = utils::split(line,' '); if(tokens[0].substr(0,1) == "#") { diff --git a/src/tools/loader.h b/src/tools/loader.h index 7214602..34401c6 100644 --- a/src/tools/loader.h +++ b/src/tools/loader.h @@ -6,6 +6,7 @@ class Image; class Mesh; +class Font; class Loader { @@ -17,8 +18,10 @@ public: static std::string* loadTextFile(const std::string &filename); static Image* loadImage(const std::string &filename, bool hasAlpha = true); static std::vector loadMesh(const std::string &filename); + static Font* loadFont(const std::string &texture, const std::string &description); static bool loadMTL(const std::string &filename); + static void setObjDirectory(std::string); static void setMtlDirectory(std::string); static void setTexDirectory(std::string); diff --git a/src/tools/utils.cpp b/src/tools/utils.cpp new file mode 100644 index 0000000..d777a7f --- /dev/null +++ b/src/tools/utils.cpp @@ -0,0 +1,12 @@ +#include "utils.h" + +std::vector utils::split(const std::string &line, char sep){ + std::vector tokens; + std::size_t start=0, end=0; + while((end = line.find(sep,start)) != std::string::npos){ + tokens.push_back(line.substr(start,end-start)); + start=end+1; + } + tokens.push_back(line.substr(start)); + return tokens; +} diff --git a/src/tools/utils.h b/src/tools/utils.h new file mode 100644 index 0000000..7a2454c --- /dev/null +++ b/src/tools/utils.h @@ -0,0 +1,12 @@ +#ifndef UTILS_H +#define UTILS_H + +#include +#include + +namespace utils +{ +std::vector split(const std::string &line, char sep); +} + +#endif // UTILS_H