Professional Documents
Culture Documents
INTRODUCTION
Now about the libraries. They have been renamed. Initially, we had to use
numbers to indicate that the current library was no longer the previous
library. As routines functions were added, the library numbers grew. Now
we are at the conclusion, and we need simple generic names. The chart on
the next page shows the old names, used throughout this book in some
chapter or chapters and the new name that is used in this appendix. We
suggest you use the new names.
╔══════════════════════════╦══════════════════════════╗
║ OLD NAME ║ NEW NAME ║
╠══════════════════════════╬══════════════════════════╣
║ GFXLIB8.HPP ║ GFX320.HPP ║
╟──────────────────────────╫──────────────────────────╢
║ GFXLIB6.HPP ║ GFX640.HPP ║
╟──────────────────────────╫──────────────────────────╢
║ CLRLIB3.HPP ║ COLOR.HPP ║
╟──────────────────────────╫──────────────────────────╢
║ ANIMLIB4.HPP ║ ANIMATE.HPP ║
╟──────────────────────────╫──────────────────────────╢
║ MOUSE.HPP ║ MOUSE.HPP ║
╟──────────────────────────╫──────────────────────────╢
║ SOUNDLIB.HPP ║ SOUND.HPP ║
╟──────────────────────────╫──────────────────────────╢
║ None ║ BOOL.H ║
╚══════════════════════════╩══════════════════════════╝
#include <fstream.h>
#include <dos.h>
#include <conio.h>
#include <math.h>
#include <mem.h>
#include "BOOL.H"
/*********************************************************************/
/*** USER DEFINED AND ENUMERATED DATA TYPES ***/
/*********************************************************************/
typedef unsigned short Byte; // Byte type definition.
typedef unsigned int Word; // Word type definition.
struct Coord
{
int X; // X coordinate.
int Y; // Y coordinate.
}; // End struct.
void StartGfx();
// Starts the mcga (320x200x256) graphics mode.
void EndGfx();
// Restore standard text mode (80x25x16).
void Stop();
// Pauses the program until a key is pressed.
void Line(int X1, int Y1, int X2, int Y2, Byte Color);
// Draws a line from (x1,y1) to (x2,y2).
void BoundedLine(int X1, int Y1, int X2, int Y2, Byte Color);
// Same as line and performs bounds checking.
void Rectangle(int X1, int Y1, int X2, int Y2, Byte Color, Boolean
Filled);
// Draws a rectangle with corners (x1,y1), (x1,y2), (x2,y1), and
(x2,y2).
// It has a parameter "filled" that will draw a solid rectangle rather
than
// an outline.
void BoundedRectangle(int X1, int Y1, int X2, int Y2, Byte Color, Boolean
Filled);
// Same as rectangle and performs bounds checking.
void MakeTables();
// Creates sin and cos lookup tables.
void Ellipse(int CX, int CY, int XR, int YR, Byte Color);
// Draws an ellipse with center (cx,cy) and eccentricity of xr for x
// and yr for y.
void BoundedEllipse(int CX, int CY, int XR, int YR, Byte Color);
// Same as ellipse and performs bounds checking.
void Arc(int CX, int CY, int XR, int YR, float Start, float Finish, Byte
Color);
// Draws a portion of an ellipse with center (cx,cy) and eccentricty of
// xr for x and yr for y.
void SaveImage(char* FName, int Left, int Top, int Right, int Bottom);
// Saves an image in an area bounded by (left,top) and (right,bottom);
/***********************************************************************/
/*** FUNCTION IMPLEMENTATIONS ***/
/***********************************************************************/
/*** INBOUNDS ***/
/****************/
Boolean InBounds (int X, int Y)
{
if ( (X > 0) && (Y > 0) && (X < MAXX) && (Y < MAXY) )
return True;
else
return False;
}
/****************/
/*** Set Mode ***/
/****************/
void SetMode(Byte Mode)
{
_AH = 0x00; // Select "set video mode" function (00h)
_AL = Mode; // Set mode selected.
geninterrupt(0x10); // Call video interrupt (10h).
}
/*****************/
/*** Start GFX ***/
/*****************/
void StartGfx()
{
SetMode(0x13); // Set MCGA 320x200x256 mode (13h).
MakeTables();
}
/***************/
/*** End GFX ***/
/***************/
void EndGfx()
{
if (X1 == X2)
{
int X = X1;
if (Y1 > Y2)
{
int Temp = Y1;
Y1 = Y2;
Y2 = Temp;
} // End if.
for(int Y = Y1; Y <= Y2; Y++)
pokeb(VGA_RAM, X + Y * 320, Color);
} // End if.
else
{
if(Y1 == Y2)
{
int Y = Y1;
if(X1 > X2)
{
int Temp = X1;
X1 = X2;
if (X1 == X2)
{
if (Y1 > Y2)
{
int Temp = Y1;
Y1 = Y2;
Y2 = Temp;
void DoNothing()
{ }
/*****************/
/*** Rectangle ***/
/*****************/
void Rectangle(int X1, int Y1, int X2, int Y2, Byte Color, Boolean
Filled)
{
void HLine(int X1, int X2, int Y1, Byte Color);
void VLine(int Y1, int Y2, int X1, Byte Color);
if (Filled)
for (int Y = Y1; Y <= Y2; Y++)
memset(VGAMEM + Y * 320 + X1, Color, X2 - X1 + 1);
else
{
HLine(X1, X2, Y1, Color);
HLine(X1, X2, Y2, Color);
VLine(Y1, Y2, X1, Color);
VLine(Y1, Y2, X2, Color);
} // End else.
}
if(X1 < 0) X1 = 0;
if(Y1 < 0) Y1 = 0;
if(X2 < 0) X2 = 0;
if(Y2 < 0) Y2 = 0;
if(X1 > MAXX) X1 = MAXX;
On = InBounds(CosTab[0] * XR + CX,
SinTab[0] * YR * AR + CY);
On = InBounds(CosTab[0] * XR + CX,
SinTab[0] * YR * AR + CY);
TopOfStack = 1;
Temp.X = X;
Temp.Y = Y;
do
{
if( (GetPixel(Temp.X, Temp.Y) == OldColor) && RangeOK(Temp) )
{
pokeb(VGA_RAM, Temp.X + Temp.Y * 320, NewColor);
Push(Temp);
MoveToNext(Temp);
} // End if.
else
{
Pop(Temp);
MoveToNext(Temp);
} // End else.
} // End do loop.
while(TopOfStack > 0);
} // End void function Flood::QuadFlood.
Boolean RangeOK(Coord N)
{
return Boolean( (N.X >= 0) && (N.X <= MAXX) &&
(N.Y >= 0) && (N.Y <= MAXY) );
} // End function Flood::RangeOK.
void MoveToNext(Coord& N)
{
if ( ( GetPixel(N.X+1, N.Y) == OldColor ) )
N.X++;
else if ( ( GetPixel(N.X, N.Y -1) == OldColor ) )
N.Y--;
else if ( ( GetPixel(N.X-1, N.Y) == OldColor ) )
N.X--;
else if ( ( GetPixel(N.X, N.Y+1) == OldColor ) )
N.Y++;
} // End void function Flood::MoveToNext.
/************/
/*** Push ***/
/************/
void Push(Coord N)
{
if (TopOfStack >= FLOODSIZE)
TopOfStack -= FLOODSIZE;
TopOfStack++;
Stack[TopOfStack] = N;
} // End void function Push.
/***********/
/*** Pop ***/
/***********/
void Pop(Coord& N)
{
if (TopOfStack > 0)
{
N = Stack[TopOfStack];
TopOfStack--;
} // End if.
}
/****************/
/*** ClearGfx ***/
if (GetPixel(X,Y) != OldColor)
Stuck = True;
if ((X < MINX) || (X > MAXX))
Stuck = True;
if ((Y < MINY) || (Y > MAXY))
Stuck = True;
if (!Stuck)
{
Pixel(X, Y, NewColor);
RecursiveFill(X+1, Y, OldColor, NewColor);
RecursiveFill(X-1, Y, OldColor, NewColor);
RecursiveFill( X, Y+1, OldColor, NewColor);
RecursiveFill( X, Y-1, OldColor, NewColor);
} // End if.
}
/*************/
/*** Round ***/
/*************/
int Round(float X)
{
return (floor(X+0.05));
}
/*****************/
/*** SaveImage ***/
/*****************/
void SaveImage(char* FName, int Left, int Top, int Right, int Bottom)
{
ofstream Outfile(FName, ios::binary);
unsigned int Width = Right - Left + 1;
unsigned int Height = Bottom - Top + 1;
unsigned char Buffer[320];
unsigned char Temp[2];
#include <dos.h>
#include <conio.h>
#include <math.h>
/*********************************************************************/
/*** USER DEFINED AND ENUMERATED DATA TYPES ***/
/*********************************************************************/
typedef unsigned short Byte; // Byte type definition.
typedef unsigned int Word; // Word type definition.
enum Boolean {False,True}; // Boolean type definition.
/*********************************************************************/
/*** CONSTANT VARIABLE IDENTIFIERS ***/
/*********************************************************************/
const int MINX = 0; // Minimum x coordinate value.
const int MIDX = 320; // Midpoint x coordinate value.
const int MAXX = 639; // Maximum x coordinate value.
const int MINY = 0; // Minimum y coordinate value.
const int MIDY = 240; // Midpoint y coordinate value.
const int MAXY = 479; // Maximum y coordinate value.
const int MAXC = 16; // Maximum number of colors.
const float AR = 1.05; // Aspect ratio to make circles symetrical.
const float TWOPI = 6.28; // Approximation of 2 * PI.
const Boolean CLOSED = True; // Constant to draw filled-in rectangle.
const Boolean OPEN = False; // Constant to draw open rectangle.
/*********************************************************************/
/*** GLOBAL VARIABLE IDENTIFIERS ***/
/*********************************************************************/
float SinTab[630]; // Sin lookup table.
float CosTab[630]; // Cos loopup table.
/*********************************************************************/
/*** FUNCTION PROTOTYPES ***/
/*********************************************************************/
void SetMode(Byte Mode);
void StartGfx();
void EndGfx();
void Stop();
void Pixel(int X, int Y, Byte Color);
Byte GetPixel(int X, int Y);
void Line(int X1, int Y1, int X2, int Y2, Byte Color);
void Rectangle(int X1, int Y1, int X2, int Y2, Byte Color,
Boolean Filled);
void MakeTables();
void Circle(int CX, int CY, int Radius, Byte Color);
void Ellipse(int CX, int CY, int XR, int YR, Byte Color);
void Arc(int CX, int CY, int XR, int YR, float Start, float Finish,
Byte Color);
if (X1 == X2)
{
int X = X1;
if (Y1 > Y2)
{
int Temp = Y1;
Y1 = Y2;
Y2 = Temp;
} // End if.
for(int Y = Y1; Y <= Y2; Y++)
Pixel(X,Y,Color);
} // End if.
else
{
if(Y1 == Y2)
{
int Y = Y1;
if(X1 > X2)
{
int Temp = X1;
X1 = X2;
X2 = Temp;
} // End if.
for (int X=X1; X<=X2; X++)
Pixel(X,Y,Color);
} // End if.
} // End else.
if (GetPixel(X,Y) != OldColor)
Stuck = True;
if ((X < MINX) || (X > MAXX))
Stuck = True;
if ((Y < MINY) || (Y > MAXY))
Stuck = True;
if (!Stuck)
{
Pixel(X, Y, NewColor);
RecursiveFill(X+1, Y, OldColor, NewColor);
RecursiveFill(X-1, Y, OldColor, NewColor);
RecursiveFill( X, Y+1, OldColor, NewColor);
RecursiveFill( X, Y-1, OldColor, NewColor);
} // End if.
}
/*********************/
/*** ClearGfx ***/
/*********************/
void ClearGfx(Byte Color)
{
Rectangle(MINX, MINY, MAXX, MAXY, Color, CLOSED);
}
/*********************/
#include "GFX320.HPP"
struct RGBSignature
{
Byte Red;
Byte Grn;
Byte Blu;
}; // End struct RGBSignature.
// Allows the RGB palette for each one of the 256 different colors.
void SetRGB(Byte Num, RGBSignature Sig);
// Sets the RGB palette for each one of the 256 different colors based
// on the values in the global palette array.
void SetPalette(PaletteType P);
void SetPalette(PaletteType P)
{
unsigned Segment, Offset; // Segment and offset of array.
unsigned char MyPal[768]; // Temporary palette.
void MakeRainbow(PaletteType P)
{
Byte Count;
for (Count=0; Count<=63; Count++)
{
P[Count].Red = Count;
P[Count].Grn = 0;
P[Count].Blu = 0;
} // End for loop.
for (Count=64; Count<=127; Count++)
{
P[Count].Red = 127 - Count;
P[Count].Grn = Count - 64;
P[Count].Blu = 0;
} // End for loop.
for (Count=128; Count<=191; Count++)
{
P[Count].Red = 0;
P[Count].Grn = 191 - Count;
P[Count].Blu = Count - 128;
} // End for loop.
for (Count=192; Count<=255; Count++)
{
P[Count].Red = 0;
P[Count].Grn = 0;
P[Count].Blu = 255 - Count;
} // End for loop.
} // End void function MakeRainbow.
void CyclePalette(PaletteType P)
{
Byte Count;
RGBSignature T;
T = P[1];
for(Count=1; Count<=254; Count++)
P[Count] = P[Count + 1];
P[255] = T;
} // End void function CyclePalette.
#endif
─────────────────────────────────────────────────────────────────────────
#include <stdlib.h>
#include "GFXLIB8.HPP"
struct SpriteType
{
int X, Y; // X and Y position of the sprite.
int OldX, OldY; // Old X and Y position of the sprite.
int Height, Width; // Height and Width of the sprite.
char* Bitmap; // Pointer to the sprite bitmap data.
char* Beneath; // Pointer to data beneath bitmap.
}; // End struct SpriteType.
/*********************************************************************/
/*** GLOBAL CONSTANTS ***/
/*********************************************************************/
const SCREEN_SIZE = 64000; // 320 x 200 pixels.
/*********************************************************************/
/*** FUNCTION PROTOTYPES ***/
/*********************************************************************/
void LoadSprite(char* Filename, SpriteType& Image);
void PutSprite(SpriteType Image);
void ReleaseSprite(SpriteType Image);
void SetupVirtual (char*& Where);
void DestroyVirtualPage(char*& Where);
void FlipPage(char* Source, char* Destination);
void WhPutSprite(SpriteType Image, char* Where);
void WhEraseSprite(SpriteType Image, char* Where);
void WhGetSprite(SpriteType Image, char* Where);
void WhPixel (int X, int Y, Byte Color, char* Where);
Byte WhGetPixel(int X, int Y, char* Where);
void WhTransPutSprite(SpriteType Image, char* Where);
void FlipPortion(SpriteType Image, char* Where);
/*********************************************************************/
/*** FUNCTION IMPLEMENTATIONS ***/
/*********************************************************************/
void LoadSprite(char* Filename, SpriteType& Image)
{
ifstream Infile(Filename, ios::binary); // Open input file.
unsigned char Temp[2]; // Two byte bufffer.
Infile.read(Temp,2);
Image.Width = (Temp[1] << 8) + Temp[0];
Infile.read(Temp,2);
Image.Height = (Temp[1] << 8) + Temp[0];
Segment = FP_SEG(Image.Bitmap);
Offset = FP_OFF(Image.Bitmap);
for (int Loop1 = 0; Loop1 < Image.Width; Loop1++)
for (int Loop2 = 0; Loop2 < Image.Height; Loop2++)
{
Color = peekb(Segment, Offset + Loop1 + Loop2 * Image.Height);
Pixel(Image.X + Loop1, Image.Y + Loop2, Color);
} // End for loop.
} // End void function PutSprite.
Seg = FP_SEG(Image.Bitmap);
Ofs = FP_OFF(Image.Bitmap);
for (int Loop1 = 0; Loop1 < Image.Width; Loop1++)
for (int Loop2 = 0; Loop2 < Image.Height; Loop2++)
{
Color = peekb(Seg, Ofs + Loop1 + Loop2 * Image.Width);
if (Color)
WhPixel(Image.X + Loop1, Image.Y + Loop2, Color, Where);
} // End for loop.
} // End void function WhTransPutSprite.
#endif
─────────────────────────────────────────────────────────────────────────
#include "GFX320.HPP"
#include "BOOL.H"
Boolean DetectMouse()
{
_AX = MOUSE_TEST;
geninterrupt(MOUSE_INT);
return Boolean(_AX == 0xFFFF);
} // End function DetectMouse.
Word NumberOfButtons()
{
_AX = MOUSE_TEST;
geninterrupt(MOUSE_INT);
return _BX;
} // End function Number of Buttons.
void MouseCursorOn()
{
_AX = CURSOR_ON;
geninterrupt(MOUSE_INT);
} // End function MouseCursorOn.
void MouseCursorOff()
Word ButtonInfo()
{
_AX = MOUSE_INFO;
geninterrupt(MOUSE_INT);
return ( _BL );
} // End function ButtonInfo.
Word XMousePos()
{
_AX = MOUSE_INFO;
geninterrupt(MOUSE_INT);
return ( _CX/2 );
} // End function XMousePos.
Word YMousePos()
{
_AX = MOUSE_INFO;
geninterrupt(MOUSE_INT);
return ( _DX );
} // End function YMousePos.
void WaitForRelease()
{
do{} while (ButtonInfo() != 0);
} // End void function WaitForRelease().
#endif
─────────────────────────────────────────────────────────────────────────
SOUND.HPP
─────────────────────────────────────────────────────────────────────────
/**********************************************************************/
/*** SOUND.HPP is a collection of all the sound-related routines ***/
/*** and constants introduced in this book. This library can be ***/
/*** used to create sound and music using the PC speaker. ***/
/**********************************************************************/
#ifndef _SOUND_HPP
#define _SOUND_HPP
#include <dos.h>
void RealDelay(int D)
{
unsigned int T1,T2;
#endif
─────────────────────────────────────────────────────────────────────────
#ifndef _BOOL_H
#define _BOOL_H
#endif
─────────────────────────────────────────────────────────────────────────
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