Hello, I was wondering, using SDL_TTF, how can I render a changing variable? I have tried a couple of things, but they have not seemed to work out. The text is not being rendered at all. Any help would be greatly appreciated.
Code :
auto surface = IMG_Load(“Tetris.png”);
SDL_Surface* textSurface = NULL;
TTF_Font* font = TTF_OpenFont(“PixelGameFont.ttf”, 32);
SDL_Color textColour = { 0xff, 0xff, 0xff, 0xff };
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
//SDL_Texture *texture2 = SDL_CreateTextureFromSurface(renderer, surface2);
//SDL_FreeSurface(surface2);
SDL_Rect rect{ 425, 150, 200, 100 };
//SDL_Rect rect2{ 600, 600, 100, 100 };
//sets the screen colour to black
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xff);
SDL_RenderClear(renderer);
//Draws the map (See “Well.cpp” for more details
well_.draw(renderer);
//Draws the tetromino (See “Tetromino.cpp” for more details
tetromino_.draw(renderer);
//SDL_RenderCopy(renderer, texture2, nullptr, &rect2);
//if statement that moves the tetromino downwards every second
if (SDL_GetTicks() > moveTime_) {
moveTime_ += 1000;
Tetromino t = tetromino_;
t.move(0, 1);
check(t);
if (well_.score != well_.oldScore) {
SDL_FreeSurface(textSurface);
std::string text = "SCORE: " + Game::IntToString(well_.score);
textSurface = TTF_RenderText_Solid(font, text.c_str(), textColour);
}
}
SDL_Surface* screen = NULL;
//present renderer
SDL_Rect textRect{ 600, 600, 200, 50 };
SDL_BlitSurface(textSurface, NULL, screen, &textRect);
SDL_RenderCopy(renderer, texture, nullptr, &rect);
SDL_FreeSurface(surface);
SDL_RenderPresent(renderer);
//Function ‘IntToString’
std::string Game::IntToString(int score) {
std::stringstream sstream;
sstream << score;
return sstream.str();
}
//The entire top half is all under one function, however the rest of the function is unrelated to the topic, and as such I have not included it.