Changes

Kempston Mouse

59 bytes added, 07:50, 10 August 2023
/* Get mouse position on screen */
Here is the algorithm in pseudo-code:
# initMouse init_mouse initializes variables and centers the mouse pointer on screen def initMouseinit_mouse(): maxX max_x = 639 maxY max_y = 399
# centering the mouse pointer on the screen
virtualX virtual_x = maxX max_x >> 1 # maxXmax_x/2 virtualY virtual_y = maxY max_y >> 1 # maxYmax_y/2
# store raw mouse values
oldX old_x = inp(&FBEE) oldY old_y = inp(&FBEF)
# get mouse pointer position
refreshMouserefresh_mouse()
# refreshMouse refresh_mouse has to be called before you redraw the mouse pointer (and ideally on every frame) def refreshMouserefresh_mouse():
# get raw mouse values
rawX raw_x = inp(&FBEE) rawY raw_y = inp(&FBEF)
# get the relative mouse displacement since last call
deltaX delta_x = rawX raw_x - oldXold_x deltaY delta_y = rawY raw_y - oldYold_y
# store raw mouse values
oldX old_x = rawXraw_x oldY old_y = rawYraw_y
# calculate new unclipped virtual position
virtualX virtual_x = virtualX virtual_x + deltaXdelta_x virtualY virtual_y = virtualY virtual_y - deltaY delta_y # Kempston mouse Y-axis is inverted compared to screen coordinates!
# perform clipping
if virtualX virtual_x < 0: virtualX virtual_x = 0 elif virtualX virtual_x > maxXmax_x: virtualX virtual_x = maxXmax_x if virtualY virtual_y < 0: virtualY virtual_y = 0 elif virtualY virtual_y > maxYmax_y: virtualY virtual_y = maxYmax_y
# now we translate position from the virtual screen to the current CPC screen mode
mouseY mouse_y = virtualY virtual_y >> 1 # virtualYvirtual_y/2 if graphicsMode graphics_mode == 2: mouseX mouse_x = virtualXvirtual_x elif graphicsMode graphics_mode == 1: mouseX mouse_x = virtualX virtual_x >> 1 # virtualXvirtual_x/2 elif graphicsMode graphics_mode == 0: mouseX mouse_x = virtualX virtual_x >> 2 # virtualXvirtual_x/4
== Manual ==
4,607
edits