Here is the best project in C language I have ever made .I won 1st prize in project competition for this project in my college .I like to share the source code of program on my blog .Before going to source code see the video I have uploaded in the output section.
SOURCE CODE OUTPUT DOWNLOAD
SOURCE CODE OUTPUT DOWNLOAD
#include <graphics.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void *x,*d,*p[3],*cur,*cm;
int corx=100,cory=100,menuch=0,rub=3,maxx,maxy,se=0;
int mix,set=1,key,miy,minx=100,miny=100,setting[7]={0,0,0,0,1,0,0};
char *mainmenu[]={"FILE","DRAW","EDIT","COLORS",
"FILLSTYLE","LINESTYLE"};
char *filemenu[]={"OPEN","SAVE","EXIT"};
char *drawmenu[]={"CIRCLE","ELLIPSE","RECTANGLE ",
"LINE","FREEHAND"};
char *editmenu[]={"COPY","MOVE","ERASER"};
void draw();
void drawbox();
void mainscreen();
void getresponse(int);
void color(int,int,int,int,int);
void menuscreen();
void dcursor(int *cur);
void menuop();
void curpos();
void edit();
void file();
int main(void)
{
int gdriver = DETECT, gmode,error;
int j,i;
char msg[10];
initgraph(&gdriver, &gmode, "");
error = graphresult();
if (error != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(error));
printf("Press any key to halt:");
getch();
exit(1);
}
mainscreen();
for(i=0;i<3;i++)
{
p[i]=malloc(imagesize(0,55,164,372));
if(p[i]==NULL)
{
printf("\n\n\n\n\t\t\tNot enough space");
getch();
exit(1);
}
getimage(53+164*i,55,55+164*(i+1),372,p[i]);
}
setviewport(53,55,544,372,1);
cur=malloc(imagesize(0,0,7,7));
dcursor(cur);
while(1)
{
key=getch();
if(!key)
key=getch()+1;
if(key==65||key==97)
corx--,cory--;
else if(key==87||key==119)
corx++,cory--;
else if(key==83||key==115)
corx--,cory++;
else if(key==68||key==100)
corx++,cory++;
else if(key==76)
corx--;
else if(key==78)
corx++;
else if(key==73)
cory--;
else if(key==81)
cory++;
else if(key==13)
set++;
else if(key=='+')
rub++;
else if(key=='-')
rub--;
else if(key==27)
set=1;
else if(key==77||key==109)
menuscreen();
else if(key==98)
{
free(x);
free(d);
free(p[0]);
free(p[1]);
free(p[2]);
closegraph();
exit(0);
}
moveto(corx,cory);
curpos();
for(i=0;i<3;i++)
putimage(164*i,0,p[i],0);
if(se)
{
edit();
if(set>4)
set=1;
}
else
{
draw();
if(set>2)
set=1;
}
putimage(corx-3,cory-3,cur,0);
sprintf(msg, " (%d,%d)",corx-3,cory-3);
outtextxy(corx,cory, msg);
}
}
void dcursor(int *cur)
{
line(corx,cory-3,corx,cory+3);
line(corx-3,cory,corx+3,cory);
getimage(corx-3,cory-3,corx+3,cory+3,cur);
}
void menuscreen()
{
int i=0,j,key;
int runc[6]={2,4,2,14,11,3};
menuop();
drawbox();
for(j=0;j<3;j++)
putimage(164*j,0,p[j],0);
while(1)
{
getresponse(i);
key=getch();
if(!key)
key=getch();
switch(key)
{
case 72:
i--;
break;
case 80:
i++;
break;
case 75:
free(x);
menuch--,i=0;
if(menuch<0)
menuch=5;
drawbox();
for(j=0;j<3;j++)
putimage(164*j,0,p[j],0);
break;
case 77:
free(x);
menuch++,i=0;
if(menuch>5)
menuch=0;
drawbox();
for(j=0;j<3;j++)
putimage(164*j,0,p[j],0);
break;
case 13:
if(menuch==2)
se=1;
else
se=0;
setting[menuch]=i;
if(menuch==0)
file();
return;
default:;
}
if(i<0)
i=runc[menuch];
else if(i>runc[menuch])
return;
}
}
void color(ini,iny,fii,fiy,color)
int ini,iny,fii,fiy,color;
{
int i,j;
for(i=ini;i<=fii;i++)
for(j=iny;j<=fiy;j++)
putpixel(i,j,color);
}
void drawbox()
{
int y=4,j;
switch(menuch+1)
{
case 1:
case 2:
case 3:
x=malloc(imagesize(menuch*60,0,84+menuch*60,140));
setcolor(11);
setpalette(11,0);
color(6+menuch*60,0,84+menuch*60,132,7);
if(menuch==0)
for(j=0;j<3;j++)
outtextxy(10+menuch*60,y+18*j,filemenu[j]);
if(menuch==1)
for(j=0;j<5;j++)
outtextxy(10+menuch*60,y+18*j,drawmenu[j]);
if(menuch==2)
for(j=0;j<3;j++)
outtextxy(10+menuch*60,y+18*j,editmenu[j]);
getimage(6+menuch*60,0,84+menuch*60,132,x);
break;
case 4:
color(186,0,264,162,7);
for(j=1;j<=15;j++)
{
setfillstyle(1,j);
bar(190,y+10*(j-1),260,y+10*j);
}
x=malloc(imagesize(180,0,264,170));
getimage(186,0,264,162,x);
break;
case 5:
color(257,0,337,145,7);
for(j=0;j<=11;j++)
{
setfillstyle(j,15);
bar(261,y+10*j,333,y+10*(j+1));
}
x=malloc(imagesize(250,0,337,150));
getimage(257,0,337,145,x);
break;
case 6:
color(351,0,431,65,7);
for(j=0;j<4;j++)
{
setlinestyle(j,1,1);
line(353,y+10*j,423,y+10*j);
}
x=malloc(imagesize(345,0,431,70));
getimage(351,0,431,65,x);
break;
}
if(x==NULL)
{
printf("Not enough space");
getch();
exit(1);
}
}
void getresponse(int i)
{
int j,k,hl=18*i+4;
switch(menuch+1)
{
case 1:
case 2:
case 3:
putimage(6+menuch*60,0,x,0);
color(6+menuch*60,hl-3,84+menuch*60,hl+10,12);
setcolor(15);
if(menuch==0)
outtextxy(10+menuch*60,hl,filemenu[i]);
if(menuch==1)
outtextxy(10+menuch*60,hl,drawmenu[i]);
if(menuch==2)
outtextxy(10+menuch*60,hl,editmenu[i]);
break;
case 4:
putimage(186,0,x,0);
color(186,4+10*i,264,14+10*i,12);
setfillstyle(1,i+1);
bar(190,5+10*i,260,13+10*i);
break;
case 5:
putimage(257,0,x,0);
color(257,4+10*i,337,14+10*i,12);
setfillstyle(i,15);
bar(261,5+10*i,333,13+10*i);
break;
case 6:
putimage(351,0,x,0);
color(351,3+10*i,431,9+10*i,12);
setlinestyle(i,1,1);
line(353,5+10*i,425,5+10*i);
break;
}
}
void mainscreen()
{
int x,y=0;
setcolor(7);
rectangle(50,1,550,400);
rectangle(52,3,548,398);
rectangle(52,30,550,378);
rectangle(52,32,548,376);
line(52,52,548,52);
line(52,54,548,54);
color(240,8,340,25,7);
color(54,34,546,50,7);
settextstyle(0, HORIZ_DIR,1);
outtextxy(60,385,"Made by : Mohammed Ujjainwala");
setcolor(11);
setpalette(11,0);
for(x=0;x<4;x++)
outtextxy(y+=60,38,mainmenu[x]);
outtextxy(y+=74,38,mainmenu[4]);
outtextxy(y+=92,38,mainmenu[5]);
settextstyle(0, HORIZ_DIR, 2);
outtextxy(250,10,"MYCAD");
settextstyle(0, HORIZ_DIR, 1);
d=malloc(imagesize(1,1,50,10));
setcolor(15);
}
void menuop()
{
cory++;
if(corx<59)
menuch=0;
else if(corx<118)
menuch=1;
else if(corx<180)
menuch=2;
else if(corx<253)
menuch=3;
else if(corx<345)
menuch=4;
else
menuch=5;
}
void curpos()
{
if(cory<3)
cory++;
else if(corx<3)
corx++;
else if(corx>491)
corx--;
else if(cory>317)
cory--;
}
void draw()
{
static int xrad=0,yrad=0,rad=0;
int i,j;
setlinestyle(setting[5],1,1);
setfillstyle(setting[4],setting[3]+1);
switch(set)
{
case 1:
minx=corx,miny=cory;
rad=0,yrad=0,xrad=0;
break;
case 2:
switch(setting[1]+1)
{
case 1:
rad++;
fillellipse(minx,miny,rad,rad);
break;
case 2:
if(key==76||key==78)
xrad++;
else
yrad++;
fillellipse(minx,miny,xrad,yrad);
break;
case 3:
bar3d(minx,miny,corx,cory,0,0);
break;
case 4:
line(minx,miny,corx,cory);
break;
case 5:
if(rub<2)
putpixel(corx,cory,setting[3]+1);
else
color(corx-rub,cory-rub,corx+rub,cory+rub,setting[3]+1);
for(i=0;i<3;i++)
getimage(164*i,0,2+164*(i+1),317,p[i]);
break;
}
break;
case 3:
for(j=0;j<3;j++)
putimage(164*j,0,p[j],0);
if(setting[1]==0)
fillellipse(minx,miny,rad,rad);
if(setting[1]==1)
fillellipse(minx, miny, xrad, yrad);
if(setting[1]==2)
bar3d(minx,miny,corx,cory,0,0);
if(setting[1]==3)
line(minx,miny,corx,cory);
for(i=0;i<3;i++)
getimage(164*i,0,2+164*(i+1),317,p[i]);
break;
}
}
void edit()
{
int i;
switch(set)
{
case 1:
mix=corx,miy=cory;
break;
case 2:
if(setting[2]==2)
{
if(rub<1)
rub=1;
color(corx-rub,cory-rub,corx+rub,cory+rub,11);
for(i=0;i<3;i++)
getimage(164*i,0,2+164*(i+1),317,p[i]);
break;
}
rectangle(mix,miy,corx,cory);
break;
case 3:
if(setting[2]==2)
set+=2;
cm=malloc(imagesize(mix+1,miy+1,corx-1,cory-1));
getimage(mix+1,miy+1,corx-1,cory-1,cm);
maxx=corx-1,maxy=cory-1,set++;
putimage(corx,cory,cm,3);
if(setting[2]==1)
{
setfillstyle(1,11);
bar(mix+1,miy+1,maxy,maxx);
}
break;
case 4:
putimage(corx,cory,cm,0);
if(setting[2]==1)
bar(mix+1,miy+1,maxx,maxy);
break;
case 5:
putimage(corx,cory,cm,0);
if(setting[2]==1)
bar(mix+1,miy+1,maxx,maxy);
for(i=0;i<3;i++)
getimage(164*i,0,2+164*(i+1),317,p[i]);
free(cm);
setfillstyle(setting[4],setting[3]+1);
break;
}
}
void file()
{
int out,i;
switch(setting[0]+1)
{
case 1:
out=open("Mycad",O_BINARY|O_RDONLY);
if(out==-1)
{
printf("Unable to open File");exit(0);
}
for(i=0;i<3;i++)
read(out,p[i],imagesize(0,0,164,317));
break;
case 2:
out=open("Mycad",O_BINARY|O_CREAT|O_TRUNC|O_RDWR,S_IWRITE);
if(out==-1)
{
printf("Unable to open File");getch();exit(0);
}
for(i=0;i<3;i++)
write(out,p[i],imagesize(0,0,164,317));
break ;
case 3:
exit(0);
}
close(out);
}
#include <fcntl.h>
#include <sys\stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void *x,*d,*p[3],*cur,*cm;
int corx=100,cory=100,menuch=0,rub=3,maxx,maxy,se=0;
int mix,set=1,key,miy,minx=100,miny=100,setting[7]={0,0,0,0,1,0,0};
char *mainmenu[]={"FILE","DRAW","EDIT","COLORS",
"FILLSTYLE","LINESTYLE"};
char *filemenu[]={"OPEN","SAVE","EXIT"};
char *drawmenu[]={"CIRCLE","ELLIPSE","RECTANGLE ",
"LINE","FREEHAND"};
char *editmenu[]={"COPY","MOVE","ERASER"};
void draw();
void drawbox();
void mainscreen();
void getresponse(int);
void color(int,int,int,int,int);
void menuscreen();
void dcursor(int *cur);
void menuop();
void curpos();
void edit();
void file();
int main(void)
{
int gdriver = DETECT, gmode,error;
int j,i;
char msg[10];
initgraph(&gdriver, &gmode, "");
error = graphresult();
if (error != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(error));
printf("Press any key to halt:");
getch();
exit(1);
}
mainscreen();
for(i=0;i<3;i++)
{
p[i]=malloc(imagesize(0,55,164,372));
if(p[i]==NULL)
{
printf("\n\n\n\n\t\t\tNot enough space");
getch();
exit(1);
}
getimage(53+164*i,55,55+164*(i+1),372,p[i]);
}
setviewport(53,55,544,372,1);
cur=malloc(imagesize(0,0,7,7));
dcursor(cur);
while(1)
{
key=getch();
if(!key)
key=getch()+1;
if(key==65||key==97)
corx--,cory--;
else if(key==87||key==119)
corx++,cory--;
else if(key==83||key==115)
corx--,cory++;
else if(key==68||key==100)
corx++,cory++;
else if(key==76)
corx--;
else if(key==78)
corx++;
else if(key==73)
cory--;
else if(key==81)
cory++;
else if(key==13)
set++;
else if(key=='+')
rub++;
else if(key=='-')
rub--;
else if(key==27)
set=1;
else if(key==77||key==109)
menuscreen();
else if(key==98)
{
free(x);
free(d);
free(p[0]);
free(p[1]);
free(p[2]);
closegraph();
exit(0);
}
moveto(corx,cory);
curpos();
for(i=0;i<3;i++)
putimage(164*i,0,p[i],0);
if(se)
{
edit();
if(set>4)
set=1;
}
else
{
draw();
if(set>2)
set=1;
}
putimage(corx-3,cory-3,cur,0);
sprintf(msg, " (%d,%d)",corx-3,cory-3);
outtextxy(corx,cory, msg);
}
}
void dcursor(int *cur)
{
line(corx,cory-3,corx,cory+3);
line(corx-3,cory,corx+3,cory);
getimage(corx-3,cory-3,corx+3,cory+3,cur);
}
void menuscreen()
{
int i=0,j,key;
int runc[6]={2,4,2,14,11,3};
menuop();
drawbox();
for(j=0;j<3;j++)
putimage(164*j,0,p[j],0);
while(1)
{
getresponse(i);
key=getch();
if(!key)
key=getch();
switch(key)
{
case 72:
i--;
break;
case 80:
i++;
break;
case 75:
free(x);
menuch--,i=0;
if(menuch<0)
menuch=5;
drawbox();
for(j=0;j<3;j++)
putimage(164*j,0,p[j],0);
break;
case 77:
free(x);
menuch++,i=0;
if(menuch>5)
menuch=0;
drawbox();
for(j=0;j<3;j++)
putimage(164*j,0,p[j],0);
break;
case 13:
if(menuch==2)
se=1;
else
se=0;
setting[menuch]=i;
if(menuch==0)
file();
return;
default:;
}
if(i<0)
i=runc[menuch];
else if(i>runc[menuch])
return;
}
}
void color(ini,iny,fii,fiy,color)
int ini,iny,fii,fiy,color;
{
int i,j;
for(i=ini;i<=fii;i++)
for(j=iny;j<=fiy;j++)
putpixel(i,j,color);
}
void drawbox()
{
int y=4,j;
switch(menuch+1)
{
case 1:
case 2:
case 3:
x=malloc(imagesize(menuch*60,0,84+menuch*60,140));
setcolor(11);
setpalette(11,0);
color(6+menuch*60,0,84+menuch*60,132,7);
if(menuch==0)
for(j=0;j<3;j++)
outtextxy(10+menuch*60,y+18*j,filemenu[j]);
if(menuch==1)
for(j=0;j<5;j++)
outtextxy(10+menuch*60,y+18*j,drawmenu[j]);
if(menuch==2)
for(j=0;j<3;j++)
outtextxy(10+menuch*60,y+18*j,editmenu[j]);
getimage(6+menuch*60,0,84+menuch*60,132,x);
break;
case 4:
color(186,0,264,162,7);
for(j=1;j<=15;j++)
{
setfillstyle(1,j);
bar(190,y+10*(j-1),260,y+10*j);
}
x=malloc(imagesize(180,0,264,170));
getimage(186,0,264,162,x);
break;
case 5:
color(257,0,337,145,7);
for(j=0;j<=11;j++)
{
setfillstyle(j,15);
bar(261,y+10*j,333,y+10*(j+1));
}
x=malloc(imagesize(250,0,337,150));
getimage(257,0,337,145,x);
break;
case 6:
color(351,0,431,65,7);
for(j=0;j<4;j++)
{
setlinestyle(j,1,1);
line(353,y+10*j,423,y+10*j);
}
x=malloc(imagesize(345,0,431,70));
getimage(351,0,431,65,x);
break;
}
if(x==NULL)
{
printf("Not enough space");
getch();
exit(1);
}
}
void getresponse(int i)
{
int j,k,hl=18*i+4;
switch(menuch+1)
{
case 1:
case 2:
case 3:
putimage(6+menuch*60,0,x,0);
color(6+menuch*60,hl-3,84+menuch*60,hl+10,12);
setcolor(15);
if(menuch==0)
outtextxy(10+menuch*60,hl,filemenu[i]);
if(menuch==1)
outtextxy(10+menuch*60,hl,drawmenu[i]);
if(menuch==2)
outtextxy(10+menuch*60,hl,editmenu[i]);
break;
case 4:
putimage(186,0,x,0);
color(186,4+10*i,264,14+10*i,12);
setfillstyle(1,i+1);
bar(190,5+10*i,260,13+10*i);
break;
case 5:
putimage(257,0,x,0);
color(257,4+10*i,337,14+10*i,12);
setfillstyle(i,15);
bar(261,5+10*i,333,13+10*i);
break;
case 6:
putimage(351,0,x,0);
color(351,3+10*i,431,9+10*i,12);
setlinestyle(i,1,1);
line(353,5+10*i,425,5+10*i);
break;
}
}
void mainscreen()
{
int x,y=0;
setcolor(7);
rectangle(50,1,550,400);
rectangle(52,3,548,398);
rectangle(52,30,550,378);
rectangle(52,32,548,376);
line(52,52,548,52);
line(52,54,548,54);
color(240,8,340,25,7);
color(54,34,546,50,7);
settextstyle(0, HORIZ_DIR,1);
outtextxy(60,385,"Made by : Mohammed Ujjainwala");
setcolor(11);
setpalette(11,0);
for(x=0;x<4;x++)
outtextxy(y+=60,38,mainmenu[x]);
outtextxy(y+=74,38,mainmenu[4]);
outtextxy(y+=92,38,mainmenu[5]);
settextstyle(0, HORIZ_DIR, 2);
outtextxy(250,10,"MYCAD");
settextstyle(0, HORIZ_DIR, 1);
d=malloc(imagesize(1,1,50,10));
setcolor(15);
}
void menuop()
{
cory++;
if(corx<59)
menuch=0;
else if(corx<118)
menuch=1;
else if(corx<180)
menuch=2;
else if(corx<253)
menuch=3;
else if(corx<345)
menuch=4;
else
menuch=5;
}
void curpos()
{
if(cory<3)
cory++;
else if(corx<3)
corx++;
else if(corx>491)
corx--;
else if(cory>317)
cory--;
}
void draw()
{
static int xrad=0,yrad=0,rad=0;
int i,j;
setlinestyle(setting[5],1,1);
setfillstyle(setting[4],setting[3]+1);
switch(set)
{
case 1:
minx=corx,miny=cory;
rad=0,yrad=0,xrad=0;
break;
case 2:
switch(setting[1]+1)
{
case 1:
rad++;
fillellipse(minx,miny,rad,rad);
break;
case 2:
if(key==76||key==78)
xrad++;
else
yrad++;
fillellipse(minx,miny,xrad,yrad);
break;
case 3:
bar3d(minx,miny,corx,cory,0,0);
break;
case 4:
line(minx,miny,corx,cory);
break;
case 5:
if(rub<2)
putpixel(corx,cory,setting[3]+1);
else
color(corx-rub,cory-rub,corx+rub,cory+rub,setting[3]+1);
for(i=0;i<3;i++)
getimage(164*i,0,2+164*(i+1),317,p[i]);
break;
}
break;
case 3:
for(j=0;j<3;j++)
putimage(164*j,0,p[j],0);
if(setting[1]==0)
fillellipse(minx,miny,rad,rad);
if(setting[1]==1)
fillellipse(minx, miny, xrad, yrad);
if(setting[1]==2)
bar3d(minx,miny,corx,cory,0,0);
if(setting[1]==3)
line(minx,miny,corx,cory);
for(i=0;i<3;i++)
getimage(164*i,0,2+164*(i+1),317,p[i]);
break;
}
}
void edit()
{
int i;
switch(set)
{
case 1:
mix=corx,miy=cory;
break;
case 2:
if(setting[2]==2)
{
if(rub<1)
rub=1;
color(corx-rub,cory-rub,corx+rub,cory+rub,11);
for(i=0;i<3;i++)
getimage(164*i,0,2+164*(i+1),317,p[i]);
break;
}
rectangle(mix,miy,corx,cory);
break;
case 3:
if(setting[2]==2)
set+=2;
cm=malloc(imagesize(mix+1,miy+1,corx-1,cory-1));
getimage(mix+1,miy+1,corx-1,cory-1,cm);
maxx=corx-1,maxy=cory-1,set++;
putimage(corx,cory,cm,3);
if(setting[2]==1)
{
setfillstyle(1,11);
bar(mix+1,miy+1,maxy,maxx);
}
break;
case 4:
putimage(corx,cory,cm,0);
if(setting[2]==1)
bar(mix+1,miy+1,maxx,maxy);
break;
case 5:
putimage(corx,cory,cm,0);
if(setting[2]==1)
bar(mix+1,miy+1,maxx,maxy);
for(i=0;i<3;i++)
getimage(164*i,0,2+164*(i+1),317,p[i]);
free(cm);
setfillstyle(setting[4],setting[3]+1);
break;
}
}
void file()
{
int out,i;
switch(setting[0]+1)
{
case 1:
out=open("Mycad",O_BINARY|O_RDONLY);
if(out==-1)
{
printf("Unable to open File");exit(0);
}
for(i=0;i<3;i++)
read(out,p[i],imagesize(0,0,164,317));
break;
case 2:
out=open("Mycad",O_BINARY|O_CREAT|O_TRUNC|O_RDWR,S_IWRITE);
if(out==-1)
{
printf("Unable to open File");getch();exit(0);
}
for(i=0;i<3;i++)
write(out,p[i],imagesize(0,0,164,317));
break ;
case 3:
exit(0);
}
close(out);
}
Any questions regarding to program
please write in comments.
Can you explain about the coding please
ReplyDelete