BGI Graphics + mouse interactivity

rhoit 0 Tallied Votes 189 Views Share

Nothing but the VERY HARD Work out for long day and night.
It was actually i made it for my project work which my friend loved it very much.
This might be the useful stuff to you also.

Ya Now days rarely anyone uses the BGI stuff but also it is useful for the knowledge.

Ya If you don't mind you can send me some stuff in CPP.
send me something in "the.nube@gmail.com"

/*
Title: Server (Mouse Interactivity)
Version: 1
Started: 19 Dec 2007
Revised: 0
Program Status: Test mode
*/

#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdio.h>
#include<process.h>

#define DELAY_TIME  150

int xco, yco; //store x & y cordinate value

int temp, tflag=NULL;
int mx, my, button; //mouse cordinate & values
int xm, ym; //stores max value of x & y cordinate which will always be same
int RESPOND; //return value

union REGS i, o;

class Layout
{
 protected:
    void Frame_cut(int, int, int, int, int, int, char); //Draw Frame Outline

 public:
    void inti_screen(); //set variable values
};

class Builder:public Layout
{
 protected:
    int item_c; //items contains
    char *items_n[]; //items name container
    void Form_component(char);
    void Menubox(int);

 public:
    void Form();
    void Menu(char *, char *[]);
};

int CFP=0; //current Focus position
class Access : public Builder
{
 void Focus(int);
 void intimouse();
 void mousefocus(int *);
 void showmouseptr(int);
 void putpointer(int, int);
 void getmousepos();
 void Clickstroke(int *);
 void Keystroke();
 void Trackmouse();

 public:
    int devices();
}A;

void Layout::inti_screen()
{
 setbkcolor(1);
 setlinestyle(0,0,0);
 xm=getmaxx(); //can't use constructor
 ym=getmaxy();
}

void Layout::Frame_cut(int c, int l, int t, int r, int b, int ra, char cut = 'n')
{
 setcolor(c);
 switch(cut)
 {
  case 'n': //rectangle no cut
	ra=0;

  case 'r': //right digonally cut
	line(l, t, r-ra, t); //top horizontal
	line(l, t, l, b-ra); //left vertical
	arc(l+ra, b-ra, 180, 270, ra); //left bottom arc
	line(l+ra, b, r, b); //bottom horizontal
	line(r, t+ra, r, b); //right vertical
	arc(r-ra, t+ra, 0, 90, ra); //right top arc
	break;

  case 'l': //left diagonallu cut
	line(l+ra, t, r, t); //top horizontal
	line(l, t+ra, l, b); //left vertical
	arc(l+ra, t+ra, 90, 180, ra); //left top arc
	line(l, b, r-ra, b); //bottom horizontal
	line(r, t, r, b-ra); //right vertical
	arc(r-ra, b-ra, 270, 0, ra); //right bottom arc
 }
}

void Builder::Form()
{
 cleardevice(); //clear the screen for new form
 //creating the platform
   Frame_cut(14, 20, 20, xm-20, ym-20, 40, 'l');
   setfillstyle(2, 15);
   floodfill(1,1,14); //outside

 //creating Outlines
   line(60, 20, 60, ym-100); //left vertical
   line(100, ym-20, 100, ym-60); //left bottom vertical
   arc(100, ym-100, 180, 270, 40); //left bottom arc
   line(20, ym-100, 60, ym-100); //left bottom horizontal
   line(100, ym-60, xm-20, ym-60); //bottom horizontal

 //Loading text
   settextstyle(1,2,4);
   outtextxy(18,70, "My Manager 1.0");
   settextstyle(2,0,4);
   outtextxy(110, ym-40, "Programed By a Fool | Any Comments Mail me at 'the.nube@gmail.com'");
   setcolor(15);
   settextstyle(2,0,5);
   outtextxy(110, ym-55, "Esc: Return Back | Enter: Open Menu");
}

void Builder::Form_component(char c)
{
 switch(c)
 {
  case 't': //Form Title box
	setfillstyle(1,15);
	bar(70, 30, xm-30, 75);
	break;

  case 'f': //Menu Frame
	setcolor(1);
	setlinestyle(0,0,0);
	setfillstyle(9,9);
	pieslice(100, ym-100, 180, 270, 30); //left bottom pie
	bar(70, 75, xm-30, ym-100);
	bar(100, ym-100, xm-30, ym-70);
	break;
 }
}

void Builder::Menubox(int P)
{
 yco=(P+2)*50;

 Frame_cut(15, 150, yco, xm-120, yco+30, 15, 'r');
 circle(157, yco+7, 3); //push pin left
 circle(xm-127, yco+23, 3); //push pin right

 setfillstyle(1,15);
 floodfill(165, yco+10,15);

 setcolor(9);
 outtextxy((xm/2)-120, yco-1, items_n[P]); //center text
}

