Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit 3ae993cd446cbd737dee890fd440688f16fc0911

  • Tree SHA1: 3e781b6
  • Parent SHA1: cf5c10c (New Canvas class, for integrating Pygame into existing activities. The Canvas allows a traditional GTK activity to add one PygameCanvas to its UI.)
  • raw diff | raw patch
Remove sugargame.activity module.
Code should derive directly from the Sugar activity class and use sugargame.canvas.PygameCanvas.
  
1import os
2import gobject
3import gtk
4
5import sugar.activity.activity
6import sugar.graphics.style
7
8import pygame
9import event
10
11ACTIVITY = None
12
13def get_activity():
14 return ACTIVITY
15
16class PygameActivity(sugar.activity.activity.Activity):
17 def __init__(self, handle):
18 super(PygameActivity, self).__init__(handle)
19
20 self._socket = None
21 self._screen = None
22
23 # Fudge the toolbar size.
24 TOOLBAR_HEIGHT = 75
25 TAB_HEIGHT = 45
26 self.width = gtk.gdk.screen_width()
27 self.height = gtk.gdk.screen_height() - TOOLBAR_HEIGHT - TAB_HEIGHT
28
29 global ACTIVITY
30 ACTIVITY = self
31
32 def build_canvas( self ):
33 # Build the widget in which to embed Pygame.
34 self._socket = gtk.Socket()
35
36 eventbox = gtk.EventBox()
37 eventbox.set_flags(gtk.CAN_FOCUS)
38 eventbox.set_size_request(self.width, self.height)
39 eventbox.add(self._socket)
40 eventbox.show_all()
41
42 self.set_canvas(eventbox)
43
44 # Preinitialize Pygame so we can hook its methods.
45 os.environ['SDL_WINDOWID'] = str(self._socket.get_id())
46 pygame.init()
47
48 # Hook certain Pygame functions with GTK equivalents.
49 translator = event.Translator(self, eventbox)
50 translator.hook_pygame()
51
52 # Initialize the Pygame window.
53 self._screen = pygame.display.set_mode((self.width,self.height))
54
55 # Restore the default cursor.
56 self._socket.get_window().set_cursor(None)
57
58 def run_pygame(self, main_fn):
59 # Run the main loop after a short delay. The reason for the delay is that the
60 # Sugar activity is not properly created until after its constructor returns.
61 # If the Pygame main loop is called from the activity constructor, the
62 # constructor never returns and the activity freezes.
63 gobject.idle_add(self._run_pygame_cb, main_fn)
64
65 def _run_pygame_cb(self, main_fn):
66 # Run the Pygame main loop.
67 main_fn()
68 return False
69
70 def get_pygame_widget(self):
71 return self._socket
72
73 def get_pygame_screen(self):
74 return self._screen
75
  
2121 self._socket = gtk.Socket()
2222 self.add(self._socket)
2323 self.show_all()
24
24
2525 def run_pygame(self, main_fn):
26 assert not pygame.display.get_init(), "PygameCanvas.run_pygame can only be called once."
27
2826 # Run the main loop after a short delay. The reason for the delay is that the
2927 # Sugar activity is not properly created until after its constructor returns.
3028 # If the Pygame main loop is called from the activity constructor, the
3030 gobject.idle_add(self._run_pygame_cb, main_fn)
3131
3232 def _run_pygame_cb(self, main_fn):
33 assert pygame.display.get_surface() is None, "PygameCanvas.run_pygame can only be called once."
34
3335 # Preinitialize Pygame with the X window ID.
36 assert pygame.display.get_init() == False, "Pygame must not be initialized before calling PygameCanvas.run_pygame."
3437 os.environ['SDL_WINDOWID'] = str(self._socket.get_id())
3538 pygame.init()
36
39
3740 # Restore the default cursor.
3841 self._socket.get_window().set_cursor(None)
3942
  
