c++ - How to embed font and other image files when using graphic libraries like SFML -




many of have wondered- how can rid of font or other single file drive when using library sfml, needs load font file path.

so, how embed data executable, resulting executable not depend on resource files anymore?

first of all, have our resource. have downloaded "baloobhaijaan-regular.ttf" font google fonts.
then, 1 should binary data of given font. easiest way achieve in opinion use linux "xxd" command -i parameter outputs in c-style array.
let's redirect output file because going long if talking true type fonts or larger images:

xxd -i baloobhaijaan-regular.ttf > font_data.txt 

create empty c/c++ header or put font data existing file. prefer using new header files output going long.
of course, after pasting ide can change array type const content of font doesn't change.
how looks in ide: enter image description here might of course wonder why char array - because in char array each "field" represents 1 byte.

as might have noticed, xxd creates variable - last variable in font_data.txt unsigned int informs length of array. need later. name of "length-informing" integer same name of array "_len" suffix

now, there 2 ways proceed:
1. load font memory using builtin method (some libraries support it, sfml does)
2. create "fake" file , load it
lets talk both cases


1.
1 simple, sfml supports loading file memory given it's address , size, can this:

#include "baloobhaijaanfont.hpp" #include <sfml/graphics.hpp>  int main(int argc, char** argv) {     sf::renderwindow mainwindow(sf::videomode(200,100), l"test");     sf::font frommem;     frommem.loadfrommemory(&baloobhaijaan_regular_ttf, baloobhaijaan_regular_ttf_len);     sf::text text("works!", frommem);     while(mainwindow.isopen()){         mainwindow.draw(text);         mainwindow.display();     }     return 0; } 

as can see, loading builtin function easy.



2.
it's time temporary file approach not recommend - libraries support loading memory , if making own library going end having memory load function anyway.
whilst still possible create file read font class , remove it, not see sense of using method unless extremely annoyed additional files in folders.
reference:

#include "baloobhaijaanfont.hpp" #include <sfml/graphics.hpp>  int main(int argc, char** argv) {     sf::renderwindow mainwindow(sf::videomode(200,100), l"test");     sf::font fromfile;     {         file * tempfile = fopen("tmpfont.ttf", "wb");         fwrite( baloobhaijaan_regular_ttf, sizeof(char), baloobhaijaan_regular_ttf_len, tempfile );         fclose(tempfile);         fromfile.loadfromfile("tmpfont.ttf");         std::remove("tmpfont.ttf");     }     sf::text text("works!", fromfile);     while(mainwindow.isopen()){         mainwindow.draw(text);         mainwindow.display();     }     return 0; } 




wiki

Comments

Popular posts from this blog

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -

Asterisk AGI Python Script to Dialplan does not work -