Où sommes nous ?

Retour

Text drawing

A small example aiming to display some texts on the screen, inspired by DirectFB own tutorial.

Initialisation

Classical shebang to use Séléné as script interpretor. Obviously, the path has to be changed as per your own installation.

#!./Selene

Tell to Séléné we want to use DirectFB and pass some option to it :

Selene.UseDirectFB("--dfb:quiet,no-vt")

We want whole screen and then we get its primary surface.
Finally, clear it and get its size.

DirectFB.init( DirectFB.CooperativeConst('FULLSCREEN') )
srf = SelSurface.create { caps=SelSurface.CapabilityConst('PRIMARY') }
w,h = srf:GetSize()
srf:Clear(0,0,0,0)

Loading ...

Load our font : obviously, you will have to adapt the path as per your own installation.
This font comes from : http://www.dafont.com

font = SelFont.create("/usr/local/share/fonts/corpuscare_light.ttf", { height=30, width=25} )

The sizes are only hints, meaning the real hight can different and has to be retrieved like this :

fonth = font:GetHeight()

Now, we have to make it the active one on our surface.

srf:SetFont( font )

This is meaning that each and every (sub)surfaces may have different active font.

Using ...

 A simple text for which the top left corner is at pixel (15,20)

srf:SetColor( 0xff, 0xff, 0xff, 0xff)	-- forground color : white
srf:DrawString("This is a test", 15,20)

another one, the line bellow : directFB doesn't handle by itself the notion of "text cursor", we have to specify every time the absolute place of the text.

srf:DrawString("The line bellow", 15,20+fonth)

Idem, if we want to place a text after, we need to calculate its position

srf:SetColor( 0x80, 0xff, 0x80, 0xff ) -- New color
offset = font:StringWidth("The line bellow")
srf:DrawString("- After", 15 + offset,20+fonth )

For an application that has to display random text, like a console, it's obviously convenient to create an upper class that hand pseudo cursor movement ... but it's out of DirectFB scope which is dealing only with low level graphics.

SelSurface's DrawString() accepts an additionnal parameter to specify text justification

srf:DrawString("An other one on the right", w-15,20+fonth*2, 
SelSurface.TextLayoutConst("RIGHT") + SelSurface.TextLayoutConst("TOP"))

TextLayoutConst() knows following (self explanatiory) justification :

and

Cleaning ...

font:destroy()
srf:destroy()

The full source code is here for the following result.


Visitez :
La liste de nos voyages
Nos sorties Ski et rando
Copyright Laurent Faillie 2001-2017
N'oubliez pas d'entrer le mot de passe pour voir aussi les photos perso.
Contactez moi si vous souhaitez réutiliser ces photos et pour les obtenir avec une plus grande résolution.
Visites durant les 7 derniers jours Nombre de visites au total.

Vous pouvez laissez un commentaire sur cette page.