Ejemplo n.º 1
0
    def test_SDL_EnclosePoints(self):
        pt1 = rect.SDL_Point(0, 0)
        pt2 = rect.SDL_Point(5, 7)
        clip = rect.SDL_Rect(0, 0, 10, 10)
        res = rect.SDL_Rect()
        ret = rect.SDL_EnclosePoints(to_ctypes([pt1, pt2], rect.SDL_Point), 2,
                                     byref(clip), byref(res))
        self.assertTrue(ret)
        self.assertEqual(res, rect.SDL_Rect(0, 0, 6, 8))

        clip = rect.SDL_Rect(-10, -10, 3, 3)
        ret = rect.SDL_EnclosePoints(to_ctypes([pt1, pt2], rect.SDL_Point), 2,
                                     byref(clip), byref(res))
        self.assertFalse(ret)
        self.assertNotEqual(res, rect.SDL_Rect(0, 0, 0, 0))

        ret = rect.SDL_EnclosePoints(to_ctypes([pt1, pt2], rect.SDL_Point), 2,
                                     None, byref(res))
        self.assertTrue(ret)
        self.assertEqual(res, rect.SDL_Rect(0, 0, 6, 8))

        ret = rect.SDL_EnclosePoints(None, 0, None, byref(res))
        self.assertFalse(ret)
        self.assertNotEqual(res, rect.SDL_Rect())

        self.assertRaises(TypeError, rect.SDL_EnclosePoints, None, None)
        self.assertRaises(TypeError, rect.SDL_EnclosePoints, "Test", None)
        self.assertRaises(TypeError, rect.SDL_EnclosePoints, (1, 2, 3), None)
        self.assertRaises(TypeError, rect.SDL_EnclosePoints, (None,), None)
Ejemplo n.º 2
0
 def test_SDL_Point__ne__(self):
     assert not rect.SDL_Point() != rect.SDL_Point()
     coords = [(0, 0), (10, 0), (0, 10), (12, 10), (7, 10)]
     for x1, y1 in coords:
         for x2, y2 in coords:
             notequal = rect.SDL_Point(x1, y1) != rect.SDL_Point(x2, y2)
             assert notequal if (x1 != x2 or y1 != y2) else not notequal
Ejemplo n.º 3
0
 def test_SDL_Point(self):
     pt = rect.SDL_Point()
     self.assertEqual((pt.x, pt.y), (0, 0))
     for x in range(-100, 100):
         for y in range(-100, 100):
             pt = rect.SDL_Point(x, y)
             self.assertEqual((pt.x, pt.y), (x, y))
Ejemplo n.º 4
0
def test_SDL_EnclosePoints():
    tests = [
        [rect.SDL_Rect(0, 0, 10, 10), SDL_TRUE, (0, 0, 6, 8)],
        [rect.SDL_Rect(-10, -10, 3, 3), SDL_FALSE, (0, 0, 0, 0)],
        [None, SDL_TRUE, (0, 0, 6, 8)],
    ]
    pt1, pt2 = [rect.SDL_Point(0, 0), rect.SDL_Point(5, 7)]
    points = to_ctypes([pt1, pt2], rect.SDL_Point)
    res = rect.SDL_Rect()
    for clip, expected_ret, expected_rect in tests:
        clip_p = byref(clip) if isinstance(clip, rect.SDL_Rect) else None
        ret = rect.SDL_EnclosePoints(points, 2, clip_p, byref(res))
        assert ret == expected_ret
        r = rect.SDL_Rect(*expected_rect)
        assert res == r if ret == SDL_TRUE else res != r
    # Test with no points
    ret = rect.SDL_EnclosePoints(None, 0, None, byref(res))
    assert not ret
    assert res != rect.SDL_Rect()
    # Test expceptions
    with pytest.raises(TypeError):
        rect.SDL_EnclosePoints(None, None)
    with pytest.raises(TypeError):
        rect.SDL_EnclosePoints("Test", None)
    with pytest.raises(TypeError):
        rect.SDL_EnclosePoints((1, 2, 3), None)
    with pytest.raises(TypeError):
        rect.SDL_EnclosePoints((None, ), None)
