วันจันทร์ที่ 29 กันยายน พ.ศ. 2551

เปิดรูป BMP 16 BIT

#include
#include
#include
#include
#include


struct BMP
{
char Type[2]; //File type. Set to "BM".
unsigned long Size; //Size in BYTES of the file.
unsigned long Reserved; //Reserved. Set to zero.
unsigned long OffSet; //Offset to the data.
unsigned long headsize; //Size of rest of header. Set to 40.
unsigned long Width; //Width of bitmap in pixels.
unsigned long Height; // Height of bitmap in pixels.
unsigned int Planes; //Number of Planes. Set to 1.
unsigned int BitsPerPixel; //Number of Bits per pixels.
unsigned long Compression; //Compression. Usually set to 0.
unsigned long SizeImage; //Size in bytes of the bitmap.
unsigned long XPixelsPreMeter; //Horizontal pixels per meter.
unsigned long YPixelsPreMeter; //Vertical pixels per meter.
unsigned long ColorsUsed; //Number of colors used.
unsigned long ColorsImportant; //Number of "important" colors.
} Obj;

int ShowBMP(int x, int y, char* FileName)
{
int b,a,im;

unsigned char* Datas;
int in=0;
unsigned char c=0;
FILE * fp;

fp = fopen(FileName,"rb");
if(!fp){
printf("Error : Unable to open file ..");
exit(0);
}

fread(&Obj, sizeof(Obj), 1, fp);
if(Obj.BitsPerPixel!=4) // This isn't a 16 color bmp we can read;
{
fclose(fp);
printf("Error : File format not supported ..");
exit(0);
};
fseek(fp,Obj.OffSet,SEEK_SET);
Datas=(unsigned char*) calloc(Obj.Width, sizeof(unsigned char));
im = 0;
for(b=Obj.Height;b>=0;b--)
{
fread(Datas, sizeof(unsigned char), Obj.Width/2, fp);
c=0;
in=0;
for(a=0;a<=Obj.Width;a+=2)
{
c = (Datas[in] | 0x00) >>4;
putpixel(a+x,b+y,c);
c = (Datas[in] | 0xF0) & 0x0F;
putpixel(a+1+x,b+y,c);
in++;
}
//fread(Datas, sizeof(unsigned char),1, fp);
//getch();
}
free (Datas);
fclose(fp);
return 1;
}


void main()
{
int color;
int gd , gm ;
gd = VGA ; gm = VGAHI;

initgraph(&gd,&gm,"");
ShowBMP(0,0,"test.bmp");
getch();
closegraph();
}

เกมงูกินหาง

#include
#include
#include
#include
#include
#include

check();
end();
win();
int m[500],n[500],con=20;
clock_t start,stop;
void main(void)
{

int gd=DETECT,gm,ch,maxx,maxy,x=13,y=14,p,q,spd=100;

initgraph(&gd,&gm,"..\bgi");

setcolor(WHITE);
settextstyle(3,0,6);
outtextxy(200,2," SNAKE 2 ");
settextstyle(6,0,2);
outtextxy(20,80," Use Arrow Keys To Direct The Snake ");
outtextxy(20,140," Avoid The Head Of Snake Not To Hit Any Part Of Snake");
outtextxy(20,160," Pick The Beats Untill You Win The Game ");
outtextxy(20,200," Press 'Esc' Anytime To Exit ");
outtextxy(20,220," Press Any Key To Continue ");
ch=getch();
if(ch==27) exit(0);
cleardevice();
maxx=getmaxx();
maxy=getmaxy();

randomize();

p=random(maxx);
int temp=p%13;
p=p-temp;
q=random(maxy);
temp=q%14;
q=q-temp;



start=clock();
int a=0,i=0,j,t;
while(1)
{

setcolor(WHITE);
setfillstyle(SOLID_FILL,con+5);
circle(p,q,5);
floodfill(p,q,WHITE);

if( kbhit() )
{
ch=getch(); if(ch==0) ch=getch();
if(ch==72&& a!=2) a=1;
if(ch==80&& a!=1) a=2;
if(ch==75&& a!=4) a=3;
if(ch==77&& a!=3) a=4;
}
else
{
if(ch==27
) break;
}

if(i<20){
m[i]=x;
n[i]=y;
i++;
}

if(i>=20)

{
for(j=con;j>=0;j--){
m[1+j]=m[j];
n[1+j]=n[j];
}
m[0]=x;
n[0]=y;

setcolor(WHITE);
setfillstyle(SOLID_FILL,con);
circle(m[0],n[0],8);
floodfill(m[0],n[0],WHITE);

setcolor(WHITE);
for(j=1;j setfillstyle(SOLID_FILL,con+j%3);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}
delay(spd);

setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(m[0],n[0],8);
floodfill(m[0],n[0],BLACK);

setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(m[j],n[j],5);
floodfill(m[j],n[j],BLACK);

}
stop=clock();
t=(stop-start)/CLK_TCK;

//printf(" TIME %d sec ",t);
//printf("SCORE %d",con-5);
check();

if(x==p&&y==q) { con=con+5; if(spd>=5) spd=spd-5; else spd=5;
if(con>490) win();
p=random(maxx); temp=p%13; p=p-temp;
q=random(maxy); temp=q%14; q=q-temp;
}
if(a==1) y = y-14; if(y<0) { temp=maxy%14;y=maxy-temp;}
if(a==2) y = y+14; if(y>maxy) y=0;
if(a==3) x = x-13; if(x<0) { temp=maxx%13;x=maxx-temp;}
if(a==4) x = x+13; if(x>maxx) x=0;
if(a==0){ y = y+14 ; x=x+13; }
}

}


check(){
int a;
for(a=1;a
if(m[0]==m[a] && n[0]==n[a]) end();
else continue;

}
end()

{

int j,i;
setcolor(WHITE);
for(i=0;i<5;i++){
delay(500);
cleardevice();
delay(500);
for(j=0;j<=con;j++){
setfillstyle(SOLID_FILL,RED);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}

}

settextstyle(3,0,4);
outtextxy(150,150," GAME OVER ");
getch();
getch();
exit(0);
}

win()
{
int j,i;
setcolor(WHITE);
for(i=0;i<5;i++){
for(j=0;j<=con;j++){
setfillstyle(SOLID_FILL,con);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}
delay(500);
cleardevice();
delay(500);
}
settextstyle(3,0,4);
outtextxy(210,320," YOU WIN ");
getch();
exit(0);
}

สี่เหลี่ยม พิศวง

//n[n*n+1]/2.
void BuildMagicSquare()
{
int order=5,loop;
int MagicMat[5][5];
int SqrOfOrder = order * order;
int nCount=0, mid=order/2;
for(loop=1; loop<=SqrOfOrder ; ++loop)
{
MagicMat[nCount--][mid++] = loop;
if (loop % order == 0)
{
nCount += 2;
--mid;
}
else
{
if (mid==order)
mid -= order;
else if (nCount<0)
nCount += order;
}
}
}

สร้างเกม Tetris

/* Tetris in C*/

#include
#include
#include
#include
#include

#define NUM_OF_BLCKS 30

// Symbolic Constants
const int Sys_MAXX=640,Sys_MAXY=480;
const int EMPTY=-1,TRUE=1,FALSE=0;

// Global Variables
int driver=DETECT,mode,error;

// Structure and Class Declarations
class Window;
class Tetris;

class Window
{
private:
int xleft,ytop,xright,ybottom;
int Win_MAXX_Span,Win_MAXY_Span;
int linecolor,bgcolor;
public:
Window()
{
// NULL CONSTRUCTOR
// Assigning in the Sequence :
// Upper_Left(UL) , Lower_Right(LR)
xleft=ytop=0;
xright=Sys_MAXX;ybottom=Sys_MAXY;
Win_MAXX_Span=Sys_MAXX;
Win_MAXY_Span=Sys_MAXY;
linecolor=WHITE;
bgcolor=BLACK;
}
void set_window_par(int[],int,int);
void draw_window(void);
void putstring(char*,int,int);
int get_parameter(int);
};

void Window :: set_window_par(int xyco[],int lc=WHITE,int bgc=BLACK)
{
// Assigning in the Sequence :
// Upper_Left(UL) , Lower_Right(LR)
xleft=xyco[0];
ytop=xyco[1];
xright=xyco[2];
ybottom=xyco[3];

Win_MAXX_Span=xright-xleft;
Win_MAXY_Span=ybottom-ytop;
linecolor=lc;
bgcolor=bgc;
}

void Window :: draw_window(void)
{
setcolor(linecolor);
setlinestyle(0,0,1);
rectangle(xleft,ytop,xright,ybottom);
setfillstyle(1,bgcolor);
floodfill(xleft+1,ytop+1,linecolor);
}

void Window :: putstring(char s[],int xco,int yco)
{
moveto(xco,yco);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,0);
setcolor(WHITE);
setusercharsize(1,2,1,2);
outtext(s);
}

int Window :: get_parameter(int Para_id)
{
int Para_value;
switch(Para_id)
{
case 1: Para_value=xleft;
case 2: Para_value=ytop;
case 3: Para_value=xright;
case 4: Para_value=ybottom;
case 5: Para_value=Win_MAXX_Span;break;
case 6: Para_value=Win_MAXY_Span;break;
case 7: Para_value=linecolor;break;
case 8: Para_value=bgcolor;break;
}
return Para_value;
}


class Tetris
{
private:
int SCode,PCode,TCode;
int **tarr,fbsize,boxtype;
int VXCh,HYCh,fillcol;
Window W1; // Main Window
Window W2; // Side Window
Window W3; // Next_Piece Window
Window W4; // Total Score Window
Window W5; // "Total Lines Destroyed" Window
Window W6; // "Lines Remaining for Game Over" Window
Window W7; // "Level" Window
public:
Tetris(){fbsize=10;fillcol=RED;}
void regulate_play(void);
void initialize_windows(void);
void gen_randshape(void);
void draw_shape(int,int,int,Window*);
int verify_vert_movement(int,int);
}Temp;


