def test_SDL_SetTextInputRect(self): # TODO: this test is a bit pointless coords = [(0, 0, 0, 0), (-10, -70, 3, 6), (10, 10, 10, 10)] for x, y, w, h in coords: r = rect.SDL_Rect(x, y, w, h) keyboard.SDL_SetTextInputRect(r) keyboard.SDL_SetTextInputRect(rect.SDL_Rect())
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)
def test_SDL_Rect(self): rt = rect.SDL_Rect() self.assertEqual((rt.x, rt.y, rt.w, rt.h), (0, 0, 0, 0)) for x in range(-10, 10): for y in range(-10, 10): for w in range(-10, 10): for h in range(-10, 10): rt = rect.SDL_Rect(x, y, w, h) self.assertEqual((rt.x, rt.y, rt.w, rt.h), (x, y, w, h))
def test_SDL_Rect(self): rt = rect.SDL_Rect() assert (rt.x, rt.y, rt.w, rt.h) == (0, 0, 0, 0) for i in range(0, 50): x = random.randint(-1000, 1000) y = random.randint(-1000, 1000) w = random.randint(-1000, 1000) h = random.randint(-1000, 1000) rt = rect.SDL_Rect(x, y, w, h) assert (rt.x, rt.y, rt.w, rt.h) == (x, y, w, h)
def test_SDL_UnionRect(self): r1 = rect.SDL_Rect(0, 0, 10, 10) r2 = rect.SDL_Rect(20, 20, 10, 10) r3 = rect.SDL_Rect() rect.SDL_UnionRect(r1, r2, byref(r3)) self.assertEqual((r3.x, r3.y, r3.w, r3.h), (0, 0, 30, 30)) r1 = rect.SDL_Rect(0, 0, 0, 0) r2 = rect.SDL_Rect(20, 20, 10, 10) rect.SDL_UnionRect(r1, r2, byref(r3)) self.assertEqual((r3.x, r3.y, r3.w, r3.h), (20, 20, 10, 10)) r1 = rect.SDL_Rect(-200, -4, 450, 33) r2 = rect.SDL_Rect(20, 20, 10, 10) rect.SDL_UnionRect(r1, r2, byref(r3)) self.assertEqual((r3.x, r3.y, r3.w, r3.h), (-200, -4, 450, 34)) r1 = rect.SDL_Rect(0, 0, 15, 16) r2 = rect.SDL_Rect(20, 20, 0, 0) rect.SDL_UnionRect(r1, r2, byref(r3)) self.assertEqual((r3.x, r3.y, r3.w, r3.h), (0, 0, 15, 16)) self.assertRaises((AttributeError, TypeError), rect.SDL_UnionRect, None, None) self.assertRaises((AttributeError, TypeError), rect.SDL_UnionRect, "Test", r2) self.assertRaises((AttributeError, TypeError), rect.SDL_UnionRect, r1, None) self.assertRaises((AttributeError, TypeError), rect.SDL_UnionRect, r1, "Test")
def test_SDL_HasIntersection(): tests = [[(0, 0, 0, 0), (0, 0, 0, 0), SDL_FALSE], [(0, 0, -200, 200), (0, 0, -200, 200), SDL_FALSE], [(0, 0, 10, 10), (-5, 5, 10, 2), SDL_TRUE], [(0, 0, 10, 10), (-5, -5, 10, 2), SDL_FALSE], [(0, 0, 10, 10), (-5, -5, 2, 10), SDL_FALSE], [(0, 0, 10, 10), (-5, -5, 5, 5), SDL_FALSE], [(0, 0, 10, 10), (-5, -5, 6, 6), SDL_TRUE]] for rect1, rect2, expected in tests: r1 = rect.SDL_Rect(*rect1) r2 = rect.SDL_Rect(*rect2) assert rect.SDL_HasIntersection(r1, r2) == expected
def test_SDL_RectEquals(): r1 = rect.SDL_Rect(0, 0, 1, 1) r2 = rect.SDL_Rect(0, 0, 1, 1) assert rect.SDL_RectEquals(r1, r2) r2 = rect.SDL_Rect(-1, 2, 1, 1) assert not rect.SDL_RectEquals(r1, r2) r2 = rect.SDL_Rect(0, 0, 1, 2) assert not rect.SDL_RectEquals(r1, r2) # Test exceptions with pytest.raises(AttributeError): rect.SDL_RectEquals("Test", r2) with pytest.raises(AttributeError): rect.SDL_RectEquals(r1, None)
def test_SDL_RectEquals(self): r1 = rect.SDL_Rect(0, 0, 0, 0) r2 = rect.SDL_Rect(0, 0, 0, 0) self.assertTrue(rect.SDL_RectEquals(r1, r2)) self.assertEqual(r1, r2) r2 = rect.SDL_Rect(-1, 2, 0, 0) self.assertFalse(rect.SDL_RectEquals(r1, r2)) self.assertNotEqual(r1, r2) r2 = rect.SDL_Rect(0, 0, 1, 2) self.assertFalse(rect.SDL_RectEquals(r1, r2)) self.assertNotEqual(r1, r2) self.assertRaises(AttributeError, rect.SDL_RectEquals, "Test", r2) self.assertRaises(AttributeError, rect.SDL_RectEquals, r1, None) self.assertRaises(AttributeError, rect.SDL_RectEquals, r1, "Test")
def test_SDL_IntersectRect(): tests = [[(0, 0, 0, 0), (0, 0, 0, 0), SDL_FALSE, None], [(0, 0, -200, 200), (0, 0, -200, 200), SDL_FALSE, None], [(0, 0, 10, 10), (-5, 5, 10, 2), SDL_TRUE, (0, 5, 5, 2)], [(0, 0, 10, 10), (-5, -5, 10, 2), SDL_FALSE, None], [(0, 0, 10, 10), (-5, -5, 2, 10), SDL_FALSE, None], [(0, 0, 10, 10), (-5, -5, 5, 5), SDL_FALSE, None], [(0, 0, 10, 10), (-5, -5, 6, 6), SDL_TRUE, (0, 0, 1, 1)]] res = rect.SDL_Rect() for rect1, rect2, expected_ret, expected_rect in tests: r1 = rect.SDL_Rect(*rect1) r2 = rect.SDL_Rect(*rect2) ret = rect.SDL_IntersectRect(r1, r2, byref(res)) assert ret == expected_ret if ret == SDL_TRUE: res == rect.SDL_Rect(*expected_rect)
def test_SDL_GetDisplayUsableBounds(self): numdisplays = video.SDL_GetNumVideoDisplays() for index in range(numdisplays): bounds = rect.SDL_Rect() ret = video.SDL_GetDisplayUsableBounds(index, byref(bounds)) assert ret == 0 assert not rect.SDL_RectEmpty(bounds)
def render(self, sprites, x=None, y=None): """Overrides the render method of sdl2.ext.TextureSpriteRenderSystem to use "SDL_RenderCopyEx" instead of "SDL_RenderCopy" to allow sprite rotation: http://wiki.libsdl.org/SDL_RenderCopyEx """ r = rect.SDL_Rect(0, 0, 0, 0) if isiterable(sprites): rcopy = render.SDL_RenderCopyEx renderer = self.sdlrenderer x = x or 0 y = y or 0 for sp in sprites: r.x = x + int(sp.x) r.y = y + int(sp.y) r.w, r.h = sp.size if rcopy(renderer, sp.texture, None, r, sp.angle, None, render.SDL_FLIP_NONE) == -1: raise sdl2.ext.error() else: r.x = sprites.x r.y = sprites.y r.w, r.h = sprites.size if x is not None and y is not None: r.x = x r.y = y render.SDL_RenderCopyEx(self.sdlrenderer, sprites.texture, None, r, sprites.angle, None, render.SDL_FLIP_NONE) render.SDL_RenderPresent(self.sdlrenderer)
def test_SDL_GetDisplayUsableBounds(self): numdisplays = video.SDL_GetNumVideoDisplays() for index in range(numdisplays): bounds = rect.SDL_Rect() ret = video.SDL_GetDisplayUsableBounds(index, byref(bounds)) self.assertEqual(ret, 0) self.assertFalse(rect.SDL_RectEmpty(bounds))
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)
def test_SDL_FillRects(self): rectlist = to_ctypes([ rect.SDL_Rect(0, 0, 0, 0), rect.SDL_Rect(0, 0, 10, 10), rect.SDL_Rect(0, 0, -10, 10), rect.SDL_Rect(0, 0, -10, -10), rect.SDL_Rect(-10, -10, 10, 10), rect.SDL_Rect(10, -10, 10, 10), rect.SDL_Rect(10, 10, 10, 10) ], rect.SDL_Rect) bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32(), Uint32() for fmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt): continue if pixels.SDL_BITSPERPIXEL(fmt) < 8: continue # Skip < 8bpp, SDL_FillRect does not work on those ret = pixels.SDL_PixelFormatEnumToMasks(fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) for w in range(1, 100, 5): for h in range(1, 100, 5): sf = surface.SDL_CreateRGBSurface(0, w, h, bpp, rmask, gmask, bmask, amask) # TODO: check for changed pixels surface.SDL_FillRects(sf, rectlist, 7, 0xff00ff00) surface.SDL_FreeSurface(sf)
def test_SDL_RectEmpty(self): for w in range(-100, 100): for h in range(-100, 100): r = rect.SDL_Rect(0, 0, w, h) if w > 0 and h > 0: self.assertFalse(rect.SDL_RectEmpty(r)) else: self.assertTrue(rect.SDL_RectEmpty(r)) self.assertRaises(AttributeError, rect.SDL_RectEmpty, "Test")
def test_SDL_RectEmpty(): for i in range(0, 50): w = random.randint(-100, 100) h = random.randint(-100, 100) r = rect.SDL_Rect(0, 0, w, h) empty = rect.SDL_RectEmpty(r) assert empty if not (w > 0 and h > 0) else not empty with pytest.raises(AttributeError): rect.SDL_RectEmpty("Test")
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)
def updateRectangle(self, x, y, width, height, data): """new bitmap data. data is a string in the pixel format set up earlier.""" port = rect.SDL_Rect() port.x = x port.y = y port.w = width port.h = height pitch = int(port.w * self.bpp / 8) self._events.append((EV_UPDATE_RECT, (port, data, pitch)))
def test_SDL_IntersectRectAndLine(self): r = rect.SDL_Rect() x1, y1, x2, y2 = c_int(-5), c_int(-5), c_int(5), c_int(5) ret = rect.SDL_IntersectRectAndLine(r, byref(x1), byref(y1), byref(x2), byref(y2)) self.assertFalse(ret) r = rect.SDL_Rect(0, 0, 2, 2) x1, y1, x2, y2 = c_int(-1), c_int(-1), c_int(3), c_int(3) ret = rect.SDL_IntersectRectAndLine(r, byref(x1), byref(y1), byref(x2), byref(y2)) self.assertTrue(ret) self.assertEqual((x1.value, y1.value, x2.value, y2.value), (0, 0, 1, 1)) r = rect.SDL_Rect(-4, -4, 14, 14) x1, y1, x2, y2 = c_int(8), c_int(22), c_int(8), c_int(33) ret = rect.SDL_IntersectRectAndLine(r, byref(x1), byref(y1), byref(x2), byref(y2)) self.assertFalse(ret)
def test_SDL_UpdateWindowSurfaceRects(self): rectlist = (rect.SDL_Rect * 4)(rect.SDL_Rect(), rect.SDL_Rect(10, 10, 10, 10), rect.SDL_Rect(0, 0, 5, 4), rect.SDL_Rect(-5, -5, 6, 2)) rptr = cast(rectlist, POINTER(rect.SDL_Rect)) flags = (video.SDL_WINDOW_BORDERLESS, video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN, video.SDL_WINDOW_RESIZABLE | video.SDL_WINDOW_MINIMIZED) for flag in flags: window = video.SDL_CreateWindow(b"Test", 200, 200, 200, 200, flag) # self.assertRaises(sdl.SDLError, # video.SDL_UpdateWindowSurfaceRects, # window, rectlist) sf = surface.SDL_Surface() video.SDL_GetWindowSurface(window, byref(sf)) ret = video.SDL_UpdateWindowSurfaceRects(window, rptr, 4) assert ret == 0 video.SDL_DestroyWindow(window)
def test_SDL_UnionRect(): tests = [[(0, 0, 10, 10), (20, 20, 10, 10), (0, 0, 30, 30)], [(0, 0, 0, 0), (20, 20, 10, 10), (20, 20, 10, 10)], [(-200, -4, 450, 33), (20, 20, 10, 10), (-200, -4, 450, 34)], [(0, 0, 15, 16), (20, 20, 0, 0), (0, 0, 15, 16)]] out = rect.SDL_Rect() for rect1, rect2, expected in tests: r1 = rect.SDL_Rect(*rect1) r2 = rect.SDL_Rect(*rect2) rect.SDL_UnionRect(r1, r2, byref(out)) assert (out.x, out.y, out.w, out.h) == expected # Test exceptions with pytest.raises((AttributeError, TypeError)): rect.SDL_UnionRect(None, None) with pytest.raises((AttributeError, TypeError)): rect.SDL_UnionRect("Test", r2) with pytest.raises((AttributeError, TypeError)): rect.SDL_UnionRect(r1, None) with pytest.raises((AttributeError, TypeError)): rect.SDL_UnionRect(r1, "Test")
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))
def test_SDL_Rect__copy__(self): rt = rect.SDL_Rect() rt2 = copy.copy(rt) assert rt == rt2 assert (rt.x, rt.y, rt.w, rt.h) == (rt2.x, rt2.y, rt2.w, rt2.h) rt2.x = 5 rt2.y = 33 rt2.w = 17 rt2.w = 212 rt3 = copy.copy(rt2) assert rt != rt2 assert rt3 == rt2
def test_SDL_Rect_h(self): rt = rect.SDL_Rect() def seth(r, val): r.h = val for x in range(-1000, 1000): rt.h = x self.assertEqual((rt.x, rt.y, rt.w, rt.h), (0, 0, 0, x)) self.assertRaises(TypeError, seth, rt, 10.4) self.assertRaises(TypeError, seth, rt, "Test") self.assertRaises(TypeError, seth, rt, None)
def test_SDL_IntersectRectAndLine(): tests = [[(0, 0, 0, 0), (-5, -5, 5, 5), SDL_FALSE, None], [(0, 0, 2, 2), (-1, -1, 3, 3), SDL_TRUE, (0, 0, 1, 1)], [(-4, -4, 14, 14), (8, 22, 8, 33), SDL_FALSE, None]] for rect1, line, expected_ret, expected_coords in tests: r = rect.SDL_Rect(*rect1) x1, y1, x2, y2 = line x1, y1, x2, y2 = c_int(x1), c_int(y1), c_int(x2), c_int(y2) ret = rect.SDL_IntersectRectAndLine(r, byref(x1), byref(y1), byref(x2), byref(y2)) assert ret == expected_ret if ret == SDL_TRUE: assert (x1.value, y1.value, x2.value, y2.value) == expected_coords
def update_screen(self): draw_rect = rect.SDL_Rect(0, 0, 0, 0) h = 0 for y in self.world.in_height(self.offset.y, self.offset.y + self.blocks_in_height): w = 0 for x in self.world.in_width(self.offset.x, self.offset.x + self.blocks_in_width): if not self.world[x][y].dirty and not self.dirty: w += 1 continue draw_rect.x = w * self.BLOCK_SIZE draw_rect.y = h * self.BLOCK_SIZE sdl2.surface.SDL_BlitSurface(self.world[x][y].sprite.surface, None, self.c_surface, draw_rect) if self.world[x][y].pickable: draw_rect.x += self.BLOCK_SIZE // 5 draw_rect.y += self.BLOCK_SIZE // 5 sdl2.surface.SDL_BlitSurface( self.world[x][y].drop_sprite.surface, rect.SDL_Rect(0, 0, self.BLOCK_SIZE // 2, self.BLOCK_SIZE // 2), self.c_surface, draw_rect) if self.world[x][y].dirty and self.DEBUG: self.mark_rect((x, y, x + 1, y + 1)) self.world[x][y].dirty = False w += 1 h += 1
def test_SDL_Rect__copy__(self): rt = rect.SDL_Rect() rt2 = copy.copy(rt) self.assertEqual(rt, rt2) self.assertEqual((rt.x, rt.y, rt.w, rt.h), (rt2.x, rt2.y, rt2.w, rt2.h)) rt2.x = 5 rt2.y = 33 rt2.w = 17 rt2.w = 212 rt3 = copy.copy(rt2) self.assertNotEqual(rt, rt2) self.assertEqual(rt3, rt2)
def render(self, sprite_elements, max_depth): """Overrides the render method of sdl2.ext.TextureSpriteRenderSystem to use "SDL_RenderCopyEx" instead of "SDL_RenderCopy" to allow sprite rotation: http://wiki.libsdl.org/SDL_RenderCopyEx """ # r = rect.SDL_Rect(0, 0, 0, 0) # texture_sprites = sprites[0] # text_sprites = sprites[1] # rcopy = render.SDL_RenderCopyEx # renderer = self.sdlrenderer # x = x or 0 # y = y or 0 # for i in range(Z_ORDER.MAX_DEPTH): # for sprite_element in texture_sprites[i]: # sp = sprite_element.on_draw() # r.x = x + sp.x # r.y = y + sp.y # r.w, r.h = sp.size # # print("DRAW") # # print(type(sprite_element)) # # print(type(sp)) # r.w = int(r.w / sprite_element.scale) # r.h = int(r.h / sprite_element.scale) # # https://wiki.libsdl.org/SDL_RenderCopyEx # if rcopy(renderer, sp.texture, None, r, sp.angle, None, render.SDL_FLIP_NONE) == -1: # raise SDLError() # for text_element in text_sprites[i]: # self.renderer.copy(text_element.value, dstrect = (text.x, text.y, text.value.size[0], text.value.size[1])) r = rect.SDL_Rect(0, 0, 0, 0) rcopy = render.SDL_RenderCopyEx renderer = self.sdlrenderer # count = 0 for i in range(max_depth): for sprite_element in sprite_elements[i]: if not sprite_element.is_on_screen(): continue sp = sprite_element.on_draw() r.x = sp.x - sprite_element.h_w r.y = sp.y - sprite_element.h_h r.w = sprite_element.w r.h = sprite_element.h if rcopy(renderer, sp.texture, None, r, sp.angle, None, render.SDL_FLIP_NONE) == -1: raise SDLError()
def _blit_characters_on(self, target_sprite, lines, offset=None): """Blit each character on surface""" if offset: x, y = offset else: x, y = 0, 0 cw, ch = self.size target = target_sprite.surface blit_surface = surface.SDL_BlitSurface fontsf = self.surface offsets = self.offsets for i, line in enumerate(lines): for j, char in enumerate(line): dstr = rect.SDL_Rect(x + j * cw, y + i * ch, 0, 0) offset = offsets[ord(char)] blit_surface(fontsf, offset, target, dstr)
def __init__(self, window): super(HWRenderer, self).__init__(window) self._window = window self.renderer = self.sdlrenderer self._root_node = None self._uiroot = None self._asset_pixel_ratio = -1 self._base_scale = 1.0 self._zoom = 1.0 self._sprite_loader = None self._view_pos = vec2.Vec2(0.0, 0.0) self._view_aabb = aabb.AABB(0.0, 0.0, 0.0, 0.0) self._dirty = True self._last_w_size = 0, 0 # window.size self._sprites = {} # type: Dict[int, Sprite] self._texts = {} # type: Dict[int, str] self._rcopy = render.SDL_RenderCopyEx self._rect = rect.SDL_Rect(0, 0, 0, 0)