• Advertisement
Sign in to follow this  

Why my text isn't showing?(SDL/SDL_TTF)

This topic is 4481 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I am just about done with my Pong Clone. Just have the Main Menu to fix after I figure out what is wrong with my scoring. Now you see...since I am working on the scoring, I have some problems with the text being displayed. Not sure why the text isn't being displayed, so I decided to ask here. I didn't want to, as I wanted to make this game the farthest I could without asking for help. This is making me mad! I am going to show you my function, so you know what everything is, and then I will show you the code that dosen't show my text. Function Decaration:
void Draw_Text(char* string, int size, int x, int y, 
                                                                   int fr, int fg, int fb,
                                                                   int br, int bg, int bb)
{
     TTF_Font* font=TTF_OpenFont("lazy.ttf", size);
     SDL_Color foreground_Color={fr, fg, fb};
     SDL_Color background_Color={br, bg, bb};

     SDL_Surface* text_surface = TTF_RenderText_Shaded(font, string, foreground_Color, background_Color);
     SDL_Rect text_location={x, y, 0, 0};
     SDL_BlitSurface(text_surface, NULL, SDL_GetVideoSurface(), &text_location);
     SDL_FreeSurface(text_surface);
     TTF_CloseFont(font);
}

Code that it won't show up in:
if ( ball_position.x == 0 )
        {
             player2_score++;
             Draw_Text("Player 2 has scored!", 24, 400, 544, 66, 66, 255, 77, 77, 77);
             ballXVel=0;   
             ballYVel=0;
             ball_position.x=WINDOW_WIDTH/2;
             ball_position.y=WINDOW_HEIGHT/2; 
             if(player2_score == 10 )
             {
                  Draw_Text("Player 2 has won!  Congradulation Player 2!", 24, 400, 544, 255, 255, 255, 0, 0, 0);
             }
             
        }
        if(ball_position.x + ball_position.w > WINDOW_WIDTH)
        {    
             Draw_Text("Player 1 has scored!", 24, 400, 544, 255, 255, 255, 0, 0, 0);
             ballXVel=0;   
             ballYVel=0;
             ball_position.x=WINDOW_WIDTH/2;
             ball_position.y=WINDOW_HEIGHT/2;
             player1_score++;
             if(player1_score == 10 )
             {
                  Draw_Text("Player 1 has won!  Congradulation Player 1!", 24, 400, 544, 255, 255, 255, 0, 0, 0);
             }

        }

I am using text in some of the Main Menu(just need to add functionality to it)and that shows up. So I am wondering why it won't work here? All the other part of the code works except for the text. Can anyone please help? Also lazyfoo if you read this, you are right I am using your font that you made. I am only using it for testing purposes though. So in the final release, it will not be their. So what's wrong with it? Chad.

Share this post


Link to post
Share on other sites
Advertisement
the function DrawText works for me on changing lazy.ttf to one i have.

so here are some pointers

has TTF_Init() been called?

check if font == NULL

check if text_surface == NULL

Share this post


Link to post
Share on other sites
So...That didn't make a WHOLE of since, but I tried what you said and here were the results:


TTF_Init(); was called where it was needs to be called, and the rest didn't work at all. Even though that didn't make alot of since.


Anyone else?


It works for the Main Menu, BUT NOT when someone scores. Why is that?


Chad.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chad Smith
It works for the Main Menu, BUT NOT when someone scores. Why is that?


Chances are you are erasing it or drawing over it. Check to make sure you clear the screen if you do first, then draw that score stuff last before you flip. So for example:

Clear Screen
Draw Paddles
Draw Ball
Draw Current Scores
--On Score
Draw Score
Flip

Share this post


Link to post
Share on other sites
Ok I fixed the problem...well....a little bit.


The problem was I wasn't fliping it. Now...it shows up and disapears real fast. I don't even have time to read it. What is up with it? How can I make it stay up their?

Share this post


Link to post
Share on other sites
I'm sorry, what excatly do you mean? lol, sorry.



I'm about to go to school, and I should be able to get on during school at certain points, so maybe someone can tell me then.


What excatly do you mean? Sorry. lol.


Chad.

Share this post


Link to post
Share on other sites
Well, if you are displaying it for only one frame, it will only flicker. You need to display that information until a player presses something, or it will just be refreshed over. For example, have a variable that says whether the game is running, the player has won, or the player has lost. If the player wins or loses, draw some text. Otherwise, keep with gameplay mechanics. If the player won or lost, wait for input to start the game again. Your issue is probably that you only make the draw_text call during one frame, and never again.

Share this post


