Skip to content

Commit b8b89d3

Browse files
authored
Merge pull request #2038 from robertpfeiffer/better-vsync
Display doc changes
2 parents 7837635 + 7a0997d commit b8b89d3

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

docs/reST/ref/display.rst

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,15 @@ required).
171171

172172
.. versionadded:: 2.0.0 ``SCALED``, ``SHOWN`` and ``HIDDEN``
173173

174+
.. versionadded:: 2.0.0 ``vsync`` parameter
175+
174176
By setting the ``vsync`` parameter to ``1``, it is possible to get a display
175-
with vertical sync at a constant frame rate. Subsequent calls to
176-
:func:`pygame.display.flip()` will block (i.e. *wait*) until the screen has
177-
refreshed.
177+
with vertical sync at a constant frame rate determined by the monitor and
178+
graphics drivers. Subsequent calls to :func:`pygame.display.flip()` or
179+
:func:`pygame.display.update()` will block (i.e. *wait*) until the screen
180+
has refreshed, in order to prevent "screen tearing"
181+
<https://en.wikipedia.org/wiki/Screen_tearing>.
182+
178183
Be careful when using this feature together with ``pygame.time.Clock`` or
179184
:func:`pygame.time.delay()`, as multiple forms of waiting and frame rate
180185
limiting may interact to cause skipped frames.
@@ -185,7 +190,7 @@ required).
185190
``set_mode()`` may raise an exception.
186191

187192
Setting the ``vsync`` parameter to ``-1`` in conjunction with ``OPENGL``
188-
will request the OpenGL-specific feature "adaptive vsync".
193+
will request the OpenGL-specific feature "adaptive vsync" <https://www.khronos.org/opengl/wiki/Swap_Interval#Adaptive_Vsync>.
189194

190195
Here is an example usage of a call
191196
to ``set_mode()`` that may give you a display with vsync:
@@ -739,9 +744,20 @@ required).
739744

740745
.. versionaddedold:: 2.0.0
741746

747+
.. function:: is_fullscreen
748+
749+
| :sl:`Returns True if the pygame window created by pygame.display.set_mode() is in full-screen mode`
750+
| :sg:`is_fullscreen() -> bool`
751+
752+
Edge cases:
753+
If the window is in windowed mode, but maximized, this will return `False`.
754+
If the window is in "borderless fullscreen" mode, this will return `True`.
755+
756+
.. versionadded:: 2.2.0
757+
742758
.. function:: is_vsync
743759

744-
| :sl:`Returns True if vertical synchronisation for pygame.display.flip() is enabled`
760+
| :sl:`Returns True if vertical synchronisation for pygame.display.flip() and pygame.display.update() is enabled`
745761
| :sg:`is_vsync() -> bool`
746762
747763
.. versionadded:: 2.2.0

examples/glcube.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
print("numpy missing. The GLCUBE example requires: pyopengl numpy")
4242
raise SystemExit
4343

44-
4544
# do we want to use the 'modern' OpenGL API or the old one?
4645
# This example shows you how to do both.
4746
USE_MODERN_GL = True
@@ -508,7 +507,7 @@ def draw_cube_modern(shader_data, filled_cube_indices, outline_cube_indices, rot
508507

509508
# Rotate cube
510509
# rotation.theta += 1.0 # degrees
511-
rotation.phi += 1.0 # degrees
510+
# rotation.phi += 1.0 # degrees
512511
# rotation.psi += 1.0 # degrees
513512
model = eye(4, dtype=float32)
514513
# rotate(model, rotation.theta, 0, 0, 1)
@@ -521,7 +520,9 @@ def main():
521520
"""run the demo"""
522521

523522
# initialize pygame-ce and setup an opengl display
523+
524524
pygame.init()
525+
clock = pygame.time.Clock()
525526

526527
gl_version = (3, 0) # GL Version number (Major, Minor)
527528
if USE_MODERN_GL:
@@ -535,19 +536,24 @@ def main():
535536
pygame.GL_CONTEXT_PROFILE_MASK, pygame.GL_CONTEXT_PROFILE_CORE
536537
)
537538

538-
fullscreen = False # start in windowed mode
539-
539+
# start in windowed mode
540540
display_size = (640, 480)
541-
pygame.display.set_mode(
542-
display_size, pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE
543-
)
541+
try:
542+
pygame.display.set_mode(
543+
display_size, pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE, vsync=1
544+
)
545+
except:
546+
pygame.display.set_mode(
547+
display_size, pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE
548+
)
544549

545550
if USE_MODERN_GL:
546551
gpu, f_indices, o_indices = init_gl_modern(display_size)
547552
rotation = Rotation()
548553
else:
549554
init_gl_stuff_old()
550555

556+
delta_time = 0
551557
going = True
552558
while going:
553559
# check for quit'n events
@@ -559,34 +565,41 @@ def main():
559565
going = False
560566

