/* GRAPHICS DEMO FOR Borland C++ Copyright (c) 1987, 1993 Borland International. All rights reserved. From the command line, use: bcc bgidemo graphics.lib */ #ifdef __TINY__ #error BGIDEMO will not run in the tiny model. #endif #include #include #include #include #include #include #include #define ESC 0x1b /* Define the escape key */ #define TRUE 1 /* Define some handy constants */ #define FALSE 0 /* Define some handy constants */ #define PI 3.14159 /* Define a value for PI */ #define ON 1 /* Define some handy constants */ #define OFF 0 /* Define some handy constants */ #define NFONTS 11 char *Fonts[NFONTS] = { "DefaultFont", "TriplexFont", "SmallFont", "SansSerifFont", "GothicFont", "ScriptFont", "SimplexFont", "TriplexScriptFont", "ComplexFont", "EuropeanFont", "BoldFont" }; char *LineStyles[] = { "SolidLn", "DottedLn", "CenterLn", "DashedLn", "UserBitLn" }; char *FillStyles[] = { "EmptyFill", "SolidFill", "LineFill", "LtSlashFill", "SlashFill", "BkSlashFill", "LtBkSlashFill", "HatchFill", "XHatchFill", "InterleaveFill", "WideDotFill", "CloseDotFill" }; char *TextDirect[] = { "HorizDir", "VertDir" }; char *HorizJust[] = { "LeftText", "CenterText", "RightText" }; char *VertJust[] = { "BottomText", "CenterText", "TopText" }; struct PTS { int x, y; }; /* Structure to hold vertex points */ int GraphDriver; /* The Graphics device driver */ int GraphMode; /* The Graphics mode value */ double AspectRatio; /* Aspect ratio of a pixel on the screen*/ int MaxX, MaxY; /* The maximum resolution of the screen */ int MaxColors; /* The maximum # of colors available */ int ErrorCode; /* Reports any graphics errors */ struct palettetype palette; /* Used to read palette info */ /* */ /* Function prototypes */ /* */ void Initialize(void); void PutImageDemo(void); void Pause(void); void MainWindow(); /* */ /* Begin main function */ /* */ int main() { Initialize(); /* Set system into Graphics mode */ PutImageDemo(); closegraph(); /* Return the system to text mode */ return(0); } /* */ /* INITIALIZE: Initializes the graphics system and reports */ /* any errors which occured. */ /* */ void Initialize(void) { int xasp, yasp; /* Used to read the aspect ratio*/ GraphDriver = DETECT; /* Request auto-detection */ initgraph( &GraphDriver, &GraphMode, "" ); ErrorCode = graphresult(); /* Read result of initialization*/ if( ErrorCode != grOk ){ /* Error occured during init */ printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) ); exit( 1 ); } getpalette( &palette ); /* Read the palette from board */ MaxColors = getmaxcolor() + 1; /* Read maximum number of colors*/ MaxX = getmaxx(); MaxY = getmaxy(); /* Read size of screen */ getaspectratio( &xasp, &yasp ); /* read the hardware aspect */ AspectRatio = (double)xasp / (double)yasp; /* Get correction factor */ } class Saucer { private: int x; int y; int r; int speed; int height; int width; void *Sauc; int xstep; int ystep; void move(void); void undraw(void); void draw(void); public: Saucer(void); ~Saucer(void); void update(void); }; Saucer::Saucer(void) { int ulx, uly, lrx, lry, size; x = 100; y = 50; r = 20; speed = 10; /* Draw Saucer */ setfillstyle( SOLID_FILL, getmaxcolor() ); fillellipse(x, y, r, (r/3)+2); ellipse(x, y-4, 190, 357, r, r/3); line(x+7, y-6, x+10, y-12); circle(x+10, y-12, 2); line(x-7, y-6, x-10, y-12); circle(x-10, y-12, 2); /* Read saucer image */ ulx = x-(r+1); uly = y-14; lrx = x+(r+1); lry = y+(r/3)+3; width = lrx - ulx + 1; height = lry - uly + 1; size = imagesize(ulx, uly, lrx, lry); Sauc = malloc( size ); getimage(ulx, uly, lrx, lry, Sauc); putimage(ulx, uly, Sauc, XOR_PUT); x = MaxX/2; y = MaxY/2; draw(); } void Saucer::draw(void) { putimage(x, y, Sauc, XOR_PUT); } void Saucer::undraw(void) { putimage(x, y, Sauc, XOR_PUT); } void Saucer::update(void) { undraw(); move(); draw(); } void Saucer::move(void) { if(random(10) == 9) { xstep = random(speed) - speed/2; ystep = random(speed) - speed/2; } x += xstep; y += ystep; if (x + width - 1 > MaxX) { x -= MaxX; x += width; } else if (x < 0) { x += MaxX; x -= width; } if (y + height - 1 > MaxY) y = MaxY - height + 1; else if (y < 0) y = 0; } Saucer::~Saucer(void) { free(Sauc); } /* */ /* PUTIMAGEDEMO */ /* */ void PutImageDemo(void) { struct viewporttype vp; int PauseTime, i; MainWindow(); getviewsettings( &vp ); Saucer saucer; randomize(); /* Plot some "stars" */ for ( i=0 ; i<1000; ++i ) putpixel(random(MaxX), random(MaxY), random( MaxColors-1 )+1); PauseTime = 20; /* until a key is hit */ while ( !kbhit() ) { /* Draw the Saucer */ saucer.update(); delay(PauseTime); } Pause(); } /* */ /* PAUSE: Pause until the user enters a keystroke. If the */ /* key is an ESC, then exit program, else simply return. */ /* */ void Pause(void) { int c; c = getch(); /* Read a character from kbd */ if( ESC == c ){ /* Does user wish to leave? */ closegraph(); /* Change to text mode */ exit( 1 ); /* Return to OS */ } if( 0 == c ){ /* Did use hit a non-ASCII key? */ c = getch(); /* Read scan code for keyboard */ } cleardevice(); /* Clear the screen */ } /* */ /* MAINWINDOW: Establish the main window for the demo and set */ /* a viewport for the demo code. */ /* */ void MainWindow() { cleardevice(); /* Clear graphics screen */ setcolor( MaxColors - 1 ); /* Set current color to white */ setviewport( 0, 0, MaxX, MaxY, 1 ); /* Open port to full screen */ }