python - 使用 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):
解决方案
这是 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>
推荐阅读
- date - Tableau 度量计数项目(如果在日期之间)
- javascript - 在 for 循环中进行过多的 API 调用(超出限制)Javascript
- node.js - npm i http-proxy-middleware 安装较旧的软件包版本
- r - 在 R 中切割栅格数据
- android - 错误:使用未声明的标识符“vmaxq_f16”
- exception - 是否有任何 CPU 状态位指示处于 ARM Cortex-A 处理器中的异常/中断处理程序中?
- postgresql - TypeOrm 比较逗号分隔的字符串
- spring-boot - waitingList 实体的优先级队列
- python - 对象作为 Python 多处理池的参数默认为相同的对象内存(错误)
- bash - env 未从命令结果导出