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

4

3 回答 3

3

SDL_BlitSurface需要源表面(您的汽车变量)和目标表面(您的字形变量)。您的第一个片段没有显示字形的创建方式和位置,但您的第二个片段明确将字形设置为NULL

在 SDL_BlitSurface 函数中使用之前,您应该将创建的表面分配给字形。

编辑:为了在表面上渲染字形,首先创建新表面,用背景颜色填充它,然后在其上使用 blit 字形。如果需要,您可以使用矩形来定义 blit 位置:

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);
于 2013-10-15T09:58:59.690 回答
1

手册说你不应该在使用SDL_BlitSurface(). SDL_UnlockSurface()在调用SDL_BlitSurface()您的表面之前尝试。有关更多信息,请检查 . 的返回值是什么SDL_BlitSurface()。在此之前,您必须检查源表面以查看它是否已填充,并尝试SDL_FillRect()在目标表面上使用,然后再进行 blitting 并查看会发生什么。

虽然,检查正确的表面格式:

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

于 2013-10-15T09:55:24.650 回答
0

正如MahamGM所说,现在解决了一个格式问题:

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);
于 2013-10-15T13:14:20.870 回答