def test_SDL_GetSetColorKey(self): colorkeys = ( (0, 0, 0), (32, 64, 128), (10, 20, 30), (1, 2, 4), (255, 255, 255), (128, 128, 128), (127, 127, 127), ) bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32(), Uint32() for fmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt): continue pformat = pixels.SDL_AllocFormat(fmt) self.assertIsInstance(pformat.contents, pixels.SDL_PixelFormat) ret = pixels.SDL_PixelFormatEnumToMasks(fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 10, bpp, rmask, gmask, bmask, amask) for r, g, b in colorkeys: key = pixels.SDL_MapRGB(pformat, r, g, b) surface.SDL_SetColorKey(sf, 1, key) skey = Uint32() ret = surface.SDL_GetColorKey(sf, byref(skey)) self.assertEqual(ret, 0) self.assertEqual( skey.value, key, "Could not set color key (%d, %d, %d)" % (r, g, b)) pixels.SDL_FreeFormat(pformat) surface.SDL_FreeSurface(sf)
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_LockUnlockMUSTLOCKSurface(self): bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32(), Uint32() for fmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt): continue ret = pixels.SDL_PixelFormatEnumToMasks(fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 10, bpp, rmask, gmask, bmask, amask) # TODO: locking seems to be only necessary for RLE surfaces... if surface.SDL_MUSTLOCK(sf.contents): self.assertFalse(sf.contents.locked) surface.SDL_LockSurface(sf) self.assertTrue(sf.contents.locked) surface.SDL_LockSurface(sf) self.assertTrue(sf.contents.locked) surface.SDL_LockSurface(sf) self.assertTrue(sf.contents.locked) surface.SDL_LockSurface(sf) self.assertTrue(sf.contents.locked) surface.SDL_LockSurface(sf) self.assertTrue(sf.contents.locked) surface.SDL_UnlockSurface(sf) self.assertFalse(sf.contents.locked) surface.SDL_FreeSurface(sf)
def test_SDL_GetSetSurfaceColorMod(self): colormods = ( (0, 0, 0), (32, 64, 128), (10, 20, 30), (1, 2, 4), (255, 255, 255), (128, 128, 128), (127, 127, 127), ) bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32(), Uint32() for fmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt): continue ret = pixels.SDL_PixelFormatEnumToMasks(fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 10, bpp, rmask, gmask, bmask, amask) for r, g, b in colormods: sr, sg, sb = Uint8(), Uint8(), Uint8() surface.SDL_SetSurfaceColorMod(sf, r, g, b) ret = surface.SDL_GetSurfaceColorMod(sf, byref(sr), byref(sg), byref(sb)) self.assertEqual(ret, 0) self.assertEqual((sr.value, sg.value, sb.value), (r, g, b)) surface.SDL_FreeSurface(sf)
def test_SDL_ConvertSurfaceFormat(self): tenbit = [ pixels.SDL_PACKEDLAYOUT_2101010, pixels.SDL_PACKEDLAYOUT_1010102 ] for pfmt in pixels.ALL_PIXELFORMATS: for fmt in pixels.ALL_PIXELFORMATS: # SDL2 doesn't support converting fancier formats (e.g YUV, 10bit) if pixels.SDL_ISPIXELFORMAT_FOURCC( pfmt) or pixels.SDL_PIXELLAYOUT(pfmt) in tenbit: continue if pixels.SDL_ISPIXELFORMAT_FOURCC( fmt) or pixels.SDL_PIXELLAYOUT(fmt) in tenbit: continue # SDL2 doesn't support converting to formats w/ less than 8 bpp if pixels.SDL_BITSPERPIXEL(pfmt) < 8: continue # SDL2 doesn't support converting from indexed formats w/ 4 bpp if pixels.SDL_PIXELTYPE(fmt) == pixels.SDL_PIXELTYPE_INDEX4: continue bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32( ), Uint32() ret = pixels.SDL_PixelFormatEnumToMasks( fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 20, bpp, rmask, gmask, bmask, amask) self.assertIsInstance(sf.contents, surface.SDL_Surface) csf = surface.SDL_ConvertSurfaceFormat(sf, pfmt, 0) self.assertTrue(csf, error.SDL_GetError()) self.assertIsInstance(csf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf) surface.SDL_FreeSurface(csf)
def test_SDL_PixelFormatEnumToMasks(self): bpp = c_int() r, g, b, a = Uint32(), Uint32(), Uint32(), Uint32() pixels.SDL_PixelFormatEnumToMasks(pixels.SDL_PIXELFORMAT_INDEX1LSB, byref(bpp), byref(r), byref(g), byref(b), byref(a)) self.assertEqual((bpp.value, r.value, g.value, b.value, a.value), (1, 0, 0, 0, 0)) pixels.SDL_PixelFormatEnumToMasks(pixels.SDL_PIXELFORMAT_INDEX1MSB, byref(bpp), byref(r), byref(g), byref(b), byref(a)) self.assertEqual((bpp.value, r.value, g.value, b.value, a.value), (1, 0, 0, 0, 0)) pixels.SDL_PixelFormatEnumToMasks(pixels.SDL_PIXELFORMAT_RGBA8888, byref(bpp), byref(r), byref(g), byref(b), byref(a)) if sys.byteorder == "little": self.assertEqual((bpp.value, r.value, g.value, b.value, a.value), (32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF)) else: self.assertEqual((bpp.value, r.value, g.value, b.value, a.value), (32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000)) pixels.SDL_PixelFormatEnumToMasks(pixels.SDL_PIXELFORMAT_RGBX8888, byref(bpp), byref(r), byref(g), byref(b), byref(a)) if sys.byteorder == "little": self.assertEqual((bpp.value, r.value, g.value, b.value, a.value), (32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0)) else: self.assertEqual((bpp.value, r.value, g.value, b.value, a.value), (32, 0, 0x0000FF00, 0x00FF0000, 0xFF000000)) # self.assertRaises(sdl.SDLError, pixels.SDL_PixelFormatEnumToMasks, # 99999) pixels.SDL_PixelFormatEnumToMasks(0, byref(bpp), byref(r), byref(g), byref(b), byref(a)) self.assertEqual((bpp.value, r.value, g.value, b.value, a.value), (0, 0, 0, 0, 0)) pixels.SDL_PixelFormatEnumToMasks(pixels.SDL_PIXELFORMAT_UNKNOWN, byref(bpp), byref(r), byref(g), byref(b), byref(a)) self.assertEqual((bpp.value, r.value, g.value, b.value, a.value), (0, 0, 0, 0, 0))
def test_SDL_ConvertSurface(self): bad_combos = [] good_combos = [] tenbit = [ pixels.SDL_PACKEDLAYOUT_2101010, pixels.SDL_PACKEDLAYOUT_1010102 ] for idx in pixels.ALL_PIXELFORMATS: pfmt = pixels.SDL_AllocFormat(idx) for fmt in pixels.ALL_PIXELFORMATS: idx_name = pixels.SDL_GetPixelFormatName(idx).decode('utf-8') fmt_name = pixels.SDL_GetPixelFormatName(fmt).decode('utf-8') # SDL2 doesn't support converting fancier formats (e.g YUV, 10bit) if pixels.SDL_ISPIXELFORMAT_FOURCC( idx) or pixels.SDL_PIXELLAYOUT(idx) in tenbit: continue if pixels.SDL_ISPIXELFORMAT_FOURCC( fmt) or pixels.SDL_PIXELLAYOUT(fmt) in tenbit: continue # SDL2 doesn't support converting to formats w/ less than 8 bpp if pixels.SDL_BITSPERPIXEL(idx) < 8: continue # SDL2 doesn't support converting from indexed formats w/ 4 bpp if pixels.SDL_PIXELTYPE(fmt) == pixels.SDL_PIXELTYPE_INDEX4: continue bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32( ), Uint32() ret = pixels.SDL_PixelFormatEnumToMasks( fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 20, bpp, rmask, gmask, bmask, amask) self.assertIsInstance(sf.contents, surface.SDL_Surface) csf = surface.SDL_ConvertSurface(sf, pfmt, 0) self.assertTrue(csf) if error.SDL_GetError() == b'Blit combination not supported': bad_combos.append('{0} -> {1}'.format(fmt_name, idx_name)) error.SDL_ClearError() else: good_combos.append('{0} -> {1}'.format(fmt_name, idx_name)) self.assertIsInstance(csf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf) surface.SDL_FreeSurface(csf) pixels.SDL_FreeFormat(pfmt) self.assertEqual(len(bad_combos), 0)
def test_SDL_GetSetSurfaceBlendMode(self): modes = (blendmode.SDL_BLENDMODE_NONE, blendmode.SDL_BLENDMODE_BLEND, blendmode.SDL_BLENDMODE_ADD, blendmode.SDL_BLENDMODE_MOD) bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32(), Uint32() for fmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt): continue ret = pixels.SDL_PixelFormatEnumToMasks(fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 10, bpp, rmask, gmask, bmask, amask) for mode in modes: smode = blendmode.SDL_BlendMode() ret = surface.SDL_SetSurfaceBlendMode(sf, mode) self.assertEqual(ret, 0) surface.SDL_GetSurfaceBlendMode(sf, byref(smode)) self.assertEqual(smode.value, mode) surface.SDL_FreeSurface(sf)
def test_SDL_ConvertSurface(self): tenbit = [ pixels.SDL_PACKEDLAYOUT_2101010, pixels.SDL_PACKEDLAYOUT_1010102 ] for idx in pixels.ALL_PIXELFORMATS: pfmt = pixels.SDL_AllocFormat(idx) for fmt in pixels.ALL_PIXELFORMATS: idx_name = pixels.SDL_GetPixelFormatName(idx).decode( 'utf-8') # for debug fmt_name = pixels.SDL_GetPixelFormatName(fmt).decode( 'utf-8') # for debug # SDL2 doesn't support converting fancier formats (e.g YUV, 10bit) if pixels.SDL_ISPIXELFORMAT_FOURCC( idx) or pixels.SDL_PIXELLAYOUT(idx) in tenbit: continue if pixels.SDL_ISPIXELFORMAT_FOURCC( fmt) or pixels.SDL_PIXELLAYOUT(fmt) in tenbit: continue # SDL2 doesn't support converting to formats w/ less than 8 bpp if pixels.SDL_BITSPERPIXEL(idx) < 8: continue # SDL2 doesn't support converting from indexed formats w/ 4 bpp if pixels.SDL_PIXELTYPE(fmt) == pixels.SDL_PIXELTYPE_INDEX4: continue bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32( ), Uint32() ret = pixels.SDL_PixelFormatEnumToMasks( fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) assert ret == SDL_TRUE sf = surface.SDL_CreateRGBSurface(0, 10, 20, bpp, rmask, gmask, bmask, amask) assert isinstance(sf.contents, surface.SDL_Surface) csf = surface.SDL_ConvertSurface(sf, pfmt, 0) assert csf, error.SDL_GetError() assert isinstance(csf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf) surface.SDL_FreeSurface(csf) pixels.SDL_FreeFormat(pfmt)
def test_SDL_ConvertSurfaceFormat(self): for pfmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(pfmt): continue for fmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt): continue bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32( ), Uint32() ret = pixels.SDL_PixelFormatEnumToMasks( fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 20, bpp, rmask, gmask, bmask, amask) self.assertIsInstance(sf.contents, surface.SDL_Surface) csf = surface.SDL_ConvertSurfaceFormat(sf, pfmt, 0) self.assertTrue(csf, error.SDL_GetError()) self.assertIsInstance(csf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf) surface.SDL_FreeSurface(csf)
def test_SDL_GetSetSurfaceAlphaMod(self): bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32(), Uint32() for fmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt): continue ret = pixels.SDL_PixelFormatEnumToMasks(fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 10, bpp, rmask, gmask, bmask, amask) salpha = Uint8() ret = surface.SDL_GetSurfaceAlphaMod(sf, byref(salpha)) self.assertEqual(ret, 0) self.assertEqual(salpha.value, 255) for alpha in range(0, 255): ret = surface.SDL_SetSurfaceAlphaMod(sf, alpha) self.assertEqual(ret, 0) ret = surface.SDL_GetSurfaceAlphaMod(sf, byref(salpha)) self.assertEqual(ret, 0) self.assertEqual(salpha.value, alpha) surface.SDL_FreeSurface(sf)
def test_SDL_CreateRGBSurface(self): for w in range(1, 100, 5): for h in range(1, 100, 5): for bpp in alldepths: sf = surface.SDL_CreateRGBSurface(0, w, h, bpp, 0, 0, 0, 0) self.assertIsInstance(sf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf) bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32(), Uint32() for fmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt): continue for w in range(1, 100, 5): for h in range(1, 100, 5): ret = pixels.SDL_PixelFormatEnumToMasks( fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 20, bpp, rmask, gmask, bmask, amask) self.assertIsInstance(sf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf)
def test_SDL_FreeSurface(self): # self.assertRaises((AttributeError, TypeError), # surface.SDL_FreeSurface, None) # self.assertRaises((AttributeError, TypeError), # surface.free_surface, "Test") # self.assertRaises((AttributeError, TypeError), # surface.free_surface, 5) formats = (pixels.SDL_PIXELFORMAT_INDEX1LSB, pixels.SDL_PIXELFORMAT_RGB332, pixels.SDL_PIXELFORMAT_RGBA4444, pixels.SDL_PIXELFORMAT_BGR888, pixels.SDL_PIXELFORMAT_ARGB8888, pixels.SDL_PIXELFORMAT_ARGB2101010) bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32(), Uint32() for fmt in formats: ret = pixels.SDL_PixelFormatEnumToMasks(fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 5, 5, bpp, rmask, gmask, bmask, amask) surface.SDL_FreeSurface(sf)
def test_SDL_QueryTexture(self): window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, video.SDL_WINDOW_HIDDEN) self.assertIsInstance(window.contents, video.SDL_Window) renderer = render.SDL_CreateRenderer(window, -1, self._RENDERFLAGS) self.assertIsInstance(renderer.contents, render.SDL_Renderer) formats = (pixels.SDL_PIXELFORMAT_ARGB8888, pixels.SDL_PIXELFORMAT_RGB555, pixels.SDL_PIXELFORMAT_RGBA4444, pixels.SDL_PIXELFORMAT_ARGB2101010, pixels.SDL_PIXELFORMAT_YUY2 ) access = (render.SDL_TEXTUREACCESS_STATIC, render.SDL_TEXTUREACCESS_STREAMING, render.SDL_TEXTUREACCESS_TARGET) for fmt in formats: for acc in access: for w in range(1, 300, 5): for h in range(1, 300, 5): tex = render.SDL_CreateTexture(renderer, fmt, acc, w, h) self.assertIsInstance(tex.contents, render.SDL_Texture) qf, qa, qw, qh = Uint32(), c_int(), c_int(), c_int() ret = render.SDL_QueryTexture(tex, byref(qf), byref(qa), byref(qw), byref(qh)) self.assertEqual(ret, 0) self.assertEqual(qf.value, fmt) self.assertEqual(qa.value, acc) self.assertEqual(qw.value, w) self.assertEqual(qh.value, h) render.SDL_DestroyTexture(tex) if _ISPYPY and (w % 50) == 0: gc.collect() render.SDL_DestroyRenderer(renderer) video.SDL_DestroyWindow(window) dogc()