Using the Kempston Mouse

Discuss game and other programming topics not specifically covered in another forum

Moderator: Programming Moderators

User avatar
Piku
Posts: 6
Joined: Tue May 30, 2017 11:13 am

Using the Kempston Mouse

Postby Piku » Wed Apr 08, 2020 4:17 pm

I'm trying to make a game that makes use of the mouse.

So far I can read the mouse X,Y position and I get back two numbers between 0 and 255 that change as I move the mouse around.

How can I translate this into screen co-ords though?

Code: Select all

R Tape loading error, 0:1

User avatar
SevenFFF
Posts: 565
Joined: Mon Jun 05, 2017 5:30 pm
Location: USA

Re: Using the Kempston Mouse

Postby SevenFFF » Wed Apr 08, 2020 8:34 pm

The simple way to do it is remember your current screen coordinates, and remember the previous X and Y values you read last time. The difference between the new X and Y values and these is your delta, which you can add onto the current screen coordinates.

The only problem with doing it this way, is that if the user moves very fast in one direction, it will appear the same as moving backwards.

To get round this, you can calculate an acceleration between each time you read, then you can detect when you're moving fast enough to treat the fast movement as forward instead of backwards. This works because, to start genuinely moving backwards, you will deccelerate before stopping and accelerating in the opposite direction. I don't have any example code for this one at present.

Chris Cowley's mouse driver is a good example of the simpler way: https://spectrumcomputing.co.uk/index.p ... 6&id=14620. This is written in assembly language. Are you doing your game is asm or BASIC?
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins

User avatar
Timbucus
Posts: 287
Joined: Tue May 30, 2017 7:43 pm

Re: Using the Kempston Mouse

Postby Timbucus » Wed Apr 08, 2020 9:05 pm

There is a version of this driver adapted for the next in the SRC directory that does just what you need... If you are doing it in BASIC then you can just use the driver documented in the doc directory.
I'm Infinite Imaginations when not in work... PAWS for thought.

User avatar
Piku
Posts: 6
Joined: Tue May 30, 2017 11:13 am

Re: Using the Kempston Mouse

Postby Piku » Fri Apr 10, 2020 10:10 pm

Timbucus wrote:
Wed Apr 08, 2020 9:05 pm
There is a version of this driver adapted for the next in the SRC directory that does just what you need... If you are doing it in BASIC then you can just use the driver documented in the doc directory.
Ahh this is helpful. I was using BASIC, but I've started trying things in C using z88dk. Is there a way to access drivers from there, or are they only for use in BASIC?

Code: Select all

R Tape loading error, 0:1

User avatar
Timbucus
Posts: 287
Joined: Tue May 30, 2017 7:43 pm

Re: Using the Kempston Mouse

Postby Timbucus » Sat Apr 11, 2020 3:07 pm

Yes you can use the drivers from Z88DK - I have not tried the mouse but, it should work - this shows how for the ESP drivers https://gitlab.com/thesmog358/tbblue/-/ ... esp/term.c
I'm Infinite Imaginations when not in work... PAWS for thought.

User avatar
Piku
Posts: 6
Joined: Tue May 30, 2017 11:13 am

Re: Using the Kempston Mouse

Postby Piku » Sat Apr 11, 2020 10:43 pm

I've managed to get it working. Took a bit of confused head scratching and poking around in z88dk's source and the mouse driver source to figure out I needed to enable interrupts in my C program. As a bonus though, I understand a tiny bit more Z80 asm than before I started ;)

This bit of code makes it all work...

Code: Select all

// Put this somewhere
static struct esx_drvapi mouse;

// Then in your main loop
mouse.call.driver = 126;    // Mouse is driver 126
mouse.call.function = 1;    // function 1 to get X,Y pos
mouse.de = 0;
mouse.hl = 0;
 
if(esx_m_drvapi(&mouse)) {
        printf ("Mouse Open Driver error %u.\n",errno);
}
The X,Y position of the mouse are then in "mouse.de" and "mouse.hl". "mouse.bc" contains the bitmap of the buttons pressed.

What threw me for a while was needing to load the driver. The Next's manual doesn't make it obvious that "installing" a driver isn't a permanent thing, so I added it to my autoexec.bas.

The mouse is a bit jittery in CSpect and doesn't take kindly to being moved quickly, but it does the same kind of thing in the sprite editor too. I'll try it on my real machine when the PS/2 splitter I ordered turns up (I'm using the VGA connector, and the plug is too fat to let me plug my mouse's USB-PS/2 converter into the PS/2 port).

Code: Select all

R Tape loading error, 0:1

User avatar
Timbucus
Posts: 287
Joined: Tue May 30, 2017 7:43 pm

Re: Using the Kempston Mouse

Postby Timbucus » Sat Apr 11, 2020 10:57 pm

Nice work - I wonder if the jerkiness is due to the IRQ not being on a lot in the code. Is it smooth on the BASIC demo software (demos/mouse) - at least that proves that it is not your mouse?
I'm Infinite Imaginations when not in work... PAWS for thought.

User avatar
Piku
Posts: 6
Joined: Tue May 30, 2017 11:13 am

Re: Using the Kempston Mouse

Postby Piku » Sun Apr 12, 2020 9:34 am

It works OK in the demo with the attribute cursors - sprite ones didn't seem to show up. Although I did just clear a quantity of cat hair from out the sensor on my mouse(!) and that seems to have improved things - even though in Windows it didn't cause a problem. I'll try it on real hardware, the mouse I have for my Next is lower resolution so might behave differently.

Now, if I wiggle the mouse left-right quickly the "pointer" (a hardware sprite I'm just drawing at the mouse co-ords, and clamping it to the visible screen) sometimes vanishes. Doesn't happen if I move it up/down quickly. I might play with this later anyway, having the player in my game act like a mouse pointer doesn't "feel" right.

Code: Select all

R Tape loading error, 0:1


Who is online

Users browsing this forum: funkheld and 3 guests