Tuesday, December 9, 2008

C program for Drawing with characters

This program is designed to draw pictures with various characters. You can use ASCII characters to dwar pictures. 

The main drawback of this program is that it cannot save the picture directly. To save the picture, right click on DOS window and Select All, then Right click again and COPY. Now paste it in a text editor like Notepad and then Save it. Another way is to press the "Print Screen" and to save it in mspaint. 

I think, this program can be used effectively to create beautiful sms messages with colours.

You are welcome to develop this source code and let me know if you can add a SAVE feature to the program.

The numbers 1, 2  and 3 should be replaced by stdio.h, conio.h and dos.h respectively and must be enclosed by angle brackets.

Source code:--


/* Program to draw pictures with characters */
#include1
#include2
#include3
getkey();
status(char ch, int col, int write);
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define E 18
#define Q 16
#define U 22
#define D 32
#define C 46
#define N 49
#define L 38
void main()
{
int x, y, i=40, j=25,c, cl=7,key,write=1;
char ch='.',chstor='.';
clrscr();
textcolor(5);
printf("\nCreated and customized by Debdeep Bhattcharya on 17th May, 2008.\n");
getch();
clrscr();
textcolor(4);
cprintf("Welcome to the First program for drawing, created by Debdeep Bhattacharya.");
textcolor(2);
printf("\n\nList of Controlls:--\n");
printf("\nGo right:\tRight\nGo left:\tLeft\nGo up:\t\tUp\nGo down:\tDown");
printf("\nChange character(hold key for ASCII):\tC");
printf("\nColor change(1-14):\tL");
printf("\nPen up:\t\tU\nPen down:\tD\nErase mode:\tE\nDelete all:\tN\nQuit:\t\tQ");
textcolor(6);
cprintf("\n\nHit any key to continue...");
textcolor(cl);   /* Restore default color */
getch();
clrscr();

status(ch,cl,write);
     gotoxy(i,j);

     for(c=0;c<5;c=c)
     {
key=getkey();
switch (key)
{
     case(UP):
     {
j--;
break;
     }
     case(DOWN):
     {
j++;
break;
     }
     case(LEFT):
     {
i--;
break;
     }
     case(RIGHT):
     {
i++;
break;
     }
     case(Q):
     {
gotoxy(68,10);
printf("Hit any  key");
gotoxy(68,11);
printf("to exit...");
goto end;

     }
     case(D):
     {
write=1;             /* write mode is on */
if(ch!=chstor)       /* if erase key was not hit previously */
ch=chstor;   /* To restore the previous character  */
status(ch,cl,write);
gotoxy(i,j);
break;
     }
     case(E):
     {
write=1;
chstor=ch;    /* To keep the previous character  */
ch=' ';
status(ch,cl,write);
gotoxy(i,j);
break;
     }
     case(C):
     {
gotoxy(69,6);
printf("CHAR=?");
ch=getch();
chstor=ch;
status(ch,cl,write);
gotoxy(i,j);
break;
     }
     case(N):
     {
clrscr();
status(ch,cl,write);
break;
     }
     case(L):
     {
gotoxy(69,5);
printf("COLOR=?");
scanf("%d",&cl);
textcolor(cl);
status(ch,cl,write);
gotoxy(i,j);
break;
     }
     case(U):
{
   write=0;       /* turns the write mode off */
   status(ch,cl,write);
   break;
}



     default:
     {
break;
     }

}
if(i<0)
i=-i;
if(j<0)
j=-j;
gotoxy(i,j);
if(write==1)       /* prints the character (writes) only if write mode is on i.e. iff in Pen down mode or pen erase mode */
cprintf("%c",ch);
gotoxy(i,j);     /* To bring the cursor to the character that has been printed. */
   }


   /* This setion of the program is to test the getkey() function */
   /* printf("\nHit a key to get its value...\n");
key=getkey();
printf("\nThe value of the key is: %d",key);
getch();
   */


   end:
    getch();
}
getkey()
{
union REGS i, o;
while(!kbhit())
;
i.h.ah=0;
int86(22, &i, &o);
return(o.h.ah);
}

status(char cha, int col,int write)
{
int c, d;
for(c=68;c<=79;c++)          /* To clear the status box */
{
for(d=1;d<=8;d++)
{
gotoxy(c,d);
printf(" ");
}
}
gotoxy(59,50);
printf("Debdeep Bhattacharya");
gotoxy(68,1);
for(d=1;d<=8;d++)
{
gotoxy(68,d);
printf("|");
}
for(c=68;c<=79;c++)
{
gotoxy(c,8);
printf("--");
}
gotoxy(71,2);
printf("STATUS");
gotoxy(69,4);
if(write==0)
printf("PEN  :UP");
else if(cha==' ')
printf("PEN  :ERASE");
else
printf("PEN  :DOWN");
gotoxy(69,5);
printf("COLOR:%d",col);
gotoxy(69,6);
printf("CHAR :%c",cha);
return 0;
}

No comments: