Skip to content

Commit e5e009b

Browse files
committed
Support enforcing aspect ratio independent of window buffer size
1 parent 8bb078c commit e5e009b

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

demosys/conf/default_settings.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
# Window size
2424
WINDOW = {
2525
"size": (1280, 720),
26+
"aspect_ratio": 16 / 9,
2627
"fullscreen": False,
27-
"resizable": False,
28+
"resizable": True,
2829
"title": "demosys-py",
2930
"vsync": True,
30-
"cursor": False,
31+
"cursor": True,
3132
}
3233

3334
MUSIC = None

demosys/opengl/fbo.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
from OpenGL import GL
22
from demosys.opengl import Texture
33

4-
# Reference to the window
5-
WINDOW = None
4+
WINDOW_FBO = None
65

76

87
class WindowFBO:
98
"""
109
Fake FBO representing default render target
1110
"""
12-
def __init__(self):
13-
self.window = WINDOW
14-
self.color_buffers = []
15-
self.color_buffers_ids = []
16-
self.depth_buffer = None
11+
def __init__(self, window):
12+
self.window = window
1713

1814
def bind(self):
1915
"""
2016
Sets the viewport back to the buffer size of the screen/window
2117
"""
22-
GL.glViewport(0, 0, WINDOW.buffer_width, WINDOW.buffer_height)
18+
# The expected height with the current viewport width
19+
expected_height = int(self.window.buffer_width / self.window.aspect_ratio)
20+
# How much positive or negative y padding
21+
blank_space = self.window.buffer_height - expected_height
22+
23+
GL.glViewport(0, int(blank_space / 2),
24+
self.window.buffer_width, expected_height)
2325

2426
def release(self):
2527
"""
@@ -34,9 +36,6 @@ def clear(self):
3436
pass
3537

3638

37-
WINDOW_FBO = WindowFBO()
38-
39-
4039
class FBO:
4140
"""Frame buffer object"""
4241
def __init__(self):

demosys/view/controller.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from demosys.view.window import Window
88
from demosys.effects.registry import Effect
99
from demosys.opengl import fbo
10-
from demosys.opengl.fbo import WINDOW_FBO
1110
from demosys import resources
1211
from demosys.conf import settings
1312
from demosys.scene import camera
@@ -31,18 +30,19 @@ def run(manager=None):
3130

3231
global WINDOW
3332
WINDOW = Window()
34-
fbo.WINDOW = WINDOW
33+
34+
fbo.WINDOW_FBO = fbo.WindowFBO(WINDOW)
3535

3636
print("Loader started at", glfw.get_time())
3737

3838
# Inject attributes into the base Effect class
3939
Effect.window_width = WINDOW.buffer_width
4040
Effect.window_height = WINDOW.buffer_height
41-
Effect.window_aspect = WINDOW.width / WINDOW.height
41+
Effect.window_aspect = WINDOW.aspect_ratio
4242

4343
# Set up the default system camera
4444
global CAMERA
45-
CAMERA = camera.SystemCamera(aspect=Effect.window_aspect, fov=60.0, near=1, far=1000)
45+
CAMERA = camera.SystemCamera(aspect=WINDOW.aspect_ratio, fov=60.0, near=1, far=1000)
4646
Effect.sys_camera = CAMERA
4747

4848
# Initialize Effects
@@ -77,12 +77,13 @@ def run(manager=None):
7777

7878
# Set the viewport as FBOs will change the values
7979
GL.glViewport(0, 0, WINDOW.buffer_width, WINDOW.buffer_height)
80+
8081
# Clear the buffer
8182
GL.glClearColor(0.0, 0.0, 0.0, 0.0)
8283
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT)
8384

8485
# Tell the manager to draw stuff
85-
manager.draw(t, ft, WINDOW_FBO)
86+
manager.draw(t, ft, fbo.WINDOW_FBO)
8687

8788
# Swap buffers and deal with events and statistics
8889
WINDOW.swap_buffers()

demosys/view/window.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def __init__(self):
1919
self.height = settings.WINDOW['size'][1]
2020
self.resizable = settings.WINDOW.get('resizable') or False
2121
self.title = settings.WINDOW.get('title') or "demosys-py"
22+
self.aspect_ratio = settings.WINDOW.get('aspect_ratio', 16 / 9)
2223

2324
if not glfw.init():
2425
raise ValueError("Failed to initialize glfw")
@@ -70,8 +71,6 @@ def __init__(self):
7071
if not settings.WINDOW.get('cursor'):
7172
glfw.set_input_mode(self.window, glfw.CURSOR, glfw.CURSOR_DISABLED)
7273

73-
glfw.set_window_aspect_ratio(self.window, self.width, self.height)
74-
7574
# Get the actual buffer size of the window
7675
# This is important for some displays like Apple's Retina as reported window sizes are virtual
7776
self.buffer_width, self.buffer_height = glfw.get_framebuffer_size(self.window)

0 commit comments

Comments
 (0)