UpFRONT
Last episode, we initialized console graphics (at great risk to our system's health) but we didn't do much afterwards. Now it's time for something truly impressive, a feat which Microsoft has apparently not yet accomplished—a smooth scrolltext.
“What is a scrolltext?” you may ask, in particular if you haven't been around on a Commodore 64, Amiga or PC and names like Fairlight, Red Sector and Future Crew mean very little to you. Hopefully, you've at least seen the intro screen from Jet Set Willy. What it comes down to is this: scrolltexts are the most exciting form of communication ever. Words that glide across the screen, often advertising the latest release or copy party, complaining about high school teachers, or detailing unfortunate experiences at the hands of public transit, usually accompanied by music (MODs) blaring in the background with animated graphics and the ubiquitous star fields. Perhaps you've seen Microsoft's screen saver which scrolls words across the screen and noticed it flickers terribly. It is very simple to fix, so our scrolltext won't flicker; then we can taunt the Microsofties to fix their screen saver.
The routine is quite simple. We start by initializing three graphics screens: a physical screen, a virtual screen and a scroll board screen. The physical screen is the graphics context which will be displayed, the virtual screen is the graphics context we use as a whole-screen copy to the physical screen. That is, we make changes to the virtual screen, and when everything's ready, copy it to the physical screen. The third screen is the scroll board, a virtual graphics context which will be wider than the physical screen by one character (8 pixels) and will be only as tall as the font itself (again, 8 pixels). Since we're using an 8x8 font and a 320x200 graphics screen, we can fit 40 (320/8) characters on a line, making 41 characters for our scroll board. Once we've set up these three contexts, we'll use a simple loop to get the letters scrolling. After that, we can add anything we like: 3-D graphics, dancing animals, star fields or anything else. Here's the loop:
Write 41 letters to scroll_board.
Copy 40 letters from scroll_board to virtual_screen, always copying to the same location on virtual_screen, but copying from one pixel farther to the right each time, so that at first we get the first 40 letters, then the first 40 letters minus the first row of pixels from the first letter but with the first row of pixels of the 41st letter and so on, until we have scrolled 8 pixels (the width of our current font).
After each copy of scroll_board to virtual_screen, copy virtual_screen to physical_screen and hold for a vertical refresh. (This makes things look smooth and lets us add things later, like the dancing animals.)
Once we have moved 8 pixels forward in scroll_board, such that we are copying the 2nd through the 41st letter (instead of the 1st through 40th), we reprint 41 letters to scroll_board, starting one letter in, such that what was once, for example, “Hello world welcome to my glorious scroll” would become, “ello world welcome to my glorious scrollt”.
This routine is fairly simple and requires only a couple of variables: one to keep track of how many pixels in we are and one to keep track of how many letters in we are. Also, we want to make sure we don't run out of scrolltext and start scrolling bits of random memory, which would ultimately lead to a segmentation fault. While we might prefer to draw each letter individually and just keep modulating around the length of the scrolltext, we get faster drawing if we print characters as a string once instead of calling gl_writen 41 times every time we move 8 pixels. So, leave some blank space at the beginning and end of your text to ensure smooth wrapping. We could also create the whole scrolltext as one really long graphic, but that would be cheating.
Once the basic scrolltext is going, we can do all sorts of fun things. We could, for example, have a sinusoid equation for the y value of where the text is placed such that it would bounce up and down on the screen, or we could insert some graphics and possibly call mikmod or playmidi to get some music going. Compile with
gcc -Wall -O2 scrolltext.c -lvgagl\ -lvga -o scrolltext
For information on the specifics of svgalib or vgagl, try their respective man pages. Library functions also have their own man pages. Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <vga.h>
#include <vgagl.h>
#define VGAMODE G320x200x256
#define FONTW 8 // font width
#define FONTH 8 // font height
#define TEXTL 600 // text length
int main(void)
{
char d;
char text[TEXTL]=" Megagreetings from whomever this happens to be! This is where the scrolltext words are, so fill them as you like. Fun rarely entices everyone. Keep enjoying video interfaces nevertheless................. There is space to be filled!! ";
short int text_pos;
unsigned char pixel_pos;
unsigned char speed;
GraphicsContext *physical_screen;
GraphicsContext *virtual_screen;
GraphicsContext *scroll_board;
vga_init();
vga_setmode(VGAMODE);
gl_setcontextvga(VGAMODE);
physical_screen = gl_allocatecontext();
gl_getcontext(physical_screen);
gl_setcontextvgavirtual(VGAMODE);
virtual_screen = gl_allocatecontext();
gl_getcontext(virtual_screen);
gl_clearscreen(0);
scroll_board = malloc( (WIDTH/FONTW+1) * FONTW *
FONTH * BYTESPERPIXEL);
gl_setcontextvirtual(WIDTH+FONTW, FONTH,
BYTESPERPIXEL, 8, scroll_board);
scroll_board = gl_allocatecontext();
gl_getcontext(scroll_board);
gl_clearscreen(0);
gl_setwritemode(FONT_COMPRESSED);
gl_setfont(8, 8, gl_font8x8);
gl_setpalettecolor(1,63,63,63);
text_pos = 0; // text offset
pixel_pos = 0; // pixel offset
speed = 1; // scroller speed
gl_setcontext(virtual_screen);
for (d=0; d==0; d=vga_getkey()) {
pixel_pos+=speed;
while (pixel_pos > FONTW) {
gl_setcontext(scroll_board);
gl_writen(0, 0, WIDTH/FONTW,
&text[text_pos]);
text_pos++;
pixel_pos-=FONTW;
if (text_pos > TEXTL - WIDTH/FONTW)
text_pos -= (TEXTL-WIDTH/FONTW);
gl_setcontext(virtual_screen);
}
gl_copyboxfromcontext(scroll_board, pixel_pos,
0, WIDTH, FONTH, 0, HEIGHT-FONTH-1);
gl_copyscreen(physical_screen);
vga_waitretrace();
}
return 0;
}
This code can be downloaded from
ftp.linuxjournal.com/ftp/pub/lj/listings/issue68/3722.tgz.
—Jason Kroll
Doc Searls is Senior Editor of Linux Journal
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- RSS Feeds
- Validate an E-Mail Address with PHP, the Right Way
- Readers' Choice Awards
- Tech Tip: Really Simple HTTP Server with Python
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




25 min 16 sec ago
57 min 38 sec ago
3 hours 21 min ago
3 hours 24 min ago
3 hours 25 min ago
7 hours 50 min ago
9 hours 41 min ago
14 hours 54 min ago
18 hours 6 min ago
20 hours 21 min ago