Skip to content

Commit 6fddc81

Browse files
authored
GLFW window start at correct size and draw during resize (#70)
1 parent d695e95 commit 6fddc81

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

rendercanvas/glfw.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def get_physical_size(window):
147147

148148

149149
def enable_glfw():
150-
glfw.init()
150+
glfw.init() # this also resets all window hints
151151
glfw._rc_alive = True
152152

153153

@@ -180,13 +180,15 @@ def __init__(self, *args, present_method=None, **kwargs):
180180
# Set window hints
181181
glfw.window_hint(glfw.CLIENT_API, glfw.NO_API)
182182
glfw.window_hint(glfw.RESIZABLE, True)
183+
glfw.window_hint(glfw.VISIBLE, False) # start hidden
183184

184185
# Create the window (the initial size may not be in logical pixels)
185186
self._window = glfw.create_window(640, 480, "", None, None)
186187

187188
# Other internal variables
188189
self._changing_pixel_ratio = False
189190
self._is_minimized = False
191+
self._is_in_poll_events = False
190192

191193
# Register callbacks. We may get notified too often, but that's
192194
# ok, they'll result in a single draw.
@@ -218,6 +220,9 @@ def __init__(self, *args, present_method=None, **kwargs):
218220
# Set size, title, etc.
219221
self._final_canvas_init()
220222

223+
# Now show the window
224+
glfw.show_window(self._window)
225+
221226
def _on_window_dirty(self, *args):
222227
self.request_draw()
223228

@@ -297,7 +302,11 @@ def _set_logical_size(self, new_logical_size):
297302

298303
def _rc_gui_poll(self):
299304
glfw.post_empty_event() # Awake the event loop, if it's in wait-mode
300-
glfw.poll_events()
305+
try:
306+
self._is_in_poll_events = True
307+
glfw.poll_events() # Note: this blocks when the window is being resized
308+
finally:
309+
self._is_in_poll_events = False
301310
self._maybe_close()
302311

303312
def _rc_get_present_methods(self):
@@ -368,6 +377,15 @@ def _on_pixelratio_change(self, *args):
368377
def _on_size_change(self, *args):
369378
self._determine_size()
370379
self.request_draw()
380+
# During a resize, the glfw.poll_events() function blocks, so
381+
# our event-loop is on pause. However, glfw still sends resize
382+
# events, and we can use these to draw, to get a smoother
383+
# experience. Note that if the user holds the mouse still while
384+
# resizing, there are no draws. Also note that any animations
385+
# that rely on the event-loop are paused (only animations
386+
# updated in the draw callback are alive).
387+
if self._is_in_poll_events and not self._is_minimized:
388+
self._draw_frame_and_present()
371389

372390
def _on_mouse_button(self, window, but, action, mods):
373391
# Map button being changed, which we use to update self._pointer_buttons.

0 commit comments

Comments
 (0)