DOS Mode 13h Coding Tools & Examples

C Programming for DOS in mode 13h

As I get into back into programming for DOS, I expect to make some handy tools for software development. I'm making it a priority to share the tools to help other retro game developers so you can make some good games for me to play.

The zip file contains three programs, the source code, a script for compiling, and a BMP file for demonstration of the bmp.com program.

Colors

This small DOS program helps with selecting a color from the default 256-color palette of mode 13h for VGA graphics programming for DOS. Being open source software, it also demonstrates how to code features for DOS in C and compile the code successfully.

The included shell script for Ubuntu, Mint, or probably Debian Linux shows how to cross-compile for DOS while revealing some options for optimizations. You can use that script to compile other programs, for an example of usage, the command ./cc.sh colors will compile the colors.c source code into a colors.com, which is a COM executable file for DOS.

The program runs on DOSBox. Not yet tested on DOSBox-X, FreeDOS, or others.

DOS Color Tool Screenshot
DOS Color Tool Screenshot

When using the colors.com tool, press your left and right arrow keys to select a color from the palette. Press up or down to step by 16. The selected color's index in the palette is shown in hexadecimal at the bottom of the screen.

In addition to selecting a color from the default palette, you can press spacebar to enter RGB values to view a custom color. Values may range from 0 to 63. Colors are not saved permanently, so you should write down your values or otherwise save them yourself.

Press your escape key (or just close your emulator) to quit the program.

BMP Viewer

This program demonstrates displaying an 8-bit BMP bitmap image file in DOS. You might use the code for your title screen, sprites, or a point-and-click adventure game.

The showBMP function takes several parameters, including the name of the BMP file, the x and y pixel coordinates where to display it on the screen, and the base color for the palette, which tells which color index to start at, because if you display multiple BMP files at once in your game, you might want the colors for each image in a different region of the palette.

To create a compatible BMP, use GIMP or your favorite image editor to convert a picture to an indexed 256-color image up to 320x200 in size and save as a BMP.

But wait. After resizing, images can seem blurry, so consider using unsharp mask to sharpen the resized image a bit. If you use unsharp mask, it's probably best to change the precision to 32-bit first, before resizing or any other operation.

Use ctrl-z to undo any step in GIMP.

Once the image is resized and possibly sharpened, you need to convert the image from RGB to indexed. If the option to convert to indexed is disabled in GIMP, you should now change the precision back to 8-bit integer. Now that the image is resized and indexed, exporting as out.bmp should save as a compatible BMP file. Mode 13h doesn't have square pixels, so to get the aspect ratio right, make sure you start with a 4:3 image before scaling it down to 320x200.

Simple Game Template

Many games and diversions began with something much like this when I was a teenager programming in BASIC in the '90s — just a dot that you can move around with your arrow keys. Maybe the dot represents a person walking around or a spaceship rocketing around. What objects and mechanics will you add in this new world? Some games play fine with just dots, but at some point, perhaps once you've developed a decent game, you might improve the graphics from dots to sprites. For some games, you might keep the dot-style display to use for a map.

All of these are in the zip file.