• Advertisement
Sign in to follow this  

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

This topic is 4574 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
Sign in to follow this  

  • Advertisement