简体   繁体   English

如何更改pyglet中的pixel_ratio?

[英]How to change pixel_ratio in pyglet?

I coded a game using pylet.我使用pylet编写了一个游戏。 It uses a static window with width 1600 and height 900 assuming users have a fullHD display so everything will be visible.它使用 static window,宽度为 1600,高度为 900,假设用户拥有全高清显示屏,因此一切都将可见。 However on some devices (with small displays) the window is way bigger as expected.然而,在某些设备(带有小显示器)上,window 比预期的要大得多。 I figured out that the pixel_ratio is set up (for example to 2.0) making each virtual pixel to be displayed double size (2x2) in physical pixel.我发现pixel_ratio已设置(例如设置为2.0),使每个虚拟像素以物理像素显示双倍大小(2x2)。

I want to prevent this behavior but can't figure out how, I know I can get the pixel ratio easily by get_pixel_ratio() but I actually don't know how to set them or prevent pyglet from automatically setting them.我想防止这种行为,但不知道如何,我知道我可以通过 get_pixel_ratio() 轻松获得像素比,但我实际上不知道如何设置它们或防止 pyglet 自动设置它们。

I also tried to use glViewport which seemed to have an effect but it didn't worked the way I wanted.我还尝试使用 glViewport 似乎有效果,但它没有按我想要的方式工作。

So how can I change the pixel_ratio or prevent changing it automatically.那么如何更改 pixel_ratio 或防止自动更改它。

Asked around in the official discord server for information, as I tried to reproduce the issue myself with some code, and this is what I used to test it:在官方 discord 服务器中询问信息,因为我试图用一些代码自己重现这个问题,这是我用来测试它的:

import math
from pyglet import *
from pyglet.gl import *

key = pyglet.window.key

class main(pyglet.window.Window):
    def __init__ (self, width=800, height=600, fps=False, *args, **kwargs):
        super(main, self).__init__(width, height, *args, **kwargs)
        self.x, self.y = 0, 0

        self.keys = {}

        verts = []
        for i in range(30):
            angle = math.radians(float(i)/30 * 360.0)
            x = 100*math.cos(angle) + 300
            y = 100*math.sin(angle) + 200
            verts += [x,y]

        self.pixel_ratio = 100
        self.circle = pyglet.graphics.vertex_list(30, ('v2f', verts))

        self.alive = 1

    def on_draw(self):
        self.render()

    def on_close(self):
        self.alive = 0

    def on_key_release(self, symbol, modifiers):
        try:
            del self.keys[symbol]
        except:
            pass

    def on_key_press(self, symbol, modifiers):
        if symbol == key.ESCAPE: # [ESC]
            self.alive = 0

        self.keys[symbol] = True

    def render(self):
        self.clear()

        glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
        glColor3f(1,1,0)
        self.circle.draw(GL_LINE_LOOP)

        self.flip()

    def run(self):
        while self.alive == 1:
            self.render()

            # -----------> This is key <----------
            # This is what replaces pyglet.app.run()
            # but is required for the GUI to not freeze
            #
            event = self.dispatch_events()

if __name__ == '__main__':
    x = main()
    x.run()

Not that the code mattered heh, since the variable pixel_ratio is just, and I quote: "indicating that the actual number of pixels in the window is larger than the size of the Window created."并不是说代码很重要,因为变量pixel_ratio只是,我引用: “表明 window 中的实际像素数大于创建的 Window 的大小。”

This is something OSX does to cope with the high DPI, using this information you should be able to scale your graphics accordingly.这是 OSX 为应对高 DPI 所做的事情,使用此信息您应该能够相应地缩放图形。 Window.get_framebuffer_size() will show you the difference in requested Window size and Framebuffer size, if any. Window.get_framebuffer_size()将显示请求的 Window 大小和帧缓冲区大小(如果有)的差异。

So your only way to actually scale up, would be to use glScale or if you're using sprites you can use Sprite.scale to scale the image-data.因此,您实际放大的唯一方法是使用glScale ,或者如果您使用精灵,您可以使用Sprite.scale来缩放图像数据。 If you're using 2D graphics I'd go with the sprite option as it's pretty easy to work with.如果您使用的是 2D 图形,我会使用带有 sprite 选项的 go,因为它很容易使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM