• Advertisement
Sign in to follow this  

TTF tells me that I have 0 width...

This topic is 4295 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

    Text has zero width
This is stdout.txt... I am using ttf to take a .ttf file and then make a bitmap font with it for use with my bitmap-font engine. The only bad part is that it fails with the above message... This is the code:
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>

#include <string>
#include <fstream>
#include <iostream>
using namespace std;

void init() {

void deinit() {

int main(int argc, char *argv[]) {

    ifstream f("data.txt");
    string filename;
    int size, fr, fg, fb, br, bg, bb;
    f >> filename >> size >> fr >> fg >> fb >> br >> bg >> bb;

    TTF_Font *font = TTF_OpenFont((filename + ".ttf").c_str(), size);
    if (!font) {
        cout << TTF_GetError();
        return 1;

    char ascii[256];
    for (unsigned i = 0; i < 256; i++)
        ascii = (char)i;

    SDL_Color foreground = {fr, fg, fb};
    SDL_Color background = {br, bg, bb};

    SDL_Surface *str = TTF_RenderText_Shaded(

    if (!str) {
        cout << TTF_GetError();
        return 1;

    SDL_SaveBMP(str, (filename + ".bmp").c_str());


Any ideas? I just hacked this together very quickly, but it should work... And yes, data.txt exists as follows:
    times 16 255 255 255 0 0 0
And again yes, times.ttf is in there too. THANKS!

Share this post

Link to post
Share on other sites
The C string you passed to TTF_RenderText_Shaded() has zero width.

This is because you set the very first character in the string to zero.
for (unsigned i = 0; i < 256; i++)
ascii = (char)i;

When i =0, then
ascii[0] = (char)0;//This line will terminate the C -string,resulting in zero width.

Try this as a test.
char ascii[27];//space for 26 letters and the string terminator(0).
//Print out the lower case alphabet
for (unsigned i = 0; i < 26; i++)
ascii = (char)i+'a'; //string consisting of letters 'a' through 'z'

Make sure to NULL terminate C strings
ascii[26] = 0;//goes after the above example

For your purposes you can change the NULL terminate to a space
ascii = (char)(i==0)?' ':i;
or choose a range (SFont uses ascii 33-127)
for(int i =0; i < 94;i++)
ascii = (char)(33+i);
ascii[94] = 0;
Good Luck.

[Edited by - Jack Sotac on June 14, 2006 4:53:17 PM]

Share this post

Link to post
Share on other sites
Oops, thanks! I hadn't use character arrays for so long now that I forgot '\0' terminates the string. Again, thanks (rat++).

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement