Commits

Marcus von Appen  committed 322de75

- added new sdl2.ext.Renderer.scale attribute, which denotes the horizontal
and vertical drawing scale
- added new sdl2.ext.point_on_line() function to test, if a point lies on a
line segment
- added unit tests for sdl2.ext.Renderer.draw_rect()

  • Participants
  • Parent commits c1c58d2

Comments (0)

Files changed (6)

 	$(top_srcdir)/doc/modules \
 	$(top_srcdir)/examples
 
-INTERPRETERS = python2.7 python3.2 python3.3 pypy
+INTERPRETERS = python2.7 python3.2 python3.3 python3.4 pypy
 
 all: clean build
 

File doc/modules/sdl2ext_algorithms.rst

    y2 : int[,method=liangbarsky]) -> int, int, int, int
 
    Clips a line to a rectangular area.
+
+.. function:: point_on_line(p1 : iterable, p2 : iterable, point : iterable) -> bool
+
+   Checks, if *point*, a two-value tuple, is on the line segment defined by *p1*
+   and *p2*.
+

File doc/modules/sdl2ext_sprite.rst

       * ``SDL_BLENDMODE_ADD`` for additive color blending
       * ``SDL_BLENDMODE_MOD`` for multiplied color blending
 
+   .. attribute:: scale
+   
+      The horizontal and vertical drawing scale as two-value tuple.
+
    .. method:: clear([color=None])
 
       Clears the rendering context with the currently set or passed

File sdl2/ext/algorithms.py

 """Common algorithms."""
+import sys
 
-__all__ = ["liangbarsky", "cohensutherland", "clipline"]
+__all__ = ["liangbarsky", "cohensutherland", "clipline", "point_on_line"]
 
 
 def cohensutherland(left, top, right, bottom, x1, y1, x2, y2):
 
 clipline = lambda l, t, r, b, x1, y1, x2, y2, method = liangbarsky: \
     method(l, t, r, b, x1, y1, x2, y2)
+
+
+def point_on_line(p1, p2, point):
+    """Checks, if point is on the line segment [p1, p2]."""
+    x1, y1 = p1
+    x2, y2 = p2
+    px, py = point
+    det = (py - y1) * (x2 - x1) - (px - x1) * (y2 - y1)
+    if abs(det) > sys.float_info.epsilon:
+        return False
+    return (min(x1, x2) <= px <= max(x1, x2) and
+            min(y1, y2) <= py <= max(y1, y2))

File sdl2/ext/sprite.py

 """Sprite, texture and pixel surface routines."""
 import abc
-from ctypes import byref, cast, POINTER, c_int
+from ctypes import byref, cast, POINTER, c_int, c_float
 from .common import SDLError
 from .compat import *
 from .color import convert_to_color
         if ret == -1:
             raise SDLError()
 
+    @property
+    def scale(self):
+        """The horizontal and vertical drawing scale."""
+        sx = c_float(0.0)
+        sy = c_float(0.0)
+        render.SDL_RenderGetScale(self.renderer, byref(sx), byref(sy))
+        return sx.value, sy.value
+        
+    @scale.setter
+    def scale(self, value):
+        """The horizontal and vertical drawing scale."""
+        ret = render.SDL_RenderGetScale(self.renderer, value[0], value[1])
+        if ret != 0:
+            raise SDLError()
+
     def clear(self, color=None):
         """Clears the renderer with the currently set or passed color."""
         if color is not None:

File sdl2/test/sdl2ext_sprite_test.py

                     self.assertTrue(view[y][x] in c2,
                                     msg % (x, y, view[y][x], c2))
 
+    def check_lines(self, view, w, h, points, c1, c2):
+        def _online(x, y, pts):
+            for p1, p2 in pts:
+                if sdl2ext.point_on_line(p1, p2, (x, y)):
+                    return True
+            return False
+        msg = "color mismatch at %d,%d: %d not in %s"
+        for y in range(w):
+            for x in range(h):
+                if _online(x, y, points):
+                    self.assertEqual(view[y][x], c1,
+                                     msg % (x, y, view[y][x], c1))
+                else:
+                    self.assertTrue(view[y][x] in c2,
+                                    msg % (x, y, view[y][x], c2))
+            
     def test_SpriteFactory(self):
         factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)
         self.assertIsInstance(factory, sdl2ext.SpriteFactory)
     def test_Renderer_draw_point(self):
         pass
 
-    @unittest.skip("not implemented")
+    @unittest.skipIf(_ISPYPY, "PyPy's ctypes can't do byref(value, offset)")
     def test_Renderer_draw_rect(self):
-        pass
+        surface = SDL_CreateRGBSurface(0, 128, 128, 32, 0, 0, 0, 0).contents
+        sdl2ext.fill(surface, 0x0)
+        renderer = sdl2ext.Renderer(surface)
+        factory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
+        w, h = 32, 32
+        sp = factory.from_color(0xFF0000, (w, h))
+        sp.x, sp.y = 40, 50
+        renderer.draw_rect((sp.x, sp.y, w, h), 0x0000FF)
+        view = sdl2ext.PixelView(surface)
+        self.check_lines(view, 128, 128,
+            [((40, 50), (71, 50)),
+             ((40, 50), (40, 81)),
+             ((40, 81), (71, 81)),
+             ((71, 50), (71, 81))], 0x0000FF, (0x0,))
+        del view
+        sdl2ext.fill(surface, 0x0)
+        renderer.draw_rect([(5, 5, 10, 10), (20, 15, 8, 10)], 0x0000FF)
+        view = sdl2ext.PixelView(surface)
+        self.check_lines(view, 128, 128,
+            [((5, 5), (14, 5)),
+             ((5, 5), (5, 14)),
+             ((5, 14), (14, 14)),
+             ((14, 5), (14, 14)),
+             ((20, 15), (27, 15)),
+             ((20, 15), (20, 24)),
+             ((20, 24), (27, 24)),
+             ((27, 15), (27, 24))], 0x0000FF, (0x0,))
+        del view
 
     @unittest.skipIf(_ISPYPY, "PyPy's ctypes can't do byref(value, offset)")
     def test_Renderer_fill(self):