From 3bcb85c352b6bf86dda46f2379bbd993d78832b6 Mon Sep 17 00:00:00 2001 From: Manuel QuiƱones Date: Mon, 30 Sep 2013 18:33:36 +0000 Subject: 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. --- 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): -- cgit v0.9.1