I’m basically a beginner in C++ but was looking into how to update a string that is in a while loop?
Currently every iteration of the loop it writes the text on top of the previous still its basically a blur of white colour.
This is my loop that im testing with:
while(!quit){
while( SDL_PollEvent( &event ) ){
switch(event.type){
case SDL_QUIT: quit = true; break;
case SDL_MOUSEMOTION: handle_mouse_position();
}
}
SDL_Rect offset;
offset.x = 400;
offset.y = 290;
std::stringstream s;
s << "Mouse xpos: " << mouseX << " Mouse ypos: " << mouseY;
font_surface = TTF_RenderText_Solid(font,s.str().c_str(),font_color);
SDL_BlitSurface(font_surface, NULL, screen, &offset);
//Update the screen
if( SDL_Flip( screen ) == -1 ) {
return 1;
}
}
Is there some way to clear the previous text output and update it each loop so that it will display mouse position clearly?
You can do that redrawing everything each time, or if your background is a solid color, paint a rectangle and draw the text above it.
The second one is more efficient. But if you have a complex scenario it is better to work with layers. First you render the bottom layer, then the second, and so on… By layers I mean, you can have a
background()which draws the background andforeground()which, obviously, draws the foreground. So what you will have is something like:So you can easily handle more complex scenarios.