I'm currently hardcoding my images into my C++ program as a struct (the source code with the pixel data can be created with GIMP), like so:

static const struct {
  unsigned int   width;
  unsigned int   height;
  unsigned int   bytes_per_pixel;
  unsigned char  pixel_data[16 * 16 * 4 + 1];
} my_icon = {
  16, 16, 4,
  "...", pixel data here
};

This is working fine and I'm very happy! But now I want to do the same for the font file I'm using.

Since GIMP did the conversion for me but obviously can't convert the font for me, I'm stuck now and don't know how I would go about hardcoding the font.ttf like I do with the images.

If it is of any relevance, this is how I currently load the font file:

sf::Font font;
if (!font.loadFromFile("font.ttf")) {
    std::cout << "Could not load font" << std::endl;
    return -1;
}

Could someone please help me? (The font is less than 25 KB in size so no problem there)

Thanks!!

有帮助吗?

解决方案

It seem Font has a fine loadFromMemory method. So you can use it exactly as your previous example, presenting the data in a string literal and pass it over.

It's not hard to write a program that converts the binary file in \xHH hexdump-like format suitable for inclusion.

其他提示

All right, this is my solution:

I used the tool Bin2h (http://www.deadnode.org/sw/bin2h/) to create the following code:

unsigned int fontSize = 22256;
unsigned char fontChar[] = {
0x00,0x01,0x00,...
};

Then I use

font.loadFromMemory(fontChar, fontSize)

to load the font.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top