demo improvements

This commit is contained in:
James Turk 2011-03-28 20:54:04 -04:00
parent 181dea87f1
commit 29c5681066
3 changed files with 69 additions and 54 deletions

32
demo/demo_utils.py Normal file
View File

@ -0,0 +1,32 @@
from csdl import init, InitFlags
from csdl.video import Window
from csdl.events import EventType, poll_event, WindowEventType
import time
def simple_timed_loop(draw_func):
init(InitFlags.EVERYTHING)
window = Window('timed sdloppy demo', 100, 100, 512, 512)
running = True
start_time = time.time()
frames = 0
while running:
frames += 1
if frames == 1000:
print('FPS: {0:.2f}'.format(frames/(time.time()-start_time)))
frames = 0
start_time = time.time()
# event loop
while True:
event = poll_event()
if not event:
break
elif event.type == EventType.QUIT:
running = False
elif event.type == EventType.WINDOWEVENT:
if event.window.event == WindowEventType.CLOSE:
running = False
draw_func(window)

52
demo/random_rectangles.py Normal file → Executable file
View File

@ -1,35 +1,23 @@
from csdl import init, INIT
from csdl.video import Window, Rect
from csdl.events import EventType, poll_event
import random
import time
from csdl.video import Rect
from demo_utils import simple_timed_loop
def main():
init(INIT.EVERYTHING)
window = Window('test', 100, 100, 512, 512, 0)
def draw(window):
window.renderer.set_draw_color(0,0,0,255)
window.renderer.clear()
window.renderer.set_draw_color(255,0,0,255)
for i in xrange(20):
window.renderer.set_draw_color(random.randint(0,255),
random.randint(0,255),
random.randint(0,255))
r = Rect(random.randint(0,512), random.randint(0, 512),
random.randint(0,300), random.randint(0, 300))
s = Rect(random.randint(0,512), random.randint(0, 512),
random.randint(0,300), random.randint(0, 300))
t = Rect(random.randint(0,512), random.randint(0, 512),
random.randint(0,300), random.randint(0, 300))
window.renderer.fill_rects([r,s,t])
window.renderer.present()
while True:
while True:
event = poll_event()
if not event:
break
else:
print repr(EventType.from_int(event.type))
window.renderer.set_draw_color(0,0,0,255)
window.renderer.clear()
window.renderer.set_draw_color(255,0,0,255)
for i in xrange(20):
window.renderer.set_draw_color(random.randint(0,255),
random.randint(0,255),
random.randint(0,255))
r = Rect(random.randint(0,512), random.randint(0, 512),
random.randint(0,300), random.randint(0, 300))
s = Rect(random.randint(0,512), random.randint(0, 512),
random.randint(0,300), random.randint(0, 300))
t = Rect(random.randint(0,512), random.randint(0, 512),
random.randint(0,300), random.randint(0, 300))
window.renderer.fill_rects([r,s,t])
window.renderer.present()
time.sleep(.001)
main()
if __name__ == '__main__':
simple_timed_loop(draw)

39
demo/random_triangles.py Normal file → Executable file
View File

@ -1,25 +1,20 @@
from csdl import init, INIT
from csdl.video import Window
#!/usr/bin/env python
from demo_utils import simple_timed_loop
import random
import time
def main():
init(INIT.EVERYTHING)
window = Window('test', 100, 100, 512, 512, 0)
def draw(window):
window.renderer.set_draw_color(0,0,0,255)
window.renderer.clear()
window.renderer.set_draw_color(255,0,0,255)
for i in xrange(20):
window.renderer.set_draw_color(random.randint(0,255),
random.randint(0,255),
random.randint(0,255))
p1 = (random.randint(0,512), random.randint(0, 512))
p2 = (random.randint(0,512), random.randint(0, 512))
p3 = (random.randint(0,512), random.randint(0, 512))
window.renderer.draw_lines([p1, p2, p3, p1])
window.renderer.present()
while True:
window.renderer.set_draw_color(0,0,0,255)
window.renderer.clear()
window.renderer.set_draw_color(255,0,0,255)
for i in xrange(20):
window.renderer.set_draw_color(random.randint(0,255),
random.randint(0,255),
random.randint(0,255))
p1 = (random.randint(0,512), random.randint(0, 512))
p2 = (random.randint(0,512), random.randint(0, 512))
p3 = (random.randint(0,512), random.randint(0, 512))
window.renderer.draw_lines([p1, p2, p3, p1])
window.renderer.present()
time.sleep(.001)
main()
if __name__ == '__main__':
simple_timed_loop(draw)