76
Python / How to display window in a separate thread/process?
« on: November 20, 2011, 11:42:38 pm »Quote from: "kolofsson"
You see, the problem is that I would not like to tell the app to sleep, I would not like to waste the computational power, I would prefer to handle window events in the meantime. The window.display with vsync enabled goes to sleep and waits until it is safe to write the buffer.
Unless you have very specific needs, there's no reason why you would want more than, say, 60 FPS. Your application can run for an extended period of time, and it will waste a lot of resource if you try to avoid sleeping after each iteration of the main loop.
Quote
I actually figured out a workaround that looks like this:Code: [Select]import time
import sf
def main():
window = sf.RenderWindow(sf.VideoMode(800, 600), 'Window', sf.Style.DEFAULT)
window.vertical_sync_enabled = True
last_display = time.clock()
while window.opened:
for event in window.iter_events():
#handle events
if time.clock() - last_display > 0.016:
window.clear(sf.Color(200, 0, 0))
window.display()
last_display = time.clock()
if __name__ == '__main__':
main()
That's kind of scary.
![Smiley :)](https://www.sfml-dev.org/forums/Smileys/default/smiley.gif)
Quote from: "kolofsson"
The crucial part is if time.clock() - last_display > 0.016:. Without it I get 60 FPS. With it I get 6000 FPS! That's because This little if triggers the display just before the Vsync window get opened, which makes the display wait time minimal.
To be clear, the window only gets opened at the start. window.opened simply returns True if the window is currently opened, until you call window.close().
Quote from: "kolofsson"
However, the 0.016 value will only work for 60 Hz. For different refresh rates bad things will happen. Btw, is there a way to retrieve screen refresh rate in SFML?
Not that I know of. You can probably get it with OpenGL, but you can't do OpenGL calls with this binding currently.