Here is my followup for anyone else that needs to draw text like a
console.  The code I have is pasted at the bottom.

My goal was to pop up a 80x25 console window, and then draw black text,
colored text, underlined text, and inverted text to it.


The code is mostly there except 1) I can't switch the window background
from black to white and 2) I can't change the text color either.


On Wed, Jun 20, 2012 at 5:17 AM, Txema Vicente <[email protected]> wrote:

> Take a look at this classes:
>
> document = pyglet.text.document.**FormattedDocument()
> textbox = pyglet.text.layout.**IncrementalTextLayout(**document, w, h,
> batch=batch)
>
>
About 95% of the files in pyglet/text/* are about attributes and text
layout.  _GlyphBox.place() seems to be the code to do the color, back color
and underline via extending arrays of vertices/quads.  I don't know OpenGL
well enough but it seems like moving this into the text API would allow
this to be used by all.  I suspect that a line of text could involve 80
rectangles (triangles?) plus 80 little line segments underneath to
underline with this method though.

If this is the only way pyglet supports colored text then I'd love the
right vertices code to paste in and get it done.  I was hoping though to
just pass in a color, when not none, like pyglet.font.GlyphString(text,
glyphs, x, y, black, white), or even better, have the colors in a context
so they don't have to keep being passed around when drawing lots of text.


-Roger

console.py:
import sys

import pyglet


class Console():

        def __init__(self):
                self.font = pyglet.font.load('Arial', 14)  # substitute a
monospaced font in
                # get the font metrics (average width and height).  Use '0'
as average.
                text= '0'
                glyphs = self.font.get_glyphs(text)
                glyph_string = pyglet.font.GlyphString(text, glyphs)
                self.char_width = glyph_string.get_subwidth(0, 1)
                self.char_height = self.font.ascent - self.font.descent
                #self.char_width = 9
                #self.char_height = 14

                self.window = pyglet.window.Window(80 * self.char_width, 25
* self.char_height, caption='console 80x25')
                pyglet.gl.glEnable( pyglet.gl.GL_TEXTURE_2D)


        def handle_events(self):
                if self.window.has_exit:
                        sys.exit()

                self.window.dispatch_events()


        def draw(self):
                self.window.clear()  # how to clear to white instead of
black?
                x = 0
                y = self.window.height

                # black text
                text= 'hello world'
                glyphs = self.font.get_glyphs(text)
                glyph_string = pyglet.font.GlyphString(text, glyphs, x, y -
self.font.ascent)
                glyph_string.draw()
                y = y - self.char_height

                # red text
                text= 'hello red world'
                glyphs = self.font.get_glyphs(text)
                glyph_string = pyglet.font.GlyphString(text, glyphs, x, y -
self.font.ascent)
                glyph_string.draw()  # how to set red?
                y = y - self.char_height

                # underlined text
                text= 'hello underlined world'
                glyphs = self.font.get_glyphs(text)
                glyph_string = pyglet.font.GlyphString(text, glyphs, x, y -
self.font.ascent)
                glyph_string.draw()  # how to underline?
                y = y - self.char_height

                # white text and black background
                text= 'hello inverted world'
                glyphs = self.font.get_glyphs(text)
                glyph_string = pyglet.font.GlyphString(text, glyphs, x, y -
self.font.ascent)
                glyph_string.draw()
                y = y - self.char_height

                self.window.flip()


con = Console()

# It's preferable to use pyglet.app.run() to call your draw routine as
# needed (decorate the draw function).  But if that's not possible, handle
# events frequently, but without consuming all the CPU.
while True:
    con.handle_events()
    con.draw()

-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en.

Reply via email to