示例#1
0
    def write_text(self,
                   text,
                   position=None,
                   font=gl_font("fixed", 13),
                   colour=(0, 1, 0)):
        """
        Write text on the screen.

        Parameters
        ----------
        text : str
            The text to write.
        position : Optional[Sequence[int]]
            A sequence containing the horizontal and vertical positions, in
            pixels, of the lower left pixel of the first line of the text.
            Default is 40% of the screen right of centre, and 80% of the screen
            above centre.
        font : Optional[ctypes.c_void_p]
            The font to use. Default is 13-point Fixed.
        colour : Optional[Sequence[float]]
            The text colour, as RGB values between 0 and 1. Default is green.

        """
        if position is None:
            x = self.width * 0.2
            y = self.height * 0.4
        else:
            x, y = position
        with gl_ortho(self.width, self.height):
            with new_state():
                gl.glColor3fv(colour)
                gl.glRasterPos2f(x, y)
                glut.glutBitmapString(font, text)
示例#2
0
    def draw_background(self,
                        texture_number=1,
                        scale=1,
                        centre=None,
                        rotation=0):
        """
        Draw the background image.

        Parameters
        ----------
        texture_number : Optional[int]
            The number of the texture, by the order it was added. Default is
            the latest texture added.
        scale : Optional[float]
            The amount of zoom applied to the image. Default is no zoom.
        centre : Optional[tuple[int]]
            The coordinates of the centre of zoom. Default is the centre of the
            image.
        rotation : Optional[float]
            The amount of clockwise rotation, in degrees. Default is no
            rotation.

        """

        # TODO: Fix method for zoom.
        def find_vertices(x, y):
            # centre_x, centre_y = centre

            # Temporarily turn off zooming
            # scale = 1
            centre_x, centre_y = self.size / 2

            # Real zooming code.
            vertex_x = self.width / 2 - scale * (centre_x - self.width * x)
            vertex_y = self.height / 2 - scale * (centre_y - self.height * y)
            return vertex_x, vertex_y

        # Clear background
        if texture_number != 0:
            self.draw_background(texture_number=0)

        try:
            self.select_texture(texture_number)
            if "no_texture" in self.text:
                del self.text["no_texture"]
        except IndexError:
            self.text["no_texture"] = ("No textures yet", None, None, (1, 0,
                                                                       0))
            return

        if centre is None:
            centre = self.size / 2

        with gl_flag(gl.GL_TEXTURE_2D):
            with gl_ortho(self.width, self.height):
                gl.glRotate(rotation, 0, 0, 1)
                gl.glTranslate(-self.width / 2, -self.height / 2, 0)
                with gl_primitive(gl.GL_QUADS):
                    for x, y in ((0, 0), (0, 1), (1, 1), (1, 0)):
                        gl.glTexCoord2f(x, y)
                        tx, ty = find_vertices(x, y)
                        gl.glVertex(tx, ty, 0)