问题
You will have to forgive me if I can't explain myself properly, I am trying to create a highscore table using c++ sfml string array. I tried drawing them separately using window.draw(array[1],array[2] and so on.
But if I put the array in a for loop and use an int variable, it only draws array 1, 3 and 5.
for(cnt = 0;cnt <5;cnt++)
{
thiswindow.draw(topscores[cnt]);
thiswindow.draw(topnames[cnt]);
thiswindow.display();
}
回答1:
Take the display out of the loop. This should fix the problem.
回答2:
As stated by Rosme RenderWindow::display should not be called within the loop. Actually,RenderWindow::display should only ever be called once per frame of rendered content... SFML uses double buffering, and RenderWindow::display is the command to switch the back buffer. Let's take a moment to discuss how this works:
When you are drawing content in a double buffered system, you actually draw to two separate surfaces ("buffers") - at any given time one is hidden (the buffer in which drawing occurs) and one is shown on the screen. The hidden buffer currently used for drawing is called the "back buffer". When RenderWindow::display is called (or its equivalent in other double buffered systems) the buffer currently displayed on the screen becomes the new back buffer, and the old back buffer is displayed on the screen. This allows you to make lots of changes with less risk of pop-ins, tearing, or flicker - you never draw to the buffer that is being shown.
With that knowledge, you should understand what was happening as a result of the display calls within the loop:
instruction | back buffer | front buffer (shown)
----------------------------------------------------
| (empty) | (empty)
draw A | A | (empty)
display | (empty) | A
draw B | B | A
display | A | B
draw C | A, C | B
display | B | A, C
draw D | B, D | A, C
display | A, C | B, D
draw E | A, C, E | B, D
display | B, D | A, C, E
Thus, after the loop completes, you see the first, third, and fifth items drawn. The others are drawn as well, but exist in the back buffer where they can't be seen.
来源:https://stackoverflow.com/questions/28074005/c-sfml-array5-of-strings-in-for-loop-window-display-only-showing-line-1-3-5