I noticed my game (really just a barebones program at this stage) was experiencing some unexplained slowdown and when I printed out the time between logic frames there was the occasional one every so often that was around 0.5 of a second longer than most.
At first I thought it was the way the game loop was set up, but I decided to investigate it properly when I changed the game loop to a different system and was still getting the same issue.
I isolated it to the rendering, then I found it was due to the draw function of an object of my BitmapFont class. I'm using OpenGL and my custom BitmapFont class which is essentially a specialized wrapper for my SpriteSheet class. As you'd expect, it loads an image to use as a spritesheet for a font, and based on the std::string input text it's given, it draws the appropriate letters to the screen.
All seems to be working, and I don't see anything that looks unreasonably costly or like a loop that would be at risk of hanging or anything. Either way, the stuttering only happens once in many many frames (even though the text/position may be completely static) so I can't see why it would take longer some times and not others.
HOWEVER, I have seemed to narrow the behaviour down to a very specific situation and I have no idea why this would be the case:
I'm currently displaying time and date values using the BitmapFont class. What I was doing was I had a single object called timeFont, which I'd initialize by loading the font image etc. then every rendering pass I'd call the following functions:
timeFont.setText( timeString );
timeFont.setPos( 125, 50 );
timeFont.drawFont();
- setText() simply changes the value of a string in the BitmapFont object (in this case timeFont) which is used to store the text, in this case the time.
- setPos() simply changes X and Y position values.
- drawFont() draws the font by wrapping the underlying SpriteSheet object's draw functions and calculating/using the appropriate font-specific values.
Then I would calculate a string to represent the date I wanted to draw, and call the following:
timeFont.setText( dateString );
timeFont.setPos( 0, 50 );
timeFont.drawFont();
Here I'm obviously intending to change the timeFont object's text value, change its position and then draw it again.
If I do this, I experience the occasional half second or so extra time between some updates like I described.
BUT, what is interesting to me is that the stuttering only seems to happen if I draw some text and then set the text to something else AND then draw it. Even if I reset the text it doesn't seem to happen unless I draw it again, and drawing the same text multiple times doesn't seem to cause it either.
So then I decided to create another BitmapFont object called dateFont, set it up exactly like timeFont, and then when it came to printing the date I'd simply call:
dateFont.setText( dateString );
dateFont.setPos( 0, 50 );
dateFont.drawFont();
The result is that the problem seems not to happen.
Why might this be? I find it hard to understand when the situation seems to be 100% repeatable in the ways I've described, and doesn't seem to be caused by extremely faulty or inefficient code (at least in the sense it works fine most of the time either way), AND simply doing the same thing but using 2 objects to do it appears to avoid it altogether.
In my ignorance, the only thing I can think of is maybe some sort of strange attempt at a compiler optimization? Like it isn't expecting the text to change and so it does something it shouldn't that slows it down sometimes? Even still, why only sometimes and not most of the time? I can't understand that explanation either.
I'll provide more specific code if anyone has an idea about what it could be and where to look. Probably no point spamming tons of code here yet when I have no idea what is relevant.