Home » Linux Magazine » Porting MS-DOS Graphics Applications

Porting MS-DOS Graphics Applications

Jawed Karim

Issue #53, September 1998

Are you hesitant about porting your favorite VGA MS-DOS program to Linux? Using this tutorial and SVGALIB, porting will truly become a matter of minutes.

I first started VGA programming under MS-DOS, using the popular DJGPP C compiler. In recent years, this protected-mode 32-bit compiler, which is basically an MS-DOS port of gcc, has established itself as one of the preferred compilers in the MS-DOS game programmers’ community. DJGPP was in fact the MS-DOS compiler of choice for idsoftware’s game Quake. For the Linux console port of Quake, the Linux Super VGA library, SVGALIB was used.

When I first decided that I was going to port my own 3-D Model Viewer, jaw3d, from MS-DOS to Linux, it seemed logical to use the same approach. SVGALIB is very intuitive and allows me to easily maintain and further develop my 3-D Model Viewer for both platforms.

I found the easiest way to work with one set of source files for both platforms was to use preprocessor directives in places where different code was needed. Since I had already written and used DJGPP’s low-level VGA and mouse instructions for the MS-DOS version, I simply added the equivalent SVGALIB Linux code in each instance, and separated the MS-DOS and Linux code using the preprocessor directive #ifdef. The following code snippet represents one of the many ways in which this can be accomplished:

#ifdef __DJGPP__
...
...
#endif
#ifndef __DJGPP__
...
...
#endif

__DJGPP__ is automatically defined by the DJGPP compiler, and is not defined by gcc under Linux.

An additional advantage of using SVGALIB under Linux is the fact that there is also a DJGPP version of SVGALIB. Let’s try not to get confused at this point: SVGALIB is a graphics library that does some behind-the-scenes low-level VGA and mouse work for the user. Although SVGALIB was first developed for Linux, someone eventually released a version that worked with DJGPP under MS-DOS. Why not use SVGALIB for both MS-DOS and Linux? This would allow us to have 100% identical code for both platforms.

I don’t recommend this approach, however, for two reasons. First, when I made speed comparisons of my 3-D engine between the two platforms, I noticed that when using SVGALIB with DJGPP under MS-DOS, graphics performance was sluggish in comparison with SVGALIB under Linux. Second, the MS-DOS executable was unnecessarily big because it had to be statically linked with the SVGALIB library. Using SVGALIB under Linux did not seem to present any problems. Due to the use of shared libraries under Linux, the executable remained tiny when dynamically linked, and graphics performance was actually slightly better under Linux than under MS-DOS. For the sake of performance and executable size, I found it best to use DJGPP’s low-level instructions under MS-DOS and to use SVGALIB under Linux. This makes a difference, especially in a setting like 3-D engines, where every frame-per-second counts.

The advantage obtained from using the DJGPP port of SVGALIB is the fact that you can test your SVGALIB Linux code under MS-DOS, without having to reboot. Except for speed and executable size, both versions of SVGALIB behave identically.

Note that the DJGPP port of SVGALIB is still in beta, but I ran across only one minor problem and that was easily fixed. The file vgakeybo.h included with the DJGPP port of SVGALIB seemed to differ from the file vgakeyboard.h under Linux; therefore, making cross compilations was impossible in cases where keyboard code was used. The two files should be identical, of course, and the solution is to copy the Linux version of the include file over the DOS one.

The three compiler-specific code aspects are VGA, mouse input and keyboard input. If you have completed an MS-DOS graphics application, you may be using much of this code already and can quickly add on the SVGALIB equivalent code. On the other hand, if you do not have any previous graphics programming experience, you will find the code shown in Listings 1 through 4 to be very useful.

Listing 1. Include Files

   #include <stdio.h>
   #include <math.h>
   ...


   #include <sys/movedata.h>
   #include <keys.h>
   #include <dos.h>
   #include <pc.h>


   #include <vga.h>
   #include <vgamouse.h>
   #include <vgakeyboard.h>

Listing 2. Initializing the VGA Mode

   char *video_buff;
   vga_init();
   /*
    * the above statement must be in the
    * earliest possible position in your
    * main() function. The * vga_init()
    * function is not directly related to
    * the VGA graphics mode, but is needed
    * for any of the SVGALIB functions to
    * work properly. It is important that this
    * function is called as soon as possible
    * after the program executes.
    */
   vga_setmode(G320x200x256);
   video_buff = vga_getgraphmem();


   union REGS r, s;
   r.w.ax = 0x13; /* 0x13==VGA, 0x03== ext */
   int386 (0x10, &r, &s);

Listing 3. Keyboard Code

   if (keyboard_init()) {
      fprintf(stderr,<\n>
   "\nCould not initialize keyboard.\n");
      exit(1);
   }


   /* first check for input, then handle it */
   while (kbhit())
      switch (getkey()) {
         case K_Down: {
      printf("\nYou pressed the DOWN key");
         break;
         }
         case K_Up: {
      printf("\nYou pressed the UP key");
            break;
         }
      }
    /*
     * Predefined constants such as K_Down and
     * K_Up may be found in DJGPP's `keys.h'<\n>
     * include file.
     */


   /* first check for input, then handle it */
   keyboard_update();
   if (keyboard_keypressed(SCANCODE_CURSORUP))
      printf("\nYou pressed the UP key");
   if (keyboard_keypressed(SCANCODE_CURSORDOWN))
      printf("\nYou pressed the DOWN key");
    /*
     * Predefined constants such as
     * SCANCODE_CURSORUP and SCANCODE_CURSORDOWN
     * may be found in SVGALIB's vgakeyboard.h
     * include file.
     */

