Debian Codegraph @

examples/window/fixed_resolution.py

raw symbols SCIP
1 #!/usr/bin/env python
2 """Demonstrates one way of fixing the display resolution to a certain
3 size, but rendering to the full screen.
4
5 The method used in this example is:
6
7 1. Create a Framebuffer object, a Texture, and a depth buffer.
8 2. Attach the Texture and depth buffer to the Framebuffer.
9 3. Bind the Framebuffer as the current render target.
10 4. Render the scene using any OpenGL functions (here, just a shape).
11 5. Unbind the Framebuffer, and blit the Texture scaled to fill the screen.
12 """
13
14 from math import floor
15
16 import pyglet
17
18 from pyglet.gl import *
19
20
21 class FixedResolution:
22 def __init__(self, window, width, height):
23 self.window = window
24 self.window.set_minimum_size(width, height)
25 self.width = width
26 self.height = height
27
28 self._render_area = 0, 0, 0, 0, 0
29 self._mouse_scaling = 0, 0, 1.0, 1.0
30
31 self.framebuffer = pyglet.image.Framebuffer()
32 self._color_buffer = pyglet.image.Texture.create(width, height, min_filter=GL_NEAREST, mag_filter=GL_NEAREST)
33 self._depth_buffer = pyglet.image.buffer.Renderbuffer(width, height, GL_DEPTH_COMPONENT)
34 self.framebuffer.attach_texture(self._color_buffer)
35 self.framebuffer.attach_renderbuffer(self._depth_buffer, attachment=GL_DEPTH_ATTACHMENT)
36
37 self.window.push_handlers(self)
38
39 def get_mouse_scale(self, x, y, dx, dy):
40 offset_x, offset_y, scale_w, scale_h = self._mouse_scaling
41 scaled_x = floor((x - offset_x) * scale_w)
42 scaled_y = floor((y - offset_y) * scale_h)
43 scaled_dx = floor(dx * scale_w)
44 scaled_dy = floor(dy * scale_h)
45 return scaled_x, scaled_y, scaled_dx, scaled_dy
46
47 def on_resize(self, new_screen_width, new_screen_height):
48 aspect_ratio = self.width / self.height
49 aspect_width = new_screen_width
50 aspect_height = aspect_width / aspect_ratio + 0.5
51 if aspect_height > new_screen_height:
52 aspect_height = new_screen_height
53 aspect_width = aspect_height * aspect_ratio + 0.5
54
55 offset_x = int((new_screen_width / 2) - (aspect_width / 2))
56 offset_y = int((new_screen_height / 2) - (aspect_height / 2))
57 render_width = int(aspect_width)
58 render_height = int(aspect_height)
59
60 self._render_area = offset_x, offset_y, 0, render_width, render_height
61
62 # mouse resolution scaling:
63 width_scale = self.width / new_screen_width
64 height_scale = self.height / new_screen_height
65 # optional scaling for pillar/letterboxing:
66 pillar_scale = new_screen_width / render_width
67 letter_scale = new_screen_height / render_height
68
69 self._mouse_scaling = offset_x, offset_y, width_scale * pillar_scale, height_scale * letter_scale
70
71 def __enter__(self):
72 self.framebuffer.bind()
73 self.window.clear()
74
75 def __exit__(self, *unused):
76 self.framebuffer.unbind()
77 self._color_buffer.blit(*self._render_area)
78
79 def begin(self):
80 self.__enter__()
81
82 def end(self):
83 self.__exit__()
84
85
86 ###################################
87 # Simple program using the Viewport:
88 ###################################
89
90 if __name__ == '__main__':
91 window = pyglet.window.Window(960, 540, resizable=True)
92
93 # Create an instance of the FixedResolution class. Use
94 # 320x180 resolution to make the effect completely obvious:
95 fixed_res = FixedResolution(window, width=320, height=180)
96
97
98 @window.event
99 def on_draw():
100 window.clear()
101
102 # The FixedResolution instance can be used as a context manager:
103 with fixed_res:
104 rectangle.draw()
105
106 # # Alternatively, you can do it manually:
107 # fixed_res.begin()
108 # rectangle.draw()
109 # fixed_res.end()
110
111
112 # Create a simple Rectangle to show the effect
113 rectangle = pyglet.shapes.Rectangle(x=160, y=90, color=(200, 50, 50), width=100, height=100)
114 rectangle.anchor_position = 50, 50
115
116 # Combine update & drawing to avoid stutter from mismatched update rates
117 def update(dt):
118 global rectangle
119 rectangle.rotation += dt * 10
120
121 # This method automatically calls any on_draw method registered
122 # using @window.event, as we did above.
123 window.draw(dt)
124
125
126 # Create a simple Rectangle to show the effect
127 rectangle = pyglet.shapes.Rectangle(x=160, y=90, color=(200, 50, 50), width=100, height=100)
128 rectangle.anchor_position = 50, 50
129
130
131 # Call the combined update & redraw function at 60 FPS
132 pyglet.clock.schedule_interval(update, 1/60)
133
134
135 # Start the example
136 pyglet.app.run()