Thread: Help on Mouse support for my text based

  1. #1
    Anteneh
    Guest

    Red face Help on Mouse support for my text based

    Hi, I have recently moved my programming practice from Pascal to C++. I could not find the mouse support header files, if any, to include on my new C++ consol program. I am working with Turbo C++ ,not Visual c++.
    Is there any heared file like that of TView in turbo pascal wich enables me use the mouse events ? Or I need to write it myself ?
    Many thanks.. and very Best regards//

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    i had alread posted this some where.. any way.. here i go again..
    Hi...
    Try using my class... this should give you mouse support.. but mind me... it is not garunteed to work on all platforms and compilers.... But i asume you are talking about DOS console..




    Code:
    # include <conio.h>
    # include <string.h>
    # include <fstream.h>
    # include <stdio.h>
    # include <dos.h>
    # include <graphics.h>
    
    
    
    
    //Mouse Class starting//
    //------------------------------------------------------------------------------------------------------//
    int  graphicsdriver,gm,maxx,maxy,graphicsx,graphicsy,bu
    tton;
    static union REGS inregs, outregs;
    static struct SREGS segregs;
    typedef enum {ARROW, HAND, HOUR_GLASS, PEN} cursor_type;
    union REGS graphicsi,graphicso;
    
    static const int cursor_data[] = {
    	/* standard arrow cursor */
    	1, 0,                            /* x, graphicsy of hotpoint                    */
    
    	0x9FFF, 0x8FFF, 0x87FF, 0x83FF,  /* SCREEN MASK (16 words)              */
    	0x81FF, 0x80FF, 0x807F, 0x803F,  /* Each word marks 16 pixels (lowest   */
    	0x801F, 0x800F, 0x80FF, 0x887F,  /* bit is rightmost pixel). The mask   */
    	0x987F, 0xBC3F, 0xFC3F, 0xFE7F,  /* is AND_ed with screen contents.     */
    
    	0x0000, 0x2000, 0x3000, 0x3800,  /* CURSOR MASK (16 words)              */
    	0x3C00, 0x3E00, 0x3F00, 0x3F80,  /* After the screen is AND_ed with the */
    	0x3FC0, 0x3E00, 0x3600, 0x2300,  /* screen mask it is XOR_ed with this  */
    	0x0300, 0x0180, 0x0180, 0x0000,  /* cursor mask.                        */
    
    	/* pointing hand cursor */
    	2, 1,
    	0xCFFF, 0x87FF, 0x87FF, 0x87FF, 0x813F, 0x8007, 0x8003, 0x8003,
    	0x8003, 0x8003, 0x8003, 0x8003, 0x8007, 0xC007, 0xC007, 0xE03F,
    
    	0x0000, 0x3000, 0x3000, 0x3000, 0x3000, 0x36C0, 0x36D8, 0x36D8,
    	0x36D8, 0x36D8, 0x3FF8, 0x3FF8, 0x3FF0, 0x1FF0, 0x1FC0, 0x0000,
    
    	/* hourglass cursor */
    	7, 7,
    	0xC003, 0xC003, 0xC003, 0xC003, 0xE007, 0xF00F, 0xF81F, 0xFC3F,
    	0xFC3F, 0xF81F, 0xF00F, 0xE007, 0xC003, 0xC003, 0xC003, 0xC003,
    
    	0x0000, 0x1FF8, 0x0000, 0x1FF8, 0x0D50, 0x06A0, 0x0340, 0x0180,
    	0x0180, 0x03C0, 0x07E0, 0x0D50, 0x1AA8, 0x0000, 0x1FF8, 0x0000,
    
    	/* pen cursor */
    	0, 15,
    	0xFFF1, 0xFFE0, 0xFFC0, 0xFF80, 0xFF01, 0xFE03, 0xFC07, 0xF80F,
    	0xF01F, 0xE03F, 0xE07F, 0xC0FF, 0xC1FF, 0x87FF, 0x1FFF, 0x3FFF,
    
    	0x0000, 0x000E, 0x001C, 0x003A, 0x0074, 0x00E8, 0x01D0, 0x03A0,
    	0x0740, 0x0E80, 0x0D00, 0x1A00, 0x1800, 0x2000, 0x4000, 0x0000
    
    };
    
    
    
    static class mouse{
    		protected:
    
    		public:
    			mouse();
    			initmouse();
    			showmouseptr();
    			getpoint();
    			restrictmouseptr(int,int,int,int);
    			getmousepos(int*,int*,int*);
    			void setCursor(cursor_type cursor);
    			void disableMouse();
    
    
    		};
    
    mouse::mouse()
    {
    setviewport(1,57,maxx-1,maxy-1,1);
    restrictmouseptr(1,1,maxx-1,maxy-1);
    }
    
    mouse::getpoint()
    {
    int x,y,button;
    getmousepos(&button,&x,&y);
    if(x>30 && x<430 && y>70 && y<470)
    {
    int num=0;
    x=x-30;
    y=y-70;
    x=x/50;
    y=y/50;
    num=y*8;
    num=num+x;
    return num;
    }
    return -1;
    }
    
    
    
    mouse::initmouse()
    {
     graphicsi.x.ax=0;
     int86(0x33,&graphicsi,&graphicso);
     return(graphicso.x.ax);
    }
    
    mouse::showmouseptr()
    {
     graphicsi.x.ax=1;
     int86(0x33,&graphicsi,&graphicso);
    
     return 0;
    }
    
    mouse::restrictmouseptr(int x1,int y1,int x2,int y2)
    {
     graphicsi.x.ax=7;
     graphicsi.x.cx=x1;
     graphicsi.x.dx=x2;
     int86(0x33,&graphicsi,&graphicso);
    
     graphicsi.x.ax=8;
     graphicsi.x.cx=y1;
     graphicsi.x.dx=y2;
     int86(0x33,&graphicsi,&graphicso);
     return 0;
    }
    
    mouse::getmousepos(int *button,int *x, int *graphicsy)
    {
     graphicsi.x.ax=3;
     int86(0x33,&graphicsi,&graphicso);
     *button=graphicso.x.bx;
     *x=graphicso.x.cx;
     *graphicsy=graphicso.x.dx;
     return 0;
    }
    
    
    void mouse::setCursor(cursor_type cursor) { /* types defined in header */
    	inregs.x.ax = 9;
    	inregs.x.bx = *(cursor_data + 34 * cursor);      /* x hotpoint */
    	inregs.x.cx = *(cursor_data + 34 * cursor + 1);    /* graphicsy hotpoint */
    	inregs.x.dx = FP_OFF(cursor_data + 34 * cursor + 2); /* pointer to */
    	segregs.es = FP_SEG(cursor_data + 34 * cursor + 2);  /* the masks  */
    	int86x(0x33, &inregs, &outregs, &segregs);
    }
    
    
    void mouse::disableMouse()
    {
     inregs.x.ax = 2;
     int86(0x33,&inregs,&outregs);
    }
    
    //------------------------------------------------------------------------------------------------------//
    //Mouse Class End//


    The functions available are
    mouse();
    initmouse();
    showmouseptr();
    getpoint();
    restrictmouseptr(int,int,int,int);
    getmousepos(int*,int*,int*);
    void setCursor(cursor_type cursor);
    void disableMouse();


    You can even set the cursor image like pointer, hand, hour glass etc...


    For this to work you have to run showmouseptr in a loop....
    like


    Code:
    int mian()
    {
    mouse m1;
    m1.initmouse();
    
    int x,y,button=0;
    while(button!=2)
    {
    showmouseptr();
    getmousepos(&button,&x,&y);
    }
    
    return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    2

    Thumbs up

    Many many thanks Vasanth. I just used it and it works perfect. You are real!

  4. #4
    stovellp
    Guest

    or

    You could use the Microsoft Console header.

    Code:
    #include <windows.h>
    #include <iostream.h>
    
    PINPUT_RECORD myRecord;
    unsigned long One = 100;
    unsigned long * Two = new unsigned long;
    
    int main()
    {
    GetConsoleInputA(GetStdHandle(STD_INPUT_HANDLE), myRecord, One, Two);
    
    int MouseX = myRecord->MouseEvent.dwPosition.X;
    int MouseY =  myRecord->MouseEvent.dwPosition.Y;
    cout << "Mouse X position was: " << MouseX << endl;
    cout << "And Y position: " << MouseY;
    }
    I'm not too sure if the functions are intirely correct, you'd better check the header (wincon.h), but i think it's right.

    Youl'll also find plenty of other things you can look at in the mouse event record part of the header.

    It took me about two days of searching and playing, but I got it in the end.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    2

    Wink

    stovellp,

    Appreciate your reply, but I am using the 1992 Turbo c++ compiler and there is no such file as windows.h included.
    even if there is, I am not sure if it can work on a PC with no windows installd.
    stovellp's header is fine, though I could not use the different cursor types precented, my be I need to use "the initgraph" command but I don't want to use it as I am entirely working on character basesd inveronment.
    //thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text based game
    By wipeout4wh in forum C Programming
    Replies: 12
    Last Post: 03-26-2009, 04:39 PM
  2. A begginer writing a text based dungeon game.
    By deadherorising in forum C Programming
    Replies: 55
    Last Post: 11-10-2007, 06:16 PM
  3. Text Based RPG & AI?
    By TylerMoyer in forum General AI Programming
    Replies: 4
    Last Post: 10-12-2007, 06:40 AM
  4. Mouse support in console mode
    By GaPe in forum C Programming
    Replies: 1
    Last Post: 04-17-2002, 05:15 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM