Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugargame/canvas.py
diff options
context:
space:
mode:
authorAlan Aguiar <alanjas@hotmail.com>2012-10-20 09:22:01 (GMT)
committer Gary Martin <gary@garycmartin.com>2012-10-23 23:44:17 (GMT)
commite6cff9307917aa97019785c32fbf0e412ba0e7ea (patch)
treed1d8dc602db9dab662bcfa0376ac97d81f80abe7 /sugargame/canvas.py
parentec034421db0933df5065dd681320ea39e96fc616 (diff)
some changes to back to life
Diffstat (limited to 'sugargame/canvas.py')
-rw-r--r--sugargame/canvas.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/sugargame/canvas.py b/sugargame/canvas.py
new file mode 100644
index 0000000..980cb73
--- /dev/null
+++ b/sugargame/canvas.py
@@ -0,0 +1,62 @@
+import os
+import gtk
+import gobject
+import pygame
+import event
+
+CANVAS = None
+
+class PygameCanvas(gtk.EventBox):
+
+ """
+ mainwindow is the activity intself.
+ """
+ def __init__(self, mainwindow, pointer_hint = True):
+ gtk.EventBox.__init__(self)
+
+ global CANVAS
+ assert CANVAS == None, "Only one PygameCanvas can be created, ever."
+ CANVAS = self
+
+ # Initialize Events translator before widget gets "realized".
+ self.translator = event.Translator(mainwindow, self)
+
+ self._mainwindow = mainwindow
+
+ self.set_flags(gtk.CAN_FOCUS)
+
+ self._socket = gtk.Socket()
+ self.add(self._socket)
+ self.show_all()
+
+ def run_pygame(self, main_fn):
+ # Run the main loop after a short delay. The reason for the delay is that the
+ # Sugar activity is not properly created until after its constructor returns.
+ # If the Pygame main loop is called from the activity constructor, the
+ # constructor never returns and the activity freezes.
+ 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."
+
+ # 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())
+ pygame.init()
+
+ # Restore the default cursor.
+ self._socket.window.set_cursor(None)
+
+ # Initialize the Pygame window.
+ r = self.get_allocation()
+ pygame.display.set_mode((r.width, r.height), pygame.RESIZABLE)
+
+ # Hook certain Pygame functions with GTK equivalents.
+ self.translator.hook_pygame()
+
+ # Run the Pygame main loop.
+ main_fn()
+ return False
+
+ def get_pygame_widget(self):
+ return self._socket