2. Renderer

The renderer is responsible for building the primitives, and complex objects to be drawn by pyglet. This module also contains various base classes for primitives which can be modified on the fly.

class renderer.Primitive(renderer=None)[source]

This is the base class for any primitive to draw to the screen. To be used most effectively, all library-provided primitives should be declared within renderer.Renderer.

Parameters:renderer (renderer.Renderer) – renderer instance to add this primitive to.
Raises :AttributeError if renderer is not passed to constructor and not instantiated from an instance of a renderer
class renderer.Renderer[source]

This class allows access to the primitives (the basic drawing building blocks) to draw just about anything. Widgets should use these to draw themselves:

class Button(RectWidget):
    def __init__(self, renderer, x, y, width, height, text):
        btn_base = renderer.Rectangle(x, y, width, height,
            color=(0.2, 0.3, 0.2))
        btn_text = renderer.Text(x, y, width, height, text)
        # etc.
        # etc.

primitives should be called through the Renderer instance instead of through the class (i.e.):

renderer = Renderer()
renderer.Rectangle()

instead of:

renderer = Renderer()
Renderer.Rectangle(renderer=renderer)

There is special python magic to make that work. See __init__() for that.

class Rectangle(x, y, width, height, texture=None, group=None, color=None, filled=True, **kwargs)[source]

This class creates a rectangle primitive.

Parameters:
  • x (float) – the x offset (from the left side of the screen) to draw the rectangle.
  • y (float) – is the y offset (from the bottom of the screen) to draw the rectangle.
  • width (float) – is the width of the rectangle
  • height (float) – is the height of the rectangle
  • texture – is the texture to paint the rectangle with
  • group (Group) – is the group, if any, that the rectangle should be associated with. Using texture will automatically make this a part of the appropriate TextureGroup and make group its parent.
  • color (3-tuple or 4-tuple of floats from 0 to 1) – is the color to paint the rectangle. If not specified, the renderer’s default fg_color will be used instead.
  • filled (boolean) – specified whether to draw this as a filled-in rectangle or rectangle outline.
move(x, y)

This move method actually modifies the vertex lists to update the positions of the polygons. This is most efficient when in unit-mode where only one unit is moving at a time.

This is because at this point very few objects are moving per frame. So all objects that have not moved take up zero cpu time.

This may not be as efficient when applied to normal, condensed, or liquid mode because far more polygons will be moving at the same time. It may be better to draw each vertex list separately after having gone through a matrix transformation.

As a final note, it may be best to move these primitives into a class built in cython so that python doesn’t have to deal with all this bullshit processing.

Parameters:
  • x (float or int) – The new x position.
  • y (float or int.) – The new y position.
Renderer.draw_frame()[source]

This method should be called at (or near) the end of every game loop after all the objects have been updated and are ready to draw to the screen.

This will draw the batch associated with the renderer.

Return type:None
Renderer.init_frame()[source]

This method should be called at the beginning of every game loop. It does the pre-loop set up, if any.

Return type:None

Previous topic

1. Application

Next topic

3. Input

This Page