Mouse
The Hybrix virtual machine supports a virtual mouse input device. Mouse input is provided by moving your real mouse pointer over the Hybrix emulator screen. If you click on the emulator screen, the real mouse will become "captured" so that you can more accurately control the virtual machine. To release the capture, press the ESC key on your keyboard.
The IO::MOUSE location uses the same IO_HAND_CONTROLLER class as gamepad devices. However, the IO_HAND_CONTROLLER.X and IO_HAND_CONTROLLER.Y fields behave differently: For a gamepad, these return the absolute position of the analog stick or directional controller. For the mouse, they are motion counters. For example, X will increase when you move the mouse to the right, and decrease when you move to the left. The value of X does not correspond to an absolute position, and the counter will simply roll over if it reaches the PAIR limits of 32,767 or -32,768.
Reading the mouse
Suppose we want to show a mouse pointer on the screen, for example an arrow sprite. Its position will be SPRITE_X and SPRITE_Y. The program below illustrates how to read the IO::MOUSE counters, calculate their amount of change ("delta"), and then update the pointer location by adding the change.
MODULE EXAMPLE
VAR SPRITE_X: INT
VAR SPRITE_Y: INT
VAR _LAST_MOUSE_X: PAIR
VAR _LAST_MOUSE_Y: PAIR
FUNC UPDATE()
VAR MOUSE_X: PAIR
VAR MOUSE_Y: PAIR
# RAW VALUES
IO::MOUSE.X -> MOUSE_X
IO::MOUSE.Y -> MOUSE_Y
VAR DELTA_X: PAIR
VAR DELTA_Y: PAIR
# "DELTA" IS THE CHANGE SINCE LAST UPDATE()
TO_PAIR(MOUSE_X - EXAMPLE::_LAST_MOUSE_X) -> DELTA_X
TO_PAIR(MOUSE_Y - EXAMPLE::_LAST_MOUSE_Y) -> DELTA_Y
MOUSE_X -> EXAMPLE::_LAST_MOUSE_X
MOUSE_Y -> EXAMPLE::_LAST_MOUSE_Y
# APPLY THE CHANGE TO OUR MOUSE POINTER SPRITE
EXAMPLE::SPRITE_X + DELTA_X -> EXAMPLE::SPRITE_X
EXAMPLE::SPRITE_Y + DELTA_Y -> EXAMPLE::SPRITE_Y
# CONSTRAIN THE MOUSE POINTER SPRITE TO STAY ON THE SCREEN
IF EXAMPLE::SPRITE_X < 0
DO 0 -> EXAMPLE::SPRITE_X
IF EXAMPLE::SPRITE_X >= 320
DO 319 -> EXAMPLE::SPRITE_X
IF EXAMPLE::SPRITE_Y < 0
DO 0 -> EXAMPLE::SPRITE_Y
IF EXAMPLE::SPRITE_Y >= 224
DO 223 -> EXAMPLE::SPRITE_Y
END FUNC
END MODULE
I/O definitions
CLASS IO_HAND_CONTROLLER # SIZE 8
# 0 = DISABLED
# 1 = GAMEPAD
# 16 = MOUSE (X AND Y ARE ROLLING COUNTERS)
VAR KIND: BYTE
# +1 = A / MOUSE MAIN BUTTON
# +2 = B / MOUSE MIDDLE BUTTON
# +4 = C / MOUSE SECONDARY BUTTON
# +8 = D
VAR BUTTONS: BYTE
# GAMEPAD: (LEFT) -1023 .. +1023 (RIGHT)
VAR X: PAIR
# GAMEPAD: (UP) -1023 .. +1023 (DOWN)
VAR Y: PAIR
VAR RESERVED: PAIR
END CLASS
MODULE IO
. . .
INSET MOUSE: IO_HAND_CONTROLLER LOCATED AT $D0_0078
. . .
END MODULE