Replace the call to the Toolbox with a raw read of the mouse from ADB registers to try to minimize the cost of mouse polling in the game loop.

This commit is contained in:
Jeremy Rand 2020-11-01 22:50:10 -05:00
parent 4e500c6f38
commit 7c0dc8ebb1
5 changed files with 100 additions and 11 deletions

View File

@ -208,6 +208,7 @@ nextWord anop
startGame entry
stz gameRunning
stz numSegments
jsl initPlayer
jsl spiderInitGame
jsl levelInit
jmp levelStart

View File

@ -13,18 +13,103 @@
gamePlayer start
using globalData
PLAYER_TILES_HIGH equ 7
MOUSE_MAX_X equ GAME_NUM_TILES_WIDE*TILE_PIXEL_WIDTH-TILE_PIXEL_WIDTH
MOUSE_MAX_Y equ GAME_NUM_TILES_TALL*TILE_PIXEL_HEIGHT-TILE_PIXEL_HEIGHT
STARTING_MOUSE_X equ GAME_NUM_TILES_WIDE*TILE_PIXEL_WIDTH/2
STARTING_MOUSE_Y equ MOUSE_MAX_Y
initPlayer entry
lda #STARTING_MOUSE_X
sta mouseX
lda #STARTING_MOUSE_Y
sta mouseY
rtl
updatePlayer entry
lda gameRunning
beq updatePlayer_gameRunning
rtl
updatePlayer_gameRunning anop
ldx #0
ldy #0
; This code for reading the mouse data is based on some code which John Brooks helpfully provided, although I did things
; quite differently because I have different needs. But this is about as low cost as can be made for reading the position
; of the mouse.
short i,m
phb
lda #$e0
pha
pha
pha
~ReadMouse
pla
pla
pla
plb
lda MOUSE_STATUS
bpl updatePlayer_noMousePoll
and #2
bne updatePlayer_mouseYOnly
ldx MOUSE_DATA_REG
updatePlayer_mouseYOnly anop
ldy MOUSE_DATA_REG
updatePlayer_doneMousePoll anop
plb
long i,m
bra updatePlayer_handleDeltas
updatePlayer_noMousePoll anop
plb
long i,m
jmp updatePlayer_skipDeltas
updatePlayer_handleDeltas anop
; The X register has the deltaX
txa
bit #$40
bne updatePlayer_negX
and #$3f
clc
adc mouseX
cmp #MOUSE_MAX_X
blt updatePlayer_doneX
lda #MOUSE_MAX_X-1
bra updatePlayer_doneX
updatePlayer_negX anop
ora #$ffc0
clc
adc mouseX
bpl updatePlayer_doneX
lda #0
updatePlayer_doneX anop
sta mouseX
; The Y register has the deltaY
tya
bit #$40
bne updatePlayer_negY
and #$3f
clc
adc mouseY
cmp #MOUSE_MAX_Y
blt updatePlayer_doneY
lda #MOUSE_MAX_Y-1
bra updatePlayer_doneY
updatePlayer_negY anop
ora #$ffc0
clc
adc mouseY
bpl updatePlayer_doneY
lda #0
updatePlayer_doneY anop
sta mouseY
; The Y register also has a bit in it which indicates whether the
; mouse is down or not.
tya
and #$0080
sta mouseDown
updatePlayer_skipDeltas anop
lda mouseX
lsr a
bcs updatePlayer_shift
adc #$9834
@ -33,7 +118,7 @@ updatePlayer_gameRunning anop
bra updatePlayer_dirty
updatePlayer_shift anop
adc #$9834
adc #$9833
tay
jsl drawShipShift
bra updatePlayer_dirty
@ -66,5 +151,9 @@ updatePlayer_dirty anop
sta tileDirty+1246
sta tileDirty+1248
rtl
mouseX dc i2'0'
mouseY dc i2'0'
mouseDown dc i2'0'
end

View File

@ -142,6 +142,8 @@ NEW_VIDEO_REGISTER gequ $e0c029
BORDER_COLOUR_REGISTER gequ $e0c034
STATE_REGISTER gequ $e1c068
VERTICAL_COUNTER gequ $e0c02e
MOUSE_STATUS gequ $c027
MOUSE_DATA_REG gequ $c024
gameRunning dc i2'1'

View File

@ -62,8 +62,6 @@ int main(void)
InitMouse(0);
SetMouse(transparent);
ClampMouse(0, (GAME_NUM_TILES_WIDE - 1) * 8, 0, 8 * 5);
PosMouse(GAME_NUM_TILES_WIDE * 4, 8 * 5);
initTiles();
initNonGameTiles();

View File

@ -126,7 +126,6 @@ extern word numInfieldMushrooms;
/* API */
extern void initTiles(void);
extern void initPlayer(void);
extern void addStartingMushrooms(void);