From 29c5681066c25f8068c49b8bd0a5a1e56e456d4a Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 28 Mar 2011 20:54:04 -0400 Subject: [PATCH] demo improvements --- demo/demo_utils.py | 32 ++++++++++++++++++++++++ demo/random_rectangles.py | 52 +++++++++++++++------------------------ demo/random_triangles.py | 39 +++++++++++++---------------- 3 files changed, 69 insertions(+), 54 deletions(-) create mode 100644 demo/demo_utils.py mode change 100644 => 100755 demo/random_rectangles.py mode change 100644 => 100755 demo/random_triangles.py diff --git a/demo/demo_utils.py b/demo/demo_utils.py new file mode 100644 index 0000000..638825f --- /dev/null +++ b/demo/demo_utils.py @@ -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) diff --git a/demo/random_rectangles.py b/demo/random_rectangles.py old mode 100644 new mode 100755 index b92409f..74bd002 --- a/demo/random_rectangles.py +++ b/demo/random_rectangles.py @@ -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) diff --git a/demo/random_triangles.py b/demo/random_triangles.py old mode 100644 new mode 100755 index b78baea..79e59f7 --- a/demo/random_triangles.py +++ b/demo/random_triangles.py @@ -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)