void Tetris :: regulate_play(void)
{
int i,j,r;
int channel,y=0;
int prevchan,prevy;

initialize_windows();

/*
Looping as many times as user desires .Each time, one Tetris-Block is
produced randomly at the top of Main Window, slides in Free-fall
and settles at floor of the Tetris Window. In the meanwhile, next piece
is produced randomly(displayed in "Next Piece Window"), which is used as
the Falling Tetris-Block in next iteration.
Please note, here:
'T' - the invoking object from main() is object for Tetris Main Window
'Temp' - Is the Object for "Next Piece Window"
*/
for(r=0;r {
if(r==0)
// Very 1st Looping
{
// Hence, Generating shape to be used currently/initially by using Next_Piece Object
Temp.gen_randshape();

// Randomly Selecting the FillColor of the Shape that will be drawn in Main Window
do{
fillcol=random(16);
}while(fillcol==WHITE||fillcol==YELLOW||fillcol==BLACK);
}
else
// Subsequent Iterations
{
// Erasing "Next Piece Window" Graphically
W3.draw_window();
// Erasing "Next Piece Window" Logically (i.e. contents of tarr[][] reset)
Temp.draw_shape(3,3,EMPTY,&W3);
}

// Making the Next Piece Obj contents as the Main Window Piece Obj contents
SCode=Temp.SCode;
PCode=Temp.PCode;
TCode=Temp.TCode;
if(r!=0)
fillcol=Temp.fillcol;

// Generating a Random Next-Piece =>
// This will by above assignments become
// the Current Piece in next iteration
Temp.gen_randshape();

// Randomly Selecting the Channel =>
// One of the (45-3) Vertical Channels that the piece can be launched
// into Free-Fall from Top of the Tetris Window
channel=random(VXCh-3);

// Randomly Selecting the FillColor of the Shape that will be drawn in the Next Piece Window
do{
Temp.fillcol=random(16);
}while(Temp.fillcol==WHITE||Temp.fillcol==YELLOW|| Temp.fillcol==BLACK);

// Drawing the Pieces in Main & Next Piece Window respectively
draw_shape(channel,y,fillcol,&W1);
Temp.draw_shape(3,3,Temp.fillcol,&W3);

// Erasing Piece from Main Window
W1.draw_window();draw_shape(channel,y,EMPTY,&W1);


// Iterating for Current Piece for Free-Fall Motion to floor of Tetris Window
// Here:
// 1. 'j' will control the FREE-FALLING Motion of Tetris Block
// 2. 'channel' will control Horizontal/Sideways Motion of Tetris Block
for(j=y;(j ||(j ||(j {
prevchan=channel;

if(channel==0)
// We have reached the LEFTMOST boundary
// Hence, we may either let 'channel' be or Increment it
// We CANNOT and MUST NOT Decrement 'channel'
{
switch(random(2))
{
case 0:break;
case 1:channel++;
}
}

else if((channel==VXCh-2&&boxtype==23) || (channel==VXCh-3&&boxtype==32) ||
(channel==VXCh-1&&boxtype==14) || (channel==VXCh-4&&boxtype==41) ||(channel==VXCh-2&&boxtype==22))
// We have reached the RIGHTMOST boundary
// Hence, we may either let 'channel' be or Decrement it
// We CANNOT and MUST NOT Increment 'channel'
{
switch(random(2))
{
case 0:break;
case 1:channel--;
}
}
else
// We are somewhere in the middle
// Hence, we may move leftwards, rightwards or stay as it is
{
switch(random(3))
{
case 0:channel++;break;
case 1:channel--;break;
case 2:break;
}
}

if(j!=y)
// Executing for all iterations except the 1st one
{
// Erasing contents of Tetris Main Window Graphically
W1.draw_window();

// Erasing contents of Tetris Main Window Logically
// (i.e. contents of tarr[][] are reset)
draw_shape(prevchan,j-1,EMPTY,&W1);
}

// Drawing the Tetris Block with new values of 'channel' & 'j'
draw_shape(channel,j,fillcol,&W1);

// Testing whether further vertical motion is possible or not
// Depending upon that, we either continue or abandon iterating
if(!verify_vert_movement(channel,j))
break;
delay(1000);
}
}
}


void Tetris :: initialize_windows(void)
{
//TESTING Window Drawings
initgraph(&driver,&mode,"\\TC\\BGI");
error=graphresult();
if(error!=grOk)
{
printf("GRAPHICS ERROR!!");
getch();
exit(1);
}

int xy1[4],xy2[4],xy3[4],xy4[4],xy5[4],xy6[4],xy7[4];

// Setting Window Parameters
xy1[0]=10;xy1[1]=10;
xy1[2]=460;xy1[3]=470;

xy2[0]=480;xy2[1]=10;
xy2[2]=630;xy2[3]=470;

xy3[0]=510;xy3[1]=40;
xy3[2]=600;xy3[3]=120;

xy4[0]=495;xy4[1]=160;
xy4[2]=615;xy4[3]=190;

xy5[0]=495;xy5[1]=230;
xy5[2]=615;xy5[3]=260;

xy6[0]=495;xy6[1]=300;
xy6[2]=615;xy6[3]=330;

xy7[0]=495;xy7[1]=370;
xy7[2]=615;xy7[3]=400;

W1.set_window_par(xy1);
W2.set_window_par(xy2);
W3.set_window_par(xy3);
W4.set_window_par(xy4);
W5.set_window_par(xy5);
W6.set_window_par(xy6);
W7.set_window_par(xy7);


// Initializing for the Main Window Tetris Obj 'T/Temp' the following parameters ;
// 1. No. of Vertical Channels or Vertical Span (VXCh)
// 2. No. of Horizontal Channels ot Horizontal Span (HYCh)
// 3. 2D-Array 'tarr' where -
// tarr[i][j] => 10pixel by 10pixel block, 3-4 of which put together forms ONE Tetris Block
VXCh=W1.get_parameter(5)/fbsize;
HYCh=W1.get_parameter(6)/fbsize;
tarr=(int**)malloc((VXCh)*sizeof(int));
for(int i=0;i tarr[i]=(int*)malloc((HYCh)*sizeof(int));

for(int j=0;j for(int k=0;k tarr[j][k]=EMPTY;


// Initializing for the Next Piece Window Tetris Obj 'Temp' the following parameters ;
// 1. No. of Vertical Channels or Vertical Span (HYCh)
// 2. No. of Horizontal Channels ot Horizontal Span (HYCh)
// 3. 2D-Array 'tarr' where -
// tarr[i][j] => 10pixel by 10pixel block, 3-4 of which put together forms ONE Tetris Block
Temp.VXCh=W3.get_parameter(5)/fbsize;
Temp.HYCh=W3.get_parameter(6)/fbsize;
Temp.tarr=(int**)malloc((Temp.VXCh)*sizeof(int));
for(i=0;i Temp.tarr[i]=(int*)malloc((Temp.HYCh)*sizeof(int));

for(j=0;j for(k=0;k Temp.tarr[j][k]=EMPTY;


// Drawing all the Windows
W1.draw_window();
W2.draw_window();
W3.draw_window();
W4.draw_window();
W5.draw_window();
W6.draw_window();
W7.draw_window();

// Writing String Entries in Windows
W2.putstring("Next Piece :",495,20);
W2.putstring("SCORE :",495,140);
W2.putstring("Tot. Lines Dest. :",495,210);
W2.putstring("Lines Rem. :",495,280);
W2.putstring("LEVEL :",495,350);
}


void Tetris :: gen_randshape(void)
{
SCode=random(7);
// Scode=0 --> L-Shape , Scode=1 --> InvL-Shape
// Scode=2 --> T-Shape , Scode=3 --> Z-Shape
// Scode=4 --> InvZ-Shape , Scode=5 --> I-Shape
// Scode=6 --> Sq-Shape

// Tcode=0 --> L1 , Tcode=1 --> L2 , Tcode=2 --> L3
// Tcode=3 --> L4 , Tcode=4 --> IL1 , Tcode=5 --> IL2
// Tcode=6 --> IL3 , Tcode=7 --> IL4 , Tcode=8 --> T1
// Tcode=9 --> T2 , Tcode=10 --> T3 , Tcode=11 --> T4
// Tcode=12 --> Z1 , Tcode=13 --> Z2 , Tcode=14 --> IZ1
// Tcode=15 --> IZ2 , Tcode=16 --> I1 , Tcode=17 --> I2
// Tcode=18 --> Sq

/*
NOTE:
1. TCode=0 --> L1 => Normal 'L' Shaped Block, i.e. '|_'
Give Anti-Clockwise Rotation to visualize L2, L3 & L4

2. TCode=4 --> IL1 => Inverted 'L' Shaped Block, i.e. '_|'
Give Anti-Clockwise Rotation to visualize IL2, IL3 & IL4

3. TCode=8 --> T1 => '_|_' Shaped Block
Give Anti-Clockwise Rotation to visualize T2, T3 & T4
_
4. TCode=12 -->Z1 => Normal 'Z' Shaped Block, i.e. ' |_'
Give Anti-Clockwise Rotation to visualize Z2

_
5. TCode=14 -->IZ1 => Inverted 'Z' Shaped Block, i.e. '_| '
Give Anti-Clockwise Rotation to visualize IZ2

6. TCode=16 -->I1 => Long Vertically Standing Block
7. TCode=17 -->I2 => Long Horizontally Sleeping Block
8. TCode=18 -->Sq => Sq Block consisting of 2 by 2 fundamental blocks

Each of the above 8 sets consist of 4 Fundamental Blocks
*/

switch(SCode)
{
case 0:
case 1:
case 2: PCode=random(4);TCode=4*SCode+PCode;break;
case 3: PCode=random(2);TCode=12+PCode;break;
case 4: PCode=random(2);TCode=14+PCode;break;
case 5: PCode=random(2);TCode=16+PCode;break;
case 6: PCode=0;TCode=18;
}
}


void Tetris :: draw_shape(int channel,int y,int DMode,Window *W)
{
if(TCode==18)
{
// Boxtype is 2Col by 2Rows (i.e. 2VXPix by 2HYPix)
boxtype=22;
// Drawing the 4 Blocks Logically
tarr[channel][y]=tarr[channel][y+1]=tarr[channel+1][y]=tarr[channel+1][y+1]=DMode;
}

else if(TCode==16)
{
// Boxtype is 1Col by 4Row (i.e. 1VXPix by 4HYPix)
boxtype=14;
// Drawing the 4 Blocks Logically
tarr[channel][y]=tarr[channel][y+1]=tarr[channel][y+2]=tarr[channel][y+3]=DMode;
}

else if(TCode==17)
{
// Boxtype is 4Col by 1Row (i.e. 4VXPix by 1HYPix)
boxtype=41;
// Drawing the 4 Blocks Logically
tarr[channel][y]=tarr[channel+1][y]=tarr[channel+2][y]=tarr[channel+3][y]=DMode;
}

else if(TCode==0||TCode==2||TCode==4||TCode==6||
TCode==9||TCode==11||TCode==13||TCode==15)
{
// Boxtype is 2Col by 3Rows (i.e. 2VXPix by 3HYPix)
boxtype=23;
if(TCode==0||TCode==6||TCode==9)
// Drawing the Common Blocks Logically
tarr[channel][y]=tarr[channel][y+1]=tarr[channel][y+2]=DMode;
else if(TCode==2||TCode==4||TCode==11)
// Drawing the Common Blocks Logically
tarr[channel+1][y]=tarr[channel+1][y+1]=tarr[channel+1][y+2]=DMode;
else if(TCode==13||TCode==15)
// Drawing the Common Blocks Logically
tarr[channel][y+1]=tarr[channel+1][y+1]=DMode;
}

else if(TCode==1||TCode==3||TCode==5||TCode==7||
TCode==8||TCode==10||TCode==12||TCode==14)
{
// Boxtype is 3Col by 2Rows (i.e. 3VXPix by 2HYPix)
boxtype=32;
if(TCode==1||TCode==7||TCode==8)
// Drawing the Common Blocks Logically
tarr[channel][y+1]=tarr[channel+1][y+1]=tarr[channel+2][y+1]=DMode;
else if(TCode==3||TCode==5||TCode==10)
// Drawing the Common Blocks Logically
tarr[channel][y]=tarr[channel+1][y]=tarr[channel+2][y]=DMode;
else if(TCode==12||TCode==14)
// Drawing the Common Blocks Logically
tarr[channel+1][y]=tarr[channel+1][y+1]=DMode;
}

// Drawing the Specialized Xtra Blocks Logically
switch(TCode)
{
case 0: tarr[channel+1][y+2]=DMode;break;
case 1: tarr[channel+2][y]=DMode;break;
case 2: tarr[channel][y]=DMode;break;
case 3: tarr[channel][y+1]=DMode;break;
case 4: tarr[channel][y+2]=DMode;break;
case 5: tarr[channel+2][y+1]=DMode;break;
case 6: tarr[channel+1][y]=DMode;break;
case 7: tarr[channel][y]=DMode;break;
case 8: tarr[channel+1][y]=DMode;break;
case 9: tarr[channel+1][y+1]=DMode;break;
case 10:tarr[channel+1][y+1]=DMode;break;
case 11:tarr[channel][y+1]=DMode;break;
case 12:tarr[channel][y]=DMode;tarr[channel+2][y+1]=DMode;break;
case 13:tarr[channel+1][y]=DMode;tarr[channel][y+2]=DMode;break;
case 14:tarr[channel][y+1]=DMode;tarr[channel+2][y]=DMode;break;
case 15:tarr[channel][y]=DMode;tarr[channel+1][y+2]=DMode;break;
}

// Actually Drawing it Graphically for Window W1
if(W==&W1)
{
for(int i=0;i for(int j=0;j if(tarr[i][j]!=EMPTY)
{
setcolor(YELLOW);
setlinestyle(0,0,1);
rectangle(fbsize*(i+1),fbsize*(j+1),fbsize*(i+2),fbsize*(j+2));
setfillstyle(1,tarr[i][j]);
floodfill(fbsize*(i+1)+1,fbsize*(j+1)+1,YELLOW);
}
}
else
{
for(int i=0;i for(int j=0;j if(tarr[i][j]!=EMPTY)
{
setcolor(YELLOW);
setlinestyle(0,0,1);
rectangle(fbsize*(i+51),fbsize*(j+4),fbsize*(i+52) ,fbsize*(j+5));
setfillstyle(1,tarr[i][j]);
floodfill(fbsize*(i+51)+1,fbsize*(j+4)+1,YELLOW);
}
}
}


int Tetris :: verify_vert_movement(int channel,int y)
{
if(TCode==18)
{
if(tarr[channel][y+2]!=EMPTY||tarr[channel+1][y+2]!=EMPTY)
return FALSE;
else
return TRUE;
}

else if(TCode==16)
{
if(tarr[channel][y+4]!=EMPTY)
return FALSE;
else
return TRUE;
}

else if(TCode==17)
{
if(tarr[channel][y+1]!=EMPTY||tarr[channel+1][y+1]!=EMPTY||tarr[channel+2][y+1]!=EMPTY||tarr[channel+3][y+1]!=EMPTY)
return FALSE;
else
return TRUE;
}

else if(TCode==0||TCode==2||TCode==4||TCode==6||
TCode==9||TCode==11||TCode==13||TCode==15)
{
if(TCode==0||TCode==6||TCode==9)
{
// Analysing the Common Blocks Logically
if(tarr[channel][y+3]!=EMPTY)
return FALSE;
}

else if(TCode==2||TCode==4||TCode==11)
{
// Analysing the Common Blocks Logically
if(tarr[channel+1][y+3]!=EMPTY)
return FALSE;
}

else if(TCode==13)
{
// Analysing the Blocks Logically
if(tarr[channel][y+3]!=EMPTY||tarr[channel+1][y+2]!=EMPTY)
return FALSE;
else
return TRUE;
}

else if(TCode==15)
{
// Analysing the Blocks Logically
if(tarr[channel][y+2]!=EMPTY||tarr[channel+1][y+3]!=EMPTY)
return FALSE;
else
return TRUE;
}
}

else if(TCode==1||TCode==3||TCode==5||TCode==7||
TCode==8||TCode==10||TCode==12||TCode==14)
{
if(TCode==1||TCode==7||TCode==8)
{
// Analysing the Common Blocks Logically
if(tarr[channel][y+2]!=EMPTY||tarr[channel+1][y+2]!=EMPTY||tarr[channel+2][y+2]!=EMPTY)
return FALSE;
else
return TRUE;
}
else if(TCode==12)
{
// Analysing the Blocks Logically
if(tarr[channel][y+1]!=EMPTY||tarr[channel+1][y+2]!=EMPTY||tarr[channel+2][y+2]!=EMPTY)
return FALSE;
else
return TRUE;
}
else if(TCode==14)
{
// Analysing the Blocks Logically
if(tarr[channel][y+2]!=EMPTY||tarr[channel+1][y+2]!=EMPTY||tarr[channel+2][y+1]!=EMPTY)
return FALSE;
else
return TRUE;
}
}

// Analysing the Specialized Xtra Blocks Logically
switch(TCode)
{
case 0: if(tarr[channel+1][y+3]!=EMPTY)return FALSE;break;
case 6: if(tarr[channel+1][y+1]!=EMPTY)return FALSE;break;
case 9: if(tarr[channel+1][y+2]!=EMPTY)return FALSE;break;

case 2: if(tarr[channel][y+1]!=EMPTY)return FALSE;break;
case 4: if(tarr[channel][y+3]!=EMPTY)return FALSE;break;
case 11:if(tarr[channel][y+2]!=EMPTY)return FALSE;break;

case 3: if(tarr[channel][y+2]!=EMPTY||tarr[channel+1][y+1]!=EMPTY||tarr[channel+2][y+1]!=EMPTY)return FALSE;break;
case 5: if(tarr[channel][y+1]!=EMPTY||tarr[channel+1][y+1]!=EMPTY||tarr[channel+2][y+2]!=EMPTY)return FALSE;break;
case 10:if(tarr[channel][y+1]!=EMPTY||tarr[channel+1][y+2]!=EMPTY||tarr[channel+2][y+1]!=EMPTY)return FALSE;//break;
}
return TRUE;
}



// Main Prog :
void main()
{
//Seeding the Random Number Generator
randomize();
Tetris T;

printf("\n\nWelcome to Tetris \n\n");
printf("\n\nThis Demo is a glimpse into a preliminary Version and just shows the Movement of Various Tetris Pieces along the Tetris Play Space\n\n");
printf("\n\nUser I/P is NOT Expected\n\n");
printf("\n\n\nPress ENTER to goto Graphics Mode and See the DEMO\n\n\n");
getch();clrscr();
T.regulate_play();
getch();
}

สร้างเกม PONG ในภาษา C แบบ 2

/* Program for a basic 2 player Pong match with no specialized shot/bounce physics */

/*
Programmed By:
Rajiv Iyer,
T.E. Computers,
SIES GST, Nerul
*/

/* SOURCE CODE */
#include
#include
#include
#include
#include
#include
#include

#define BARSIZE 4
#define DEFRAD 10
#define DEFDELAY 20
#define LONGDELAY 1500
#define HFSND 70000
#define DEFPENALTY 50
#define DEFREWARD 25

/* Global Variables */
// Symbolic Constants
const int BARYCH=10;
const int DXCH=3,DYCH=3;
const int TRUE=1,FALSE=0;
const int FWD=0,BCK=1,UP=0,DWN=1;
const int LEFT=1,RIGHT=2,TOP=3,BOTTOM=4;

// Other Variables
int gd,gm,MAXX,MAXY;
int bounce=TRUE,cuttingbound;
int linecol,fillcol;

/* Structure Declaration */
struct Player
{
int num;
int bar[BARSIZE];
int points;
int returned,missed;
}P1,P2;

struct Sphere
{
int xinst,yinst;
int xdir,ydir;
int radius;
}Ball;

/* Function Prototypes */
void instructions(void);
void initGraphics(void);
void testResolution(void);
void moveBall(void);
int testBoundary(void);
int keyhit(void);
void rebound(void);
void drawBall(void);
void drawUserBars(void);
void pauseMenu(void);
void displayStats(void);

void main()
{
testResolution();
instructions();
moveBall();
}

void instructions(void)
{
clrscr();
printf("Welcome to the Basic 2D Tennis Program!");
printf("\n\nKeys for User I :-\n");
printf("MOVE BAR UP : 'W' or 'w'\n");
printf("MOVE BAR DOWN : 'S' or 's'\n");
printf("\n\nKeys for User II :-\n");
printf("MOVE BAR UP : 'P' or 'p'\n");
printf("MOVE BAR DOWN : 'L' or 'l'\n");

printf("\n\nCommon Keys:\n");
printf("Terminate Game : 'T' or 't'\n");
printf("Halt/Pause Game : 'H' or 'h'\n");

printf("\n\nAll Keys are Case Insensitive!");
printf("\n\n\nPress Any Key to Enter Graphics Mode & PLAY ! ..... ");
getch();
}

void initGraphics(void)
{
int error;
initgraph(&gd,&gm,"\\TC\\BGI");
error=graphresult();
if(error!=grOk)
{
printf("\n\n\aGRAPHICS ERROR! Error Code : %d",error);
getch();exit(1);
}
}

void testResolution(void)
{
clrscr();
printf("Testing System Resolution! Press Any Key to enter Graphics Mode ....");
getch();

initGraphics();
MAXX=getmaxx();
MAXY=getmaxy();
closegraph();

printf("System Resolution Detected!\n");
printf("\nHORIZONTAL PIXEL SPAN : %d (0 to %d)",MAXX+1,MAXX);
printf("\nVERTICAL PIXEL SPAN : %d (0 to %d)",MAXY+1,MAXY);
getch();
clrscr();
}

void moveBall(void)
{
// Seeding Random Number Generator
randomize();
// Initializing the Graphics Mode
initGraphics();

// Initializing the Position of the Two Player Bars
P1.bar[0]=20 ; P1.bar[1]=200; P1.bar[2]=30 ; P1.bar[3]=280;
P2.bar[0]=610; P2.bar[1]=200; P2.bar[2]=620; P2.bar[3]=280;
// Initializing the Marks Tally of Both Players to Zero
P1.points=P2.points=0;
P1.returned=P2.returned=0;
P1.missed=P2.missed=0;
// Initializing the Position of the Ball
Ball.xinst=MAXX/2;
Ball.yinst=MAXY/2;
Ball.radius=DEFRAD;
// Randomizing the Initial Direction of Travel of Ball
Ball.xdir=random(2);
Ball.ydir=random(2);
// Iterating Till Either User Hits TERMINATE(T) Key
while(!keyhit())
{
if(testBoundary())
{
// Drawing the Ball
cleardevice();
//setfillstyle(1,0);
//bar(0,0,640,480);
drawBall();
drawUserBars();
delay(DEFDELAY);
// Making Changes
if(Ball.xdir==FWD)
Ball.xinst+=DXCH;
else
Ball.xinst-=DXCH;
if(Ball.ydir==UP)
Ball.yinst-=DYCH;
else
Ball.yinst+=DYCH;
}
else
rebound();
}
closegraph();
displayStats();
}

int keyhit(void)
{
int i;
char uch,tempch;
if(kbhit())
{
uch=getch();
switch(uch)
{
case 't':
case 'T':
// The TERMINATE (T) Key was hit - so Returning True
return TRUE;
case 'w':
case 'W':
// The UP-Direction Key for Player I has been pressed
// Testing Whether Player Bar goes out of window with such a move
if(P1.bar[1]-BARYCH<0)
{
//sound(HFSND);
//nosound();
}
else
{
for(i=1;i P1.bar[i]-=BARYCH;
}
return FALSE;
case 's':
case 'S':
// The DOWN-Direction Key for Player I has been pressed
// Testing Whether Player Bar goes out of window with such a move
if(P1.bar[3]+BARYCH>MAXY)
{
//sound(HFSND);
//nosound();
}
else
{
for(i=1;i P1.bar[i]+=BARYCH;
}
return FALSE;
case 'p':
case 'P':
// The UP-Direction Key for Player II has been pressed
// Testing Whether Player Bar goes out of window with such a move
if(P2.bar[1]-BARYCH<0)
{
//sound(HFSND);
//nosound();
}
else
{
for(i=1;i P2.bar[i]-=BARYCH;
}
return FALSE;
case 'l':
case 'L':
// The DOWN-Direction Key for Player II has been pressed
// Testing Whether Player Bar goes out of window with such a move
if(P2.bar[3]+BARYCH>MAXY)
{
//sound(HFSND);
//nosound();
}
else
{
for(i=1;i P2.bar[i]+=BARYCH;
}
return FALSE;

case 'h':
case 'H':
//do{
pauseMenu();
//tempch=getch();
//}while(tempch!='h' && tempch!='H');
return FALSE;
default:
return FALSE;
}
}
else
return FALSE;
}

int testBoundary(void)
{
if(bounce==TRUE)
return TRUE;
else
{
if((Ball.xinst+Ball.radius)>=MAXX || (Ball.xinst-Ball.radius)<=0)
{
if((Ball.xinst+Ball.radius)>=MAXX)
{
cuttingbound=RIGHT;
// Player 2 misses Shot => Hence Deducting Points
P2.points-=DEFPENALTY;
P2.missed++;
}
else if((Ball.xinst-Ball.radius)<=0)
{
cuttingbound=LEFT;
// Player 1 misses Shot => Hence Deducting Points
P1.points-=DEFPENALTY;
P1.missed++;
}
// Resetting Ball to Center of Screen
Ball.xinst=MAXX/2;
Ball.yinst=MAXY/2;
// Randomizing the Initial Direction of Travel
Ball.xdir=random(2);
Ball.ydir=random(2);
// Delaying for Short Period before Graphically Resetting
delay(LONGDELAY);
return TRUE;
}
else if((Ball.yinst+Ball.radius)>=MAXY || (Ball.yinst-Ball.radius)<=0)
{
if((Ball.yinst+Ball.radius)>=MAXY)
cuttingbound=BOTTOM;
else if((Ball.yinst-Ball.radius)<=0)
cuttingbound=TOP;
return FALSE;
}
else if((Ball.xinst+Ball.radius)>=P2.bar[0] && (Ball.yinst>P2.bar[1] && Ball.yinst {
cuttingbound=RIGHT;
// Player 2 Returns Shot Successfully => Hence Rewarding Points
P2.points+=DEFREWARD;
P2.returned++;
return FALSE;
}
else if((Ball.xinst-Ball.radius)<=P1.bar[2] && (Ball.yinst>P1.bar[1] && Ball.yinst {
cuttingbound=LEFT;
// Player 1 Returns Shot Successfully => Hence Rewarding Points
P1.points+=DEFREWARD;
P1.returned++;
return FALSE;
}
else
return TRUE;
}
}

void rebound(void)
{
bounce=TRUE;
switch(cuttingbound)
{
case LEFT:Ball.xdir=FWD;break;
case RIGHT:Ball.xdir=BCK;break;
case TOP:Ball.ydir=DWN;break;
case BOTTOM:Ball.ydir=UP;break;
}
}

void drawBall(void)
{
if(bounce==TRUE)
{
bounce=FALSE;
//do{
linecol=(15);
fillcol=(15);
//}while(linecol==BLACK || fillcol==BLACK || linecol==fillcol);
}
setcolor(linecol);
circle(Ball.xinst,Ball.yinst,Ball.radius);
setfillstyle(1,fillcol);
floodfill(Ball.xinst,Ball.yinst,linecol);
}

void drawUserBars(void)
{
// Drawing Player I Bar
setcolor(linecol);
rectangle(P1.bar[0],P1.bar[1],P1.bar[2],P1.bar[3]);
setfillstyle(1,fillcol);
floodfill(P1.bar[0]+5,P1.bar[1]+5,linecol);
// Drawing Player I Bar
setcolor(linecol);
rectangle(P2.bar[0],P2.bar[1],P2.bar[2],P2.bar[3]);
setfillstyle(1,fillcol);
floodfill(P2.bar[0]+5,P2.bar[1]+5,linecol);
}

void pauseMenu(void)
{
char pch;

do{
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
outtextxy(150,100,"1. Resume Game");
outtextxy(150,200,"2. Display Statistics");
outtextxy(150,300,"3. Controls");

pch=getch();
switch(pch)
{
case '1':
break;
case '2':
closegraph();
displayStats();
initGraphics();
break;
case '3':
closegraph();
instructions();
initGraphics();
break;
default:
printf("\n\n\aInvalid Choice!\n\n\a");
}
}while(pch!='1');
}

void displayStats(void)
{
// Printing All Collected Statistics
printf("Statistics Score-Card :\n");
printf("\n\nFor User I :\nShots Taken : %d\nShots Missed : %d\nTotal Score : %d",P1.returned,P1.missed,P1.points);
printf("\n\nFor User II :\nShots Taken : %d\nShots Missed : %d\nTotal Score : %d",P2.returned,P2.missed,P2.points);
printf("\n\n\nWINNER : ");
if(P1.points>P2.points)
printf("Player I !!");
else if(P1.points printf("Player II !!");
else
printf("Undeterminable !!");
getch();
}

สร้างเกม PONG ในภาษา C

/* SOURCE CODE */
#include
#include
#include
#include
#include
#include
#include

#define DEFDELAY 5

/* Global Variables */
// Symbolic Constants
const int xincr=1,yincr=1;
const int TRUE=1,FALSE=0;
const int FWD=0,BCK=1,UP=0,DWN=1;
const int LEFT=1,RIGHT=2,TOP=3,BOTTOM=4;

// Other Variables
int gd,gm,MAXX,MAXY;
int xstart,ystart,ballrad;
int xinst,yinst;
int xdir,ydir,cuttingbound;
int bounce=TRUE,linecol,fillcol;

/* Function Prototypes */
void initgraphics(void);
void testresolution(void);
void moveball(void);
int testboundary(void);
void rebound(void);
void drawball(void);
void showimpact(void);

void main()
{
testresolution();
printf("Please Enter XY Co-Ordinates of Starting Point : ");
scanf("%d%d",&xstart,&ystart);
printf("\n\nEnter the Size of Ball (Radius) : ");
scanf("%d",&ballrad);

printf("\n\n\nPress Any Key to Enter Graphics Mode ..... ");
getch();
moveball();
}

void initgraphics(void)
{
int error;
initgraph(&gd,&gm,"\\TC\\BGI");
error=graphresult();
if(error!=grOk)
{
printf("\n\n\aGRAPHICS ERROR! Error Code : %d",error);
getch();exit(1);
}
}

void testresolution(void)
{
clrscr();
printf("Testing System Resolution! Press Any Key to enter Graphics Mode ....");
getch();

initgraphics();
MAXX=getmaxx();
MAXY=getmaxy();
closegraph();

printf("System Resolution Detected!\n");
printf("\nHORIZONTAL PIXEL SPAN : %d (0 to %d)",MAXX+1,MAXX);
printf("\nVERTICAL PIXEL SPAN : %d (0 to %d)",MAXY+1,MAXY);
getch();
clrscr();
}

void moveball(void)
{
// Seeding Random Number Generator
randomize();
// Initializing the Graphics Mode
initgraphics();

// Initializing the Position of the Ball
xinst=xstart;
yinst=ystart;
// Iterating Till User Hits a Key
xdir=random(2);
ydir=random(2);
while(!kbhit())
{
if(testboundary())
{
// Drawing the Ball
cleardevice();
drawball();
delay(DEFDELAY);
// Making Changes
if(xdir==FWD)
xinst+=xincr;
else
xinst-=xincr;
if(ydir==UP)
yinst-=yincr;
else
yinst+=yincr;
}
else
rebound();
}
closegraph();
}

int testboundary(void)
{
if(bounce==TRUE)
return TRUE;
else
{
if((xinst+ballrad)>=MAXX || (xinst-ballrad)<=0 ||
(yinst+ballrad)>=MAXY || (yinst-ballrad)<=0)
{
if((xinst+ballrad)>=MAXX)
cuttingbound=RIGHT;
else if((xinst-ballrad)<=0)
cuttingbound=LEFT;
else if((yinst+ballrad)>=MAXY)
cuttingbound=BOTTOM;
else
cuttingbound=TOP;
return FALSE;
}
else
return TRUE;
}
}

void rebound(void)
{
bounce=TRUE;
switch(cuttingbound)
{
case LEFT:xdir=FWD;break;
case RIGHT:xdir=BCK;break;
case TOP:ydir=DWN;break;
case BOTTOM:ydir=UP;break;
}
}

void drawball(void)
{
if(bounce==TRUE)
{
bounce=FALSE;
do{
linecol=random(15);
fillcol=random(15);
}while(linecol==BLACK || fillcol==BLACK || linecol==fillcol);
}
setcolor(linecol);
circle(xinst,yinst,ballrad);
setfillstyle(1,fillcol);
floodfill(xinst,yinst,linecol);
}

เปิดไฟล์ เสียง WAV 8 BIT ในภาษาซี

#include "ALLOC.H"
#include "DOS.H"
#include "CONIO.H"
#include "STDIO.H"

void playwav (char wavefile[14], float delaytime );
int ResetDSP(unsigned int Test);


void main()
{
char *name;
printf("Enter the file name...:");
scanf("%s",name);


playwav(name,1.0);

}


void playwav(char wavefile[14],float delaytime);
struct WaveData {
unsigned int SoundLength, Frequency;
char *Sample;
};


struct HeaderType {
long RIFF; //RIFF header
char NI1 [18]; //not important

unsigned int Channels; //channels 1 = mono; 2 = stereo
long Frequency; //sample frequency
char NI2 [6]; //not important
char BitRes; //bit resolution 8/16 bit
char NI3 [12]; //not important
} Header;





struct WaveData Voice; //Pointer to wave file


unsigned int Base; //Sound Blaster base address


char WaveFile [25]; //File name for the wave file to be
//played
// Checks to see if a Sound Blaster exists at a given address, returns
// true if Sound Blaster found, false if not.

int ResetDSP(unsigned int Test)
{
//Reset the DSP
outportb (Test + 0x6, 1);
delay(10);
outportb (Test + 0x6, 0);
delay(10);
//Check if (reset was succesfull
if ((inportb(Test + 0xE) & 0x80 == 0x80) && (inportb(Test + 0xA) ==
0xAA))
{
//DSP was found
Base = Test;
return (1);
}
else
//No DSP was found
return (0);
}


// Send a byte to the DSP (Digital Signal Processor) on the Sound
//Blaster

void WriteDSP(unsigned char Value)
{
//Wait for the DSP to be ready to accept data
while ((inportb(Base + 0xC) & 0x80) == 0x80);
//Send byte

outportb (Base + 0xC, Value);
}


// Plays a part of the memory

void PlayBack (struct WaveData *Wave)
{
long LinearAddress;
unsigned int Page, OffSet;
unsigned char TimeConstant;


TimeConstant = (65536 - (256000000 / Wave->Frequency)) >> 8;
WriteDSP(0x40); //DSP-command 40h - Set sample
//frequency
WriteDSP(TimeConstant); //Write time constant


//Convert pointer to linear address
LinearAddress = FP_SEG (Wave->Sample);
LinearAddress = (LinearAddress << 4) + FP_OFF (Wave->Sample);
Page = LinearAddress >> 16; //Calculate page
OffSet = LinearAddress & 0xFFFF; //Calculate offset in the page
/*
Note - this procedure only works with DMA channel 1
*/
outportb (0x0A, 5); //Mask DMA channel 1
outportb (0x0C, 0); //Clear byte pointer
outportb (0x0B, 0x49); //Set mode
/*
The mode consists of the following:
0x49 = binary 01 00 10 01
| | | |
| | | +- DMA channel 01
| | +---- Read operation (the DSP reads from
memory)
| +------- Single cycle mode
+---------- Block mode
*/


outportb (0x02, OffSet & 0x100); //Write the offset to the DMA
//controller
outportb (0x02, OffSet >> 8);


outportb (0x83, Page); //Write the page to the DMA
//controller


outportb (0x03, Wave->SoundLength & 0x100);
outportb (0x03, Wave->SoundLength >> 8);


outportb (0x0A, 1); //Unmask DMA channel



WriteDSP(0x14); // DSP-command 14h - Single cycle
//playback
WriteDSP(Wave->SoundLength & 0xFF);
WriteDSP(Wave->SoundLength >> 8);
}


// Loads a wave file into memory.
// This procedure expects a _very_ standard wave header.
// It doesn't perform much error checking.

int LoadVoice (struct WaveData *Voice, char *FileName)
{
FILE *WAVFile;


//If it can't be opened...
WAVFile = fopen(FileName, "rb");
if (WAVFile == NULL) {
//..display error message


return (0);
}


//Return length of file for sound length minus 48 bytes for .WAV
//header
fseek(WAVFile, 0L, SEEK_END);
Voice->SoundLength = ftell (WAVFile) - 48;
fseek(WAVFile, 0L, SEEK_SET);


//Check RIFF header
if (Voice->SoundLength > 32000) {




if (Voice->SoundLength > 64000) {


Voice->SoundLength = 64000;
}
}
free(Voice->Sample);
Voice->Sample = (char *)malloc(Voice->SoundLength); //Assign memory
if (!Voice->Sample) {

return (0);
}


//Load the sample data
fread(&Header, 46, 1, WAVFile);


//Check RIFF header
if (Header.RIFF != 0x46464952) {
printf ("Not a wave file");
return (0);
}


//Check channels
if (Header.Channels != 1) {
printf ("Not a mono wave file");
return (0);
}


//Check bit resolution
if (Header.BitRes != 8) {
printf ("Not an 8-bit wave file");
return (0);
}


Voice->Frequency = Header.Frequency;


//Load the sample data
fread(Voice->Sample, Voice->SoundLength + 2, 1, WAVFile);


fclose (WAVFile); //Close the file


return (1);
}


void playwav (char wavefile[14], float delaytime )
{
delaytime=1.0;
if (ResetDSP (0x220)) {
//at 220h
printf ("");
} else {
if (ResetDSP (0x240)) {

//at 240h
printf ("");
} else {
//or none at all
printf ("");
return;
}
}




//Load wave file
if (LoadVoice (&Voice, wavefile)) {


//Start playback
PlayBack (&Voice);


delay(delaytime*1000);


//Stops DMA-transfer
WriteDSP (0xD0);
}
}

การเลื่อนบิต

#include "stdio.h"
#include "conio.h"

void main()
{
int a = 1;
clrscr();
printf("%d\n",a);

a = a<<1;
printf("%d\n",a);

a = a<<1;
printf("%d\n",a);

a = a<<1;
printf("%d\n",a);


getch();
}

การแสดงผลทั่วไป

#include "stdio.h"
#include "conio.h"

void main()
{ int a;
float b;
char c;
char *d ="THIS IS A BOOK";

a = 10;
b = 10.5;
c = 'D';

clrscr();
printf("%d\n",a);
printf("%.2f\n",b);
printf("%c\n",c);
printf("%s\n",d);
}

hello word

#include "stdio.h"
void main()
{
printf("hello word");
}

การเข้าสู่ GRAPHICS MODE

/*
GRAPHICS DEMO FOR Borland C++ 3.0

Copyright (c) 1987,88,91 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 ReportStatus(void);
void TextDump(void);
void Bar3DDemo(void);
void RandomBars(void);
void TextDemo(void);
void ColorDemo(void);
void ArcDemo(void);
void CircleDemo(void);
void PieDemo(void);
void BarDemo(void);
void LineRelDemo(void);
void PutPixelDemo(void);
void PutImageDemo(void);
void LineToDemo(void);
void LineStyleDemo(void);
void CRTModeDemo(void);
void UserLineStyleDemo(void);
void FillStyleDemo(void);
void FillPatternDemo(void);
void PaletteDemo(void);
void PolyDemo(void);
void SayGoodbye(void);
void Pause(void);
void MainWindow(char *header);
void StatusLine(char *msg);
void DrawBorder(void);
void changetextstyle(int font, int direction, int charsize);
int gprintf(int *xloc, int *yloc, char *fmt, ... );

/* */
/* Begin main function */
/* */

int main()
{

Initialize(); /* Set system into Graphics mode */
ReportStatus(); /* Report results of the initialization */

ColorDemo(); /* Begin actual demonstration */
if( GraphDriver==EGA || GraphDriver==EGALO || GraphDriver==VGA )
PaletteDemo();
PutPixelDemo();
PutImageDemo();
Bar3DDemo();
BarDemo();
RandomBars();
ArcDemo();
CircleDemo();
PieDemo();
LineRelDemo();
LineToDemo();
LineStyleDemo();
UserLineStyleDemo();
TextDump();
TextDemo();
CRTModeDemo();
FillStyleDemo();
FillPatternDemo();
PolyDemo();
SayGoodbye(); /* Give user the closing screen */

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 */

}

/* */
/* REPORTSTATUS: Report the current configuration of the system */
/* after the auto-detect initialization. */
/* */

void ReportStatus(void)
{
struct viewporttype viewinfo; /* Params for inquiry procedures*/
struct linesettingstype lineinfo;
struct fillsettingstype fillinfo;
struct textsettingstype textinfo;
struct palettetype palette;

char *driver, *mode; /* Strings for driver and mode */
int x, y;

getviewsettings( &viewinfo );
getlinesettings( &lineinfo );
getfillsettings( &fillinfo );
gettextsettings( &textinfo );
getpalette( &palette );

x = 10;
y = 4;

MainWindow( "Status report after InitGraph" );
settextjustify( LEFT_TEXT, TOP_TEXT );

driver = getdrivername();
mode = getmodename(GraphMode); /* get current setting */

gprintf( &x, &y, "Graphics device : %-20s (%d)", driver, GraphDriver );
gprintf( &x, &y, "Graphics mode : %-20s (%d)", mode, GraphMode );
gprintf( &x, &y, "Screen resolution : ( 0, 0, %d, %d )", getmaxx(), getmaxy() );

gprintf( &x, &y, "Current view port : ( %d, %d, %d, %d )",
viewinfo.left, viewinfo.top, viewinfo.right, viewinfo.bottom );
gprintf( &x, &y, "Clipping : %s", viewinfo.clip ? "ON" : "OFF" );

gprintf( &x, &y, "Current position : ( %d, %d )", getx(), gety() );
gprintf( &x, &y, "Colors available : %d", MaxColors );
gprintf( &x, &y, "Current color : %d", getcolor() );

gprintf( &x, &y, "Line style : %s", LineStyles[ lineinfo.linestyle ] );
gprintf( &x, &y, "Line thickness : %d", lineinfo.thickness );

gprintf( &x, &y, "Current fill style : %s", FillStyles[ fillinfo.pattern ] );
gprintf( &x, &y, "Current fill color : %d", fillinfo.color );

gprintf( &x, &y, "Current font : %s", Fonts[ textinfo.font ] );
gprintf( &x, &y, "Text direction : %s", TextDirect[ textinfo.direction ] );
gprintf( &x, &y, "Character size : %d", textinfo.charsize );
gprintf( &x, &y, "Horizontal justify : %s", HorizJust[ textinfo.horiz ] );
gprintf( &x, &y, "Vertical justify : %s", VertJust[ textinfo.vert ] );

Pause(); /* Pause for user to read screen*/

}

/* */
/* TEXTDUMP: Display the all the characters in each of the */
/* available fonts. */
/* */

void TextDump()
{
static int CGASizes[] = {
1, 3, 7, 3, 3, 2, 2, 2, 2, 2, 2 };
static int NormSizes[] = {
1, 4, 7, 4, 4, 2, 2, 2, 2, 2, 2 };

char buffer[80];
int font, ch, wwidth, lwidth, size;
struct viewporttype vp;

for( font=0 ; font sprintf( buffer, "%s Character Set", Fonts[font] );
MainWindow( buffer ); /* Display fontname as banner */
getviewsettings( &vp ); /* read current viewport */

settextjustify( LEFT_TEXT, TOP_TEXT );
moveto( 2, 3 );

buffer[1] = '\0'; /* Terminate string */
wwidth = vp.right - vp.left; /* Determine the window width */
lwidth = textwidth( "H" ); /* Get average letter width */

if( font == DEFAULT_FONT ){
changetextstyle( font, HORIZ_DIR, 1 );
ch = 0;
while( ch < 256 ){ /* For each possible character */
buffer[0] = ch; /* Put character into a string */
outtext( buffer ); /* send string to screen */
if( (getx() + lwidth) > wwidth )
moveto( 2, gety() + textheight("H") + 3 );
++ch; /* Goto the next character */
}
}
else{

size = (MaxY < 200) ? CGASizes[font] : NormSizes[font];
changetextstyle( font, HORIZ_DIR, size );

ch = '!'; /* Begin at 1st printable */
while( ch < 256 ){ /* For each printable character */
buffer[0] = ch; /* Put character into a string */
outtext( buffer ); /* send string to screen */
if( (lwidth+getx()) > wwidth ) /* Are we still in window? */
moveto( 2, gety()+textheight("H")+3 );
++ch; /* Goto the next character */
}

}

Pause(); /* Pause until user acks */

} /* End of FONT loop */

}

/* */
/* BAR3DDEMO: Display a 3-D bar chart on the screen. */
/* */

void Bar3DDemo(void)
{
static int barheight[] = {
1, 3, 5, 4, 3, 2, 1, 5, 4, 2, 3 };
struct viewporttype vp;
int xstep, ystep;
int i, j, h, color, bheight;
char buffer[10];

MainWindow( "Bar 3-D / Rectangle Demonstration" );

h = 3 * textheight( "H" );
getviewsettings( &vp );
settextjustify( CENTER_TEXT, TOP_TEXT );
changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
outtextxy( MaxX/2, 6, "These are 3-D Bars" );
changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
setviewport( vp.left+50, vp.top+40, vp.right-50, vp.bottom-10, 1 );
getviewsettings( &vp );

line( h, h, h, vp.bottom-vp.top-h );
line( h, (vp.bottom-vp.top)-h, (vp.right-vp.left)-h, (vp.bottom-vp.top)-h );
xstep = ((vp.right-vp.left) - (2*h)) / 10;
ystep = ((vp.bottom-vp.top) - (2*h)) / 5;
j = (vp.bottom-vp.top) - h;
settextjustify( CENTER_TEXT, CENTER_TEXT );

for( i=0 ; i<6 ; ++i ){
line( h/2, j, h, j );
itoa( i, buffer, 10 );
outtextxy( 0, j, buffer );
j -= ystep;
}

j = h;
settextjustify( CENTER_TEXT, TOP_TEXT );

for( i=0 ; i<11 ; ++i ){
color = random( MaxColors );
setfillstyle( i+1, color );
line( j, (vp.bottom-vp.top)-h, j, (vp.bottom-vp.top-3)-(h/2) );
itoa( i, buffer, 10 );
outtextxy( j, (vp.bottom-vp.top)-(h/2), buffer );
if( i != 10 ){
bheight = (vp.bottom-vp.top) - h - 1;
bar3d( j, (vp.bottom-vp.top-h)-(barheight[i]*ystep), j+xstep, bheight, 15, 1 );
}
j += xstep;
}

Pause(); /* Pause for user's response */

}

/* */
/* RANDOMBARS: Display random bars */
/* */

void RandomBars(void)
{
int color;

MainWindow( "Random Bars" );
StatusLine( "Esc aborts or press a key..." ); /* Put msg at bottom of screen */
while( !kbhit() ){ /* Until user enters a key... */
color = random( MaxColors-1 )+1;
setcolor( color );
setfillstyle( random(11)+1, color );
bar3d( random( getmaxx() ), random( getmaxy() ),
random( getmaxx() ), random( getmaxy() ), 0, OFF);
}

Pause(); /* Pause for user's response */

}


/* */
/* TEXTDEMO: Show each font in several sizes to the user. */
/* */

void TextDemo(void)
{
int charsize[] = {
1, 3, 7, 3, 4, 2, 2, 2, 2, 2, 2 };
int font, size;
int h, x, y, i;
struct viewporttype vp;
char buffer[80];

for( font=0 ; font
sprintf( buffer, "%s Demonstration", Fonts[font] );
MainWindow( buffer );
getviewsettings( &vp );

changetextstyle( font, VERT_DIR, charsize[font] );
settextjustify( CENTER_TEXT, BOTTOM_TEXT );
outtextxy( 2*textwidth("M"), vp.bottom - 2*textheight("M"), "Vertical" );

changetextstyle( font, HORIZ_DIR, charsize[font] );
settextjustify( LEFT_TEXT, TOP_TEXT );
outtextxy( 2*textwidth("M"), 2, "Horizontal" );

settextjustify( CENTER_TEXT, CENTER_TEXT );
x = (vp.right - vp.left) / 2;
y = textheight( "H" );

for( i=1 ; i<5 ; ++i ){ /* For each of the sizes */
size = (font == SMALL_FONT) ? i+3 : i;
changetextstyle( font, HORIZ_DIR, size );
h = textheight( "H" );
y += h;
sprintf( buffer, "Size %d", size );
outtextxy( x, y, buffer );

}

if( font != DEFAULT_FONT ){ /* Show user declared font size */
y += h / 2; /* Move down the screen */
settextjustify( CENTER_TEXT, TOP_TEXT );
setusercharsize( 5, 6, 3, 2 );
changetextstyle( font, HORIZ_DIR, USER_CHAR_SIZE );
outtextxy( (vp.right-vp.left)/2, y, "User Defined Size" );
}

Pause(); /* Pause to let user look */

} /* End of FONT loop */

}

/* */
/* COLORDEMO: Display the current color palette on the screen. */
/* */

void ColorDemo(void)
{
struct viewporttype vp;
int color, height, width;
int x, y, i, j;
char cnum[5];

MainWindow( "Color Demonstration" ); /* Show demonstration name */

color = 1;
getviewsettings( &vp ); /* Get the current window size */
width = 2 * ( (vp.right+1) / 16 ); /* Get box dimensions */
height = 2 * ( (vp.bottom-10) / 10 );

x = width / 2;
y = height / 2; /* Leave 1/2 box border */

for( j=0 ; j<3 ; ++j ){ /* Row loop */

for( i=0 ; i<5 ; ++i ){ /* Column loop */

setfillstyle(SOLID_FILL, color); /* Set to solid fill in color */
setcolor( color ); /* Set the same border color */

bar( x, y, x+width, y+height ); /* Draw the rectangle */
rectangle( x, y, x+width, y+height ); /* outline the rectangle */

if( color == BLACK ){ /* If box was black... */
setcolor( WHITE ); /* Set drawing color to white */
rectangle( x, y, x+width, y+height ); /* Outline black in white*/
}

itoa( color, cnum, 10 ); /* Convert # to ASCII */
outtextxy( x+(width/2), y+height+4, cnum ); /* Show color # */

color = ++color % MaxColors; /* Advance to the next color */
x += (width / 2) * 3; /* move the column base */
} /* End of Column loop */

y += (height / 2) * 3; /* move the row base */
x = width / 2; /* reset column base */
} /* End of Row loop */

Pause(); /* Pause for user's response */

}

/* */
/* ARCDEMO: Display a random pattern of arcs on the screen */
/* until the user says enough. */
/* */

void ArcDemo(void)
{
int mradius; /* Maximum radius allowed */
int eangle; /* Random end angle of Arc */
struct arccoordstype ai; /* Used to read Arc Cord info */

MainWindow( "Arc Demonstration" );
StatusLine( "ESC Aborts - Press a Key to stop" );

mradius = MaxY / 10; /* Determine the maximum radius */

while( !kbhit() ){ /* Repeat until a key is hit */
setcolor( random( MaxColors - 1 ) + 1 ); /* Randomly select a color */
eangle = random( 358 ) + 1; /* Select an end angle */
arc( random(MaxX), random(MaxY), random(eangle), eangle, mradius );
getarccoords( &ai ); /* Read Cord data */
line( ai.x, ai.y, ai.xstart, ai.ystart ); /* line from start to center */
line( ai.x, ai.y, ai.xend, ai.yend ); /* line from end to center */
} /* End of WHILE not KBHIT */

Pause(); /* Wait for user's response */

}

/* */
/* CIRCLEDEMO: Display a random pattern of circles on the screen */
/* until the user says enough. */
/* */

void CircleDemo(void)
{
int mradius; /* Maximum radius allowed */

MainWindow( "Circle Demonstration" );
StatusLine( "ESC Aborts - Press a Key to stop" );

mradius = MaxY / 10; /* Determine the maximum radius */

while( !kbhit() ){ /* Repeat until a key is hit */
setcolor( random( MaxColors - 1 ) + 1 ); /* Randomly select a color */
circle( random(MaxX), random(MaxY), random(mradius) );
} /* End of WHILE not KBHIT */

Pause(); /* Wait for user's response */

}

/* */
/* PIEDEMO: Display a pie chart on the screen. */
/* */

#define adjasp( y ) ((int)(AspectRatio * (double)(y)))
#define torad( d ) (( (double)(d) * PI ) / 180.0 )

void PieDemo(void)
{
struct viewporttype vp;
int xcenter, ycenter, radius, lradius;
int x, y;
double radians, piesize;

MainWindow( "Pie Chart Demonstration" );

getviewsettings( &vp ); /* Get the current viewport */
xcenter = (vp.right - vp.left) / 2; /* Center the Pie horizontally */
ycenter = (vp.bottom - vp.top) / 2+20;/* Center the Pie vertically */
radius = (vp.bottom - vp.top) / 3; /* It will cover 2/3rds screen */
piesize = (vp.bottom - vp.top) / 4.0; /* Optimum height ratio of pie */

while( (AspectRatio*radius) < piesize ) ++radius;

lradius = radius + ( radius / 5 ); /* Labels placed 20% farther */

changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
settextjustify( CENTER_TEXT, TOP_TEXT );
outtextxy( MaxX/2, 6, "This is a Pie Chart" );
changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
settextjustify( CENTER_TEXT, TOP_TEXT );

setfillstyle( SOLID_FILL, RED );
pieslice( xcenter+10, ycenter-adjasp(10), 0, 90, radius );
radians = torad( 45 );
x = xcenter + (int)( cos( radians ) * (double)lradius );
y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
settextjustify( LEFT_TEXT, BOTTOM_TEXT );
outtextxy( x, y, "25 %" );

setfillstyle( WIDE_DOT_FILL, GREEN );
pieslice( xcenter, ycenter, 90, 135, radius );
radians = torad( 113 );
x = xcenter + (int)( cos( radians ) * (double)lradius );
y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
settextjustify( RIGHT_TEXT, BOTTOM_TEXT );
outtextxy( x, y, "12.5 %" );

setfillstyle( INTERLEAVE_FILL, YELLOW );
settextjustify( RIGHT_TEXT, CENTER_TEXT );
pieslice( xcenter-10, ycenter, 135, 225, radius );
radians = torad( 180 );
x = xcenter + (int)( cos( radians ) * (double)lradius );
y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
settextjustify( RIGHT_TEXT, CENTER_TEXT );
outtextxy( x, y, "25 %" );

setfillstyle( HATCH_FILL, BLUE );
pieslice( xcenter, ycenter, 225, 360, radius );
radians = torad( 293 );
x = xcenter + (int)( cos( radians ) * (double)lradius );
y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
settextjustify( LEFT_TEXT, TOP_TEXT );
outtextxy( x, y, "37.5 %" );

Pause(); /* Pause for user's response */

}

/* */
/* BARDEMO: Draw a 2-D bar chart using Bar and Rectangle. */
/* */

void BarDemo(void)
{
int barheight[] = {
1, 3, 5, 2, 4 };
int styles[] = {
1, 3, 10, 5, 9, 1 };
int xstep, ystep;
int sheight, swidth;
int i, j, h;
struct viewporttype vp;
char buffer[40];

MainWindow( "Bar / Rectangle demonstration" );
h = 3 * textheight( "H" );
getviewsettings( &vp );
settextjustify( CENTER_TEXT, TOP_TEXT );
changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
outtextxy( MaxX /2, 6, "These are 2-D Bars" );
changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
setviewport( vp.left+50, vp.top+30, vp.right-50, vp.bottom-10, 1 );

getviewsettings( &vp );
sheight = vp.bottom - vp.top;
swidth = vp.right - vp.left;

line( h, h, h, sheight-h );
line( h, sheight-h, sheight-h, sheight-h );
ystep = (sheight - (2*h) ) / 5;
xstep = (swidth - (2*h) ) / 5;
j = sheight - h;
settextjustify( CENTER_TEXT, CENTER_TEXT );

for( i=0 ; i<6 ; ++i ){
line( h/2, j, h, j );
itoa( i, buffer, 10 );
outtextxy( 0, j, buffer );
j -= ystep;
}

j = h;
settextjustify( CENTER_TEXT, TOP_TEXT );
for( i=0 ; i<6 ; ++i ){
setfillstyle( styles[i], random(MaxColors) );
line( j, sheight - h, j, sheight- 3 - (h/2) );
itoa( i, buffer, 10 );
outtextxy( j, sheight - (h/2), buffer );
if( i != 5 ){
bar( j, (sheight-h)-(barheight[i] * ystep), j+xstep, sheight-h-1 );
rectangle( j, (sheight-h)-(barheight[i] * ystep), j+xstep, sheight-h);
}
j += xstep;
}

Pause();

}

/* */
/* LINERELDEMO: Display pattern using moverel and linerel cmds. */
/* */

void LineRelDemo(void)
{
struct viewporttype vp;
int h, w, dx, dy, cx, cy;
struct PTS outs[7];


MainWindow( "MoveRel / LineRel Demonstration" );
StatusLine( "Press any key to continue, ESC to Abort" );

getviewsettings( &vp );
cx = (vp.right - vp.left) / 2; /* Center of the screen coords */
cy = (vp.bottom - vp.top ) / 2;

h = (vp.bottom - vp.top ) / 8;
w = (vp.right - vp.left) / 9;

dx = 2 * w;
dy = 2 * h;

setcolor( BLACK );

setfillstyle( SOLID_FILL, BLUE );
bar( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /* Draw backgnd */

outs[0].x = cx - dx;
outs[0].y = cy - dy;
outs[1].x = cx - (dx-w);
outs[1].y = cy - (dy+h);
outs[2].x = cx + dx;
outs[2].y = cy - (dy+h);
outs[3].x = cx + dx;
outs[3].y = cy + dy;
outs[4].x = cx + (dx-w);
outs[4].y = cy + (dy+h);
outs[5].x = cx - dx;
outs[5].y = cy + (dy+h);
outs[6].x = cx - dx;
outs[6].y = cy - dy;

setfillstyle( SOLID_FILL, WHITE );
fillpoly( 7, (int far *)outs );

outs[0].x = cx - (w/2);
outs[0].y = cy + h;
outs[1].x = cx + (w/2);
outs[1].y = cy + h;
outs[2].x = cx + (w/2);
outs[2].y = cy - h;
outs[3].x = cx - (w/2);
outs[3].y = cy - h;
outs[4].x = cx - (w/2);
outs[4].y = cy + h;

setfillstyle( SOLID_FILL, BLUE );
fillpoly( 5, (int far *)outs );

/* Draw a Tesseract object on the screen using the LineRel and */
/* MoveRel drawing commands. */

moveto( cx-dx, cy-dy );
linerel( w, -h );
linerel( 3*w, 0 );
linerel( 0, 5*h );
linerel( -w, h );
linerel( -3*w, 0 );
linerel( 0, -5*h );

moverel( w, -h );
linerel( 0, 5*h );
linerel( w+(w/2), 0 );
linerel( 0, -3*h );
linerel( w/2, -h );
linerel( 0, 5*h );

moverel( 0, -5*h );
linerel( -(w+(w/2)), 0 );
linerel( 0, 3*h );
linerel( -w/2, h );

moverel( w/2, -h );
linerel( w, 0 );

moverel( 0, -2*h );
linerel( -w, 0 );

Pause(); /* Wait for user's response */

}

/* */
/* PUTPIXELDEMO: Display a pattern of random dots on the screen */
/* and pick them back up again. */
/* */

void PutPixelDemo(void)
{
int seed = 1958;
int i, x, y, h, w, color;
struct viewporttype vp;

MainWindow( "PutPixel / GetPixel Demonstration" );

getviewsettings( &vp );
h = vp.bottom - vp.top;
w = vp.right - vp.left;

srand( seed ); /* Restart random # function */

for( i=0 ; i<5000 ; ++i ){ /* Put 5000 pixels on screen */
x = 1 + random( w - 1 ); /* Generate a random location */
y = 1 + random( h - 1 );
color = random( MaxColors );
putpixel( x, y, color );
}

srand( seed ); /* Restart Random # at same # */

for( i=0 ; i<5000 ; ++i ){ /* Take the 5000 pixels off */
x = 1 + random( w - 1 ); /* Generate a random location */
y = 1 + random( h - 1 );
color = getpixel( x, y ); /* Read the color pixel */
if( color == random( MaxColors ) ) /* Used to keep RANDOM in sync */
putpixel( x, y, 0 ); /* Write pixel to BLACK */
}

Pause(); /* Wait for user's response */

}

/* */
/* PUTIMAGEDEMO */
/* */
void PutImageDemo(void)
{
static int r = 20;
static int StartX = 100;
static int StartY = 50;

struct viewporttype vp;
int PauseTime, x, y, ulx, uly, lrx, lry, size, i, width, height, step;
void *Saucer;

MainWindow("GetImage / PutImage Demonstration");
getviewsettings( &vp );

/* Draw Saucer */
setfillstyle( SOLID_FILL, getmaxcolor() );
fillellipse(StartX, StartY, r, (r/3)+2);
ellipse(StartX, StartY-4, 190, 357, r, r/3);

line(StartX+7, StartY-6, StartX+10, StartY-12);
circle(StartX+10, StartY-12, 2);
line(StartX-7, StartY-6, StartX-10, StartY-12);
circle(StartX-10, StartY-12, 2);


/* Read saucer image */
ulx = StartX-(r+1);
uly = StartY-14;
lrx = StartX+(r+1);
lry = StartY+(r/3)+3;
width = lrx - ulx + 1;
height = lry - uly + 1;
size = imagesize(ulx, uly, lrx, lry);

Saucer = malloc( size );
getimage(ulx, uly, lrx, lry, Saucer);
putimage(ulx, uly, Saucer, XOR_PUT);

/* Plot some "stars" */
for ( i=0 ; i<1000; ++i )
putpixel(random(MaxX), random(MaxY), random( MaxColors-1 )+1);
x = MaxX / 2;
y = MaxY / 2;
PauseTime = 70;

/* until a key is hit */
while ( !kbhit() ) {

/* Draw the Saucer */
putimage(x, y, Saucer, XOR_PUT); /* draw image */
delay(PauseTime);
putimage(x, y, Saucer, XOR_PUT); /* erase image */

/* Move Saucer */

step = random( 2*r );
if ((step/2) % 2 != 0 )
step = -1 * step;
x = x + step;
step = random( r );
if ((step/2) % 2 != 0 )
step = -1 * step;
y = y + step;

if (vp.left + x + width - 1 > vp.right)
x = vp.right-vp.left-width + 1;
else
if (x < 0)
x = 0;
if (vp.top + y + height - 1 > vp.bottom)
y = vp.bottom-vp.top-height + 1;
else
if (y < 0)
y = 0;
}
free( Saucer );
Pause();
}


/* */
/* LINETODEMO: Display a pattern using moveto and lineto commands. */
/* */

#define MAXPTS 15

void LineToDemo(void)
{
struct viewporttype vp;
struct PTS points[MAXPTS];
int i, j, h, w, xcenter, ycenter;
int radius, angle, step;
double rads;

MainWindow( "MoveTo / LineTo Demonstration" );

getviewsettings( &vp );
h = vp.bottom - vp.top;
w = vp.right - vp.left;

xcenter = w / 2; /* Determine the center of circle */
ycenter = h / 2;
radius = (h - 30) / (AspectRatio * 2);
step = 360 / MAXPTS; /* Determine # of increments */

angle = 0; /* Begin at zero degrees */
for( i=0 ; i rads = (double)angle * PI / 180.0; /* Convert angle to radians */
points[i].x = xcenter + (int)( cos(rads) * radius );
points[i].y = ycenter - (int)( sin(rads) * radius * AspectRatio );
angle += step; /* Move to next increment */
}

circle( xcenter, ycenter, radius ); /* Draw bounding circle */

for( i=0 ; i for( j=i ; j moveto(points[i].x, points[i].y); /* Move to beginning of cord */
lineto(points[j].x, points[j].y); /* Draw the cord */
}
}

Pause(); /* Wait for user's response */

}

/* */
/* LINESTYLEDEMO: Display a pattern using all of the standard */
/* line styles that are available. */
/* */

void LineStyleDemo(void)
{
int style, step;
int x, y, w;
struct viewporttype vp;
char buffer[40];

MainWindow( "Pre-defined line styles" );

getviewsettings( &vp );
w = vp.right - vp.left;

x = 35;
y = 10;
step = w / 11;

settextjustify( LEFT_TEXT, TOP_TEXT );
outtextxy( x, y, "Normal Width" );

settextjustify( CENTER_TEXT, TOP_TEXT );

for( style=0 ; style<4 ; ++style ){
setlinestyle( style, 0, NORM_WIDTH );
line( x, y+20, x, vp.bottom-40 );
itoa( style, buffer, 10 );
outtextxy( x, vp.bottom-30, buffer );
x += step;
}

x += 2 * step;

settextjustify( LEFT_TEXT, TOP_TEXT );
outtextxy( x, y, "Thick Width" );
settextjustify( CENTER_TEXT, TOP_TEXT );

for( style=0 ; style<4 ; ++style ){
setlinestyle( style, 0, THICK_WIDTH );
line( x, y+20, x, vp.bottom-40 );
itoa( style, buffer, 10 );
outtextxy( x, vp.bottom-30, buffer );
x += step;
}

settextjustify( LEFT_TEXT, TOP_TEXT );

Pause(); /* Wait for user's response */

}

/* */
/* CRTMODEDEMO: Demonstrate the effects of the change mode */
/* commands on the current screen. */
/* */

void CRTModeDemo(void)
{
struct viewporttype vp;
int mode;

MainWindow( "SetGraphMode / RestoreCRTMode demo" );
getviewsettings( &vp );
mode = getgraphmode();
settextjustify( CENTER_TEXT, CENTER_TEXT );

outtextxy( (vp.right-vp.left)/2, (vp.bottom-vp.top)/2,
"Now you are in graphics mode..." );
StatusLine( "Press any key for text mode..." );
getch();

restorecrtmode();
printf( "Now you are in text mode.\n\n" );
printf( "Press any key to go back to graphics..." );
getch();

setgraphmode( mode );
MainWindow( "SetGraphMode / RestoreCRTMode demo" );
settextjustify( CENTER_TEXT, CENTER_TEXT );
outtextxy( (vp.right-vp.left)/2, (vp.bottom-vp.top)/2,
"Back in Graphics Mode..." );

Pause(); /* Wait for user's response */

}

/* */
/* USERLINESTYLEDEMO: Display line styles showing the user */
/* defined line style functions. */
/* */

void UserLineStyleDemo(void)
{
int x, y, i, h, flag;
unsigned int style;
struct viewporttype vp;

MainWindow( "User defined line styles" );

getviewsettings( &vp );
h = vp.bottom - vp.top;

x = 4;
y = 10;
style = 0;
i = 0;

settextjustify( CENTER_TEXT, TOP_TEXT );
flag = TRUE; /* Set the bits in this pass */

while( x < vp.right-2 ){ /* Draw lines across the screen */

if( flag ) /* If flag, set bits... */
style = style | (1 << i); /* Set the Ith bit in word */
else /* If no flag, clear bits */
style = style & !(0x8000 >> i); /* Clear the Ith bit in word */

setlinestyle( USERBIT_LINE, style, NORM_WIDTH );
line( x, y, x, h-y ); /* Draw the new line pattern */

x += 5; /* Move the X location of line */
i = ++i % 16; /* Advance to next bit pattern */

if( style == 0xffff ){ /* Are all bits set? */
flag = FALSE; /* begin removing bits */
i = 0; /* Start with whole pattern */
}
else{ /* Bits not all set... */
if( style == 0 ) /* Are all bits clear? */
flag = TRUE; /* begin setting bits */
}
}

settextjustify( LEFT_TEXT, TOP_TEXT );

Pause(); /* Wait for user's response */

}

/* */
/* FILLSTYLEDEMO: Display the standard fill patterns available. */
/* */

void FillStyleDemo(void)
{
int h, w, style;
int i, j, x, y;
struct viewporttype vp;
char buffer[40];

MainWindow( "Pre-defined Fill Styles" );

getviewsettings( &vp );
w = 2 * ((vp.right + 1) / 13);
h = 2 * ((vp.bottom - 10) / 10);

x = w / 2;
y = h / 2; /* Leave 1/2 blk margin */
style = 0;

for( j=0 ; j<3 ; ++j ){ /* Three rows of boxes */
for( i=0 ; i<4 ; ++i ){ /* Four column of boxes */
setfillstyle(style, MaxColors-1); /* Set the fill style and WHITE */
bar( x, y, x+w, y+h ); /* Draw the actual box */
rectangle( x, y, x+w, y+h ); /* Outline the box */
itoa( style, buffer, 10 ); /* Convert style 3 to ASCII */
outtextxy( x+(w / 2), y+h+4, buffer );
++style; /* Go on to next style # */
x += (w / 2) * 3; /* Go to next column */
} /* End of coulmn loop */
x = w / 2; /* Put base back to 1st column */
y += (h / 2) * 3; /* Advance to next row */
} /* End of Row loop */

settextjustify( LEFT_TEXT, TOP_TEXT );

Pause(); /* Wait for user's response */

}

/* */
/* FILLPATTERNDEMO: Demonstrate how to use the user definable */
/* fill patterns. */
/* */

void FillPatternDemo(void)
{
int style;
int h, w;
int x, y, i, j;
char buffer[40];
struct viewporttype vp;
static char patterns[][8] = {
{ 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 },
{ 0x33, 0x33, 0xCC, 0xCC, 0x33, 0x33, 0xCC, 0xCC },
{ 0xF0, 0xF0, 0xF0, 0xF0, 0x0F, 0x0F, 0x0F, 0x0F },
{ 0x00, 0x10, 0x28, 0x44, 0x28, 0x10, 0x00, 0x00 },
{ 0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00 },
{ 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x3C, 0x3C, 0x3C, 0x3C, 0x00, 0x00 },
{ 0x00, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x00 },
{ 0x00, 0x00, 0x22, 0x08, 0x00, 0x22, 0x1C, 0x00 },
{ 0xFF, 0x7E, 0x3C, 0x18, 0x18, 0x3C, 0x7E, 0xFF },
{ 0x00, 0x10, 0x10, 0x7C, 0x10, 0x10, 0x00, 0x00 },
{ 0x00, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x00 }
};

MainWindow( "User Defined Fill Styles" );

getviewsettings( &vp );
w = 2 * ((vp.right + 1) / 13);
h = 2 * ((vp.bottom - 10) / 10);

x = w / 2;
y = h / 2; /* Leave 1/2 blk margin */
style = 0;

for( j=0 ; j<3 ; ++j ){ /* Three rows of boxes */
for( i=0 ; i<4 ; ++i ){ /* Four column of boxes */
setfillpattern( &patterns[style][0], MaxColors-1 );
bar( x, y, x+w, y+h ); /* Draw the actual box */
rectangle( x, y, x+w, y+h ); /* Outline the box */
itoa( style, buffer, 10 ); /* Convert style 3 to ASCII */
outtextxy( x+(w / 2), y+h+4, buffer );
++style; /* Go on to next style # */
x += (w / 2) * 3; /* Go to next column */
} /* End of coulmn loop */
x = w / 2; /* Put base back to 1st column */
y += (h / 2) * 3; /* Advance to next row */
} /* End of Row loop */

settextjustify( LEFT_TEXT, TOP_TEXT );

Pause(); /* Wait for user's response */

}

/* */
/* POLYDEMO: Display a random pattern of polygons on the screen */
/* until the user says enough. */
/* */

void PaletteDemo(void)
{
int i, j, x, y, color;
struct viewporttype vp;
int height, width;

MainWindow( "Palette Demonstration" );
StatusLine( "Press any key to continue, ESC to Abort" );

getviewsettings( &vp );
width = (vp.right - vp.left) / 15; /* get width of the box */
height = (vp.bottom - vp.top) / 10; /* Get the height of the box */

x = y = 0; /* Start in upper corner */
color = 1; /* Begin at 1st color */

for( j=0 ; j<10 ; ++j ){ /* For 10 rows of boxes */
for( i=0 ; i<15 ; ++i ){ /* For 15 columns of boxes */
setfillstyle( SOLID_FILL, color++ ); /* Set the color of box */
bar( x, y, x+width, y+height ); /* Draw the box */
x += width + 1; /* Advance to next col */
color = 1 + (color % (MaxColors - 2)); /* Set new color */
} /* End of COLUMN loop */
x = 0; /* Goto 1st column */
y += height + 1; /* Goto next row */
} /* End of ROW loop */

while( !kbhit() ){ /* Until user enters a key... */
setpalette( 1+random(MaxColors - 2), random( 65 ) );
}

setallpalette( &palette );

Pause(); /* Wait for user's response */

}

/* */
/* POLYDEMO: Display a random pattern of polygons on the screen */
/* until the user says enough. */
/* */

#define MaxPts 6 /* Maximum # of pts in polygon */

void PolyDemo(void)
{
struct PTS poly[ MaxPts ]; /* Space to hold datapoints */
int color; /* Current drawing color */
int i;

MainWindow( "DrawPoly / FillPoly Demonstration" );
StatusLine( "ESC Aborts - Press a Key to stop" );

while( !kbhit() ){ /* Repeat until a key is hit */

color = 1 + random( MaxColors-1 ); /* Get a random color # (no blk)*/
setfillstyle( random(10), color ); /* Set a random line style */
setcolor( color ); /* Set the desired color */

for( i=0 ; i<(MaxPts-1) ; i++ ){ /* Determine a random polygon */
poly[i].x = random( MaxX ); /* Set the x coord of point */
poly[i].y = random( MaxY ); /* Set the y coord of point */
}

poly[i].x = poly[0].x; /* last point = first point */
poly[i].y = poly[1].y;

fillpoly( MaxPts, (int far *)poly ); /* Draw the actual polygon */
} /* End of WHILE not KBHIT */

Pause(); /* Wait for user's response */

}


/* */
/* SAYGOODBYE: Give a closing screen to the user before leaving. */
/* */

void SayGoodbye(void)
{
struct viewporttype viewinfo; /* Structure to read viewport */
int h, w;

MainWindow( "== Finale ==" );

getviewsettings( &viewinfo ); /* Read viewport settings */
changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
settextjustify( CENTER_TEXT, CENTER_TEXT );

h = viewinfo.bottom - viewinfo.top;
w = viewinfo.right - viewinfo.left;
outtextxy( w/2, h/2, "That's all, folks!" );

StatusLine( "Press any key to EXIT" );
getch();

cleardevice(); /* Clear the graphics screen */

}

/* */
/* 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( 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( char *header )
{
int height;

cleardevice(); /* Clear graphics screen */
setcolor( MaxColors - 1 ); /* Set current color to white */
setviewport( 0, 0, MaxX, MaxY, 1 ); /* Open port to full screen */

height = textheight( "H" ); /* Get basic text height */

changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
settextjustify( CENTER_TEXT, TOP_TEXT );
outtextxy( MaxX/2, 2, header );
setviewport( 0, height+4, MaxX, MaxY-(height+4), 1 );
DrawBorder();
setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1 );

}

/* */
/* STATUSLINE: Display a status line at the bottom of the screen. */
/* */

void StatusLine( char *msg )
{
int height;

setviewport( 0, 0, MaxX, MaxY, 1 ); /* Open port to full screen */
setcolor( MaxColors - 1 ); /* Set current color to white */

changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
settextjustify( CENTER_TEXT, TOP_TEXT );
setlinestyle( SOLID_LINE, 0, NORM_WIDTH );
setfillstyle( EMPTY_FILL, 0 );

height = textheight( "H" ); /* Detemine current height */
bar( 0, MaxY-(height+4), MaxX, MaxY );
rectangle( 0, MaxY-(height+4), MaxX, MaxY );
outtextxy( MaxX/2, MaxY-(height+2), msg );
setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1 );

}

/* */
/* DRAWBORDER: Draw a solid single line around the current */
/* viewport. */
/* */

void DrawBorder(void)
{
struct viewporttype vp;

setcolor( MaxColors - 1 ); /* Set current color to white */

setlinestyle( SOLID_LINE, 0, NORM_WIDTH );

getviewsettings( &vp );
rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top );

}

/* */
/* CHANGETEXTSTYLE: similar to settextstyle, but checks for */
/* errors that might occur whil loading the font file. */
/* */

void changetextstyle(int font, int direction, int charsize)
{
int ErrorCode;

graphresult(); /* clear error code */
settextstyle(font, direction, charsize);
ErrorCode = graphresult(); /* check result */
if( ErrorCode != grOk ){ /* if error occured */
closegraph();
printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
exit( 1 );
}
}

/* */
/* GPRINTF: Used like PRINTF except the output is sent to the */
/* screen in graphics mode at the specified co-ordinate. */
/* */

int gprintf( int *xloc, int *yloc, char *fmt, ... )
{
va_list argptr; /* Argument list pointer */
char str[140]; /* Buffer to build sting into */
int cnt; /* Result of SPRINTF for return */

va_start( argptr, fmt ); /* Initialize va_ functions */

cnt = vsprintf( str, fmt, argptr ); /* prints string to buffer */
outtextxy( *xloc, *yloc, str ); /* Send string in graphics mode */
*yloc += textheight( "H" ) + 2; /* Advance to next line */

va_end( argptr ); /* Close va_ functions */

return( cnt ); /* Return the conversion count */

}