diff options
author | Manuel QuiƱones <manuq@laptop.org> | 2013-09-30 18:33:36 (GMT) |
---|---|---|
committer | Manuel QuiƱones <manuq@laptop.org> | 2013-09-30 18:33:36 (GMT) |
commit | 3bcb85c352b6bf86dda46f2379bbd993d78832b6 (patch) | |
tree | 2199da59e91f36ca040e3e571aed8929df04ede8 | |
parent | b091432a4cbbfbf8e899145ee9d3dbfe3fce91e1 (diff) |
Remove asserts and properly reinitialize PyGame display, if needed
The asserts where there to 1. avoid calling run_pygame more than once,
and 2. initialize PyGame display only after SDL_WINDOWID environment
variable was set.
This commit removes the asserts. 1. is solved using a _initialized
flag. And 2. is solved by checking if the display was set, and using
pygame.display.quit to uninitialize it.
-rw-r--r-- | sugargame/canvas.py | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/sugargame/canvas.py b/sugargame/canvas.py index 0db4220..f5f5b56 100644 --- a/sugargame/canvas.py +++ b/sugargame/canvas.py @@ -27,6 +27,9 @@ class PygameCanvas(Gtk.EventBox): self._socket = Gtk.Socket() self.add(self._socket) + + self._initialized = False + self.show_all() def run_pygame(self, main_fn): @@ -37,11 +40,14 @@ class PygameCanvas(Gtk.EventBox): GObject.idle_add(self._run_pygame_cb, main_fn) def _run_pygame_cb(self, main_fn): - assert pygame.display.get_surface() is None, "PygameCanvas.run_pygame can only be called once." + # PygameCanvas.run_pygame can only be called once + if self._initialized: + return # Preinitialize Pygame with the X window ID. - assert pygame.display.get_init() == False, "Pygame must not be initialized before calling PygameCanvas.run_pygame." os.environ['SDL_WINDOWID'] = str(self._socket.get_id()) + if pygame.display.get_surface() is not None: + pygame.display.quit() pygame.init() # Restore the default cursor. @@ -56,6 +62,8 @@ class PygameCanvas(Gtk.EventBox): # Run the Pygame main loop. main_fn() + + self._initialized = True return False def get_pygame_widget(self): |