9191 pygame.event.post(pygame.event.Event(pygame.VIDEOEXPOSE))
9292 return True
9393
94 def _resize_cb( self, activity, event ):
95 #evt = pygame.event.Event(eventwrap.pygame.VIDEORESIZE,
96 # size=(event.width,event.height), width=event.width, height=event.height))
97 #pygame.event.post(evt)
94 def _resize_cb(self, widget, event):
95 evt = pygame.event.Event(pygame.event.VIDEORESIZE,
96 size=(event.width,event.height), width=event.width, height=event.height)
97 pygame.event.post(evt)
9898 return False # continue processing
9999
100100 def _quit_cb(self, data=None):
  
22
33import sys
44import gtk
5import pygame
56
67import sugar.activity.activity
78import sugar.graphics.toolbutton
89
910sys.path.append('..') # Import sugargame package from top directory.
10import sugargame.activity
11import sugargame.canvas
1112
1213import TestGame
1314
14class TestActivity(sugargame.activity.PygameActivity):
15class TestActivity(sugar.activity.activity.Activity):
1516 def __init__(self, handle):
1617 super(TestActivity, self).__init__(handle)
1718
18 self.metadata['mime_type'] = 'application/x-physics-activity'
19
20 self._resume_path = None
21
22 self.build_toolbar()
23 self.build_canvas()
24
2519 self.paused = False
2620
2721 # Create the game instance.
28 self.game = TestGame.TestGame(self.get_pygame_screen())
29
30 # If resuming from the Journal, load the data.
31 if self._resume_path:
32 self.read_file(self._resume_path)
22 self.game = TestGame.TestGame()
3323
34 # Start the main loop running.
35 self.run_pygame(self.game.run)
24 # Build the activity toolbar.
25 self.build_toolbar()
26
27 # Build the Pygame canvas.
28 self._canvas = sugargame.canvas.PygameCanvas(self)
29 # Note that set_canvas implicitly calls read_file when resuming from the Journal.
30 self.set_canvas(self._canvas)
3631
32 # Start the game running.
33 self._canvas.run_pygame(self.game.run)
34
3735 def build_toolbar(self):
3836 stop_play = sugar.graphics.toolbutton.ToolButton('media-playback-stop')
3937 stop_play.set_tooltip(_("Stop"))
6262 button.set_tooltip(_("Stop"))
6363
6464 def read_file(self, file_path):
65 # Read file is called before the constructor returns when game is not yet valid.
66 # Caching the file path seems to work in this specific instance.
67 if not self.game:
68 self._resume_path = file_path
69 else:
70 self.game.read_file(file_path)
65 self.game.read_file(file_path)
7166
7267 def write_file(self, file_path):
7368 self.game.write_file(file_path)
  
33import gtk
44
55class TestGame:
6 def __init__(self, screen):
7 self.screen = screen
8
9 # Set up a clock for managing the framerate.
6 def __init__(self):
7 # Set up a clock for managing the frame rate.
108 self.clock = pygame.time.Clock()
119
1210 # Set up a font for rendering.
11 pygame.font.init()
1312 self.font = pygame.font.Font(None, 24)
1413
1514 def set_paused(self, paused):
2525 # The main game loop.
2626 def run(self):
2727 self.running = True
28
29 screen = pygame.display.get_surface()
2830
2931 while self.running:
3032 # Pump GTK messages.
3535
3636 # Pump PyGame messages.
3737 for event in pygame.event.get():
38 pass
39
38 if event.type == pygame.QUIT:
39 return
40
4041 # Clear Display
41 self.screen.fill((255,255,255)) #255 for white
42 screen.fill((255,255,255)) #255 for white
4243
4344 # Flip Display
4445 pygame.display.flip()
5151# ./TestGame.py
5252def main():
5353 pygame.init()
54 pygame.display.init()
55 width, height = pygame.display.list_modes()[0]
56 screen = pygame.display.set_mode((width,height))
57 game = TestGame(screen)
54 pygame.display.set_mode((0, 0), pygame.RESIZABLE)
55 game = TestGame()
5856 game.run()
5957
6058if __name__ == '__main__':