Jump to content

How to display text using SDL?

- - - - -

  • Please log in to reply
16 replies to this topic

#1
even821

even821

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
i wrote a function for displaying text using the lib SDL_ttf but it doesent seem to work.

here is the function:
void display_text (int x, int y, TTF_Font* font, SDL_Surface* text, SDL_Surface* destination){

	SDL_Rect offset;


	offset.x = x;

	offset.y = y;


	SDL_BlitSurface (text, NULL, destination, &offset);


}

i cannot seem t find the problem with the function, and i dont think it has anything to do with the ttf file (i have it located in the folder with the .exe file, and i have it named corectly etc.)

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
You need to put string on SDL_Surface with TTF_RenderText_[Solid|Shaded|Blended] function. I suggest you read this tutorial.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
even821

even821

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
i know that. that is what the SDL_Surface text parameter in my function is.

text = TTF_RenderText_Solid (font, "level: ", textColor); (that is the SDL_Surface variabel wich i use to store all the stuff about the text, like color and te actual string)

do you have any idea of what else i might have done wrong?

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I thought you forgot to paste some code since your function takes font parameter. Did you check if either of the surfaces are NULL?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
even821

even821

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
umm i do not think any of the surfaces are NULL, howerver i am not completely sure (i have something stored in all of the surfaces at least) as for the font parameter, i just threw it in there to see if it fixed the problem, because i used the text variable wich used the font variabel i had declared in the actulal game function, i was uncertain if it was needed to have it included in the text function as well (i thought that the font part of my text variabel might return NULL if i didnt include it as one of the parameters in the function)

i hope that makes sense lol, and if it doesn't i am sorry. i know i might have a rather wierd way of thinking from time to time lol

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
If it's possible, please post some code. At least the relevant parts.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
even821

even821

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
ok here is the code (wich has anything to do with the function):

declaring the variabels used:
SDL_Surface* display_level = NULL;

TTF_Font* font = TTF_OpenFont("even.ttf", 32);

display_level = TTF_RenderText_Solid (font, "level: ", textColor);

calling the function:
display_text (600, 600, font, display_level, screen);

the function itself:
void display_text (int x, int y, TTF_Font* font, SDL_Surface* text, SDL_Surface* destination){

	SDL_Rect offset;


	offset.x = x;

	offset.y = y;


	SDL_BlitSurface (text, NULL, destination, &offset);


}

oh and the screen variabel is an SDL_Surface* which holds the attributes of the screen (SDL_SetVideoMode())

#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Your code is working. Did you check if it's on (visible part of) the screen?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#9
even821

even821

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
what exactly do you mean by visible part of screen? if you mean if it within the dimensions of the screen then yes it is. (i have the location of the txt set to x = 600, y = 600, and the screen is w = 800
h = 700)

#10
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Yea, I meant that.

Hm... does font load properly? Do font function initialize properly? Did you try writing to another surface? What about directly to screen? Seems problem lies elsewhere, you can try this cheap method to pin point it down.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#11
even821

even821

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
umm wont that just tell me what crashes my program? f so then it is useless since my problem doesen't crash, the text just wont show

#12
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Not sure, but I'd say it's possible that your program still runs even if, for example, font functions do not initialize properly. I'm working on a project, with SDL, and had a problem that window height was really high (around 4000, even though I set it to much much lower values). Turned out I did self assignment of a uninitialized variable (height) and program didn't crash.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users