A surface exists. How to display it?

See the code below. I’ve tried many things (not shown) to get the surface “textSurface” to display but I haven’t done it yet. I believe it must be simple. Will somebody show me how.

TIA. Bill S.

Code:

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>

int main( int argc, char* args[] ){

SDL_Init(SDL_INIT_VIDEO);

SDL_Window *win1 = NULL;
win1 = SDL_CreateWindow("", 0, 0, 640, 480, SDL_WINDOW_SHOWN);

SDL_Renderer* ren1 = SDL_CreateRenderer(win1, -1, 0);
SDL_SetRenderDrawColor(ren1, 255, 255, 255, 255);
SDL_RenderClear(ren1); // fill the screen with white

TTF_Init();
TTF_Font* font = TTF_OpenFont("/usr/share/fonts/truetype/msttcorefonts/arial.ttf", 12);
SDL_Color foregroundColor = { 0, 0, 0 };
SDL_Color backgroundColor = { 0, 0, 255 };
SDL_Surface* textSurface = TTF_RenderText_Shaded(font, “This is my text.”, foregroundColor, backgroundColor);
if (textSurface == NULL)
printf("\n\n failed to create surface \n\n");

SDL_Texture *tex1 = NULL;
tex1 = SDL_CreateTextureFromSurface(ren1,textSurface);

SDL_UpdateWindowSurface(win1);
SDL_Delay(5000);

SDL_RenderPresent(ren1);
SDL_Delay(5000);

SDL_FreeSurface(textSurface);
TTF_CloseFont(font);
TTF_Quit();
SDL_DestroyWindow(win1);
SDL_DestroyRenderer(ren1);
SDL_Quit();
return 0;
}

[/code]

You can copy image from your texture to window with SDL_RenderCopy function.
For example:
Code:
SDL_RenderCopy(ren1, tex1, NULL, NULL);
SDL_RenderPresent(ren1);

Window is default rendering target, if you do not changed this target with SDL_SetRenderTarget.

I don’t see where you are copying the texture to the renderer in your code.
Use SDL_RenderCopy() to draw the texture to the rendering context.
To do that you need a SDL_Rect to define the size and position of the
texture you are trying to render

For example:

// create display rectangle
SDL_Rect text_rect;
int text_w, text_h; // for getting texture width and height

SDL_Texture *tex1 = NULL;
tex1 = SDL_CreateTextureFromSurface(ren1,textSurface);

// grab width and height
SDL_QueryTexture(tex1, NULL, NULL, &text_w, &text_h);

// define texture display rect
text_rect.x = 0; // or whatever x coordinate you want
text_rect.y = 0; // or whatever y coordinate you want
text_rect.h = text_h;
text_rect.w = text_w;

SDL_UpdateWindowSurface(win1);
SDL_Delay(5000);

// actually render texture to renderer
SDL_RenderCopy(ren1, tex1, NULL, &text_rect);

SDL_RenderPresent(ren1);
SDL_Delay(5000);

…On Wed, Apr 15, 2015 at 6:25 AM, bilsch01 wrote:

See the code below. I’ve tried many things (not shown) to get the
surface “textSurface” to display but I haven’t done it yet. I believe it
must be simple. Will somebody show me how.

TIA. Bill S.

Code:

#include
#include

int main( int argc, char* args[] ){

SDL_Init(SDL_INIT_VIDEO);

SDL_Window *win1 = NULL;
win1 = SDL_CreateWindow("", 0, 0, 640, 480, SDL_WINDOW_SHOWN);

SDL_Renderer* ren1 = SDL_CreateRenderer(win1, -1, 0);
SDL_SetRenderDrawColor(ren1, 255, 255, 255, 255);
SDL_RenderClear(ren1); // fill the screen with white

TTF_Init();
TTF_Font* font =
TTF_OpenFont("/usr/share/fonts/truetype/msttcorefonts/arial.ttf", 12);
SDL_Color foregroundColor = { 0, 0, 0 };
SDL_Color backgroundColor = { 0, 0, 255 };
SDL_Surface* textSurface = TTF_RenderText_Shaded(font, “This is my text.”,
foregroundColor, backgroundColor);
if (textSurface == NULL)
printf("\n\n failed to create surface \n\n");

SDL_Texture *tex1 = NULL;
tex1 = SDL_CreateTextureFromSurface(ren1,textSurface);

SDL_UpdateWindowSurface(win1);
SDL_Delay(5000);

SDL_RenderPresent(ren1);
SDL_Delay(5000);

SDL_FreeSurface(textSurface);
TTF_CloseFont(font);
TTF_Quit();
SDL_DestroyWindow(win1);
SDL_DestroyRenderer(ren1);
SDL_Quit();
return 0;
}

[/code]


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

Alex wrote:

You can copy image from your texture to window with SDL_RenderCopy function.
For example:
Code:
SDL_RenderCopy(ren1, tex1, NULL, NULL);
SDL_RenderPresent(ren1);

Window is default rendering target, if you do not changed this target with SDL_SetRenderTarget.

Thank you