Link to home
Start Free TrialLog in
Avatar of PPC022499
PPC022499Flag for Canada

asked on

Using Mouse in Turbo C++

I have Turbo C++ for DOS, and would like to learn how to make programs that uses the mouse.

Can someone explain the most important information needed to
-use mouse
-detect mouse possition
-detect mouse clicks at their possition

Please give examples too.

I give plenty of points, and will increase if good answer... :)
Please put some work in this so that it's understandable - THANKS
Avatar of nietod
nietod

There are no C++ feafures for the mouse.  This all has to be done using inline assembly and OS interrupts.

Note that you may need to make soem addjustments to the assembly syntax as required by Turbo C++.  Like you might have to use "asm" instead of "_asm" etc.

First off you need to reset the mouse driver and determine if the mouse and driver are present.  like

bool ResetMouse()
{
    int i;
   _asm XOR AX,AX  ; Clear the accumulator.
   _asm int 33h;   ; Reset the driver.
   _asm MOV i,AX ; Get results.
   return i != 0;
};

continues.
To hide the cursor

void HideCursor()
{
   _asm MOV AX,2
   _asm INT 33h
}

To show the cursor

void HideCursor()
{
   _asm MOV AX,1
   _asm INT 33h
}

To move the mouse to a position,

void MoveMouse(int x,int y)
{
   _asm MOV AX,4
   _asm MOV CX,x
   _asm MOV DX,y
   _ asm INT 33h
}

continues
To get the mouse position and the button states

void GetMouseInfo(int &x,int &y,bool &lbutton bool &mbutton, bool &rbutton)
{
   int Buttons;

   _asm MOV AX,3
   _asm INT 33h
   _asm MOV x,CX
   _asm MOV y,DX
   _asm MOV Buttons,BX
   lbutton = (Buttons & 1) != 0;
   mbutton = (Buttons & 4) != 0;
   rbutton = (Buttons & 2) != 0;
}


Note I'm not sure if turbo C++ supports type "bool".   "Bool" is very old, but Turbo C++ is ancient.   If not, just use your own boolean type, like int


Let me know if you have any questions..
Avatar of Axter
FYI,
The version I have of Turbo C++ doesn't have bool, but it's very easy to add.

enum BOOL {false=0,true=1};
typedef BOOL bool;

int main(void)
{
      bool thisthing = false;
}
Here are some functions for mouse. This will work if you use standart Turbo C graphics drivers (*.bgi). If use VESA, then you should write your own show_mouse() and hide_mouse() functions, and when you move mouse, you shold every time hide and show i.

 unsigned int mouse_x, mouse_y, mouse_b;

 int detect_mouse()
 {
       int nButtons,mouseFound;

       asm {
              mov ax,0
              int 0x33
              mov mouseFound,ax
              mov nButtons,bx
       }

      if (mouseFound) return nButtons;
      else return 0;
 }

 void show_mouse()
 {
       asm {
              mov ax,0x0001
              int 0x33
       }
 }
 
 void hide_mouse()
 {
       asm {
              mov ax,0x002
              int 0x33
       }
 }
 
 void test_mouse()
 {
        asm {
               mov ax,0x0003
               int 0x33
               mov mouse_b,bx
               mov mouse_x,cx
               mov mouse_y,dx
        }
 }

 void set_mouse_speed(int sx,int sy)
 {
       asm {
              mov ax,0x000f
              mov cx,sx
              mov dx,sy
              int 0x33
      }
 }

 void move_mouse(int x,int y)
 {
       asm {
              mov ax,4
              mov cx,x
              mov dx,y
              int 0x33
       }

       mouse_x=x;
       mouse_y=y;
 }

 int main()
 {
      init_raphic_mode();

       if (detect_mouse())
          show_mouse();

      while (!kbhit()) {
              test_mouse();

              // test left mouse button. 2 for right button
              // and 4 for middle button
              if (mouse_b & 1) {
                 hide_mouse();
                 putpixel(mouse_x,mouse_y,WHITE);
                 show_mouse();
              }
      }

      return 0;
 }
>> The version I have of Turbo C++ doesn't have
>> bool, but it's very easy to add.
That doesn't give 100% the same behavior as the official "bool" but its the best that can be done on old systems.
PPC,
Both nietod and kamarey have given you some very good examples.
But I think you would just be reinventing the wheel if you're going to program your own DOS gui interface.
There are libraries on the web for Turbo C++, that have these features, and some of them are free.

If you're interested, I'll digg up my old links, and see if they're still valid, and post them here.
Avatar of PPC022499

ASKER

I'll test this, thanks

be back
nietod:

Your answer seems very good, but can you show how to initialize things - how to start the mouse from scratch (Main())
I can't make it work yet...

kamarey:

When I paste your code into TurboCPP and run it the compiler does not find anything wrong, but says with a big green window in the middle of the screen (wow) that it can't find something like "TASM" (I think that's it).

