首页 > 解决方案 > 使用 area 参数时的 pygame surface.blits

问题描述

我正在尝试将 surface.blits 与 area 参数一起使用来提高我的代码的性能。当我将 area 参数用于 blits 时,我遇到了以下错误:

SystemError: <'pygame.Surface' 对象的'method 'blits'> 返回了带有错误集的结果。

如果我删除 area 参数,blits 会按我的预期工作。关于我可能做错了什么的任何想法?下面附上我的用例和错误的示例代码。

import sys
import random

import pygame
pygame.init()

tilemap = pygame.image.load('pattern.jpg')

tilesize = 64
size = 4
w, h = size*64, size*64
screen = pygame.display.set_mode((w, h))

while True:
    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    blit_list = []
    for i in range(size):
        for j in range(size):
            xi, yi = random.randint(0, size), random.randint(0, size)
            blit_args = (tilemap, (i*tilesize, j*tilesize),
                        (xi*tilesize, yi*tilesize, tilesize, tilesize))

            # calling screen.blit here works correctly
            screen.blit(*blit_args)

            blit_list.append(blit_args)


    # instead of using multiple blit calls above, calling screen.blits here fails
    # remove the area argument (3rd arg) from each blit_arg tuple works
    # screen.blits(blit_list)

    pygame.display.flip()

    # wait a second
    pygame.time.wait(1000)

这是我使用的图像(https://www.behance.net/gallery/19447645/Summer-patterns):

图案.jpg

标签: pythonpygamepygame-surface

解决方案


这是 C 代码中的错误。在surface.c line 2258中,surf_blits有以下测试:

    if (dest->flags & SDL_OPENGL &&
        !(dest->flags & (SDL_OPENGLBLIT & ~SDL_OPENGL))) {
        bliterrornum = BLITS_ERR_NO_OPENGL_SURF;
        goto bliterror;
    }

而在surface.c line 2118中,surf_blit代码为:

#if IS_SDLv1
    if (dest->flags & SDL_OPENGL &&
        !(dest->flags & (SDL_OPENGLBLIT & ~SDL_OPENGL)))
        return RAISE(pgExc_SDLError,
                     "Cannot blit to OPENGL Surfaces (OPENGLBLIT is ok)");
#endif /* IS_SDLv1 */

注意#if IS_SDLv1.

问题似乎来自SDL_OPENGLBLIT现在已弃用的问题。

不要使用已弃用的 SDL_OPENGLBLIT 模式,该模式用于同时允许位块传输和使用 OpenGL。由于很多原因,此标志已被弃用。在许多情况下,使用 SDL_OPENGLBLIT 会破坏您的 OpenGL 状态。

不幸的是,我不是 OpenGL 方面的专家,我无法进一步解释。希望有人可以发布更准确的答案。

我可以肯定的是,我可以在BLITS_ERR_SEQUENCE_SURF之前加注(例如,通过将 apygame.Rect作为第一个对象blit_args),而我无法在BLITS_ERR_INVALID_DESTINATION之后加注。

这使我认为上面的行发生了一些事情。

编辑

我可以确认,如果我添加#if IS_SDLv1上面的测试并重新编译 pygame,它可以工作。不知道为什么!☺</p>

在 GitHub 上提出了这个问题


推荐阅读