Thread: Problem in code using C language

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    9

    Problem in code using C language

    Hello,
    I have a problem in my program .
    my program is a paint using C language.
    i have a problem with conditions .

    my program is :
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<graphics.h>
    #include<dos.h>
    #include<alloc.h>
    #include<math.h>
    
    
    int    x,y,button,color,x1,x2,y1,y2,prevx2,prevy2;
    union  REGS i,o,r,out,in,dc;
    
    
    //function to check errors
    void init()
    {
    	int gd=DETECT,gm,ec;
    	initgraph(&gd,&gm,"c:\\tc\\bgi");
    	ec=graphresult();
    	if(ec!=grOk)
    	{
    	printf("graph Error: %s",grapherrormsg(ec));
    	getch();
    	exit(1);
    	}
    	clrscr();
    }
    
    void f(int xx,int yy,int BC,int NC) {
    	if(getpixel(xx,yy) == BC || getpixel(xx,yy) == NC)
    		return;
    	putpixel(xx,yy,NC);
    	f(xx,yy+1,BC,NC);
    	f(xx,yy-1,BC,NC);
    	f(xx+1,yy,BC,NC);
    	f(xx-1,yy,BC,NC);
    }
    
    //function to detect color
    int detectcolor();
    
    
    //function freehand
    void freehand();
    //function to create colors bar and fill backgroud
    void createsides() ;
    
    //function to get postion from mouse
    void getmousepos();
    
    // function to create colors
    void createcolors();
    
    //function to create shapes bar and fill backgroud
    void createshapesbar();
    
    //function to create shapes
    void createshapes();
    
    //function to create cursor mouse
    void mousecursor();
    
    //function to create line
    void lin1();
    //function to create sequre
    void sequre();
    //function to create circle
    void cr();
    //function to create mkhms
    void msds();
    //function to create motlat
    void motlat();
    //functuion to fill background
    void fillbg(int x,int y,int BC,int NC);
    void bg();
    void show_mouse();
    void hide_mouse();
    
    void get_mouse_pos1(int *x,int *y,int *button);
    void get_mouse_pos(int *x,int *y,int *button);
    //function to erase all
    void erase();
    int getshape();
    void switchershape(int nshape);
    
    void main()
    {
    	int zz=99;
    	init();
    	outtextxy(300,5,"Paint ");
    	outtextxy(500,5,"By: Kamel & Saleh");
    
    
    	while(!kbhit())
    	{
    		createsides();
    		createcolors();
    		createshapesbar();
    		createshapes();
    		mousecursor();
    	do{
    //			 printf("Press enter and choose a shape");
    		detectcolor();
    
    				}
    		while(color>15 && color<0);
    
    
    
    //	setcolor(2);
    //		freehand(); //working  freehand
    
    //		lin1(); // working line
    //		sequre(); //working
    //	cr(); //working
    // msds(); //working
    // motlat(); //working
    //		bg();
    //circle(300,300,10);
    //erase();
    	zz=getshape();
    	gotoxy(10,10);
    	printf("the shape is %d",zz);
    	switchershape(zz);
    
    		getch();
    
    	}
    closegraph();
    }
    
    
    
    void createsides()
    {
    	setfillstyle (1,8); //color backgroud for frames
    	bar (0,419,640,480); //down bar for colors
    	bar (0,0,60,418);   // left bar for shapes
    //	setfillstyle (1,15); //color backgroud for board
     //	bar(61,20,639,419);
     //	rectangle(61,20,639,419);
    
    
    
    }
    
    
    
    void createcolors()
     {
    		//color slide color from 1 to 16
    
    		for(int colorr=0;colorr<16;colorr++)
    		{
    			setfillstyle(1,colorr);
    			bar((40*colorr)+2,430,(40*colorr)+35,465);
    			rectangle((40*colorr)+2,430,(40*colorr)+35,465);
    
    		}
    }
    
    void createshapes()
    {
    	setcolor(15);
    
    
    		//line
    		line(10,13,45,45);
    
    		//circle
    		circle(30,80,15);
    
    		//pencil
    		line(23,122,29,122);
    		line(23,123,28,123);
    		line(23,124,27,124);
    		line(23,125,26,125);
    		line(23,126,25,126);
    		line(23,127,24,127);
    		line(23,122,23,128);
    		line(29,122,38,131);
    		line(23,128,32,137);
    		line(38,131,32,137);
    		line(29,122,23,128);
    		line(26,125,35,134);
    
    
    		//msds
    		line(20,162,40,162);
    		line(20,162,30,176);
    		line(30,176,20,190);
    		line(40,162,50,176);
    		line(50,176,40,190);
    		line(20,190,40,190);
    
    
    		//sequre
    		line(10,245,50,245);
    		line(10,215,50,215);
    		line(10,215,10,245);
    		line(50,215,50,245);
    
    		//motlat
    		line(50,290,30,265);
    		line(10,290,30,265);
    		line(10,290,50,290);
    
    		//fill background
    		setfillstyle(1,4);
    		bar(25,325,35,340);
    		setcolor(4);
    		circle(30,320,7);
    		setfillstyle(5,14);
    		bar(27,327,33,337);
    		setfillstyle(1,0);
    		gotoxy(2,26);
    		printf("Clear");
    //		bar(8,400,48,415);
    }
    
    void erase()
    {
     while(!kbhit())
     {
     //printf("welcome\n");
     get_mouse_pos(&x1,&y1,&button);
    	//			printf("%d:%d\n",x1,y1);
    	if(button ==1)
    		 if(x1>8&&x1<48&&y1>400 && y1<415)
    				{
    				hide_mouse();
    				createsides();
    				createcolors();
    				createshapesbar();
    				createshapes();
    				show_mouse();
    				}
    }
    }
    void createshapesbar()
    {
    //for shapes
     for(int k=0;k<7;k++)
    		{
    			setfillstyle(1,0);
    			bar(5,5+(50*k)+3,55,(50*k)+50);
    		}
    //for the secound options (fill background , clear all)
    
    	setfillstyle(0,0);
    	bar(5,360,35,390);       //bar for color
    	setcolor(0);
    	rectangle(5,360,35,390);
    
    
    }
    void hide_mouse()
    {
    	i.x.ax=2;
    	int86(0x33,&i,&o);
    }
    void show_mouse()
    {
    	i.x.ax=1;
    	int86(0x33,&i,&o);
    }
    
    
    void mousecursor()
    {
    
    
    		//to init mouse
    		r.x.ax=0;
    		int86(0x33,&r,&r);
    
    		//to show mouse
    		r.x.ax=1;
    		int86(0x33,&r,&r);
     /*
    		while(!kbhit())
    		{
    				 r.x.ax = 3;
    				 int86(0x33,&r,&r);
    				gotoxy(72,26);
    				 if(r.x.cx>61 && r.x.dx>21 && r.x.cx<639 && r.x.dx<419 )
    							printf("%3d:%3d",r.x.cx,r.x.dx);
    				else
    							{
    							r.x.cx=0;
    							r.x.dx=0;
    							printf("%3d:%3d",r.x.cx,r.x.dx);
    							}
    
    
    		}
    							*/
    
    }
    int detectcolor()
    {
    	 //	union  REGS  dc;
    		int cl,cxx,dxx;
     while (!kbhit())
    	{
    	 //dc.x.ax = 5;
    	 //int86 (0x33,&dc,&dc);
    	 get_mouse_pos1(&x1,&y1,&button);
    	 if(y1 > 430 && y1 < 465)
    	 {
    	 if (button ==  1)    //set color
    			 {
    			 /*cxx=dc.x.cx;
    			 dxx=dc.x.dx;
    				*/
    			 for(int k=0;k<16;k++)
    					{
    					 if(x1>2+40*k && x1<35+40*k)
    								cl=k;
    					}
    
    					if(cl != 15 && cl != 8)
    					{
    						color=cl;
    						setfillstyle(1,color);
    						bar(5,360,35,390);
    						rectangle(5,360,35,390);
    					}
    
    					if(cl==15 && x1 >602 && x1 <635)
    						 {
    						color=15;
    						setfillstyle(1,color);
    						bar(5,360,35,390);
    						rectangle(5,360,35,390);
    						 }
    				if(cl == 8 && x1 >322 && x1 <355)
    						 {
    						color=8;
    						setfillstyle(1,color);
    						bar(5,360,35,390);
    						rectangle(5,360,35,390);
    						 }
    
    		//	 gotoxy(28,26);
    		 //	 printf("Press enter and choose a shape");
    			 }
    
    
    
    
    	 }
    	}
    	hide_mouse();
    	setcolor(color);
    	show_mouse();
    //	printf("bye color");
    	return (color);
    }
    
    int click(int *x,int *y)
    {
    r.x.ax=5;
    int86(0x33,&r,&r);
    if(r.x.bx &1){
    	*x=r.x.cx;
    	*y=r.x.dx;
    	return 1;
    }
    return 0;
    }
    
    
    void get_mouse_pos(int *x,int *y,int *button)
    {
    	i.x.ax=3;
    	int86(0x33,&i,&o);
    	*x=o.x.cx;
    	*y=o.x.dx;
    	*button=o.x.bx&1;
    }
    
    void freehand()
    {
    while(!click(&x1,&y1));
    //get_mouse_pos1(&x1,&y1,&button);
    while(x1>61 && x1<639 && y1>20 && y1<419)
    	{
    
    		get_mouse_pos(&x1,&y1,&button);
    		x2=x1;
    		y2=y1;
    		while(button==1 && x1>61 && x1<639 && y1>20 && y1<419)
    		{
    			hide_mouse();
    			line(x1,y1,x2,y2) ;
    			x1=x2;
    			y1=y2;
    			get_mouse_pos(&x2,&y2,&button);
    			show_mouse();
    		}
    
    	}
    
    
    
    }
    void get_mouse_pos1(int *x,int *y,int *button)
    {
    	in.x.ax=5;
    	int86(0x33,&in,&out);
    	*x=out.x.cx;
    	*y=out.x.dx;
    	*button=out.x.bx&1;
    }
    
    void lin1()
    {
     int flag=1;
     while(flag)
    {
    while(!click(&x1,&y1));
    while(x1>61 && x1<639 && y1>20 && y1<419)
    {
    while(!click(&x2,&y2));
    if(x2>61 && x2<639 && y2>20 && y2<419)
    		{
    		hide_mouse();
    		line(x1,y1,x2,y2);
    		show_mouse();
    		}
    while(!click(&x1,&y1));
    //printf("x1:%d y1:%d  x2:%d y2:%d " ,x1,y1,x2,y2);
     }
     flag=0;
     }
    }
    
    
    
    void sequre()
    {
    while(!click(&x1,&y1));
    while(x1>61 && x1<639 && y1>20 && y1<419)
    {
    		while(!click(&x2,&y2));
    		if(x2>61 && x2<639 && y2>20 && y2<419)
    		{
    			hide_mouse();
    			rectangle(x1,y1,x2,y2);
    			show_mouse();
    		}
    		while(!click(&x1,&y1));
     }
    }
    
    
    void cr()
    {   int r;
    
    while(!click(&x1,&y1));
    while(x1>61 && x1<639 && y1>20 && y1<419)
    {
    while(!click(&x2,&y2));
    if(x2>61 && x2<639 && y2>20 && y2<419)
    {
    r=pow(x2-x1,2)+pow(y2-y1,2);
    r=sqrt(r);
    //gotoxy(10,10);
    //printf("%d:%d    %d:%d   r=%d",x1,y1,x2,y2,r);
    
    if(r+x1<639 && r+x1>61 && r+y1<419 && r+y1>20 && r+x2<639 && r+x2>61 && r+y2<419 && r+y2>20	&& abs(r-y1)>20 && abs(r-y1)<419&& abs(r-y2)>20 && abs(r-y2)<419 && abs(r-x1)>61 && abs(r-x1)<639 && abs(r-x2)>61 && abs(r-x2)<639)
    					{
    	//				printf("ok");
    					hide_mouse();
    					circle(x1,y1,r);
    					show_mouse();
    					}
    //else
    		//			printf("not ok");
    }
    
    
    while(!click(&x1,&y1));
    
    }
    
    }
    
    
    
    void msds()
    {
    int fx,fy,ax,ay,flag=0;
    while(!click(&x1,&y1));
    while(x1>61 && x1<639 && y1>20 && y1<419)
    {
    for(int i=0;i<5;i++)
    		{
    
    			while(!click(&x2,&y2));
    			if(x2>61 && x2<639 && y2>20 && y2<419)
    			{
    
    			if(flag==0)
    				{
    				flag=1;
    				fx=x1;
    				fy=y1;
    				}
    			hide_mouse();
    			line(fx,fy,x2,y2);
    			show_mouse();
    			fx=x2;
    			fy=y2;
    			}
    			else
    			i--;
    	 }
    	 hide_mouse();
    	 line(x1,y1,fx,fy);
    	 show_mouse();
    	 break;
    }
    
    }
    
    void motlat()
    {
     int fx,fy,ax,ay,flag=0;
    while(!click(&x1,&y1));
    while(x1>61 && x1<639 && y1>20 && y1<419)
    {
    
    for(int i=0;i<2;i++)
    		{
    
    			while(!click(&x2,&y2));
    
    			if(flag==0)
    				{
    				flag=1;
    				fx=x1;
    				fy=y1;
    				}
    			hide_mouse();
    			line(fx,fy,x2,y2);
    			show_mouse();
    			fx=x2;
    			fy=y2;
    //printf("x1:%d y1:%d  x2:%d y2:%d " ,x1,y1,x2,y2);
    	 }
    	 hide_mouse();
    	 line(x1,y1,fx,fy);
    	 show_mouse();
    	 break;
    	 }
    
    }
    
    
    
    
    
    void bg()
    {
    show_mouse();
    gotoxy(10,12);
    printf("bacground function");
    while(!click(&x1,&y1));
    while(x1>61 && x1<639 && y1>20 && y1<419)
    {
    
    	 f(x1,y1,15,RED);
    	 show_mouse();
    }
    
    printf("bye bg");
    }
    
    int getshape()
    {
    	int cl,flag=1;
    	while (flag)
    	{
    //	 printf("welcome to get shape");
    	 //dc.x.ax = 5;
    	 //int86 (0x33,&dc,&dc);
    	 get_mouse_pos1(&x1,&y1,&button);
    	 if(x1 > 5 && x1 < 55)
    	 {
    	 if (button ==  1)
    			 {
    			 /*cxx=dc.x.cx;
    			 dxx=dc.x.dx;
    				*/
    			 for(int k=0;k<7;k++)
    					{
    					 if(y1>8+50*k && y1<50+50*k)
    								{
    								cl=k;
    								flag=0;
    								}
    					}
    			 }
    	 }
    	}
    	printf("the shape is %d",cl);
    	return (cl);
    
    }
    
    void switchershape(int nshape)
    {
    
    	switch(nshape)
    		{
    		case 0 : { lin1(); }
    
    		case 1 :{ cr();    }
    
    		case 2 :{ freehand();}
    
    		case 3 :{  msds();   }
    
    		case 4 :{ sequre();  }
    
    		case 5 : { motlat(); }
    
    		case 6 :{ bg();      }
    	 }
    
    
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have a function called f? Really?

    Since your code isn't going to tell us anything, and for that matter there's probably only one forum regular who still bothers to keep TC around, and you're apparently not going to tell us anything (error message? bad behavior? You seem to believe you have a problem, which I guess is the first step towards salvation, but what do you think the problem is?) I doubt we're ever going to get very far.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    there is no error message!
    all the function working good!

    the problem with condition! when i want to execute this program i can't play with colors or shapes when i want! try to run it and you will show what i mean!

    thank you

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Unfortunately, I am not living in 1989, so I am unable to run that code.

    If your code doesn't work, why do you think all the functions are working? Why have you decided that the conditions are the problem?

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    because i tried to run all the function manually ! and the all works!.
    if i use the function detectcolor() and getshape() the problem start!
    cause the condition maybe is wrong!.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm the "TC user regular". I'll take a look at it in about 2 hours, just stepping out now.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    all the conditions of while for all functions shape.
    I think that you must change to infinite loop and when you get the matter you will break the function because you want to continue in the main.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First thing it needs is a way to quit the program - that's really a pain not having one.

    Second thing of course, is that nothing draws in the "canvas" area, yet. The interface looks great, and I like your idea of an while(1) loop for the main activity of the program - but get that quit the program feature working asap.

    Nice work so far!

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    thank you

    you can do it ?

  10. #10
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Quote Originally Posted by tabstop View Post
    Unfortunately, I am not living in 1989, so I am unable to run that code.

    If your code doesn't work, why do you think all the functions are working? Why have you decided that the conditions are the problem?
    What does 1989 have to do with anything?

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The code is written for Turbo C, a compiler from that era. The only aficionado around here of such an ancient compiler -- other than some Asian schools, apparently -- is Adak. Most of us don't keep compilers from 20+ years ago around; we advance with the technology.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by \007 View Post
    What does 1989 have to do with anything?
    That's the year they stopped making Turbo C.

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    9

  14. #14
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Turbo C is horrible.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by kemoox View Post
    thank you

    you can do it ?
    I won't do it. I see no chance for a good program here. If you can't add features to the program. That leaves no one except myself, to work on the program, which I have no interest in using.

    Perhaps you could look in the internet archives and find a working program you like?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem uder LINUX (gcc) and same code work at Windows (DevC++)
    By miroslavgojic in forum C Programming
    Replies: 5
    Last Post: 11-24-2010, 10:05 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  5. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM