The displayScore() function

The final section of code displays the score at the bottom of the game window:

void Board::displayScore(SDL_Renderer *renderer, TTF_Font *font) {
std::stringstream message;
message << "ROWS: " << currentScore_;
SDL_Color white = { 255, 255, 255 };
SDL_Surface *surface = TTF_RenderText_Blended(
font,
message.str().c_str(),
white);
SDL_Texture *texture = SDL_CreateTextureFromSurface(
renderer,
surface);
SDL_Rect messageRect{ 20, BoardHeight + 15, surface->w, surface->h };
SDL_FreeSurface(surface);
SDL_RenderCopy(renderer, texture, nullptr, &messageRect);
SDL_DestroyTexture(texture);
}

The displayScore() function uses the SDL2_ttf library to display the current score at the bottom of the window (underneath the board). The TTF_Font *font argument is passed in from the Game class to avoid initializing the font every time the score is updated. The stringstream message variable is used to create the text value and set it to a C char* within the TTF_RenderText_Blended() function. The rest of the code draws the text on a SDL_Rect to ensure that it's properly displayed.

That's it for the Board class; let's move on to the Game to see how it all fits together.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset