如何强制 Windows 对我的 pygame 游戏使用专用 GPU?

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

如何强制 Windows 对我的 pygame 游戏使用专用 GPU?

我使用 pygame._sdl2.video 进行渲染,当我在 Windows 图形设置中将 GPU 更改为专用视频选项时,性能提高了 40%。我如何在代码中做到这一点?

如何优化字体渲染?我在每一帧中从表面创建纹理

这是我的代码:

import pygame
from pygame._sdl2.video import Window as SdlWindow, Renderer, Texture, Image
import random
import math


pygame.init()


screen_size = [900, 900]
screen = SdlWindow(title='Sdl2 Test', size=screen_size)
renderer = Renderer(screen)

image = pygame.transform.scale(pygame.image.load('sdl2_image.png'), (75, 48))
image = Texture.from_surface(renderer, image)

font = pygame.font.SysFont('Arial', 50)

spriteGroup = pygame.sprite.Group()

clock = pygame.time.Clock()
while True:
    clock.tick(60)
    keys = pygame.key.get_pressed()
    mouse_x, mouse_y = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    renderer.clear()
    
    if len(spriteGroup) < 5000:
        for i in range(0, 8):
            rect = image.get_rect()
            rect.x = 450
            rect.y = 450
            
            sprite = pygame.sprite.Sprite(spriteGroup)
            sprite.rect = rect
            sprite.image = Image(image)
            sprite.angle = random.uniform(-3.14, 3.14)
            sprite.x = sprite.rect.x
            sprite.y = sprite.rect.y

    for sprite in spriteGroup:
        sprite.x += math.cos(sprite.angle)
        sprite.y += math.sin(sprite.angle)

        if sprite.x < 0 or sprite.x > screen_size[0] - sprite.rect.size[0]:
            sprite.angle = math.pi - sprite.angle
        if sprite.y < 0 or sprite.y > screen_size[1] - sprite.rect.size[1]:
            sprite.angle = -sprite.angle
        
        sprite.image.angle += 1
        
        sprite.rect.x = int(sprite.x)
        sprite.rect.y = int(sprite.y)
    
    spriteGroup.draw(renderer)

    font_surface = Image(Texture.from_surface(renderer, font.render(str(round(clock.get_fps())), True, (0, 0, 255))))
    rect = font_surface.get_rect()
    font_surface.draw(rect, rect)

    font_surface = Image(Texture.from_surface(renderer, font.render(str(len(spriteGroup)), True, (0, 0, 255))))
    rect = font_surface.get_rect()
    font_surface.draw(rect, (0, 900 - rect[3], rect[2], rect[3]))

    renderer.present()
python windows pygame sdl-2
1个回答
0
投票

如何优化字体渲染?我正在每一帧中从表面创建纹理”*

仅当文本发生更改时才渲染文本。例如:

fps_text, fps_surface = None, None

while True:
    # [...]

    text = str(round(clock.get_fps()))
    if not fps_surface or text != fps_text:
         fps_text = text
         fps_surface = Image(Texture.from_surface(renderer, font.render(fps_text, True, (0, 0, 255))))
    rect = fps_surface.get_rect()
    font_surface.draw(rect, rect)

    # [...]
© www.soinside.com 2019 - 2024. All rights reserved.