block pixel shifting in Python/GTK

421 Views Asked by At

I want to make a sideways scrolling text box (a "ticker tape" display) using Python 2.6 and gtk+ (as per Centos 6.3).

I have made a timer driven routine that takes a text string and prints it repeatedly while incrementing the offset in the print window. That works but seems to be slightly more processor-intensive than I might like.

Rather than printing the string fully and repeatedly with an incrementing offset - is there a way to use block move acceleration in some way and benefit from the use of "blitting" hardware on most GPU's? I was wondering if the string can be printed to a pixel buffer of some type and then the relevant portion can be "blitted" to screen memory? Any comments or experience would be appreciated.

My target hardware is Intel 945GME based.

1

There are 1 best solutions below

0
On

My best attempt (so far) at some code to do this follows. No, I am not very experienced in these things and this is hardly "exemplary code" - but thought I should share "wot I dun"

import gtk
import pango
import glib
import cairo


class Scroller(gtk.DrawingArea):
    def __init__(self, TextString):
        super(Scroller, self).__init__()
        self.offset_x = 0
        self.offset_y = 0
        self.screen_x = 1000
        self.screen_y = 0
# process the y=text string to make an image of it
        self.p = self.create_pango_layout(TextString)
        self.p.set_font_description(pango.FontDescription('Sans 36'))
        attr = pango.AttrList()
        attr.insert(pango.AttrForeground(60535, 60535, 0, 0, -1))
        self.p.set_attributes(attr)
        self.p.set_width(-1)
        self.text_width, self.text_height = self.p.get_size()
        self.text_width = self.text_width / pango.SCALE
        self.text_height = self.text_height / pango.SCALE

#create a pixmap with the data of the pixbuf
        self.pixelbuffer = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8,  self.text_width, self.text_height)
        self.pixelbuffer.fill(0) 
        pixmap,_ = self.pixelbuffer.render_pixmap_and_mask()
        self.pgc = pixmap.new_gc()

        pixmap.draw_layout(self.pgc, 0, 0, self.p)
# now copy image back to pixelbuiffer
        self.pixelbuffer.get_from_drawable(pixmap, pixmap.get_colormap(), 0, 0, 0, 0, -1, -1)

    def initscreen(self):
        print "screen initialised"
        self.window.clear()
        self.update_display()

    def update_display(self):
        offset = -1
# this scrolls the text sideways
        self.screen_x = self.screen_x + offset
        self.window.draw_pixbuf(self.pgc, self.pixelbuffer, self.offset_x, self.offset_y, self.screen_x, self.screen_y)
# and repeat until bored.
        if self.screen_x == -self.text_width: 
            self.screen_x =1000
        return True


# Main program 
class PyApp(gtk.Window): 
    def __init__(self):
        super(PyApp, self).__init__()

        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(0, 0, 0))
        self.connect("destroy", gtk.main_quit)
        self.set_title("side scroller")


        scroller_screen=Scroller('Breaking News - scrolling text works! More soon....Olympic Games in London are highly entertaining. No more please!')
        self.add(scroller_screen)       
        self.set_size_request(1000, 100)
        self.show_all()
        scroller_screen.initscreen
# set a 20 millisec scroll refreash timer
        glib.timeout_add(20, scroller_screen.update_display)

PyApp()

gtk.main()