Introduction
We all know that graphics play a significant and crucial role in gaming. In gaming, it is all about the graphics. Hence if we talk about pyglet, graphics must be there in this technology.
So today, we will discuss the graphics in pyglet and the used libraries.
Graphics
Pyglet has an event loop and routines for 2D graphics (with the help of another library, OpenGL). Pyglet creates graphics in programme windows at the lowest level using OpenGL. The pyglet.gl module offers the OpenGL interface.
Using the OpenGL interface directly, on the other hand, can be time-consuming. The pyglet graphics module provides a more straightforward method of generating images using vertex arrays and buffer objects internally to improve speed.
Package pyglet.graphics
Over OpenGL, this module provides an efficient low-level abstraction. It renders OpenGL primitives very quickly, significantly faster than the standard immediate-mode implementation, and, in many situations, faster than utilising display lists on recent graphics cards. Other parts of the pyglet use the module internally.
For more information on how to utilise this graphics API, see the Programming Guide.
Batches and Groups
Developers can use Batch and Group objects to boost the performance of sprite and text rendering without having to grasp the technicalities of how to draw primitives with the graphics API.
The constructors of the Sprite, Label and TextLayout classes all accept a batch and group parameter. A batch is used to handle a collection of items that will be rendered all at once, while a group is used to specify how an object is drawn.
The example below generates a batch, adds two sprites to it, and then draws the entire batch:
#creating the batch
batch = pyglet.graphics.Batch()
#adding two sprites
book = pyglet.sprite.Sprite(book_image, batch=batch)
pen = pyglet.sprite.Sprite(pen_image, batch=batch)
# on_draw event
def on_draw()
batch.draw()
Drawing a batch as a whole is substantially faster than drawing each individual item in the batch, especially when the objects all belong to the same group.
Data item Parameters
The final parameters of many of the functions and methods in this module can be any number of data parameters.
A data parameter specifies the format of a vertex attribute as well as an optional sequence for initialising it. The following are some examples of typical attribute formats:
- v2f: Two float points are used to specify the vertex position.
- c3B: Three unsigned bytes are used to specify the vertex colour.
- t4f: Four float points are used to specify the texture coordinate.
Drawing Modes
Any value in the OpenGL drawing mode enumeration will be accepted by methods in this module that receive a mode parameter; for example, GL_POINTS, GL_LINES, GL_TRIANGLES, and so on.
GL_POLYGON, GL_LINE_LOOP, and GL_TRIANGLE_FAN cannot be utilised because of the way the graphics API produces multiple primitives with a shared state- the results are undefined.
Submodules
Here are some examples of submodules that help in the working of graphics in pyglet.
Classes
Here are some examples of classes that help in the working of graphics in pyglet.