HelpMe000
HelpMe000

Reputation: 98

SDL2 - Adding a surface to a surface, texture to texture or something in between

Basically, I want to have one texture which contains both text and an image.

How would I go about doing that? I've been Google-ing around, but I can't seem to find a way to combine surfaces with other surfaces, textures with other textures, nor surfaces with textures.

EDIT: So, I'm making a simple RPG, and I want to have it so that when you talk to an NPC, that NPC either can or can't have an image attached to the text.

I would ideally do this by sending the text and image to a function which will gererate a texture I can just render, instead of having to worry about rendering two different ones, and where they should be positioned.

Something like this:

void setSayingText(std::string const& text, std::string const& imageLoc = "") {
   SDL_Surface* text = TTF_RenderText_Blended(font, text.c_str(), whiteColor);
   if (imageLoc != "") {
      SDL_Surface* image = IMG_Load(imageLoc);
      texture = SDL_CreateTextureFromSurface(renderer, text + image);
   } else {
      texture = SDL_CreateTextureFromSurface(renderer, text);
   }
}

Upvotes: 0

Views: 4285

Answers (1)

joeforker
joeforker

Reputation: 41787

It should be trivial to get your text and image as surfaces and then call https://wiki.libsdl.org/SDL_BlitSurface to copy one surface onto the other surface. Afterwards you would probably want to load the combined image into a texture to use with the render API.

It's also possible to use the Render API to render to a texture using https://wiki.libsdl.org/SDL_SetRenderTarget, but if I were you I'd stick to the simpler Surface API for one-off compositing.

Upvotes: 1

Related Questions