Exemple #1
0
    def test_SDL_QueryTexture(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        assert isinstance(window.contents, video.SDL_Window)
        renderer = render.SDL_CreateRenderer(window, -1, self._RENDERFLAGS)
        assert isinstance(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)
                        assert isinstance(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))
                        assert ret == 0
                        assert qf.value == fmt
                        assert qa.value == acc
                        assert qw.value == w
                        assert qh.value == h
                        render.SDL_DestroyTexture(tex)
                    if _ISPYPY and (w % 50) == 0:
                        gc.collect()

        render.SDL_DestroyRenderer(renderer)
        video.SDL_DestroyWindow(window)
        dogc()
    def test_IMG_LoadTextureTyped_RW(self):
        sf = surface.SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0)
        rd = render.SDL_CreateSoftwareRenderer(sf)

        fname = "surfacetest.%s"
        for fmt in formats:
            if fmt == "webp":
                # FIXME: crashew with SDL_image 2.0.2
                continue
            if fmt == "svg":
                # FIXME: svg crashes with SDL2_image 2.0.2
                continue
            filename = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    "resources", fname % fmt)
            print(filename)
            with open(filename, "rb") as fp:
                rw = rwops.rw_from_object(fp)
                fmtx = fmt.upper().encode("utf-8")
                tex = sdlimage.IMG_LoadTextureTyped_RW(rd, rw, 0, fmtx)
                self.assertIsNotNone(tex)
                self.assertIsInstance(tex.contents, render.SDL_Texture)
            render.SDL_DestroyTexture(tex)
        render.SDL_DestroyRenderer(rd)
        surface.SDL_FreeSurface(sf)
Exemple #3
0
    def test_SDL_RenderGetSetLogicalSize(self):
        w, h = 100, 100
        sf = surface.SDL_CreateRGBSurface(0, w, h, 32, 0xFF000000, 0x00FF0000,
                                          0x0000FF00, 0x000000FF)

        renderer = render.SDL_CreateSoftwareRenderer(sf)
        view = PixelView(sf.contents)

        magenta = 255, 0, 255, 255
        green = 0, 255, 0, 255

        magenta_int = sum(c << (i * 8)
                          for i, c in enumerate(reversed(magenta)))
        green_int = sum(c << (i * 8) for i, c in enumerate(reversed(green)))

        def clear_green():
            ret = render.SDL_SetRenderDrawColor(renderer, green[0], green[1],
                                                green[2], green[3])
            self.assertEqual(ret, 0)
            ret = render.SDL_RenderClear(renderer)
            self.assertEqual(ret, 0)

        def draw_magenta_pixel(x, y):
            ret = render.SDL_SetRenderDrawColor(renderer, magenta[0],
                                                magenta[1], magenta[2],
                                                magenta[3])
            self.assertEqual(ret, 0)
            ret = render.SDL_RenderDrawPoint(renderer, x, y)
            self.assertEqual(ret, 0)

        # Test 1
        # If we set the logical renderer size to 1 x 1, drawing a point
        # at 0, 0 should have the same effect as filling the entire
        # (square) window with magenta - no green should show through.
        got_width, got_height = c_int(), c_int()

        ret = render.SDL_RenderSetLogicalSize(renderer, 1, 1)
        self.assertEqual(ret, 0)
        render.SDL_RenderGetLogicalSize(renderer, byref(got_width),
                                        byref(got_height))
        self.assertEqual(got_width.value, 1)
        self.assertEqual(got_height.value, 1)

        clear_green()
        draw_magenta_pixel(0, 0)

        for x, y in itertools.product(range(w), range(h)):
            self.assertEqual(view[y][x], magenta_int,
                             'No pixel should be green')

        # Test 2
        # Reset the logical size to the original target by using 0, 0
        # only the first and last pixel should be magenta. The rest
        # should be green.
        got_width, got_height = c_int(), c_int()

        ret = render.SDL_RenderSetLogicalSize(renderer, 0, 0)
        self.assertEqual(ret, 0)

        render.SDL_RenderGetLogicalSize(renderer, byref(got_width),
                                        byref(got_height))
        self.assertEqual(got_width.value, 0)
        self.assertEqual(got_height.value, 0)

        clear_green()

        draw_magenta_pixel(0, 0)
        draw_magenta_pixel(w - 1, h - 1)

        for x, y in itertools.product(range(w), range(h)):
            if (x, y) == (0, 0) or (x, y) == (w - 1, h - 1):
                self.assertEqual(view[y][x], magenta_int,
                                 'First and last pixel should be magenta')
            else:
                self.assertEqual(view[y][x], green_int,
                                 'All other pixels should be green')

        # Test 3
        # Set the logical size to 1/10, making a logical pixel draw be
        # 10 x 10 real pixel blocks.
        got_width, got_height = c_int(), c_int()

        ret = render.SDL_RenderSetLogicalSize(renderer, w // 10, h // 10)
        self.assertEqual(ret, 0)

        render.SDL_RenderGetLogicalSize(renderer, byref(got_width),
                                        byref(got_height))
        self.assertEqual(got_width.value, w // 10)
        self.assertEqual(got_height.value, h // 10)

        clear_green()

        draw_magenta_pixel(0, 0)
        for x, y in itertools.product(range(w), range(h)):
            if x < 10 and y < 10:
                self.assertEqual(
                    view[y][x], magenta_int,
                    'Top-left 10 x 10 pixel block should be magenta')
            else:
                self.assertEqual(view[y][x], green_int,
                                 'All other pixels should be green')

        render.SDL_DestroyRenderer(renderer)
        surface.SDL_FreeSurface(sf)