SDL 2 Blitting TTF_RenderText_Solid onto surface

Hello and thanks to all who will read this post, it is my first so I’ll try to explain well.
So, I am stuck on a pretty simle problem but haven’t found the right answer yet: I create a surface with TTF_RenderText_Solid, and then try to Blit it onto a monotone surface, then convert the resulting surface to a Texture, and rendering to screen. Only, the text doesn’t show, on my screen i get the “box”, but not the text that should have been blitted on it… Help! (checked errors and i do not get any)

Code:

SDL_Color paint={0,0,0};
SDL_Surface *Text;
Text=TTF_RenderText_Solid(Font,“HELLO”, paint);

SDL_Surface *Box=SDL_CreateRGBSurface(0,60,30,32,0,0,0,0);
Uint32 color=SDL_MapRGB(Box->format,200,200,200);
SDL_FillRect(Box,NULL,color);
SDL_Rect Rect={0-0-0-0};

SDL_BlitSurface(Text, NULL, Box,NULL );

SDL_Texture *Texture;
Texture=SDL_CreateTextureFromSurface(Renderer,Box);

SDL_RenderCopy(Renderer,Texture,NULL,&Rect);

Are you sure with this line:

Code:
SDL_Rect Rect={0-0-0-0};

Try to use NULL as dstrect:

Code:
SDL_RenderCopy(Renderer,Texture,NULL, NULL);

Yes i did, and didn’t get anything better!
The scales do not matter at the point, it just fills the whole screen, but no text appears either…
Thanks for the quick reply though :smiley:

Tried again, and again, but to no avail!
I modified the rectangle definition, i think it was c++ type definitions? And also made the destination surface of the blit an RGBA surface…

Code:
SDL_Color paint={0,0,0};
SDL_Surface *Text;
Text=TTF_RenderText_Solid(Font,“HELLO”, paint);

SDL_Surface *Box=SDL_CreateRGBSurface(0,60,30,32,0,0,0,0);
Uint32 color=SDL_MapRGBA(Box->format,0,200,0,255);
ErrorsNegativeNumber(SDL_FillRect(Box,NULL,color));
SDL_Rect Rect={50,50,60,30};

SDL_BlitSurface(Text, NULL, Box,NULL );

SDL_Texture *Texture;
ErrorsNull(Texture=SDL_CreateTextureFromSurface(Renderer,Box));

ErrorsNegativeNumber(SDL_RenderCopy(Renderer,Texture,NULL,&Rect));

SDL_RenderPresent(Renderer);

Have you tried changing the first line to a colour other than black?
Can I ask why you blit to another surface and the a texture? Rather than just converting straight to a texture?

Also, you should set your colours with 4 parameters, otherwise you could be drawing with zero alpha. E.g paint = {255,255,255,255} for white or paint={0,0,0,255} for black.

Just in case, did you enable/set the blend mode (https://wiki.libsdl.org/SDL_SetRenderDrawBlendMode) before the RenderCopy call ?

lorin wrote:

Are you sure with this line:

Code:
SDL_Rect Rect={0-0-0-0};

Looks like C++11’s initialized list to me. Although effectively it’s similar to :-

Code:
SDL_Rect Rect={0};

As 0-0-0-0 = 0. The others member are left uninitialized, or undefined.

mr_tawan wrote:

Just in case, did you enable/set the blend mode (https://wiki.libsdl.org/SDL_SetRenderDrawBlendMode) before the RenderCopy call ?

Guess I misread the thread.

Anyway have you tried setting all 4 members of paint ? Something like

Code:
SDL_Color paint={0,0,0,255};

The last one is alpha(translucency) byte of the color. I think it’s ordered as rgba. Just in case I’m wrong, be set it to white

Code:
SDL_Color paint={255,255,255,255};

As the order is now not relevant (everything is 255).

Ok, i checked all the things you pointed out, the rect values, the alpha channel of the color struct, and the color codes, but the text still does not show. I’d like to do this so it will be easier to only handle one texture after the blit, but for now what i will do is make two textures and render them on top of one another.
So, the text doesn’t show after the blit, and if i instead blit the monotone surface on top of the text surface, then make a texture from that and render it, it shows the text, with a weird small square on the top right of it… It would be really nice to have just one texture to render around, than two!
Thank you!

I don’t see why would you need 2 texture for that.

Anyway I had spent an hour creating the test, and looks like it could be archive with only 1 texture. The complete code is below. It’s a bit long, btw.

Code:

#include “SDL.h”
#include “SDL_ttf.h”

int main(int argc, char** args)
{
SDL_Color color{0xFF, 0xFF, 0xFF, 0xFF};

SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *pWindow;
SDL_Renderer *pRenderer;

SDL_CreateWindowAndRenderer(800,
600,
SDL_WINDOW_OPENGL,
&pWindow,
&pRenderer);

SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 0);

TTF_Init();
TTF_Font *pFont = TTF_OpenFont("./TS-Kaewpet-NP/TS-kaewpet Bold-NP.ttf", 64);
SDL_Surface *pSurface = TTF_RenderText_Solid(pFont, “This is a Test Text”, color);
SDL_Texture *pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface);

while(true)
{
SDL_RenderClear(pRenderer);
SDL_Event event;
while (SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT) goto quit;
}

SDL_RenderCopy(pRenderer, pTexture, NULL, NULL);

SDL_RenderPresent(pRenderer);
SDL_Delay(33);

}

quit:
TTF_Quit();
SDL_DestroyRenderer(pRenderer);
SDL_DestroyWindow(pWindow);

SDL_Quit();

return 0;
}

Tested on Arch Linux 64 bit, GNOME 3.12, SDL2_2.0.3 and SDL2_ttf_2.0.12. What is your platform, btw?

One more thing, I’m working on a text rendering lib using FreeType2 and Harfbuzz. Glyph is rendered directly, no surface or texture (though it could be rendered to texture using render target). I’ll let people here know when it’s matured enough.

Thank you mr_tawan, but my problem is with blitting a text surface on top of another surface. When I do this, it is like the text surface disappears during the blit. More specifically, the test surface isn’t a picture, just a unicolor surface colored in with SDL_FillRect.
Thank you again!

Windows 8 64bit codeblocks mingw32

If you’re blitting RGBA SDL_Surfaces onto each other, then you should set
the surface blend mode. Most of SDL’s blend modes will preserve the
destination alpha (which is 0 for new RGBA surfaces, so you don’t see
anything at all). Specifically, try SDL_SetSurfaceBlendMode(surface,
SDL_BLENDMODE_NONE):
https://wiki.libsdl.org/SDL_SetSurfaceBlendMode

Jonny DOn Fri, May 23, 2014 at 1:14 AM, YouCOde wrote:

Thank you mr_tawan, but my problem is with blitting a text surface on
top of another surface. When I do this, it is like the text surface
disappears during the blit. More specifically, the test surface isn’t a
picture, just a unicolor surface colored in with SDL_FillRect.
Thank you again!

Windows 8 64bit codeblocks mingw32


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org