Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWade Brainerd <wadetb@gmail.com>2009-10-30 02:29:42 (GMT)
committer Wade Brainerd <wadetb@gmail.com>2009-10-30 02:29:42 (GMT)
commit802387fcbca231fb54fa5caad3affda942b5bc39 (patch)
treee6dcd14a45a6f18b7ef849b828f815c59c18aad4
parente245a2fbca067dee091ae27c78cad063bd3ffbec (diff)
Update to newer sugargame.
-rw-r--r--activity.py24
-rw-r--r--physics.py35
-rw-r--r--sugargame/activity.py75
-rw-r--r--sugargame/canvas.py55
-rw-r--r--sugargame/event.py8
5 files changed, 89 insertions, 108 deletions
diff --git a/activity.py b/activity.py
index be85085..c689147 100644
--- a/activity.py
+++ b/activity.py
@@ -11,25 +11,26 @@ import sugargame.activity
import physics
import tools
-class PhysicsActivity(sugargame.activity.PygameActivity):
+class PhysicsActivity(activity.Activity):
def __init__(self, handle):
super(PhysicsActivity, self).__init__(handle)
self.metadata['mime_type'] = 'application/x-physics-activity'
-
- self._resume_path = None
+
+ self.game = physics.PhysicsGame()
self.build_toolbar()
self.build_canvas()
- self.game = physics.PhysicsGame(self.get_pygame_screen())
- if self._resume_path:
- self.read_file(self._resume_path)
- self.run_pygame(self.run)
+ self.canvas.run_pygame(self.run)
def run(self):
self.game.run()
-
+
+ def build_canvas(self):
+ self._canvas = sugargame.canvas.PygameCanvas(self)
+ self.set_canvas(self._canvas)
+
def build_toolbar(self):
# make a toolbox
toolbox = activity.ActivityToolbox(self)
@@ -96,12 +97,7 @@ class PhysicsActivity(sugargame.activity.PygameActivity):
self.game.set_tool(self.radioList[button])
def read_file(self, file_path):
- # Read file is called before the constructor returns when game is not yet valid.
- # Caching the file path seems to work in this specific instance.
- if not self.game:
- self._resume_path = file_path
- else:
- self.game.read_file(file_path)
+ self.game.read_file(file_path)
def write_file(self, file_path):
self.game.write_file(file_path)
diff --git a/physics.py b/physics.py
index da30c7b..748366a 100644
--- a/physics.py
+++ b/physics.py
@@ -30,23 +30,12 @@ from helpers import *
import gtk
class PhysicsGame:
- def __init__(self,screen):
- self.screen = screen
- # get everything set up
- self.clock = pygame.time.Clock()
- self.font = pygame.font.Font(None, 24) # font object
+ def __init__(self):
# create the name --> instance map for components
self.toolList = {}
for c in tools.allTools:
self.toolList[c.name] = c(self)
self.currentTool = self.toolList[tools.allTools[0].name]
- # set up the world (instance of Elements)
- self.box2d = box2d
- self.world = elements.Elements(self.screen.get_size())
- self.world.renderer.set_surface(self.screen)
-
- # set up static environment
- self.world.add.ground()
def stop_start_toggle(self):
self.world.run_physics = not self.world.run_physics
@@ -62,6 +51,20 @@ class PhysicsGame:
self.world.json_load(file_path)
def run(self):
+ self.screen = pygame.display.get_surface()
+
+ # get everything set up
+ self.clock = pygame.time.Clock()
+ self.font = pygame.font.Font(None, 24) # font object
+
+ # set up the world (instance of Elements)
+ self.box2d = box2d
+ self.world = elements.Elements(self.screen.get_size())
+ self.world.renderer.set_surface(self.screen)
+
+ # set up static environment
+ self.world.add.ground()
+
self.running = True
while self.running:
# Pump GTK messages.
@@ -70,8 +73,10 @@ class PhysicsGame:
# Pump PyGame messages.
for event in pygame.event.get():
+ if event.type == pygame.QUIT:
+ return
self.currentTool.handleEvents(event)
-
+
# Clear Display
self.screen.fill((255,255,255)) #255 for white
if self.world.run_physics:
@@ -102,8 +107,8 @@ def main():
pygame.init()
pygame.display.init()
width, height = pygame.display.list_modes()[0]
- screen = pygame.display.set_mode((width,height))
- game = PhysicsGame(screen)
+ pygame.display.set_mode((width,height))
+ game = PhysicsGame()
game.run()
# make sure that main get's called
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):