Pyglet Programming Guide - xivilization

pyglet Programming Guide

pyglet Programming Guide

Table of Contents

Welcome .................................................................................................................... vi Sections .............................................................................................................. vi Table of contents ................................................................................................. vi

Installation ................................................................................................................... 1 Installing using setup.py ......................................................................................... 1 Installation from the runtime eggs ............................................................................ 1

Writing a pyglet application ............................................................................................ 2 Hello, World ........................................................................................................ 2 Image viewer ....................................................................................................... 2 Handling mouse and keyboard events ....................................................................... 3 Playing sounds and music ...................................................................................... 4 Where to next? ..................................................................................................... 4

Creating an OpenGL context ........................................................................................... 6 Displays, screens, configs and contexts ..................................................................... 6 Contexts and configs ..................................................................................... 6 Displays ...................................................................................................... 7 Screens ....................................................................................................... 7 OpenGL configuration options ................................................................................ 8 The default configuration .............................................................................. 10 Simple context configuration ................................................................................. 10 Selecting the best configuration ............................................................................. 11 Sharing objects between contexts ........................................................................... 11

The OpenGL interface ................................................................................................. 13 Using OpenGL ................................................................................................... 13 Resizing the window ........................................................................................... 14 Error checking .................................................................................................... 14 Using extension functions ..................................................................................... 15 Using multiple windows ....................................................................................... 15 AGL, GLX and WGL .......................................................................................... 15

Graphics .................................................................................................................... 17 Drawing primitives .............................................................................................. 17 Vertex attributes .................................................................................................. 18 Vertex lists ......................................................................................................... 19 Updating vertex data .................................................................................... 20 Data usage ................................................................................................. 21 Indexed vertex lists ...................................................................................... 21 Batched rendering ............................................................................................... 21 Setting the OpenGL state .............................................................................. 22 Hierarchical state ......................................................................................... 22 Sorting vertex lists ...................................................................................... 23 Batches and groups in other modules ...................................................................... 24

Windowing ................................................................................................................ 25 Creating a window .............................................................................................. 25 Context configuration ................................................................................... 25 Fullscreen windows ..................................................................................... 26 Size and position ................................................................................................. 26 Appearance ........................................................................................................ 27 Window style ............................................................................................. 27 Caption ...................................................................................................... 28 Icon .......................................................................................................... 28 Visibility ............................................................................................................ 28 Subclassing Window ............................................................................................ 29 Windows and OpenGL contexts ............................................................................. 29 Double-buffering ......................................................................................... 29 Vertical retrace synchronisation ..................................................................... 29

iii

pyglet Programming Guide

The application event loop ............................................................................................ 31 Customising the event loop ................................................................................... 31 Event loop events ........................................................................................ 31 Overriding the default idle policy ................................................................... 31 Dispatching events manually ................................................................................. 32

The pyglet event framework .......................................................................................... 33 Setting event handlers .......................................................................................... 33 Stacking event handlers ........................................................................................ 33 Creating your own event dispatcher ........................................................................ 35 Implementing the Observer pattern ................................................................. 36 Documenting events .................................................................................... 36

Working with the keyboard ........................................................................................... 38 Keyboard events ................................................................................................. 38 Defined key symbols ................................................................................... 38 Modifiers ................................................................................................... 39 User-defined key symbols ............................................................................. 40 Remembering key state ................................................................................ 40 Text and motion events ........................................................................................ 40 Motion events ............................................................................................. 41 Keyboard exclusivity ........................................................................................... 42

Working with the mouse .............................................................................................. 43 Mouse events ..................................................................................................... 43 Changing the mouse cursor ................................................................................... 44 Mouse exclusivity ............................................................................................... 46

Keeping track of time .................................................................................................. 47 Calling functions periodically ................................................................................ 47 Animation techniques ........................................................................................... 48 The frame rate .................................................................................................... 48 Displaying the frame rate .............................................................................. 48 User-defined clocks ............................................................................................. 49

Displaying text ........................................................................................................... 50 Simple text rendering ........................................................................................... 50 The document/layout model .................................................................................. 50 Documents ................................................................................................. 51 Layouts ..................................................................................................... 51 Formatted text .................................................................................................... 52 Character styles ........................................................................................... 52 Paragraph styles .......................................................................................... 53 Attributed text ............................................................................................ 53 HTML ....................................................................................................... 55 Custom elements ................................................................................................. 55 User-editable text ................................................................................................ 55 Loading system fonts ........................................................................................... 56 Font sizes .......................................................................................................... 57 Font resolution ............................................................................................ 57 Determining font size ................................................................................... 57 Loading custom fonts ........................................................................................... 57 Supported font formats ................................................................................. 58 OpenGL font considerations .................................................................................. 58 Context affinity ........................................................................................... 59 Blend state ................................................................................................. 59

Images ....................................................................................................................... 60 Loading an image ................................................................................................ 60 Supported image formats ...................................................................................... 61 Working with images ........................................................................................... 62 The AbstractImage hierarchy ................................................................................. 62 Accessing or providing pixel data .......................................................................... 63 Performance concerns .................................................................................. 64

iv

pyglet Programming Guide

Image sequences and atlases ................................................................................. 65 Image grids ................................................................................................ 65 3D textures ................................................................................................ 66 Texture bins and atlases ............................................................................... 67

Animations ......................................................................................................... 67 Buffer images ..................................................................................................... 68 Displaying images ............................................................................................... 69

Sprites ....................................................................................................... 69 Simple image blitting ................................................................................... 70 OpenGL imaging ................................................................................................ 71 Texture dimensions ...................................................................................... 71 Texture internal format ................................................................................. 72 Saving an image ................................................................................................. 73 Sound and video ......................................................................................................... 74 Audio drivers ..................................................................................................... 74 DirectSound ............................................................................................... 74 OpenAL ..................................................................................................... 75 ALSA ....................................................................................................... 75 Linux Issues ............................................................................................... 75 Supported media types ......................................................................................... 75 Loading media .................................................................................................... 76 Simple audio playback ......................................................................................... 77 Controlling playback ............................................................................................ 77 Incorporating video .............................................................................................. 79 Positional audio .................................................................................................. 79 Application resources ................................................................................................... 80 Loading resources ............................................................................................... 80 Resource locations ....................................................................................... 81 Specifying the resource path ................................................................................. 81 Multiple loaders .................................................................................................. 82 Saving user preferences ........................................................................................ 82 Debugging tools .......................................................................................................... 84 Debugging OpenGL ............................................................................................. 84 Error checking ............................................................................................ 85 Tracing ...................................................................................................... 85 Tracing execution ................................................................................................ 85 Platform-specific debugging .................................................................................. 85 Linux ........................................................................................................ 85 Windows ................................................................................................... 85 Appendix: Migrating to pyglet 1.1 ................................................................................. 86 Compatibility and deprecation ............................................................................... 86 Deprecated methods ............................................................................................. 86 New features replacing standard practice ................................................................. 86 Importing pyglet ......................................................................................... 86 Application event loop ................................................................................. 87 Loading resources ....................................................................................... 88 New graphics features .......................................................................................... 88 New text features ................................................................................................ 89 Other new features .............................................................................................. 89

v

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download