From 0bd9c80cdb0784f269715e9cd022c88f113bf186 Mon Sep 17 00:00:00 2001 From: James Turk Date: Tue, 15 Mar 2011 23:26:07 -0400 Subject: [PATCH] draw points, rects, and filled rects --- csdl/__init__.py | 28 ++++++++++++++++++++++++++-- demo/random_rectangles.py | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 demo/random_rectangles.py diff --git a/csdl/__init__.py b/csdl/__init__.py index c0196b0..7c24195 100644 --- a/csdl/__init__.py +++ b/csdl/__init__.py @@ -156,11 +156,35 @@ class Renderer(object): errcheck(_SDL.SDL_RenderDrawLine(self._renderer, x1, y1, x2, y2)) def draw_lines(self, points): - T = Point*len(points) - _sdl_points = T(*[Point(*pt) for pt in points]) + PtArray = Point*len(points) + _sdl_points = PtArray(*[Point(*pt) for pt in points]) errcheck(_SDL.SDL_RenderDrawLines(self._renderer, _sdl_points, len(points))) + def draw_point(self, x, y): + errcheck(_SDL.SDL_RenderDrawPoint(self._renderer, x, y)) + + def draw_points(self, points): + PtArray = Point*len(points) + _sdl_points = PtArray(*[Point(*pt) for pt in points]) + errcheck(_SDL.SDL_RenderDrawPoints(self._renderer, _sdl_points, + len(points))) + + def draw_rect(self, rect): + _SDL.SDL_RenderDrawRect(self._renderer, ctypes.pointer(rect)) + + def draw_rects(self, rects): + RectArray = Rect*len(rects) + rects = RectArray(*rects) + _SDL.SDL_RenderDrawRects(self._renderer, rects, len(rects)) + + def fill_rect(self, rect): + _SDL.SDL_RenderFillRect(self._renderer, ctypes.pointer(rect)) + + def fill_rects(self, rects): + RectArray = Rect*len(rects) + rects = RectArray(*rects) + _SDL.SDL_RenderFillRects(self._renderer, rects, len(rects)) _SDL.SDL_GetWindowTitle.restype = ctypes.c_char_p diff --git a/demo/random_rectangles.py b/demo/random_rectangles.py new file mode 100644 index 0000000..fd9f8bb --- /dev/null +++ b/demo/random_rectangles.py @@ -0,0 +1,26 @@ +from csdl import init, Window, SDL_INIT_EVERYTHING, Rect +import random +import time + +def main(): + init(SDL_INIT_EVERYTHING) + window = Window('test', 100, 100, 512, 512, 0) + + 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)) + 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()