Link to post
Share on other sites
Ok I understand that and I think I can fix that. What about when the player scores? That only displays fast and then quits. How can I get that to stay up? Anyone have any ideas? I could just get them to press a button when they score, and then continue. How about that?

Share this post


Link to post
Share on other sites
Just draw the scores every frame.
Ex:

void DrawGame()
{
// Draw paddles, balls, whatever

Draw_Text(score,...);

/*
*Most likely, if you have score as an int, change it to a char
*with a stringstream or something.
*
*/

SDL_Flip(...);
}


I mean, dont do

if (player_scores)
Draw_Text(...);

, but do it all the time.

Share this post


Link to post
Share on other sites
Ok, I have tried all that and still nothing! I have no idea why it won't freaking stay up. Anyone else? PLEASE!



EDIT:HERE IS SOME OF MY NEW CODE. THIS IS FROM MY NEW MAIN MENU. IT WON'T STAY UP FOR SOMEREASON.


Draw_Text("(P)lay Game", 24, WINDOW_WIDTH/2, WINDOW_HEIGHT/2, 255, 255, 255, 0, 0, 0);
Draw_Text("(E)xit Game", 24, WINDOW_WIDTH/2+20, WINDOW_HEIGHT/2+20, 255, 255, 255, 0, 0, 0);
Uint8* KeysHeld= SDL_GetKeyState(0);

if(KeysHeld[SDLK_p])
{

Init();
KeysHeld[SDLK_p]=0;
}
if(KeysHeld[SDLK_e])
{
SDL_Quit();
KeysHeld[SDLK_e]=0;
}




and the Draw_Text function:


void Draw_Text(char* string, int size, int x, int y, int fr, int fg, int fb, int br, int bg, int bb)
{
TTF_Font* font = TTF_OpenFont("lazy.ttf", size);
SDL_Color fgc={fr, fg, fb};
SDL_Color bgc={br, bg, bb};
SDL_Surface* textsurface= TTF_RenderText_Shaded(font, string, fgc, bgc);
SDL_Rect textlocation={x, y, 0, 0};
SDL_BlitSurface(textsurface, NULL, SDL_GetVideoSurface(), &textlocation);
SDL_FreeSurface(textsurface);
TTF_CloseFont(font);
}




the Main menu text comes up. I read it, then it goes. Why won't it stay again? I think it should be so simple, but it not! lol.


Chad!

[Edited by - Chad Smith on October 11, 2005 9:03:43 PM]

Share this post


Link to post
Share on other sites
Now, I'm really sleepy, and I may not be thinking right... But what about putting it in a do {} while () loop?

-DBZ-

Share this post


Link to post
Share on other sites
After your code:

Draw_Text("(P)lay Game", 24, WINDOW_WIDTH/2, WINDOW_HEIGHT/2, 255, 255, 255, 0, 0, 0);
Draw_Text("(E)xit Game", 24, WINDOW_WIDTH/2+20, WINDOW_HEIGHT/2+20, 255, 255, 255, 0, 0, 0);
Uint8* KeysHeld= SDL_GetKeyState(0);

I'm betting that you draw something else that ends up covering the text. Instead, make sure it's structured like this:

- Clear Screen
- Draw Everything
- Draw that code above
- Flip Screen

Also let's see the entire rendering loop to make sure.

Share this post


Link to post
Share on other sites
Oh yeah... drawing something over it might also be the case... Make sure that you draw everything in the order you want it to appear. In my pong game:

-Clear the screen
-If the game is paused
-Draw the pause menu
-Else
-Draw everything
-Flip the screen

Ex:

-Clear the screen
-Draw all sprites or pictures or whatever
-Draw your text last, so something isnt drawn over it
-Flip the screen

Share this post


Link to post
Share on other sites
That is the Main Menu, and I am calling that first in main(). So, nothing else is being drawn over it. Atleast I wouldn't think anything would. I will try the do...while attempt.



Chad.

Share this post


Link to post
Share on other sites
Sorry to double post, but I didn't want to edit that post, because I wanted people to see this post actually.



I tried the do...while, and guess what? It STILL didn't work!



NA, I'm justplaying. It worked! Yeah! I only have last minute bugs left, and the game is done! I'm so excited! Thanks everyone for your help!




Chad.

Share this post


Link to post
Share on other sites
Good, good. Good luck with finishing it up everywhere. Interesting, I just finsihed my pong clone too, lol. Are you going to post a screenshot or a demo of it?

-DBZ-

Share this post


Link to post
Share on other sites
Cool. I am going to post the demo/final version of it(not sure if it will be a demo or the final version)somewhere. I am thinking about implenting one more feature in their before I release it. Well....2 more features. I want to make this Pong Clone the best as I can.



Chad.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement