Question

I'm trying to make a very little and simple snippet with SDL. This one works like a charm :

SDL_Window * window = SDL_CreateWindow("SDLTest", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_SWSURFACE);
screen = SDL_GetWindowSurface(window);
SDL_Color color={0,0,0};
TTF_GlyphMetrics(font, ch, &minx, &maxx, &miny, &maxy, NULL);
SDL_Surface * car =TTF_RenderGlyph_Blended(font,ch,color);
SDL_Rect textRect = {offsetX, offsetY, 0, 0};
if(SDL_BlitSurface( car, NULL, glyph, &screen ))
qDebug() << SDL_GetError();

and this one doesn't work at all :

SDL_Surface * glyph = NULL;
SDL_Surface * car = TTF_RenderGlyph_Blended(font,ch,color);
qDebug() << TTF_GetError();
SDL_Rect textRect = {0, 0, car->w, car->h};
if(SDL_BlitSurface( car, NULL, glyph, &textRect ))
qDebug() << SDL_GetError();

TTF_GetError() return nothing so I assume TTF_RenderGlyph_Blended works well and SDL_GetError() send me this :

SDL_UpperBlit: passed a NULL surface

::::::::::::::::: EDIT ::::::::::::::::::

Ok, I've fix the NULL problem, but the blit is not good yet:

ch = 66;
SDL_Surface * glyph = TTF_RenderUTF8_Blended(font, "Z", color);
SDL_UnlockSurface(glyph);
SDL_Surface * car = TTF_RenderGlyph_Blended(font,ch,color);
SDL_Rect textRect = {0, 0, car->w, car->h};
qDebug() << SDL_BlitSurface(car, NULL, glyph, &textRect);
qDebug() << SDL_BlitSurface(glyph, NULL, screen, &textRect);

Should display B but go Z instead...

Was it helpful?

Solution 3

As MahamGM said, there was a format issue which is solved now :

Uint32 rmask, gmask, bmask, amask;
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;


ch = 65;
SDL_Surface * glyph = SDL_CreateRGBSurface(0,screen->w,screen->h,32,rmask,gmask,bmask,amask);
SDL_Surface * car = TTF_RenderGlyph_Blended(font,ch,color);

SDL_Rect glyphRect = {0, 0, 100, 100};
SDL_Rect carRect = {100, 0, 300, 300};

PHDEBUG << SDL_BlitSurface(car, NULL, glyph, &glyphRect);
PHDEBUG << SDL_BlitSurface(glyph, NULL, screen, &glyphRect);

OTHER TIPS

SDL_BlitSurface requires source surface (your car variable) and destination surface (your glyph variable). Your first snippet doesn't show how and where is glyph created, but your second snippet explicitly sets glyph to NULL.

You should assign created surface to glyph before using it in SDL_BlitSurface function.

Edit: For rendering glyphs on surface, first create new surface, fill it with background color, and then blit glyph on it. You can use rectangle to define blit position if you want:

SDL_Surface * glyph = SDL_CreateRGBSurface(0, 100, 100, 32, 0, 0, 0, 0);
SDL_FillRect(glyph, NULL, SDL_MapRGB(glyph->format, 255, 255, 255);

ch = 66;
SDL_Surface * car = TTF_RenderGlyph_Blended(font, ch, color);

qDebug() << SDL_BlitSurface(car, NULL, glyph, NULL);
qDebug() << SDL_BlitSurface(glyph, NULL, screen, NULL);

Manual says you shouldn't call for locked surfaces when using SDL_BlitSurface(). Try to SDL_UnlockSurface() before call SDL_BlitSurface() for your surfaces. And for more information check what is the returned value of SDL_BlitSurface(). Before that you have to check for source surface to see whether it's filled or not, and try to use SDL_FillRect() on destination surface before blitting and see what happens.

Although, check for correct surface format:

http://wiki.libsdl.org/SDL_BlitSurface#Remarks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top