TurboC++ has a file named tasm2xxx.exe (I can't remember the three last characters...), when renaming it to TASM.exe all stops (I did guess that would happen too, but did find out that it was the file TurboC++ was looking for).

Where can I get this TASM from? do you have it?
Could you send me an example over mail that does work on your DOS compiler, and I'll see if it works on mine? (please a simple one...)
ingvar@tjostheim.net - that is :)

Axter:

I am interested, yes - could you dig them up for me?

Thanks all!
Nietod
I'll accept the answer when I make it work, but to keep it open I'll reject it now.
To keep the question open I'll reject your answer, Nietod, but will accept it when I make your suggestion work.

All:
I'm in the military now, and we're out the next week - but I'll be back...
Kamarey:
It says exactly this:

-------------------------------------
             Error

Could not find executable:

             TASM

              -OK-
-------------------------------------

I guess you understand   :)
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Instead of using inline assembler
you must use int86 function
For example :
#include <stdio.h>
#include <conio.h>
#include <dos.h>
int function detect_mouse(void)
{
   int nButtons,mouseFound;
   union REGS regs;
   regs.h.ah = 0
   int86(0x33, &regs, &regs);
   mouseFound = regs.h.ah;
   nButtons = regs.h.bh;
   return mouseFound ?  nButtons : 0;
}
same other functions.
Alex
That shoudl make it all a bit easier.  You use the same procedures as in the code I posted.  i.e. you set the same registers (AX, BX CX etc), but inside that REGS union. the call int86, and obtain the values from the REGS struct again just as in the code I posted.
use dos interupts
by using the dos interupts and setting the specific service number u can always set the mouse active
the mouse service numbers r to be given to al register
the service numbers r
01 for initialising,
02 for show mouse cursor
03 for hide mouse cursor
....


dos interupt number is 33h which is to be set to ah register,

if u want i will let u know the full source code, if u want reply to me

>> use dos interupts
>> by using the dos interupts and setting the specific
>> service number u can always set the mouse active
>> the mouse service numbers r to be given to al register
>> the service numbers r
>> 01 for initialising,
>> 02 for show mouse cursor
>> 03 for hide mouse cursor
Great idea.  The rest of us so-called experts are pretty stupid for no suggesting that.

prakassl, You should read the _whole_ question before you post to it.
ok, I'm back from the military exercise. But still I have poor internet-access, so please excuse...

>Tasm is Turbo Assembler.   Its another borland product and it probably is part of Turbo C++.
OK, I have borlands turbo c++, but I could not find tasm. There is a file named TASM2MSG.EXE but the compiler doesn't find what I want (probably TASM.EXE). Do you know where I can get it from?

>Is there a good reason (it really has to be a great reason) why you are using Turbo C++.  Its not like its old.  It was ancient 10 years ago.
Well I wanted to learn C instead of VB, and that's all I have. Are there any freeware or do you have any other suggestions. I do have MSVC, but that's awful clumsy to start with - when not knowing very much...

ALL:
Thank you again!
I'll see if this makes things work, but I'm afraid I still am missing TASM...
be back!
TAsm must be in you CD, but you don't need it! Do you read my comment?
>>Are there any freeware or do you have any other suggestions
If you want Borland products, you can
(free!) download BC 5.5 from
http://community.borland.com/cpp/5.5
or
http://community.borland.com/article/0,1410,20633,00.html
 
AlexVirochovsky:
Yes I read your comment, indeed! Thanks a lot!
And thanks for the download site!!!

Have a nice day!
:)
>> I do have MSVC, but that's awful clumsy to start with -
>> when not knowing very much...
If you have VC 5 or 6 I strongly recommend you us that instead.   It is pretty close to standard C++ (far far closer than Turbo C++).  it has an excelent debugger and it has FANTASTIC on-line help.  Bot of these are invaluable to someone learning C++.
>>Have a nice day!
Thanks, now who can submit Question ?

PPC, I hope, our discassion helps you.
+ 1 URL:
http://www.snippets.org/MOUSE.C
http://www.snippets.org/MOUSE.H
all mouse stuff for TC in DOS
again?
AlexVirchovsky:
I downloaded the tc from that site, buit could not find TASM anywhere there or in what I downloaded, sorry for not accepting your question.

I made things work, though... and nietod gave me the answer most close to what I ended up with, so I think it's most fair for him to get the points this time.

Thanks all!

(good comments still help, though)
1.It is you right, but I can only quote
myself:
>>Instead of using inline assembler
you must use int86 function
>>http://www.snippets.org/MOUSE.H 
all mouse stuff for TC in DOS
(WITHOUT assembler!)
2.
>>could not find TASM
Of course can't , becouse it is NOT
free and you don't need it!
(see 1.)
3. And all nietodd code is unitile for you without assebler.
i installed turbo c++ in my windows 10
 but few second later mouse not clickable  
cursor moving every side but not click
plz solution