Code:
/*
AT.C - ALIENTECH PROJECT MAIN FILE
AlienTech is an open-source project under GPL (General Public License)
For more about AlienTech visit http://alientech.sourceforge.net/
For more about GPL visit http://www.gnu.org/
*/
#ifdef __TINY__
#error BGIDEMO will not run in the tiny model.
#endif
#include <dos.h>
#include <math.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <graphics.h>
#include <keys.h> /* Keyboard cosntants definition */
#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 BTHICK 3 /* Define general border thick */
#define BCOLOR1 15 /* Define border color1 = white */
#define BCOLOR2 3 /* Define border color2 = gray */
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 Pause(void);
void DrawButton(int Bheight, int Bwidth, int BX, int BY, int BState);
void DrawRectBorder(int Bheight, int Bwidth, int BX, int BY, int BState);
void DrawMainWindow(void);
/* */
/* Begin main function */
/* */
int main()
{
Initialize(); /* Set system into Graphics mode */
DrawMainWindow(); /* Draw main program window */
Pause(); /* Pause the program. Wait for keypress */
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 */
}
/* */
/* PAUSE: Pause until the user enters a keystroke. If the */
/* key is an ESC, then exit program, else simply return. */
/* */
void Pause(void)
{
static char msg[] = "Esc aborts or press a key...";
int c;
StatusLine( msg ); /* Put msg at bottom of screen */
c = getch(); /* Read a character from kbd */
if( cEsc == 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 */
}
/* */
/* DRAWMAINWINDOW: draws the main program window. */
/* */
void DrawMainWindow(void)
{
cleardevice(); /* Clear graphics screen */
DrawRectBorder( 10, 10, MaxX-10, MaxY-10, OFF ); /* Draw WindowBorder */
DrawButton(50, 200, 100, 100, OFF);
}
/* */
/* DRAWRECTBORDER: draws a border. Can be raised or pressed */
/* (usefull for drawing buttons and windows' borders */
void DrawRectBorder( int Bheight, int Bwidth, int BX, int BY, int BState )
{
int Colr1 = BCOLOR1;
int Colr2 = BCOLOR2;
if ( BState == ON )
{
Colr1 = BCOLOR2;
Colr2 = BCOLOR1;
}
cleardevice(); /* Clear graphics screen */
setlinestyle( SOLID_LINE, 0, BTHICK ); /* Set line to thick and solid */
setcolor( Colr1 ); /* Set current color */
line( BX, BY + Bheight, BX, BY ); /* Draw border line */
lineto( BX + Bwidth, BY ); /* Draw border line */
setcolor( Colr2 ); /* Set current color */
lineto( BX + Bwidth, BY + Bheight ); /* Draw border line */
lineto( BX, BY + Bheight ); /* Draw border line */
}
/* */
/* DRAWBUTTON: draws a button. Can be ON/OFF (pressed/free) */
/* */
void DrawButton(int Bheight, int Bwidth, int BX, int BY, int BState)
{
cleardevice(); /* Clear graphics screen */
setlinestyle( SOLID_LINE, 0, BTHICK ); /* Set line to thick and solid */
if ( BState == ON )
{
setcolor( BCOLOR2 ); /* Set current color */
line( BX, BY + Bheight, BX, BY ); /* Draw border line */
lineto( BX + Bwidth, BY ); /* Draw border line */
setcolor( BCOLOR1 ); /* Set current color */
line( BX + BTHICK, BY + Bheight, BX + BTHICK, BY + BTHICK ); /* Draw border line */
lineto( BX + Bwidth, BY + BTHICK ); /* Draw border line */
}
else if ( BState == OFF )
{
setcolor( BCOLOR1 ); /* Set current color */
line( BX, BY + Bheight, BX, BY ); /* Draw border line */
lineto( BX + Bwidth, BY ); /* Draw border line */
setcolor( BCOLOR2 ); /* Set current color */
lineto( BX + Bwidth, BY + Bheight ); /* Draw border line */
lineto( BX, BY + Bheight); /* Draw border line */
}
}