Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale Resolution in Pyglet

I am using the pyglet (OpenGL) library and I want to be able to change the virtual resolution without changing the size of the window. For example a 2x2 box would be drawn as 4x4 pixels on the screen. I know I can find everything that is being draw and scale it individually, but this would probably be costly. I could not find a solution for this online (if it even exists), so any help would be greatly appreciated. Clarification: I am thinking along the lines of the resolution settings in most games. The window stays in full screen at the same size, but the scale changes.

like image 643
Nick Avatar asked Oct 18 '25 13:10

Nick


2 Answers

You need to import openGL to get access to the scaling function:

from pgylet.gl import *

Next, toss in the following code after your game's window has been initialized:

#These arguments are x, y and z respectively. This scales your window.
glScalef(2.0, 2.0, 2.0)

At this point your resolution will double, but your window will stay the same size. You can correct this easily by doubling your window's width and height. Furthermore, your textures will appear blurry, so we need to fix that. We need to set parameters for the textures in your on_draw() function:

def on_draw(self):
        self.clear() #clears the screen
        #The following two lines will change how textures are scaled.
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        self.label.draw() #blits the label to the screen

You should now have pixels displaying at double their original size.

like image 71
Vine Avatar answered Oct 21 '25 03:10

Vine


Clarification: I am thinking along the lines of the resolution settings in most games. The window stays in full screen at the same size, but the scale changes.

No, the window does change the size, because the screen resolution is changed, and the window follows.

However what you intend to do it perfectly possible: First render your image to a FBO of the desired smaller size, then render the contents of that FBO covering the full window. In the case a 3D engine uses some form of post processing (like for depth of field, color grading, compositing effects), this comes virtually for free.

like image 41
datenwolf Avatar answered Oct 21 '25 01:10

datenwolf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!