Search code examples
c++blitsdl-2

SDL_Surface refuse to blit


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


Solution

  • 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);