1. Is the sf::Context at the beginning of main() really necessary? What happens if you don't declare it?
I've taken this code from another application of mine where it is needed to run without errors because there is a special thread rendering to images without having a render window.
In this demo it seems not to be necessary so I removed it.
2. There's no need to test if the window is open after creating it, it is always open.
Ok. I removed it. But what happens if SFML cannot open the window?
3. The renderWindow.close() "just to be sure" is not necessary, the destructor takes care of that.
I want the render window to be closed before the message box eventually appears. I've changed the code comment accordingly to make that clear.
4. The event list optimization makes your code more complicated for really little gain, I don't think you need it (and you should generally not optimize anything before profiling it and making sure that it's worth it).
I admit I haven't profiled it but in my opinion the desire to use the mutex as rarely as possible and lock it as short is possible is worth the additional code complexity.
5. You probably don't need the beganToRun() test, a thread cannot be launched a second time if it's not finished first.
I want to make it impossible for the user to use the interface wrong (and create nasty multi-threading bugs). Mistakes like those:
// create and launch render thread
RenderThread renderThread(renderWindow);
sf::Thread thread(&RenderThread::run, &renderThread);
thread.launch();
// outch :(
sf::Thread thread2(&RenderThread::run, &renderThread);
thread2.launch();
// outch :(
renderThread.run();
To me there's a lot of unnecessary complexity in this code. If some tricks are indeed required, you'd better point to the corresponding issue in the tracker if it exists, or create a new one if not. A "perfect" code is supposed to be what the tutorials and documentation describe, without all these hacks. Anything else that you have to add must be reported as a bug.
I've written my demo because I haven't found a tutorial on multi-threaded rendering yet.
In addition everybody has a different definition of "perfect". I think one of the main reasons for multi-threaded rendering is not to block the rendering for of other tasks, e.x. loading resources. Nevertheless it
needs to be locked once in a while - which my code does as rarely and as shortly as possible - yes, fully accepting the additional code complexity.
To be honest I hope somebody else considers my demo helpful. Based on my experience blocking threads (using mutexes) too often and too long is one of the typical beginner mistakes in multi-threaded programming.
You shouldn't get 100% with v-sync on. What is the framerate like when you activate it?
And what CPU load do you get with framerate limit alone?
Please see the screenshots in my post above for CPU loads with different settings.
Another strange observation: in my extended demo (code attached) one can toggle the vertical sync and the frame rate limit independently and on my PC I see different CPU loads based on which setting I enable before the other setting. Huh?!
Thanks for the comments to far! But what about the last of my three initial questions: there still is some visible stretching of the viewport while resizing the window - is there any way to prevent that?
[attachment deleted by admin]