Intern type strings for faster comparison during action running.
[python-bulletml.git] / bulletml-runner
1 #!/usr/bin/env python
2
3 import os
4 import sys
5 import time
6
7 import pygame
8
9 import bulletml
10 import bulletml.bulletyaml
11
12 try:
13 import yaml
14 except ImportError:
15 yaml = None
16
17 try:
18 import psyco
19 except ImportError:
20 pass
21 else:
22 psyco.full()
23
24 def main(argv):
25 if not argv:
26 raise SystemExit("Usage: %s filename ..." % sys.argv[0])
27
28 pygame.display.init()
29 screen = pygame.display.set_mode([600, 600], pygame.DOUBLEBUF)
30 bullet = pygame.Surface([3, 3])
31 bullet.fill([255, 0, 0])
32 clock = pygame.time.Clock()
33 target = bulletml.Bullet()
34
35 file_idx = 0
36
37 while True:
38 filename = argv[file_idx % len(argv)]
39 doc = bulletml.BulletML.FromDocument(open(filename, "rU"))
40 source = bulletml.Bullet.FromDocument(
41 doc, x=150, y=150, target=target, rank=0.5)
42
43 active = set([source])
44 source.vanished = True
45 print filename
46 print " Loaded %d top-level actions." % len(source._actions)
47 frames = 0
48 total = 0
49 paused = False
50 newfile = False
51
52 pygame.display.set_caption(os.path.basename(filename))
53
54 while active and not newfile:
55 go = False
56
57 for event in pygame.event.get():
58 if event.type == pygame.QUIT:
59 raise SystemExit
60 elif event.type == pygame.KEYDOWN:
61 if event.key == pygame.K_SPACE:
62 paused ^= True
63 elif event.key == pygame.K_RIGHT:
64 go = True
65 elif event.key == pygame.K_PAGEUP:
66 file_idx -= 1
67 newfile = True
68 elif event.key == pygame.K_PAGEDOWN:
69 file_idx += 1
70 newfile = True
71 elif event.key == pygame.K_RETURN:
72 newfile = True
73 elif event.key == pygame.K_s:
74 actions = [act([], 0.5) for act in doc.actions]
75 source = bulletml.Bullet(
76 x=150, y=150, target=target,
77 actions=actions, rank=0.5)
78 source.vanished = True
79 active.add(source)
80 target.x, target.y = pygame.mouse.get_pos()
81 target.x /= 2
82 target.y /= 2
83
84 if not paused or go:
85
86 start = time.time()
87 count = len(active)
88 for obj in list(active):
89 new = obj.step()
90 total += len(new)
91 active.update(new)
92 if (obj.finished
93 or not (-50 < obj.x < 350)
94 or not (-50 < obj.y < 350)):
95 active.remove(obj)
96 elapsed = time.time() - start
97
98 frames += 1
99 if frames % 100 == 0:
100 print " Processing: %04d: %d bullets, %d active." % (
101 frames, total, count)
102 if elapsed:
103 seconds_per_bullet = elapsed / count
104 bullets_per_second = count / elapsed
105 print " %g seconds per bullet (60Hz max: %g)." % (
106 seconds_per_bullet, bullets_per_second / 60)
107
108 screen.fill([0, 0, 0])
109 for obj in active:
110 try:
111 x, y = obj.x, obj.y
112 except AttributeError:
113 pass
114 else:
115 if not obj.vanished:
116 x *= 2
117 y *= 2
118 x -= 1
119 y -= 1
120 screen.blit(bullet, [x, y])
121 clock.tick(60)
122 pygame.display.flip()
123
124 print " Finished: %04d: %d bullets." % (frames, total)
125
126 if __name__ == "__main__":
127 main(sys.argv[1:])