def test_SoftwareSprite_size(self): for w in range(0, 200): for h in range(0, 200): sf = SDL_CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0) sprite = sdl2ext.SoftwareSprite(sf.contents, True) self.assertIsInstance(sprite, sdl2ext.SoftwareSprite) self.assertEqual(sprite.size, (w, h))
def test_Renderer_draw_rect(self): surface = SDL_CreateRGBSurface(0, 128, 128, 32, 0, 0, 0, 0).contents sdl2ext.fill(surface, 0x0) renderer = sdl2ext.Renderer(surface) renderer.draw_rect((40, 50, 32, 32), 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
def test_Renderer_draw_line(self): surface = SDL_CreateRGBSurface(0, 128, 128, 32, 0, 0, 0, 0).contents sdl2ext.fill(surface, 0x0) renderer = sdl2ext.Renderer(surface) renderer.draw_line((20, 10, 20, 86), 0x0000FF) view = sdl2ext.PixelView(surface) self.check_lines(view, 128, 128, [((20, 10), (20, 86))], 0x0000FF, (0x0, )) del view
def test_SoftwareSprite_position_xy(self): sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0) sprite = sdl2ext.SoftwareSprite(sf.contents, True) assert isinstance(sprite, sdl2ext.SoftwareSprite) assert sprite.position == (0, 0) positions = [(x, y) for x in range(-50, 50) for y in range(-50, 50)] for x, y in positions: sprite.position = x, y assert sprite.position == (x, y) sprite.x = x + 1 sprite.y = y + 1 assert sprite.position == (x + 1, y + 1)
def test_Renderer_copy(self): 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.copy(sp, (0, 0, w, h), (sp.x, sp.y, w, h)) view = sdl2ext.PixelView(surface) self.check_pixels(view, 128, 128, sp, 0xFF0000, (0x0, )) del view
def test_SoftwareSprite_position_xy(self): sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0) sprite = sdl2ext.SoftwareSprite(sf.contents, True) self.assertIsInstance(sprite, sdl2ext.SoftwareSprite) self.assertEqual(sprite.position, (0, 0)) positions = [(x, y) for x in range(-50, 50) for y in range(-50, 50)] for x, y in positions: sprite.position = x, y self.assertEqual(sprite.position, (x, y)) sprite.x = x + 1 sprite.y = y + 1 self.assertEqual(sprite.position, (x + 1, y + 1))
def test_SoftwareSprite_area(self): sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0) sprite = sdl2ext.SoftwareSprite(sf.contents, True) self.assertEqual(sprite.area, (0, 0, 10, 10)) def setarea(s, v): s.area = v self.assertRaises(AttributeError, setarea, sprite, (1, 2, 3, 4)) sprite.position = 7, 3 self.assertEqual(sprite.area, (7, 3, 17, 13)) sprite.position = -22, 99 self.assertEqual(sprite.area, (-22, 99, -12, 109))
def test_SoftwareSpriteRenderSystem_render(self): sf1 = SDL_CreateRGBSurface(0, 12, 7, 32, 0, 0, 0, 0) sp1 = sdl2ext.SoftwareSprite(sf1.contents, True) sdl2ext.fill(sp1, 0xFF0000) sf2 = SDL_CreateRGBSurface(0, 3, 9, 32, 0, 0, 0, 0) sp2 = sdl2ext.SoftwareSprite(sf2.contents, True) sdl2ext.fill(sp2, 0x00FF00) sprites = [sp1, sp2] window = sdl2ext.Window("Test", size=(20, 20)) renderer = sdl2ext.SoftwareSpriteRenderSystem(window) assert isinstance(renderer, sdl2ext.SpriteRenderSystem) with pytest.raises(AttributeError): renderer.render(None, None, None) with pytest.raises(AttributeError): renderer.render([None, None], None, None) for x, y in ((0, 0), (3, 3), (20, 20), (1, 12), (5, 6)): sp1.position = x, y renderer.render(sp1) view = sdl2ext.PixelView(renderer.surface) self.check_pixels(view, 20, 20, sp1, 0xFF0000, (0x0, )) del view sdl2ext.fill(renderer.surface, 0x0) sp1.position = 0, 0 sp2.position = 14, 1 renderer.render(sprites) view = sdl2ext.PixelView(renderer.surface) self.check_pixels(view, 20, 20, sp1, 0xFF0000, (0x0, 0x00FF00)) self.check_pixels(view, 20, 20, sp2, 0x00FF00, (0x0, 0xFF0000)) del view sdl2ext.fill(renderer.surface, 0x0) renderer.render(sprites, 1, 2) view = sdl2ext.PixelView(renderer.surface) self.check_pixels(view, 20, 20, sp1, 0xFF0000, (0x0, 0x00FF00), 1, 2) self.check_pixels(view, 20, 20, sp2, 0x00FF00, (0x0, 0xFF0000), 1, 2) del view
def test_SoftwareSpriteRenderSystem_process(self): sf1 = SDL_CreateRGBSurface(0, 5, 10, 32, 0, 0, 0, 0) sp1 = sdl2ext.SoftwareSprite(sf1.contents, True) sp1.depth = 0 sdl2ext.fill(sp1, 0xFF0000) sf2 = SDL_CreateRGBSurface(0, 5, 10, 32, 0, 0, 0, 0) sp2 = sdl2ext.SoftwareSprite(sf2.contents, True) sp2.depth = 99 sdl2ext.fill(sp2, 0x00FF00) sprites = [sp1, sp2] window = sdl2ext.Window("Test", size=(20, 20)) renderer = sdl2ext.SoftwareSpriteRenderSystem(window) renderer.process("fakeworld", sprites) view = sdl2ext.PixelView(renderer.surface) # Only sp2 wins, since its depth is higher self.check_pixels(view, 20, 20, sp1, 0x00FF00, (0x0, )) self.check_pixels(view, 20, 20, sp2, 0x00FF00, (0x0, )) del view self.assertRaises(TypeError, renderer.process, None, None)
def test_SoftwareSprite(self): self.assertRaises(TypeError, sdl2ext.SoftwareSprite, None, None) self.assertRaises(TypeError, sdl2ext.SoftwareSprite, None, True) self.assertRaises(TypeError, sdl2ext.SoftwareSprite, None, False) sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0) sprite = sdl2ext.SoftwareSprite(sf.contents, False) # TODO: the following assert fails... # self.assertEqual(sprite.surface, sf.contents) self.assertFalse(sprite.free) sprite = sdl2ext.SoftwareSprite(sf.contents, True) # TODO: the following assert fails... # self.assertEqual(sprite.surface, sf.contents) self.assertTrue(sprite.free)
def test_SoftwareSprite_area(self): sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0) sprite = sdl2ext.SoftwareSprite(sf.contents, True) assert sprite.area == (0, 0, 10, 10) def setarea(s, v): s.area = v with pytest.raises(AttributeError): setarea(sprite, (1, 2, 3, 4)) sprite.position = 7, 3 assert sprite.area == (7, 3, 17, 13) sprite.position = -22, 99 assert sprite.area == (-22, 99, -12, 109)
def test_SoftwareSprite(self): with pytest.raises(TypeError): sdl2ext.SoftwareSprite(None, None) with pytest.raises(TypeError): sdl2ext.SoftwareSprite(None, True) with pytest.raises(TypeError): sdl2ext.SoftwareSprite(None, False) sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0) sprite = sdl2ext.SoftwareSprite(sf.contents, False) # TODO: the following assert fails... # self.assertEqual(sprite.surface, sf.contents) assert not sprite.free sprite = sdl2ext.SoftwareSprite(sf.contents, True) # TODO: the following assert fails... # self.assertEqual(sprite.surface, sf.contents) assert sprite.free
def test_Renderer(self): sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0).contents renderer = sdl2ext.Renderer(sf) assert renderer.rendertarget == sf assert isinstance(renderer.sdlrenderer.contents, SDL_Renderer) del renderer # Deprecated renderer = sdl2ext.Renderer(sf) assert renderer.rendertarget == sf assert isinstance(renderer.sdlrenderer.contents, SDL_Renderer) del renderer sprite = sdl2ext.SoftwareSprite(sf, True) renderer = sdl2ext.Renderer(sprite) assert renderer.rendertarget == sprite assert isinstance(renderer.sdlrenderer.contents, SDL_Renderer) del renderer dogc() window = sdl2ext.Window("Test", size=(1, 1)) renderer = sdl2ext.Renderer(window) assert renderer.rendertarget == window assert isinstance(renderer.sdlrenderer.contents, SDL_Renderer) del renderer dogc() sdlwindow = window.window renderer = sdl2ext.Renderer(sdlwindow) assert renderer.rendertarget == sdlwindow assert isinstance(renderer.sdlrenderer.contents, SDL_Renderer) del renderer del window with pytest.raises(TypeError): sdl2ext.Renderer(None) with pytest.raises(TypeError): sdl2ext.Renderer(1234) with pytest.raises(TypeError): sdl2ext.Renderer("test") dogc()
def test_Renderer_clear(self): sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF) renderer = sdl2ext.Renderer(sf.contents) self.assertIsInstance(renderer.color, sdl2ext.Color) self.assertEqual(renderer.color, sdl2ext.Color(0, 0, 0, 0)) renderer.color = 0x00FF0000 self.assertEqual(renderer.color, sdl2ext.Color(0xFF, 0, 0, 0)) renderer.clear() view = sdl2ext.PixelView(sf.contents) self.check_areas(view, 10, 10, [[0, 0, 10, 10]], 0xFF000000, (0x0, )) del view renderer.clear(0xAABBCCDD) self.assertEqual(renderer.color, sdl2ext.Color(0xFF, 0, 0, 0)) view = sdl2ext.PixelView(sf.contents) self.check_areas(view, 10, 10, [[0, 0, 10, 10]], 0xBBCCDDAA, (0x0, )) del view del renderer SDL_FreeSurface(sf) dogc()
def test_Renderer(self): sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0).contents renderer = sdl2ext.Renderer(sf) self.assertEqual(renderer.rendertarget, sf) self.assertIsInstance(renderer.sdlrenderer.contents, SDL_Renderer) del renderer # Deprecated renderer = sdl2ext.Renderer(sf) self.assertEqual(renderer.rendertarget, sf) self.assertIsInstance(renderer.renderer.contents, SDL_Renderer) del renderer sprite = sdl2ext.SoftwareSprite(sf, True) renderer = sdl2ext.Renderer(sprite) self.assertEqual(renderer.rendertarget, sprite) self.assertIsInstance(renderer.sdlrenderer.contents, SDL_Renderer) del renderer dogc() window = sdl2ext.Window("Test", size=(1, 1)) renderer = sdl2ext.Renderer(window) self.assertEqual(renderer.rendertarget, window) self.assertIsInstance(renderer.sdlrenderer.contents, SDL_Renderer) del renderer dogc() sdlwindow = window.window renderer = sdl2ext.Renderer(sdlwindow) self.assertEqual(renderer.rendertarget, sdlwindow) self.assertIsInstance(renderer.sdlrenderer.contents, SDL_Renderer) del renderer del window self.assertRaises(TypeError, sdl2ext.Renderer, None) self.assertRaises(TypeError, sdl2ext.Renderer, 1234) self.assertRaises(TypeError, sdl2ext.Renderer, "test") dogc()
def test_SpriteFactory_from_surface(self): window = sdl2ext.Window("Test", size=(1, 1)) renderer = sdl2ext.Renderer(window) tfactory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer) sfactory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE) sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0) tsprite = tfactory.from_surface(sf.contents) self.assertIsInstance(tsprite, sdl2ext.TextureSprite) ssprite = sfactory.from_surface(sf.contents) self.assertIsInstance(ssprite, sdl2ext.SoftwareSprite) SDL_FreeSurface(sf) for factory in (tfactory, sfactory): self.assertRaises((sdl2ext.SDLError, AttributeError, ArgumentError, TypeError), factory.from_surface, None) self.assertRaises((AttributeError, ArgumentError, TypeError), factory.from_surface, "test") # TODO: crashes pypy 2.0 #self.assertRaises((AttributeError, ArgumentError, TypeError), # factory.from_surface, 1234) dogc()
def test_SoftwareSprite_repr(self): sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0) sprite = sdl2ext.SoftwareSprite(sf.contents, True) self.assertEqual(repr(sprite), "SoftwareSprite(size=(10, 10), bpp=32)")