void Builder::Menu(char *caption, char *list[])
{
 //addtional
   inti_screen();
   Form();
 //Loading Menu Caption
   Form_component('t');
   setcolor(6);
   settextstyle(1,0,4);
   outtextxy((xm-textwidth(caption))/2, 30, caption); //center text

 //Setting up Menu(items)
   Form_component('f');

   settextstyle(1,0,3);

   for(item_c=0; list[item_c]!='\0'; item_c++)
    {
     items_n[item_c]=list[item_c]; //coping to class's variable
     Menubox(item_c);
    }
   item_c--;
}

void Access::Focus(int NFP=0) //NFP(New Focus Position)
{
 //rotator
   if(NFP>item_c) NFP=0;
   else
   if(NFP<0) NFP=item_c;

 //defocus  not to execute in 1st run
   if(NFP!=CFP)
    {
     yco=(CFP+2)*50;
     setfillstyle(9,9);
     bar(149, yco-1, xm-119, yco+31); //Eraseing Bitmap
     setlinestyle(0,0,0);
     Menubox(CFP);
    }

 //focusing Mode
   yco=(NFP+2)*50;

   setfillstyle(9,9);
   bar(150, yco, xm-120, yco+30); //Eraseing Bitmap

   setlinestyle(0,0,3);
   Frame_cut(15, 150, yco, xm-120, yco+30, 15, 'l');
   circle(158, yco+22, 2); //push pin left
   circle(xm-127, yco+7, 2); //push pin right

   setfillstyle(1,9);
   floodfill(165, yco+10,15);

   setcolor(15);
   outtextxy((xm/2)-120, yco-1, items_n[NFP]); //center text

  CFP=NFP; //update CFP
}

void Access::intimouse()
{
 //Initializes the mouse
   i.x.ax=0;
   int86(0x33,&i,&o);

 if(o.x.ax==0)
   {
    printf("\nMouse driver not loaded");
    getch();
    exit(1);
   }
}

void Access::showmouseptr(int m)
{
 i.x.ax=m;
 int86(0x33,&i,&o);
}

void Access::getmousepos()
{
 i.x.ax=3;
 int86(0x33,&i,&o);
 button=o.x.bx;
 mx=o.x.cx;
 my=o.x.dx;
}

void Access::putpointer(int x, int y)
{
 i.x.ax=4;
 i.x.cx=x;
 i.x.dx=y;
 int86(0x33,&i,&o);
}

void Access::mousefocus(int *c)
{
 if(CFP!=*c)
  {
   delay(DELAY_TIME);
   showmouseptr(2);
   Focus(*c);
   showmouseptr(1);
  }
}

void Access::Clickstroke(int *c)
{
 if(button==1&&tflag!=1) //avoid repetation
  {
   temp=9;
   tflag=1;
   upbutton:
   showmouseptr(2);
   setlinestyle(0,0,3);
   Frame_cut(temp, 150, yco, xm-120, yco+30, 15, 'l');
   showmouseptr(1);
  }
 else
 if(button!=1&&tflag==1)
  {
   temp=15;
   tflag=NULL;
   RESPOND=*c;
   goto upbutton;
  }

}

void Access::Trackmouse()
{
 getmousepos();
 if(mx>150&&mx<xm-120)
  {
   for(int c=0; c<=item_c; c++)
    {
     yco=(c+2)*50;
     if(my>yco&&my<yco+30)
      {
       mousefocus(&c);
       Clickstroke(&c);
      }
    }

  }
}

void Access::Keystroke()
{
 if(kbhit())  //lovely lock
  {
   showmouseptr(2);
   switch(getch()) //monitor key stroke
    {
     case 80: //Down arrow
      Focus(CFP+1);	break;

     case 72: //Up arrow
      Focus(CFP-1);	break;

     case 13: //Enter
      RESPOND=CFP;      break;

     case 27: //Esc
      RESPOND=27;
    }
   showmouseptr(1);
  }
}

int Access::devices()
{
 RESPOND=-1;
 intimouse();
 Focus(); //Place selector
 putpointer(xm/2+150, (0+2)*50+15);
 showmouseptr(1);

 while(RESPOND==-1)
 {
  Keystroke();
  Trackmouse();
 }

 return RESPOND;
}

int gdriver=DETECT, gmode;

void main()
{
 initgraph(&gdriver, &gmode, "bgi");

 int errorcode=graphresult();

 if(errorcode!=grOk)
  {
   printf("Graphics error: %s\n", grapherrormsg(errorcode));
   getch();
   exit(1);
  }

 while(1)
    {
     char *list[]={
		   "New Library",
		   "Load Library",
		   "Credits",
		   "Exit",
		   '\0'
		  };

     A.Menu("Main Menu", list);
     int cho=A.devices();
     switch(cho)
	   {
	    case 0:
	    case 1:
	    case 2: break;

	    case 3: //exit button
	    case 27:
		  closegraph();
		  restorecrtmode();
		  return;
	   }
    }

}
Ene Uran 638 Posting Virtuoso
mukulbudania 0 Newbie Poster

I am not able to accest the graphics mode. There is some problem with the path... can this work with the Dev Cpp compiler or does it work only with The Turbo C++. I have tried a lot. Can you please tell me what to do if i have to use the G++ compiler of fedora linux.
I am not able to assign the path correctly...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.