# Text not being rendered until I recreate the text class in SDL

I made a Text class, it is as shown below:

Text::Text(const int score, const float &x, const float &y, const float &w, const float &h, SDL_Renderer *rend)
{
font = TTF_OpenFont("font/Subway.ttf", 50);
if (font == NULL)
{
std::cout << "Something went wrong\n%s\n" << TTF_GetError();
}

rect = { x, y, w, h };
color = { 100, 100, 100 };

tempSurface = TTF_RenderGlyph_Blended(font, *std::to_string(score).c_str(), color);
tex = SDL_CreateTextureFromSurface(rend, tempSurface);

SDL_FreeSurface(tempSurface);
}

void Text::draw(SDL_Renderer *rend)
{
SDL_RenderCopy(rend, tex, NULL, &rect);
}

Text::~Text()
{
//Clean up stuff
font = nullptr;
tempSurface = nullptr;
SDL_DestroyTexture(tex);
tex = nullptr;
}


I created two of these class objects to hold the image that is created from the players score variables. Issue is that the first time these objects are created, they are not shown on the screen. It's only when the objects are created again that they are shown.

In gameloops constructor class:

GameLoop::GameLoop()
{
window = new Window("Pong Test", 640, 480, SDL_WINDOW_SHOWN);
player1 = new Paddle(50, 50, 10, 60);

player1ScoreText = new Text(player1Score, SCREEN_WIDTH * .25 - 50 / 2, SCREEN_HEIGHT * .25, 50, 75, r);
player2ScoreText = new Text(player2Score, SCREEN_WIDTH * .75 - 50 / 2, SCREEN_HEIGHT * .25, 50, 75, r);

ren = new Renderer((*window).GetWindow(), -1, SDL_RENDERER_ACCELERATED);

r = (*ren).getRenderer();

ball = new Ball("ball.bmp", r);
}


It's created then its rendered here:

void GameLoop::Render()
{
ren->setColor(0, 0, 0, 0);
ren->clear();
player1->Draw(r,255,255,255,255);
player2->Draw(r,255,255,255,255);
player1ScoreText->draw(r);
player2ScoreText->draw(r);
ball->draw(r,255,255,255);

ren->present();
}


It's not showing.. not until this happens:

void GameLoop::Score()
{
delete ball;

player1ScoreText = new Text(player1Score, SCREEN_WIDTH * .25 - 50 / 2, SCREEN_HEIGHT * .25, 50, 75, r);
player2ScoreText = new Text(player2Score, SCREEN_WIDTH * .75 - 50 / 2, SCREEN_HEIGHT * .25, 50, 75, r);

ball = new Ball("ball.bmp", r);
}


From what I can see it doesn't matter where you recreate the classes, it will work. Is this due to how the classes are being initialised?

My first thought is that it would be this line:

tempSurface = TTF_RenderGlyph_Blended(font, *std::to_string(score).c_str(), color);


due to the fact I pass in a pointer to converted integer. However if I don't pass in a pointer I get this error:

error C2664: 'SDL_Surface *TTF_RenderGlyph_Blended(TTF_Font *,Uint16,SDL_Color)' : cannot convert argument 2 from 'const char *' to 'Uint16'

Any help would be greatly appreciated.

This type of issue is best left to debugging, but I'll take a shot.

My concern is these lines:

player1ScoreText = new Text(player1Score, SCREEN_WIDTH * .25 - 50 / 2, SCREEN_HEIGHT * .25, 50, 75, r);
player2ScoreText = new Text(player2Score, SCREEN_WIDTH * .75 - 50 / 2, SCREEN_HEIGHT * .25, 50, 75, r);


Now if you look, we are dependent on the following variables:

• player1Score (and player2Score), which my guess is a simple int - no need to worry.
• SCREEN_WIDTH and SCREEN_HEIGHT, which my guess is a const - no need to worry.
• r - which is your renderer. Now tell me, at the time you are initializing the player1ScoreText and player2ScoreText, what is r?

Here's my corrected code:

GameLoop::GameLoop()
{
window = new Window("Pong Test", 640, 480, SDL_WINDOW_SHOWN);
ren = new Renderer((*window).GetWindow(), -1, SDL_RENDERER_ACCELERATED);
r = (*ren).getRenderer();

player1 = new Paddle(50, 50, 10, 60);