Click here if you are stuck in someone else's frames.
Building our Video Game Library

Now let's make a library out of our code snippets that we have written so far so we will never have to re-type them again.  First, let's create a basic library that can set the video mode between 13h and the text mode, provide basic information about mode 13h, and allow us to set pixels on and clear the graphics screen.  Then we'll look at ways to tweak it for maximum efficiency.  Let's begin with using the VMODE.C program to generate a library.  First, modify the VMODE.C program so that it only contains these lines:

     /* Library source file - VMODE.C
      * by Gary Neal, Jr.
      *
      * Allows us to set the video mode between modes 13h and the text mode.
      * Provides information about mode 13h (height, width, and no of colors).
      * Allows us to set pixels and blank the screen.
      */

     #include "vmode.h"  /* Header file for this module */

     /* Sets a global pointer to video memory */
     unsigned char far *videoMem = (unsigned char far *)0xA0000000L;

     void SetVideoMode(int mode)
     {
         union REGS regs;    /* Define register variable */

         regs.x.ax = mode;   /* Set video mode */

         /* Call interrupt to complete the task */
         int86(0x10, &regs, &regs);
     }

Of course, this code also requires a header file to complete it.  So here you go:

     /* Video mode header file -- VMODE.H
      * by Gary Neal, Jr
      */

     #ifndef _VMODE_H
     #define _VMODE_H      /* Prevent multiple inclusions */

     #include <dos.h>      /* Required for int86 functionality */
     #include <mem.h>      /* Required for the _fmemset functionality */

     /* Video display definitions */
     #define TEXT_MODE 0x03
     #define VGA256    0x13

     /* Define constants for Screen dimensions */
     #define ScreenWidth  (unsigned)320
     #define ScreenHeight (unsigned)200

     /* Define constant for maximum no of colors */
     #define ScreenColors (unsigned)256

     /* Define existense of global variable */
     extern unsigned char far *videoMem;

     /* This module must be compiled C style */
     #ifdef __cplusplus
     extern "C" void SetVideoMode(int mode);
     #else
     void SetVideoMode(int mode);
     #endif  /* __cplusplus */

     /* Create macros */
     #define SetPixel(X, Y, C) videoMem[Y * 320 + X] = C
     #define FillScreen(C)     _fmemset(videoMem, C, 64000);

     #endif  /* _VMODE_H */

At this point, we are ready to create our first module of our game library.   Ensure that you save both files above, VMODE.C and VMODE.H, in the same directory (folder) and that your compiler is set to work with that directory by default.  (On Turbo C++, use the File -> Change Directory menu to set the default working directory.)  Load the VMODE.C program into the compiler's IDE and compile it to an OBJ (object) file (this won't compile to an EXE because it has no 'main' function).  That's it, we've just created our first module of the game library.  To use it, we must include the header file (above) in whatever program we are writing and then link our program's OBJ file with the new VMODE.OBJ file compiled from the VMODE.C source above to create the final EXE of whatever program that we are writing.  This sounds a wee bit confusing, I know, so we'll illustrate what we need to do by writing, or shall I say re-writing, the pixels program.

     /* PIX.C -- Sets random pixels
      * by Gary Neal, Jr.
      */

     #include <stdlib.h>    /* Needed for rand() functionality */
     #include <conio.h>     /* Needed for keyboard functions */
     #include "vmode.h"     /* Our own video functions */

     int main(void)
     {
         while (kbhit()) getch();    /* Clear the keyboard */

         SetVideoMode(VGA256);       /* Switch to VGA mode 13h */

         /* Set random pixels of random colors until a key is pressed */
         while (!kbhit())
             SetPixel(rand() % ScreenWidth, rand() % ScreenHeight, rand() % ScreenColors);

         while (kbhit()) getch();    /* Clear the keyboard */

         SetVideoMode(TEXT_MODE);    /* Return to text mode */

         return 0;                   /* Return with no errors */
     }

As you can see, this is shorter than the original PIXELS.C program but unlike the original program, this program will take a little more effort on your part to successfully compile this to an executable.&nnbsp; First off, since some of the code that this program uses is in another file, it must be able to find this code to make the executable complete.

The header file, VMODE.H, gives the compiler information about the external code you are about to use.  The code itself is located within VMODE.C as source and VMODE.OBJ as object code, nothing tricky here.  Okay, so here's a couple of ways to make PIX.C into PIX.EXE.

First, you can compile PIX.C into object code, PIX.OBJ.  Then using a linker program, link the PIX.OBJ and VMODE.OBJ files with the standard libraries to create the EXE.  To do all of this with Turbo C++, make sure that the files PIX.C, VMODE.H, and VMODE.OBJ are in the same directory.  Then at the command prompt, type in this command:

     tcc pix.c vmode.obj

The command line compiler will compile the source, PIX.C, into the object file, PIX.OBJ, then it will call TLINK, Turbo C's linker program, to link PIX.OBJ and VMODE.OBJ with the standard libraries to create the final PIX.EXE program.  Here's another method you can use if you prefer to work within the IDE.

Create a project file within the IDE of your favorite compiler.  For Microsoft compilers, this file has a MAK extension, on Borland compilers, typically this file has a PRJ extension.  Okay, in our project file, insert both the PIX.C and the VMODE.OBJ files into it and make sure that your project file is called PIX, as either PIX.PRJ (Borland) or PIX.MAK (Microsoft).  Now you can compile the PIX project file into an EXE directly within the IDE.

Okay, so what if your compiler does not support the use of project files?  Then what?  Well, you can make a change to the PIX.C program and change the #include "vmode.h" line to #include "vmode.c" and recompile the program from within the IDE.  When you do this, the compiler will compile both PIX.C and VMODE.C (along with VMODE.H) into a single object code file PIX.OBJ before the linker links it with the standard libraries to create the PIX.EXE program.

This technique is good for testing and debugging the source file for the library code before you actually compile it into a seperate module for your library.  However, after having worked out all the bugs in the source and header files for each bit of library code, nothing more would be gained by continuing to compile library sources along with your code.  It would just then serve to increase the time that it takes to compile your program and enlarge the size of your EXE file.

Previous Page | Main Page | Next page