UpFRONT

Stupid Programming Tricks, LJ Index and more.
STUPID PROGRAMMING TRICKS→SCROLLTEXTS

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

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix