added parsing of fnt file for loading font
This commit is contained in:
parent
98646d264d
commit
77bfe51607
26
src/main.cpp
26
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<Mesh*> meshes = Loader::loadMesh("sword.obj");*/
|
||||
std::vector<Mesh*> meshes = Loader::loadMesh("sword.obj");
|
||||
*/
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -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){
|
||||
|
6
src/tools/font.cpp
Normal file
6
src/tools/font.cpp
Normal file
@ -0,0 +1,6 @@
|
||||
#include "font.h"
|
||||
|
||||
Font::Font()
|
||||
{
|
||||
|
||||
}
|
37
src/tools/font.h
Normal file
37
src/tools/font.h
Normal file
@ -0,0 +1,37 @@
|
||||
#ifndef FONT_H
|
||||
#define FONT_H
|
||||
|
||||
#include <map>
|
||||
#include <glm/vec2.hpp>
|
||||
|
||||
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<wchar_t,CharInfo> m_charTable;
|
||||
glm::vec2 m_scale;
|
||||
bool m_antiAliasing;
|
||||
int m_nbChar, m_lineHeight,m_base;
|
||||
};
|
||||
|
||||
#endif // FONT_H
|
@ -8,6 +8,8 @@
|
||||
#include <mesh.h>
|
||||
#include <phongmaterial.h>
|
||||
#include "texture.h"
|
||||
#include "utils.h"
|
||||
#include "font.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
@ -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<Mesh*> Loader::loadMesh(const std::string &filename){
|
||||
std::vector<Mesh*> meshes;
|
||||
|
||||
@ -194,19 +249,6 @@ std::vector<Mesh*> Loader::loadMesh(const std::string &filename){
|
||||
return meshes;
|
||||
}
|
||||
|
||||
|
||||
//move this to an utils file ?
|
||||
std::vector<std::string> split(const std::string &line, char sep){
|
||||
std::vector<std::string> 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<std::string> tokens = split(line,' ');
|
||||
std::vector<std::string> tokens = utils::split(line,' ');
|
||||
|
||||
if(tokens[0].substr(0,1) == "#")
|
||||
{
|
||||
|
@ -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<Mesh*> 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);
|
||||
|
12
src/tools/utils.cpp
Normal file
12
src/tools/utils.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
#include "utils.h"
|
||||
|
||||
std::vector<std::string> utils::split(const std::string &line, char sep){
|
||||
std::vector<std::string> 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;
|
||||
}
|
12
src/tools/utils.h
Normal file
12
src/tools/utils.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace utils
|
||||
{
|
||||
std::vector<std::string> split(const std::string &line, char sep);
|
||||
}
|
||||
|
||||
#endif // UTILS_H
|
Loading…
x
Reference in New Issue
Block a user