diff options
author | Wade Brainerd <wadetb@gmail.com> | 2009-10-30 02:29:42 (GMT) |
---|---|---|
committer | Wade Brainerd <wadetb@gmail.com> | 2009-10-30 02:29:42 (GMT) |
commit | 802387fcbca231fb54fa5caad3affda942b5bc39 (patch) | |
tree | e6dcd14a45a6f18b7ef849b828f815c59c18aad4 /sugargame | |
parent | e245a2fbca067dee091ae27c78cad063bd3ffbec (diff) |
Update to newer sugargame.
Diffstat (limited to 'sugargame')
-rw-r--r-- | sugargame/activity.py | 75 | ||||
-rw-r--r-- | sugargame/canvas.py | 55 | ||||
-rw-r--r-- | sugargame/event.py | 8 |
3 files changed, 59 insertions, 79 deletions
diff --git a/sugargame/activity.py b/sugargame/activity.py deleted file mode 100644 index 81bf481..0000000 --- a/sugargame/activity.py +++ /dev/null @@ -1,75 +0,0 @@ -import os -import gobject -import gtk - -import sugar.activity.activity -import sugar.graphics.style - -import pygame -import event - -ACTIVITY = None - -def get_activity(): - return ACTIVITY - -class PygameActivity(sugar.activity.activity.Activity): - def __init__(self, handle): - super(PygameActivity, self).__init__(handle) - - self._socket = None - self._screen = None - - # Fudge the toolbar size. - TOOLBAR_HEIGHT = 75 - TAB_HEIGHT = 45 - self.width = gtk.gdk.screen_width() - self.height = gtk.gdk.screen_height() - TOOLBAR_HEIGHT - TAB_HEIGHT - - global ACTIVITY - ACTIVITY = self - - def build_canvas( self ): - # Build the widget in which to embed Pygame. - self._socket = gtk.Socket() - - eventbox = gtk.EventBox() - eventbox.set_flags(gtk.CAN_FOCUS) - eventbox.set_size_request(self.width, self.height) - eventbox.add(self._socket) - eventbox.show_all() - - self.set_canvas(eventbox) - - # Preinitialize Pygame so we can hook its methods. - os.environ['SDL_WINDOWID'] = str(self._socket.get_id()) - pygame.init() - - # Hook certain Pygame functions with GTK equivalents. - translator = event.Translator(self, eventbox) - translator.hook_pygame() - - # Initialize the Pygame window. - self._screen = pygame.display.set_mode((self.width,self.height)) - - # Restore the default cursor. - self._socket.get_window().set_cursor(None) - - 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): - # Run the Pygame main loop. - main_fn() - return False - - def get_pygame_widget(self): - return self._socket - - def get_pygame_screen(self): - return self._screen -
\ No newline at end of file diff --git a/sugargame/canvas.py b/sugargame/canvas.py new file mode 100644 index 0000000..27f9137 --- /dev/null +++ b/sugargame/canvas.py @@ -0,0 +1,55 @@ +import os +import gtk +import gobject +import pygame +import event + +CANVAS = None + +class PygameCanvas(gtk.EventBox): + def __init__(self, mainwindow): + gtk.EventBox.__init__(self) + + global CANVAS + assert CANVAS == None, "Only one PygameCanvas can be created, ever." + CANVAS = 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.get_window().set_cursor(None) + + # Initialize the Pygame window. + pygame.display.set_mode((0, 0), pygame.RESIZABLE) + + # Hook certain Pygame functions with GTK equivalents. + translator = event.Translator(self._mainwindow, self) + translator.hook_pygame() + + # Run the Pygame main loop. + main_fn() + return False + + def get_pygame_widget(self): + return self._socket diff --git a/sugargame/event.py b/sugargame/event.py index f98aa5f..42fca1e 100644 --- a/sugargame/event.py +++ b/sugargame/event.py @@ -91,10 +91,10 @@ class Translator(object): pygame.event.post(pygame.event.Event(pygame.VIDEOEXPOSE)) return True - def _resize_cb( self, activity, event ): - #evt = pygame.event.Event(eventwrap.pygame.VIDEORESIZE, - # size=(event.width,event.height), width=event.width, height=event.height)) - #pygame.event.post(evt) + def _resize_cb(self, widget, event): + evt = pygame.event.Event(pygame.event.VIDEORESIZE, + size=(event.width,event.height), width=event.width, height=event.height) + pygame.event.post(evt) return False # continue processing def _quit_cb(self, data=None): |