Hey Everyone,
I'm building my first engine and am stunned by this weird problem.
I have the following (simplified) setup for running my game loop:
// Open window
while (Game::window.isOpen()) {
//Register events
sf::Event event;
// Check if an event happened
while (Game::window.pollEvent(event)) {
// Close windows if close button is pressed
if (event.type == sf::Event::Closed)
Game::window.close();
}
// Clear window with black background
Game::window.clear(sf::Color::Black);
// Let game logic catch up with real passed time
lag += Game::getElapsedFrameTimeAndRestart(); // Add time that needs to be caught up in ms
while (lag > 10) { // fixed update time is 10 ms
// Test moving ship to right
re::Sprite& player = scene.getSprite("player");
float movement = Game::getFixedUpdateTime() * 50;
float x = player.getPosition().x + movement;
player.setPosition(x, 50);
// Calculate remaining lag time to recover from
lag -= fixedUpdateTime; // Remove time that has been caught up
}
// Render all visible sprites
//std::cout << "text"<< std::endl;
Game::renderer.render();
}
The render function consists of:
// Render all sprites
for (re::Sprite& sprite : Game::scene.getAllSprites()) {
Renderer::mWindow.draw(sprite.getSprite());
}
// Draw sprites on screen
Renderer::mWindow.display();
Now the weird problem is that when I run this code with the std::cout enabled, everything is rendered nicely and smoothly, but when I remove the std::cout, the rendering is slow and unstable.
I want to remove my unneccesary cout, but somehow I can't get a good rendering then...
What am I doing wrong?