Pages

Wednesday, June 12, 2013

pygame.display learning notes

Now I am learning pygame.display.

pygame.display

http://www.pygame.org/docs/ref/display.html

pygame module to control the display window and screen

pygame.display.init Initialize the display module

pygame.display.quit Uninitialize the display module

pygame.display.get_init Returns True if the display module has been initialized

pygame.display.set_mode Initialize a window or screen for display

pygame.display.get_surface Get a reference to the currently set display surface

pygame.display.flip Update the full display Surface to the screen

pygame.display.update Update portions of the screen for software displays

pygame.display.get_driver Get the name of the pygame display backend

pygame.display.Info Create a video display information object

pygame.display.get_wm_info Get information about the current windowing system

pygame.display.list_modes Get list of available fullscreen modes

pygame.display.mode_ok Pick the best color depth for a display mode

pygame.display.gl_get_attribute Get the value for an OpenGL flag for the current display

pygame.display.gl_set_attribute Request an OpenGL display attribute for the display mode

pygame.display.get_active Returns True when the display is active on the display

pygame.display.iconify Iconify the display surface

pygame.display.toggle_fullscreen Switch between fullscreen and windowed displays

pygame.display.set_gamma Change the hardware gamma ramps

pygame.display.set_gamma_ramp Change the hardware gamma ramps with a custom lookup

pygame.display.set_icon Change the system image for the display window

pygame.display.set_caption Set the current window caption

pygame.display.get_caption Get the current window caption

pygame.display.set_palette Set the display color palette for indexed displays


This module offers control over the pygame display. Pygame has a single display Surface that is either contained in a window or runs full screen. Once you create the display you treat it as a regular Surface. Changes are not immediately visible onscreen, you must choose one of the two flipping functions to update the actual display.

The origin of the display, where x = 0, and y = 0 is the top left of the screen. Both axis increase positively towards the bottom right of the screen.

The pygame display can actually be initialized in one of several modes. By default the display is a basic software driven framebuffer. You can request special modules like hardware acceleration and OpenGL support. These are controlled by flags passed to pygame.display.set_mode().

Pygame can only have a single display active at any time. Creating a new one with pygame.display.set_mode() will close the previous display. If precise control is needed over the pixel format or display resolutions, use the functions pygame.display.mode_ok(), pygame.display.list_modes(), and pygame.display.Info() to query information about the display.

Once the display Surface is created, the functions from this module effect the single existing display. The Surface becomes invalid if the module is uninitialized. If a new display mode is set, the existing Surface will automatically switch to operate on the new display.

Then the display mode is set, several events are placed on the pygame event queue. pygame.QUIT is sent when the user has requested the program to shutdown. The window will receive pygame.ACTIVEEVENT events as the display gains and loses input focus. If the display is set with the pygame.RESIZABLE flag, pygame.VIDEORESIZE events will be sent when the user adjusts the window dimensions. Hardware displays that draw direct to the screen will get pygame.VIDEOEXPOSE events when portions of the window must be redrawn.

pygame.display.init()

Initialize the display module

init() -> None

Initializes the pygame display module. The display module cannot do anything until it is initialized. This is usually handled for you automatically when you call the higher level pygame.init().

Pygame will select from one of several internal display backends when it is initialized. The display mode will be chosen depending on the platform and permissions of current user. Before the display module is initialized the environment variable SDL_VIDEODRIVER can be set to control which backend is used. The systems with multiple choices are listed here.

...

pygame.display.set_mode()

Initialize a window or screen for display

set_mode(resolution=(0,0), flags=0, depth=0) -> Surface

This function will create a display Surface. The arguments passed in are requests for a display type. The actual created display will be the best possible match supported by the system.

The resolution argument is a pair of numbers representing the width and height. The flags argument is a collection of additional options. The depth argument represents the number of bits to use for color.

The Surface that gets returned can be drawn to like a regular Surface but changes will eventually be seen on the monitor.

If no resolution is passed or is set to (0, 0) and pygame uses SDL version 1.2.10 or above, the created Surface will have the same size as the current screen resolution. If only the width or height are set to 0, the Surface will have the same width or height as the screen resolution. Using a SDL version prior to 1.2.10 will raise an exception.

It is usually best to not pass the depth argument. It will default to the best and fastest color depth for the system. If your game requires a specific color format you can control the depth with this argument. Pygame will emulate an unavailable color depth which can be slow.

When requesting fullscreen display modes, sometimes an exact match for the requested resolution cannot be made. In these situations pygame will select the closest compatible match. The returned surface will still always match the requested resolution.

The flags argument controls which type of display you want. There are several to choose from, and you can even combine multiple types using the bitwise or operator, (the pipe “|” character). If you pass 0 or no flags argument it will default to a software driven window. Here are the display flags you will want to choose from:

pygame.FULLSCREEN    create a fullscreen display

pygame.DOUBLEBUF     recommended for HWSURFACE or OPENGL

pygame.HWSURFACE     hardware accelerated, only in FULLSCREEN

pygame.OPENGL        create an OpenGL renderable display

pygame.RESIZABLE     display window should be sizeable

pygame.NOFRAME       display window will have no border or controls

For example:

# Open a window on the screen

screen_width=700

screen_height=400

screen=pygame.display.set_mode([screen_width,screen_height])

...

pygame.display.flip()

Update the full display Surface to the screen

flip() -> None

This will update the contents of the entire display. If your display mode is using the flags pygame.HWSURFACE and pygame.DOUBLEBUF, this will wait for a vertical retrace and swap the surfaces. If you are using a different type of display mode, it will simply update the entire contents of the surface.

When using an pygame.OPENGL display mode this will perform a gl buffer swap.

pygame.display.update()

Update portions of the screen for software displays

update(rectangle=None) -> None

update(rectangle_list) -> None

This function is like an optimized version of pygame.display.flip() for software displays. It allows only a portion of the screen to updated, instead of the entire area. If no argument is passed it updates the entire Surface area like pygame.display.flip().

You can pass the function a single rectangle, or a sequence of rectangles. It is more efficient to pass many rectangles at once than to call update multiple times with single or a partial list of rectangles. If passing a sequence of rectangles it is safe to include None values in the list, which will be skipped.

This call cannot be used on pygame.OPENGL displays and will generate an exception.

.END























No comments:

Post a Comment