Commits

Jon Nordby committed e1311d6

run.py: Put the pycessing setup code in a function

Allows it to be used as an entrypoint from other Python files.

Also use "execfile" instead of "exec". In the future it could
allow more fine-grained control over what is exposed as
PyCessing API, to make it easier to maintain compatibility.

  • Participants
  • Parent commits 8ba96c9

Comments (0)

Files changed (1)

File pycessing/run.py

 from sounds import Sound
 from typo import ScreenText
 
-infile = open(sys.argv[1])
-filename = os.path.basename(sys.argv[1])
-
-pygame.init()
-pygame.fastevent.init()
-
-pycessing = Pycess(filename)
-drawing = Drawing()
-
-project_dir = sys.argv[2].strip()
-if len(project_dir) > 0:
-    os.chdir(project_dir)
-else:
-	project_dir = os.path.dirname(sys.argv[1])
-
-sys.path.append(project_dir)
 
 def setup():
     pass
         pycessing.screen.set_at((x, y), (r, g, b))
 
 
-images.PROJECT_DIR = project_dir
-typo.PROJECT_DIR = project_dir
 
-exec(infile)
+def main(cess_file, project_dir=None):
+    """Initialize engine, execute .cess file and start """
+
+    global pycessing
+    global drawing
 
-setup()
+    infile = open(cess_file)
+    filename = os.path.basename(cess_file)
 
-if not(pycessing._setup_done):
-    pycessing.size()
+    pygame.init()
+    pygame.fastevent.init()
 
-drawing.setSurface(pycessing.screen)
-drawing.setBackground(127, 127, 127)
-images.SCREEN = pycessing.screen
-images.CTX = drawing.ctx
-typo.SCREEN = pycessing.screen
+    pycessing = Pycess(filename)
+    drawing = Drawing()
 
-while 1:
-    pycessing.doEvents(pygame.fastevent.get())
-    draw()
-    pycessing.clock.tick(pycessing.framerate)
-    drawing._blitToScreen()
-    pygame.display.flip()
-    if(pycessing.noLoop):
-        while 1:
-            events = pygame.event.get()
-            for event in events:
-                if event.type == pygame.QUIT:
-                    sys.exit(0)
-            pycessing.clock.tick(30)
+    if project_dir:
+        os.chdir(project_dir)
+    else:
+	    project_dir = os.path.dirname(cess_file)
+
+    sys.path.append(project_dir)
+
+    images.PROJECT_DIR = project_dir
+    typo.PROJECT_DIR = project_dir
+
+    execfile(cess_file, globals())
+
+    setup()
+
+    if not(pycessing._setup_done):
+        pycessing.size()
+
+    drawing.setSurface(pycessing.screen)
+    drawing.setBackground(127, 127, 127)
+    images.SCREEN = pycessing.screen
+    images.CTX = drawing.ctx
+    typo.SCREEN = pycessing.screen
+
+    while 1:
+        pycessing.doEvents(pygame.fastevent.get())
+        draw()
+        pycessing.clock.tick(pycessing.framerate)
+        drawing._blitToScreen()
+        pygame.display.flip()
+        if(pycessing.noLoop):
+            while 1:
+                events = pygame.event.get()
+                for event in events:
+                    if event.type == pygame.QUIT:
+                        sys.exit(0)
+                pycessing.clock.tick(30)
+            
+if __name__ == '__main__':
+    main(sys.argv[1], sys.argv[2].strip())