Listing 4. Mouse Code

   vga_setmousesupport(1);


   int mouse;
   union REGS regs;

   regs.x.ax = 0;
   int86(0x33, &regs, &regs);
   mouse_on = regs.x.ax;

   if (!mouse_on) {
      printf(
"\nMouse initialization failed!\n");
      exit(1);
   }


   int mouse_x, mouse_y;
   while(1) {
      mouse_update();
      mouse_x = mouse_getx();
      mouse_y = mouse_gety();
      printf("\nCurrent mouse X: %d",
          mouse_x);
      printf("\nCurrent mouse Y: %d",
          mouse_y);
      if ((mouse_getbutton()) == 0)
         ..... ;
         /* neither mouse button is down */
      if (((mouse_getbutton() &
         MOUSE_LEFTBUTTON)) &&
      ((mouse_getbutton() &
         MOUSE_RIGHTBUTTON)))
         ..... ;
         /* both mouse buttons are down */
      if ((mouse_getbutton()) &
         MOUSE_LEFTBUTTON &&
         !(mouse_getbutton()) &
         MOUSE_RIGHTBUTTON))
         ..... ;
         /* left mouse button is down */
      if ((mouse_getbutton()) & MOUSE_RIGHTBUTTON &
         !(mouse_getbutton()) & MOUSE_LEFTBUTTON))
         ..... ;
         /* right mouse button is down */
   }


   int mouse_x, mouse_y;
   while(1) {
      get_mouse_status (&mouse_x, &mouse_y);
      printf("\nCurrent mouse X: %d", mouse_x);
      printf("\nCurrent mouse Y: %d", mouse_y);
      if ((get_buttons_mouse_status()) == 0)
         ..... ;
         /* neither mouse button is down */
      if (get_buttons_mouse_status() == 3)
         ..... ;
         /* both mouse buttons are down */
      if ((get_buttons_mouse_status()) == 1)
         ..... ;
         /* left mouse button is down */
      if ((get_buttons_mouse_status()) == 2)
         ..... ;
         /* right mouse button is down */
   }
   void get_mouse_status (int *x, int *y)
   {
           regs.x.ax = 3;
         int86(0x33, &regs, &regs);
           *x = regs.x.cx;
         *y = regs.x.dx;
   }
   int get_buttons_mouse_status(void)
   {
         regs.x.ax=3;
           int86(0x33, &regs, &regs);
         return regs.x.bx;
   }

Copying a Buffer to Video

In the case of my 3-D Model Viewer, jaw3d, a complete frame is first rendered onto a buffer which has the same dimensions as the screen, and then copied to video memory all at once, allowing us to display frequently updated screens successively without any flickering. This is done as follows:

        memcpy (video_buff, image_buffer,
                DIM_X * DIM_Y);
        /* video_buff was initialized above */
        dosmemput (image_buffer, DIM_X * DIM_Y,
                0xA0000);
        /* 0xA0000 is the video memory in VGA mode
         * 13h */

Waiting for the VGA Retrace

By waiting for the VGA retrace, we are telling the program to wait until the monitor’s electron beam reaches the bottom of the screen. Since there is a short pause before it jumps back to the top, it is a good moment to switch palettes without seeing “rainbow colors”. Thus, before switching palettes, we should wait for the VGA retrace as follows:

        while (!(inportb(0x3da) & 8));
        while ( (inportb(0x3da) & 8));
        vga_waitretrace();

Setting the VGA Palette

The following code assumes you have a character array of 768 values, representing the RGB values for 256 colors. For example:

char palette[768];
        where palette[0] = R value of color 0;
        where palette[1] = G value of color 0;
        where palette[2] = B value of color 0;
                ...
        for (i = 0; i < 256; i++)
        vga_setpalette(i, palette[i*3],
                palette[i*3+1], palette[i*3+2]);
        outportb(0x3C8,0);
        for (i = 0; i < 768; i ++)
                outportb(0x3C9, palette[i]);

Compiling

After adding SVGALIB code to the program, it’s time to compile. Simply compile with the -lvga option to link the SVGALIB library. This library is preinstalled on most Linux systems; thus, if you experience problems linking it, you probably don’t have it installed and should download it.

jaw3d was programmed by the author and is a Nullsoft Inc. product. Other cross-platform applications may be obtained at http://www.nullsoft.com/.

Jawed Karim is a freshman computer science student at the University of Illinois at Urbana-Champaign and works part-time at the National Center for Supercomputing Applications. His hobbies include programming and bicycle road racing. He can be reached at jkarim@students.uiuc.edu.

x

Check Also

linux network monitoring

Introduction to Multi-Threaded Programming

Brian Masney Issue #61, May 1999 A description of POSIX thread basics for C programmers. ...