Python SDL2 动画帧没有时间渲染

问题描述 投票:0回答:0

我在 Python SDL2 中创建了一些多边形。 我添加了一些基本动画,所以当我拖动鼠标时,多边形也会移动。

这导致不断重复使用以前的缓冲区。

如果我在每一帧动画之前清除缓冲区,多边形会在按从上到下的顺序渲染时闪烁。如果动作足够快,他们甚至没有时间渲染。

我尝试过渲染

renderflags = (
  sdl2.SDL_RENDERER_ACCELERATED | sdl2.SDL_RENDERER_PRESENTVSYNC
)

我认为这会提高性能,但它使它变得更糟。

如何避免闪烁?

这是处理点击输入并清除缓冲区的代码部分:

    grid = new_hex_grid(edge_size, font, radius, x_offset, y_offset)
    grid.render(renderer)

    # Wait for the user to close the window
    running = True
    mousedown_start_position = None
    previous_touch_down_position = None
    while running:
        for event in sdl2.ext.get_events():
            if event.type == sdl2.SDL_QUIT:
                running = False
            elif event.type == sdl2.SDL_MOUSEBUTTONDOWN:
                # print(event.motion.x, event.motion.y)
                print("hi")
                mousedown_start_position = (event.motion.x, event.motion.y)
                previous_touch_down_position = mousedown_start_position
            elif event.type == sdl2.SDL_MOUSEMOTION:
                if mousedown_start_position:
                    pass
                    renderer.clear(0)
                    # print(event.motion.x, event.motion.y)
                    grid.move_to(grid.x + event.motion.x - previous_touch_down_position[0],
                                 grid.y + event.motion.y - previous_touch_down_position[1])
                    grid.render(renderer)
                    previous_touch_down_position = (event.motion.x, event.motion.y)
            elif event.type == sdl2.SDL_MOUSEBUTTONUP:
                mousedown_start_position = None
        # Do all animations here
        # Refresh the screen
        renderer.present()
    # Clean up
    sdl2.ext.quit()

这就是我从一个单独的六边形类中渲染六边形的方式


        color = (50, 150, 50, 255)
        # Convert the vertex lists to arrays of c_int16
        x_array = (ctypes.c_int16 * vertex_count)(*vx)
        y_array = (ctypes.c_int16 * vertex_count)(*vy)
        # Draw the filled box
        sdl2.sdlgfx.filledPolygonRGBA(renderer.sdlrenderer, x_array, y_array, vertex_count, *color)

这是对六边形集合进行分类的类

class HexGrid(Renderable):
    def __init__(self, font, radius, hexagons_by_idx, x, y):
        self.radius = radius
        self.font = font
        self.x = x
        self.y = y
        self.hexagons_by_idx = hexagons_by_idx

    def move_to(self, x, y):
        self.x = x
        self.y = y

    def render(self, renderer: sdl2.ext.Renderer):
        font_offset_x = self.radius / 2 + self.x
        font_offset_y = self.radius / 3 + self.y
        font_block_width = self.radius
        left_corner_index = 3
        for idx, hexagon in self.hexagons_by_idx.items():
            hexagon.move_to(self.x, self.y)
            hexagon.render(renderer)
            render_font(self.font, font_block_width,
                        hexagon.vertices[left_corner_index][0] + font_offset_x,
                        hexagon.vertices[left_corner_index][1] + font_offset_y, renderer,
                        f"{idx[0]}, {idx[1]}")
python-3.x sdl-2 pysdl2
© www.soinside.com 2019 - 2024. All rights reserved.