1
.. include:: common.txt
6
.. module:: pygame.display
7
:synopsis: pygame module to control the display window and screen
9
| :sl:`pygame module to control the display window and screen`
11
This module offers control over the pygame display. Pygame has a single display
12
Surface that is either contained in a window or runs full screen. Once you
13
create the display you treat it as a regular Surface. Changes are not
14
immediately visible onscreen; you must choose one of the two flipping functions
15
to update the actual display.
17
The origin of the display, where x = 0 and y = 0, is the top left of the
18
screen. Both axes increase positively towards the bottom right of the screen.
20
The pygame display can actually be initialized in one of several modes. By
21
default, the display is a basic software driven framebuffer. You can request
22
special modules like hardware acceleration and OpenGL support. These are
23
controlled by flags passed to ``pygame.display.set_mode()``.
25
Pygame can only have a single display active at any time. Creating a new one
26
with ``pygame.display.set_mode()`` will close the previous display. If precise
27
control is needed over the pixel format or display resolutions, use the
28
functions ``pygame.display.mode_ok()``, ``pygame.display.list_modes()``, and
29
``pygame.display.Info()`` to query information about the display.
31
Once the display Surface is created, the functions from this module affect the
32
single existing display. The Surface becomes invalid if the module is
33
uninitialized. If a new display mode is set, the existing Surface will
34
automatically switch to operate on the new display.
36
When the display mode is set, several events are placed on the pygame event
37
queue. ``pygame.QUIT`` is sent when the user has requested the program to
38
shutdown. The window will receive ``pygame.ACTIVEEVENT`` events as the display
39
gains and loses input focus. If the display is set with the
40
``pygame.RESIZABLE`` flag, ``pygame.VIDEORESIZE`` events will be sent when the
41
user adjusts the window dimensions. Hardware displays that draw direct to the
42
screen will get ``pygame.VIDEOEXPOSE`` events when portions of the window must
45
Some display environments have an option for automatically stretching all
46
windows. When this option is enabled, this automatic stretching distorts the
47
appearance of the pygame window. In the pygame examples directory, there is
48
example code (prevent_display_stretching.py) which shows how to disable this
49
automatic stretching of the pygame display on Microsoft Windows (Vista or newer
54
| :sl:`Initialize the display module`
55
| :sg:`init() -> None`
57
Initializes the pygame display module. The display module cannot do anything
58
until it is initialized. This is usually handled for you automatically when
59
you call the higher level ``pygame.init()``.
61
Pygame will select from one of several internal display backends when it is
62
initialized. The display mode will be chosen depending on the platform and
63
permissions of current user. Before the display module is initialized the
64
environment variable ``SDL_VIDEODRIVER`` can be set to control which backend
65
is used. The systems with multiple choices are listed here.
69
Windows : windib, directx
70
Unix : x11, dga, fbcon, directfb, ggi, vgl, svgalib, aalib
72
On some platforms it is possible to embed the pygame display into an already
73
existing window. To do this, the environment variable ``SDL_WINDOWID`` must
74
be set to a string containing the window id or handle. The environment
75
variable is checked when the pygame display is initialized. Be aware that
76
there can be many strange side effects when running in an embedded display.
78
It is harmless to call this more than once, repeated calls have no effect.
80
.. ## pygame.display.init ##
84
| :sl:`Uninitialize the display module`
85
| :sg:`quit() -> None`
87
This will shut down the entire display module. This means any active
88
displays will be closed. This will also be handled automatically when the
91
It is harmless to call this more than once, repeated calls have no effect.
93
.. ## pygame.display.quit ##
95
.. function:: get_init
97
| :sl:`Returns True if the display module has been initialized`
98
| :sg:`get_init() -> bool`
100
Returns True if the :mod:`pygame.display` module is currently initialized.
102
.. ## pygame.display.get_init ##
104
.. function:: set_mode
106
| :sl:`Initialize a window or screen for display`
107
| :sg:`set_mode(resolution=(0,0), flags=0, depth=0) -> Surface`
109
This function will create a display Surface. The arguments passed in are
110
requests for a display type. The actual created display will be the best
111
possible match supported by the system.
113
The resolution argument is a pair of numbers representing the width and
114
height. The flags argument is a collection of additional options. The depth
115
argument represents the number of bits to use for color.
117
The Surface that gets returned can be drawn to like a regular Surface but
118
changes will eventually be seen on the monitor.
120
If no resolution is passed or is set to (0, 0) and pygame uses ``SDL``
121
version 1.2.10 or above, the created Surface will have the same size as the
122
current screen resolution. If only the width or height are set to 0, the
123
Surface will have the same width or height as the screen resolution. Using a
124
``SDL`` version prior to 1.2.10 will raise an exception.
126
It is usually best to not pass the depth argument. It will default to the
127
best and fastest color depth for the system. If your game requires a
128
specific color format you can control the depth with this argument. Pygame
129
will emulate an unavailable color depth which can be slow.
131
When requesting fullscreen display modes, sometimes an exact match for the
132
requested resolution cannot be made. In these situations pygame will select
133
the closest compatible match. The returned surface will still always match
134
the requested resolution.
136
The flags argument controls which type of display you want. There are
137
several to choose from, and you can even combine multiple types using the
138
bitwise or operator, (the pipe "|" character). If you pass 0 or no flags
139
argument it will default to a software driven window. Here are the display
140
flags you will want to choose from:
144
pygame.FULLSCREEN create a fullscreen display
145
pygame.DOUBLEBUF recommended for HWSURFACE or OPENGL
146
pygame.HWSURFACE hardware accelerated, only in FULLSCREEN
147
pygame.OPENGL create an OpenGL renderable display
148
pygame.RESIZABLE display window should be sizeable
149
pygame.NOFRAME display window will have no border or controls
155
# Open a window on the screen
158
screen=pygame.display.set_mode([screen_width,screen_height])
160
.. ## pygame.display.set_mode ##
162
.. function:: get_surface
164
| :sl:`Get a reference to the currently set display surface`
165
| :sg:`get_surface() -> Surface`
167
Return a reference to the currently set display Surface. If no display mode
168
has been set this will return None.
170
.. ## pygame.display.get_surface ##
174
| :sl:`Update the full display Surface to the screen`
175
| :sg:`flip() -> None`
177
This will update the contents of the entire display. If your display mode is
178
using the flags ``pygame.HWSURFACE`` and ``pygame.DOUBLEBUF``, this will
179
wait for a vertical retrace and swap the surfaces. If you are using a
180
different type of display mode, it will simply update the entire contents of
183
When using an ``pygame.OPENGL`` display mode this will perform a gl buffer
186
.. ## pygame.display.flip ##
190
| :sl:`Update portions of the screen for software displays`
191
| :sg:`update(rectangle=None) -> None`
192
| :sg:`update(rectangle_list) -> None`
194
This function is like an optimized version of ``pygame.display.flip()`` for
195
software displays. It allows only a portion of the screen to updated,
196
instead of the entire area. If no argument is passed it updates the entire
197
Surface area like ``pygame.display.flip()``.
199
You can pass the function a single rectangle, or a sequence of rectangles.
200
It is more efficient to pass many rectangles at once than to call update
201
multiple times with single or a partial list of rectangles. If passing a
202
sequence of rectangles it is safe to include None values in the list, which
205
This call cannot be used on ``pygame.OPENGL`` displays and will generate an
208
.. ## pygame.display.update ##
210
.. function:: get_driver
212
| :sl:`Get the name of the pygame display backend`
213
| :sg:`get_driver() -> name`
215
Pygame chooses one of many available display backends when it is
216
initialized. This returns the internal name used for the display backend.
217
This can be used to provide limited information about what display
218
capabilities might be accelerated. See the ``SDL_VIDEODRIVER`` flags in
219
``pygame.display.set_mode()`` to see some of the common options.
221
.. ## pygame.display.get_driver ##
225
| :sl:`Create a video display information object`
226
| :sg:`Info() -> VideoInfo`
228
Creates a simple object containing several attributes to describe the
229
current graphics environment. If this is called before
230
``pygame.display.set_mode()`` some platforms can provide information about
231
the default display mode. This can also be called after setting the display
232
mode to verify specific display options were satisfied. The VidInfo object
233
has several attributes:
237
hw: True if the display is hardware accelerated
238
wm: True if windowed display modes can be used
239
video_mem: The megabytes of video memory on the display. This is 0 if unknown
240
bitsize: Number of bits used to store each pixel
241
bytesize: Number of bytes used to store each pixel
242
masks: Four values used to pack RGBA values into pixels
243
shifts: Four values used to pack RGBA values into pixels
244
losses: Four values used to pack RGBA values into pixels
245
blit_hw: True if hardware Surface blitting is accelerated
246
blit_hw_CC: True if hardware Surface colorkey blitting is accelerated
247
blit_hw_A: True if hardware Surface pixel alpha blitting is accelerated
248
blit_sw: True if software Surface blitting is accelerated
249
blit_sw_CC: True if software Surface colorkey blitting is accelerated
250
blit_sw_A: True if software Surface pixel alpha blitting is acclerated
251
current_h, current_h: Width and height of the current video mode, or of the
252
desktop mode if called before the display.set_mode is called.
253
(current_h, current_w are available since SDL 1.2.10, and pygame 1.8.0)
254
They are -1 on error, or if an old SDL is being used.
256
.. ## pygame.display.Info ##
258
.. function:: get_wm_info
260
| :sl:`Get information about the current windowing system`
261
| :sg:`get_wm_info() -> dict`
263
Creates a dictionary filled with string keys. The strings and values are
264
arbitrarily created by the system. Some systems may have no information and
265
an empty dictionary will be returned. Most platforms will return a "window"
266
key with the value set to the system id for the current display.
268
New with pygame 1.7.1
270
.. ## pygame.display.get_wm_info ##
272
.. function:: list_modes
274
| :sl:`Get list of available fullscreen modes`
275
| :sg:`list_modes(depth=0, flags=pygame.FULLSCREEN) -> list`
277
This function returns a list of possible dimensions for a specified color
278
depth. The return value will be an empty list if no display modes are
279
available with the given arguments. A return value of -1 means that any
280
requested resolution should work (this is likely the case for windowed
281
modes). Mode sizes are sorted from biggest to smallest.
283
If depth is 0, ``SDL`` will choose the current/best color depth for the
284
display. The flags defaults to ``pygame.FULLSCREEN``, but you may need to
285
add additional flags for specific fullscreen modes.
287
.. ## pygame.display.list_modes ##
289
.. function:: mode_ok
291
| :sl:`Pick the best color depth for a display mode`
292
| :sg:`mode_ok(size, flags=0, depth=0) -> depth`
294
This function uses the same arguments as ``pygame.display.set_mode()``. It
295
is used to determine if a requested display mode is available. It will
296
return 0 if the display mode cannot be set. Otherwise it will return a pixel
297
depth that best matches the display asked for.
299
Usually the depth argument is not passed, but some platforms can support
300
multiple display depths. If passed it will hint to which depth is a better
303
The most useful flags to pass will be ``pygame.HWSURFACE``,
304
``pygame.DOUBLEBUF``, and maybe ``pygame.FULLSCREEN``. The function will
305
return 0 if these display flags cannot be set.
307
.. ## pygame.display.mode_ok ##
309
.. function:: gl_get_attribute
311
| :sl:`Get the value for an OpenGL flag for the current display`
312
| :sg:`gl_get_attribute(flag) -> value`
314
After calling ``pygame.display.set_mode()`` with the ``pygame.OPENGL`` flag,
315
it is a good idea to check the value of any requested OpenGL attributes. See
316
``pygame.display.gl_set_attribute()`` for a list of valid flags.
318
.. ## pygame.display.gl_get_attribute ##
320
.. function:: gl_set_attribute
322
| :sl:`Request an OpenGL display attribute for the display mode`
323
| :sg:`gl_set_attribute(flag, value) -> None`
325
When calling ``pygame.display.set_mode()`` with the ``pygame.OPENGL`` flag,
326
Pygame automatically handles setting the OpenGL attributes like color and
327
doublebuffering. OpenGL offers several other attributes you may want control
328
over. Pass one of these attributes as the flag, and its appropriate value.
329
This must be called before ``pygame.display.set_mode()``
331
The ``OPENGL`` flags are;
335
GL_ALPHA_SIZE, GL_DEPTH_SIZE, GL_STENCIL_SIZE, GL_ACCUM_RED_SIZE,
336
GL_ACCUM_GREEN_SIZE, GL_ACCUM_BLUE_SIZE, GL_ACCUM_ALPHA_SIZE,
337
GL_MULTISAMPLEBUFFERS, GL_MULTISAMPLESAMPLES, GL_STEREO
339
.. ## pygame.display.gl_set_attribute ##
341
.. function:: get_active
343
| :sl:`Returns True when the display is active on the display`
344
| :sg:`get_active() -> bool`
346
After ``pygame.display.set_mode()`` is called the display Surface will be
347
visible on the screen. Most windowed displays can be hidden by the user. If
348
the display Surface is hidden or iconified this will return False.
350
.. ## pygame.display.get_active ##
352
.. function:: iconify
354
| :sl:`Iconify the display surface`
355
| :sg:`iconify() -> bool`
357
Request the window for the display surface be iconified or hidden. Not all
358
systems and displays support an iconified display. The function will return
361
When the display is iconified ``pygame.display.get_active()`` will return
362
False. The event queue should receive a ``ACTIVEEVENT`` event when the
363
window has been iconified.
365
.. ## pygame.display.iconify ##
367
.. function:: toggle_fullscreen
369
| :sl:`Switch between fullscreen and windowed displays`
370
| :sg:`toggle_fullscreen() -> bool`
372
Switches the display window between windowed and fullscreen modes. This
373
function only works under the unix x11 video driver. For most situations it
374
is better to call ``pygame.display.set_mode()`` with new display flags.
376
.. ## pygame.display.toggle_fullscreen ##
378
.. function:: set_gamma
380
| :sl:`Change the hardware gamma ramps`
381
| :sg:`set_gamma(red, green=None, blue=None) -> bool`
383
Set the red, green, and blue gamma values on the display hardware. If the
384
green and blue arguments are not passed, they will both be the same as red.
385
Not all systems and hardware support gamma ramps, if the function succeeds
388
A gamma value of 1.0 creates a linear color table. Lower values will darken
389
the display and higher values will brighten.
391
.. ## pygame.display.set_gamma ##
393
.. function:: set_gamma_ramp
395
| :sl:`Change the hardware gamma ramps with a custom lookup`
396
| :sg:`set_gamma_ramp(red, green, blue) -> bool`
398
Set the red, green, and blue gamma ramps with an explicit lookup table. Each
399
argument should be sequence of 256 integers. The integers should range
400
between 0 and 0xffff. Not all systems and hardware support gamma ramps, if
401
the function succeeds it will return True.
403
.. ## pygame.display.set_gamma_ramp ##
405
.. function:: set_icon
407
| :sl:`Change the system image for the display window`
408
| :sg:`set_icon(Surface) -> None`
410
Sets the runtime icon the system will use to represent the display window.
411
All windows default to a simple pygame logo for the window icon.
413
You can pass any surface, but most systems want a smaller image around
414
32x32. The image can have colorkey transparency which will be passed to the
417
Some systems do not allow the window icon to change after it has been shown.
418
This function can be called before ``pygame.display.set_mode()`` to create
419
the icon before the display mode is set.
421
.. ## pygame.display.set_icon ##
423
.. function:: set_caption
425
| :sl:`Set the current window caption`
426
| :sg:`set_caption(title, icontitle=None) -> None`
428
If the display has a window title, this function will change the name on the
429
window. Some systems support an alternate shorter title to be used for
432
.. ## pygame.display.set_caption ##
434
.. function:: get_caption
436
| :sl:`Get the current window caption`
437
| :sg:`get_caption() -> (title, icontitle)`
439
Returns the title and icontitle for the display Surface. These will often be
442
.. ## pygame.display.get_caption ##
444
.. function:: set_palette
446
| :sl:`Set the display color palette for indexed displays`
447
| :sg:`set_palette(palette=None) -> None`
449
This will change the video display color palette for 8bit displays. This
450
does not change the palette for the actual display Surface, only the palette
451
that is used to display the Surface. If no palette argument is passed, the
452
system default palette will be restored. The palette is a sequence of
455
.. ## pygame.display.set_palette ##
457
.. ## pygame.display ##