Hello
Settings:
I've dug out my old code and decided to give multithreaded game engine approach a shot.
The
idea is to run drawing loop and update+event loop in separate threads.
drawing is done in "new thread" while event pulling and updating is supposed to run in main thread.
Problem:
Why does my drawing loop slow down aprox. 50 times if I don't put update/event loop to sleep for a short duration?
Since the two should run independently of each other (no communication is done yet, no synchronization, nothing) ran on multi-core cpu, the only connection between these loops is that I draw to same RenderWindow as I pull events from.
In the end of the day I do plan to limit how many times a sec each of these loops executes, but that doesn't change fact that I wonder how they influence each other or if my though flow is flawed
Fairly minimal example
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
sf::RenderWindow window;
sf::View Camera;
void Draw()
{
//time and fps variables
int counter = 0;
int fps = 0;
sf::Text fpsText;
sf::Clock clk;
sf::Font font;
sf::Time fpsTime = sf::Time::Zero;
std::stringstream ss;
font.loadFromFile("corbel.ttf");
fpsText.setFont(font);
//samething to draw
sf::VertexArray va;
va.setPrimitiveType(sf::Triangles);
va.append(sf::Vector2f(10, 10));
va.append(sf::Vector2f(100, 10));
va.append(sf::Vector2f(100, 100));
va[0].color = sf::Color::Red;
va[1].color = sf::Color::Blue;
va[2].color = sf::Color::Green;
window.setActive(true);
while(window.isOpen())
{
window.setView(Camera);
window.clear();
window.draw(va); //drawing of everything here
window.setView(window.getDefaultView());
//fps
counter++;
fpsTime+=clk.restart();;
if(fpsTime.asSeconds()>=1)
{
fps = (int)(counter/fpsTime.asSeconds());
counter=0;
fpsTime = sf::Time::Zero;
ss.str(std::string());
ss.clear();
ss<<fps;
fpsText.setString(ss.str());
}
window.draw(fpsText);
window.display();
}
};
int main()
{
window.create(sf::VideoMode(1680, 1050),"");
window.setVerticalSyncEnabled(false);
window.setActive(false);
//drawing thread start
sf::Thread RenderThread(Draw);
RenderThread.launch();
//GameLoop
while(window.isOpen())
{
//Event Handling
sf::Event event;
while(window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
RenderThread.wait();
break;
case sf::Event::KeyPressed:
{
switch (event.key.code)
{
case sf::Keyboard::Escape:
window.close();
RenderThread.wait();
break;
//more key events here
default:
break;
}
break;
}
default:
break;
}
}
//update here
//sleep to allow other thread to work
sf::sleep(sf::milliseconds(3));
}
}