Hardware Reference
In-Depth Information
whole line. his also makes the selection for the words used on a line easier as you only have
to do it for each line once. he new method of line scrolling is shown in Figure 4-4.
Figure 4-4:
Scrolling
part 2 - how
the lines scroll.
his involves copying the memory in the text surface from one location in the list to the next
up, when the ofset has reached the same height as the text surface. However, you have to do
this from the top down, or else you end up with all the text reading the same one line. hen,
new text is rendered into text surface 0. When the copying is complete the ofset is set to
zero, and the screen looks exactly as it did before - only a new line is hidden, clipped at the
bottom of the screen, waiting to scroll into view.
he next major improvement in this version is the placing of words onto a line of text so that
they will it. his is a bit more tricky than it at irst sounds because the font used is not a
monospaced font. In a monospaced font all characters occupy the same size box, so it is easy
to know how wide a string of characters will be from the number of them in the string.
However, when you are using a proportional font, as you are here, each character's width is
variable, depending on the character. hat means, for example, a letter I is much narrower
than a letter W. he only way you can see how wide a bunch of words is going to be is if you
render them, and then measure the size. his makes the rendering process a bit more tricky
than before. Basically what you do is to build up the line one word at a time and measure the
resulting text width. If it is less that the width you need, you add another word and measure
again. his continues until you exceed the allotted width, at which point you go with a line
that is one word short of this. In this way you ensure that the maximum number of words
get used to ill the line and no word is split. his process is complicated by the fact that if you
run out of words for one line, you just have to go with the remaining words in the line.
Finally, the words for your news story have to come from somewhere, and in this case it is a
ile called, appropriately enough, news.txt . his is just a simple text ile you can make with
the Leafpad text processor that is bundled with the Pi's operating system. he text I used for
testing was the scrolling text used at the start of the irst Star Wars ilm, which I thought was
suitably geeky. You can ind the text with a simple Google search if you want to do the same.
he news.txt ile should be stored in the same folder as the program.
Search WWH ::




Custom Search