Ejemplo n.º 5
0
 def test_SDL_Point__eq__(self):
     assert rect.SDL_Point() == rect.SDL_Point()
     coords = [(0, 0), (10, 0), (0, 10), (12, 10), (7, 10)]
     for x1, y1 in coords:
         for x2, y2 in coords:
             equal = rect.SDL_FPoint(x1, y1) == rect.SDL_FPoint(x2, y2)
             assert equal if (x1 == x2 and y1 == y2) else not equal
Ejemplo n.º 6
0
 def test_SDL_Point(self):
     pt = rect.SDL_Point()
     assert (pt.x, pt.y) == (0, 0)
     for i in range(0, 100):
         x = random.randint(-1000, 1000)
         y = random.randint(-1000, 1000)
         pt = rect.SDL_Point(x, y)
         assert (pt.x, pt.y) == (x, y)
Ejemplo n.º 7
0
def test_SDL_PointInRect():
    r = rect.SDL_Rect(0, 0, 10, 10)
    inside = [(0, 0), (4, 2)]
    outside = [(10, 10), (10, 3), (3, 10), (-1, -3)]
    for x, y in inside:
        p = rect.SDL_Point(x, y)
        assert rect.SDL_PointInRect(p, r)
    for x, y in outside:
        p = rect.SDL_Point(x, y)
        assert not rect.SDL_PointInRect(p, r)
Ejemplo n.º 8
0
    def test_SDL_Point__repr__(self):
        pt = rect.SDL_Point()
        pt2 = eval("rect.%s" % repr(pt))
        self.assertEqual(pt, pt2)
        self.assertEqual((pt.x, pt.y), (pt2.x, pt2.y))

        pt = rect.SDL_Point(10, 12)
        pt2 = eval("rect.%s" % repr(pt))
        self.assertEqual(pt, pt2)
        self.assertEqual((pt.x, pt.y), (pt2.x, pt2.y))
Ejemplo n.º 9
0
 def test_SDL_PointInRect(self):
     r1 = rect.SDL_Rect(0, 0, 10, 10)
     p = rect.SDL_Point(0, 0)
     self.assertTrue(rect.SDL_PointInRect(p, r1))
     p = rect.SDL_Point(10, 10)
     self.assertFalse(rect.SDL_PointInRect(p, r1))
     p = rect.SDL_Point(10, 3)
     self.assertFalse(rect.SDL_PointInRect(p, r1))
     p = rect.SDL_Point(3, 10)
     self.assertFalse(rect.SDL_PointInRect(p, r1))
     p = rect.SDL_Point(4, 2)
     self.assertTrue(rect.SDL_PointInRect(p, r1))
Ejemplo n.º 10
0
 def test_SDL_Point__copy__(self):
     pt = rect.SDL_Point()
     pt2 = copy.copy(pt)
     assert pt == pt2
     assert (pt.x, pt.y) == (pt2.x, pt2.y)
     pt2.x = 7
     pt2.y = 9
     pt3 = copy.copy(pt2)
     assert pt != pt2
     assert pt3 == pt2
Ejemplo n.º 11
0
    def test_SDL_Point__copy__(self):
        pt = rect.SDL_Point()
        pt2 = copy.copy(pt)
        self.assertEqual(pt, pt2)
        self.assertEqual((pt.x, pt.y), (pt2.x, pt2.y))

        pt2.x = 7
        pt2.y = 9

        pt3 = copy.copy(pt2)
        self.assertNotEqual(pt, pt2)
        self.assertEqual(pt3, pt2)
Ejemplo n.º 12
0
    def test_SDL_Point_y(self):
        pt = rect.SDL_Point()

        def sety(point, val):
            point.y = val

        for x in range(-1000, 1000):
            pt.y = x
            self.assertEqual((pt.x, pt.y), (0, x))

        self.assertRaises(TypeError, sety, pt, 10.4)
        self.assertRaises(TypeError, sety, pt, "Test")
        self.assertRaises(TypeError, sety, pt, None)
Ejemplo n.º 13
0
 def test_SDL_Point_xy(self):
     pt = rect.SDL_Point()
     for i in range(0, 50):
         x = random.randint(-1000, 1000)
         y = random.randint(-1000, 1000)
         pt.x = x
         pt.y = y
         assert (pt.x, pt.y) == (x, y)
     with pytest.raises(TypeError):
         pt.x = 10.4
     with pytest.raises(TypeError):
         pt.y = 10.4
     with pytest.raises(TypeError):
         pt.x = "point"
     with pytest.raises(TypeError):
         pt.y = "point"
     with pytest.raises(TypeError):
         pt.x = None
     with pytest.raises(TypeError):
         pt.y = None
