0

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...

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142

3 Answers3

3

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);
Srđan Tot
  • 235
  • 2
  • 7
  • I'm not really sure what are you trying to accomplish, but you can try this code instead: `ch = 66; SDL_Surface * car = TTF_RenderGlyph_Blended(font,ch,color); SDL_Surface * glyph = SDL_ConvertSurface(car, car->format, car->flags); SDL_Rect textRect = {0, 0, car->w, car->h}; qDebug() << SDL_BlitSurface(glyph, NULL, screen, &textRect);` – Srđan Tot Oct 15 '13 at 12:08
  • I want to create a home-made glyph (like this one : http://fc04.deviantart.net/images/i/2003/7/5/2/Times_New_dirty_Roman.jpg) from a given font. I start with one char to test the routine. Even if your snippet work I'm not sure it can handle this. – Thomas Ayoub Oct 15 '13 at 12:14
  • So, basically, you want to blit several glyphs onto one big surface? – Srđan Tot Oct 15 '13 at 12:19
1

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

MahanGM
  • 2,352
  • 5
  • 32
  • 45
  • Well, the only possibility left is to check your `textRect`. Try pass `NULL` since you're not doing anything different than setting on zero coordinates. – MahanGM Oct 15 '13 at 12:00
  • Have you tried to drawing images instead of texts? If this is not working well it means the problem is hid in other places. – MahanGM Oct 15 '13 at 12:04
  • Well `glyph` blit on the `screen` surface and an SDL_Image blit correctly too. I just can't blit `car` on `glyph` – Thomas Ayoub Oct 15 '13 at 12:06
  • SDL's wiki page says that surfaces with different blendings and colorkeys are blitted upon their formats. http://wiki.libsdl.org/SDL_BlitSurface#Remarks check for formats part. Maybe there is a blending problem with your surfaces. – MahanGM Oct 15 '13 at 12:16
  • You're half right, it works a bit with Solid instead of Blended... But steel unusable – Thomas Ayoub Oct 15 '13 at 12:25
  • I haven't worked with SDL2 yet, but my guess is, `TTF_RenderGlyph_Blended()` is generating a different surface format than what you think. – MahanGM Oct 15 '13 at 12:27
  • Yep. Well played. I'm gonna check this – Thomas Ayoub Oct 15 '13 at 12:31
0

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);
Community
  • 1
  • 1
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142