Showing posts with label programmin in c. Show all posts
Showing posts with label programmin in c. Show all posts

Thursday, 21 March 2013

Mouse Programming in C

This is simple tutorial to show you How you can  use Mouse  in your C program.
I have Attached a source file. This program is not developed by me. I found it on internet somewhere, But as this program is easy to understand I am using this program.

See Source code here.


To Enable Mouse in C program you need to generate Interrupt.
The interrupt number for Mouse input related service is 33h.



Files to include -> dos.h
Function from dos.h to be use ->int int86(int intno,union REGS *input , union Regs *output)
This Function Generates Software interrupts

union - REGS is defined in dos.h file
it is defined as

union REGS{
struct WORDREGS   x;
struct BYTEREGS     y;
};

WORDREGS  & BYTEREGS are defined as

struct WORDREGS{
unsigned int ax,bx,cx,dx;
unsigned int si,di,cflag,flags;
};

struct BYTEREGS{
unsigned int al,ah,bl,bh;
unsigned int cl,ch,dl,dh;
};


You don't need to remember all this stuff. This is just for understanding.
If you have studied any  Micro Controller subject then you can easily identify that these ax,bx... indicates Registers.

So, let me first tell  what we exactly gonna do.

For every type of event there are Software interrupt number are defined.
for example using keyboard, sound, mouse.
Here for mouse interrupt number is 33h.
and for all types of operation like show mouse pointer, initialize mouse, restrict mouse pointer on screen etc.. we have different Service numbers.

Generally service number will be provided in
ax register,
so here we will write something like  i.x.ax

now all the input information will be provided in   variable of type REGS, and we will get out put in similar union varible

In our program we have declared
union i,o;
we will use
 i as input 
o as output, 

so this are some of service numbers for mouse



0~ Mouse Reset/Get Mouse Installed Flag
1~ Show Mouse Cursor
2~ Hide Mouse Cursor
3~ Get Mouse Position and Button Status
4~ Set Mouse Cursor Position
5~ Get Mouse Button Press Information
6~ Get Mouse Button Release Information
7~ Set Mouse Horizontal Min/Max Position
8~ Set Mouse Vertical Min/Max Position

This is example who you  can use this  service.

To initialize mouse

i.x.ax=0;
int86(33h,&i,&o);

done !!


to Restrict mouse pointer between square (10,10) to (20,20)

        i.x.ax=7;
 i.x.cx=10;
 i.x.dx=20;
 int86(0x33,&i,&o);

 i.x.ax=8;
 i.x.cx=10;
 i.x.dx=20;
 int86(0x33,&i,&o);

very simple right?
now Go through source code and  try your self.

Wednesday, 13 March 2013

Tower Of Hanoi - Graphics in C

Hello Friends,
This is simple Tutorial about Graphics in C with example program  "Tower of Hanoi" problem.
Hope you all know about tower of Hanoi problem. If you don't know , google it :)
or visit this wikipedia page :  Tower Of Hanoi

Here is link to source file to c program : Click here

Note : program of c graphics works on 16-bit pc.
it will work on Windows xp. If you have windows 7 then you will need Dosbox to run this program.

If you traverse the program from main() function , then it is self explanatory , if you need help go through the explanation given below.





















Structures :
  • loc    : containg to integers x, y  for storing coordinates of particular object
  • struct loc
    {
    int x;
    int y;
    };
    
  • bar   : It contains two variables of loc structure. one is for permanent location of bar called barloc, other is avloc which is to store on which location a ring can b put on bar.
  • struct bar
    {
    struct loc barloc;
    struct loc avloc;
    };
    
  • ring   : This structure have variable to store location of particular ring and the size of ring (top most ring have smaller size, bottom most ring have biggest size)
  • struct ring
    {
    struct loc rloc;
    int size;
    };
    
Functions :
  • draw : this function will draw whole screen,
     first it will put all the lables on the screen
    then it will draw all bars on the screen
    then all the rings will be drawn
    void draw(struct bar a,struct bar b,struct bar c,struct ring r[],int no)
    {    int i;
        outtextxy(250,30, "TOWER OF HANOI");
        outtextxy(60, 330, "SOURCE");
        outtextxy(240,330, "DESTINATION");
        outtextxy(470,330, "TEMPORARY");
        setfillstyle(8,13);
        bar(a.barloc.x-2, a.barloc.y-200, a.barloc.x+1, a.barloc.y);
        bar(b.barloc.x-2, b.barloc.y-200, b.barloc.x+1, b.barloc.y);
        bar(c.barloc.x-2, c.barloc.y-200, c.barloc.x+1, c.barloc.y);
    
    
    for(i=1;i<=no;i++)
    {
      setfillstyle(8+(i%2),10 /*getmaxcolor()*/);
      bar(r[i].rloc.x-r[i].size,r[i].rloc.y-20,r[i].rloc.x+r[i].size,r[i].rloc.y);
    
    }
    getch();
    cleardevice();
    
    };
  • towers : This function is recursive function. to under stand the logic go through this wikipedia page
  • void towers (int n,struct bar *from,struct bar *to,struct bar *aux,struct ring r[],int no)
    {
    int i=1;
    if(n==1)
    {
    (r[1].rloc.x)= to->avloc.x;
    (r[1].rloc.y)= to->avloc.y;
    if(from->avloc.y!=300){from->avloc.y+=20; }
    to->avloc.y-=20;
    draw(*from,*to,*aux,r,no);
    return;
    }
    
    towers(n-1,from,aux,to,r,no);
    
    while(from->avloc.x!=r[i].rloc.x){i=i+1;}
    
    (r[i].rloc.x)=to->avloc.x;
    (r[i].rloc.y)=to->avloc.y;
    if(from->avloc.y!=300){from->avloc.y+=20; }
    to->avloc.y-=20;
    
    draw(*from,*to,*aux,r,no);
    
    towers(n-1,aux,to,from,r,no);
    
    }
    
I hope this will help.
If you have any query then leave comment, and suggestions are always welcome on this blog !