561567
elif event.type == pygame.KEYDOWN and event.key == pygame.K_f:
562-
if not fullscreen:
568+
if not pygame.display.is_fullscreen():
563569
print("Changing to FULLSCREEN")
564570
pygame.display.set_mode(
565-
(640, 480), pygame.OPENGL | pygame.DOUBLEBUF | pygame.FULLSCREEN
571+
display_size,
572+
pygame.OPENGL | pygame.DOUBLEBUF | pygame.FULLSCREEN,
566573
)
567574
else:
568575
print("Changing to windowed mode")
569576
pygame.display.set_mode(
570-
(640, 480), pygame.OPENGL | pygame.DOUBLEBUF
577+
display_size, pygame.OPENGL | pygame.DOUBLEBUF
571578
)
572-
fullscreen = not fullscreen
579+
573580
if gl_version[0] >= 4 or (gl_version[0] == 3 and gl_version[1] >= 2):
574581
gpu, f_indices, o_indices = init_gl_modern(display_size)
575582
rotation = Rotation()
576583
else:
577584
init_gl_stuff_old()
578585

586+
# orbit camera around by 60 degrees per second
587+
angle = (delta_time / 1000) * 60
579588
if USE_MODERN_GL:
589+
rotation.phi += angle
580590
draw_cube_modern(gpu, f_indices, o_indices, rotation)
581591
else:
582592
# clear screen and move camera
583593
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
584-
# orbit camera around by 1 degree
585-
GL.glRotatef(1, 0, 1, 0)
594+
# rotate camera to angle
595+
GL.glRotatef(angle, 0, 1, 0)
586596
drawcube_old()
587597

588598
pygame.display.flip()
589-
pygame.time.wait(10)
599+
if pygame.display.is_vsync():
600+
delta_time = clock.tick()
601+
else:
602+
delta_time = clock.tick(60)
590603

591604
pygame.quit()
592605

src_c/display.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,7 +2721,8 @@ static PyMethodDef _pg_display_methods[] = {
27212721
DOC_DISPLAY_TOGGLEFULLSCREEN},
27222722

27232723
{"_set_autoresize", (PyCFunction)pg_display_set_autoresize, METH_O,
2724-
"provisional API, subject to change"},
2724+
"DEPRECATED, never officially supported, kept only for compatibility "
2725+
"with release candidate"},
27252726
{"_resize_event", (PyCFunction)pg_display_resize_event, METH_O,
27262727
"DEPRECATED, never officially supported, kept only for compatibility "
27272728
"with release candidate"},
@@ -2730,8 +2731,7 @@ static PyMethodDef _pg_display_methods[] = {
27302731
{"get_desktop_sizes", (PyCFunction)pg_get_desktop_screen_sizes,
27312732
METH_NOARGS, DOC_DISPLAY_GETDESKTOPSIZES},
27322733
{"is_fullscreen", (PyCFunction)pg_is_fullscreen, METH_NOARGS,
2733-
"provisional API, subject to change"},
2734-
2734+
DOC_DISPLAY_ISFULLSCREEN},
27352735
{"is_vsync", (PyCFunction)pg_is_vsync, METH_NOARGS, DOC_DISPLAY_ISVSYNC},
27362736
{"get_desktop_refresh_rates", (PyCFunction)pg_desktop_refresh_rates,
27372737
METH_NOARGS, DOC_DISPLAY_GETDESKTOPREFRESHRATES},

src_c/doc/display_doc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define DOC_DISPLAY_GETWINDOWSIZE "get_window_size() -> tuple\nReturn the size of the window or screen"
2929
#define DOC_DISPLAY_GETALLOWSCREENSAVER "get_allow_screensaver() -> bool\nReturn whether the screensaver is allowed to run."
3030
#define DOC_DISPLAY_SETALLOWSCREENSAVER "set_allow_screensaver(bool) -> None\nSet whether the screensaver may run"
31-
#define DOC_DISPLAY_ISVSYNC "is_vsync() -> bool\nReturns True if vertical synchronisation for pygame.display.flip() is enabled"
31+
#define DOC_DISPLAY_ISFULLSCREEN "is_fullscreen() -> bool\nReturns True if the pygame window created by pygame.display.set_mode() is in full-screen mode"
32+
#define DOC_DISPLAY_ISVSYNC "is_vsync() -> bool\nReturns True if vertical synchronisation for pygame.display.flip() and pygame.display.update() is enabled"
3233
#define DOC_DISPLAY_GETCURRENTREFRESHRATE "get_current_refresh_rate() -> int\nReturns the screen refresh rate or 0 if unknown"
3334
#define DOC_DISPLAY_GETDESKTOPREFRESHRATES "get_desktop_refresh_rates() -> list\nReturns the screen refresh rates for all displays (in windowed mode)."

0 commit comments

Comments
 (0)