Ejemplo n.º 14
0
 def _render_image(self, nd, w, image_scale, x, y):
     scale_x, scale_y = nd.relative_scale
     n_id = nd.node_id
     if n_id not in self._sprites or n_id not in self._texts \
           or self._sprites[n_id].index != nd.index \
           or self._sprites[n_id].scale != image_scale \
           or self._sprites[n_id].name != nd.image:
         self._load_sprite(nd, image_scale, w)
     sprite = self._sprites[n_id].sprite
     rel_pos = nd.relative_pos
     self._rect.x = x + int(w * rel_pos.x * self._zoom + 0.5)
     self._rect.y = y + int(w * rel_pos.y * self._zoom + 0.5)
     self._rect.w, self._rect.h = (int(sprite.size[0] * scale_x + 0.5),
                                   int(sprite.size[1] * scale_y + 0.5))
     rot_center = nd.rotation_center
     center = rect.SDL_Point(
         int(rot_center.x * w * scale_x * self._zoom + 0.5),
         int(rot_center.y * w * scale_y * self._zoom + 0.5))
     if self._rcopy(self.renderer, sprite.texture, None, self._rect,
                    nd.relative_angle, center, sprite.flip) == -1:
         raise sdl2.ext.common.SDLError()
Ejemplo n.º 15
0
 def _render_text(self, nd, w, image_scale, x, y):
     if not nd.text:
         return
     scale_x, scale_y = nd.relative_scale
     n_id = nd.node_id
     if n_id not in self._sprites or n_id not in self._texts \
           or nd.hashkey != self._texts[n_id] \
           or self._sprites[n_id].scale != image_scale:
         self._load_sprite(nd, image_scale)
         self._texts[n_id] = nd.hashkey
     sprite = self._sprites[n_id].sprite
     rel_pos = nd.relative_pos
     self._rect.x = x + int(w * rel_pos.x * self._zoom + 0.5)
     self._rect.y = y + int(w * rel_pos.y * self._zoom + 0.5)
     self._rect.w, self._rect.h = sprite.size
     rot_center = nd.rotation_center
     center = rect.SDL_Point(
         int(rot_center.x * w * scale_x * self._zoom + 0.5),
         int(rot_center.y * w * scale_y * self._zoom + 0.5))
     if self._rcopy(self.renderer, sprite.texture, None, self._rect,
                    nd.relative_angle, center, sprite.flip) == -1:
         raise sdl2.ext.common.SDLError()
Ejemplo n.º 16
0
    def test_SDL_Point__ne__(self):
        self.assertFalse(rect.SDL_Point() != rect.SDL_Point())
        self.assertFalse(rect.SDL_Point(0, 0) != rect.SDL_Point(0, 0))
        self.assertFalse(rect.SDL_Point(10, 0) != rect.SDL_Point(10, 0))
        self.assertFalse(rect.SDL_Point(0, 10) != rect.SDL_Point(0, 10))
        self.assertFalse(rect.SDL_Point(12, 10) != rect.SDL_Point(12, 10))

        self.assertTrue(rect.SDL_Point(0, 0) != rect.SDL_Point(0, 1))
        self.assertTrue(rect.SDL_Point(0, 0) != rect.SDL_Point(1, 0))
        self.assertTrue(rect.SDL_Point(0, 0) != rect.SDL_Point(1, 1))

        self.assertTrue(rect.SDL_Point(10, 10) != rect.SDL_Point(10, 0))
        self.assertTrue(rect.SDL_Point(7, 10) != rect.SDL_Point(0, 10))
        self.assertTrue(rect.SDL_Point(12, 10) != rect.SDL_Point(12, 11))
Ejemplo n.º 17
0
 def test_SDL_Point__repr__(self):
     pt = rect.SDL_Point(10, 12)
     pt2 = eval("rect.%s" % repr(pt))
     assert pt == pt2
     assert (pt.x, pt.y) == (pt2.x, pt2.y)