From 7c0dc8ebb1810339dbefab1463ec367ac283f10a Mon Sep 17 00:00:00 2001 From: Jeremy Rand Date: Sun, 1 Nov 2020 22:50:10 -0500 Subject: [PATCH] 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. --- BuGS/game.s | 1 + BuGS/gamePlayer.s | 105 ++++++++++++++++++++++++++++++++++++++++++---- BuGS/globals.s | 2 + BuGS/main.c | 2 - BuGS/tiles.h | 1 - 5 files changed, 100 insertions(+), 11 deletions(-) diff --git a/BuGS/game.s b/BuGS/game.s index 325692a..f906ca2 100644 --- a/BuGS/game.s +++ b/BuGS/game.s @@ -208,6 +208,7 @@ nextWord anop startGame entry stz gameRunning stz numSegments + jsl initPlayer jsl spiderInitGame jsl levelInit jmp levelStart diff --git a/BuGS/gamePlayer.s b/BuGS/gamePlayer.s index df6ffb7..802da59 100644 --- a/BuGS/gamePlayer.s +++ b/BuGS/gamePlayer.s @@ -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 diff --git a/BuGS/globals.s b/BuGS/globals.s index 250007e..25a0669 100644 --- a/BuGS/globals.s +++ b/BuGS/globals.s @@ -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' diff --git a/BuGS/main.c b/BuGS/main.c index 4bf507a..7a695a1 100644 --- a/BuGS/main.c +++ b/BuGS/main.c @@ -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(); diff --git a/BuGS/tiles.h b/BuGS/tiles.h index 100ce2b..2b9dae6 100644 --- a/BuGS/tiles.h +++ b/BuGS/tiles.h @@ -126,7 +126,6 @@ extern word numInfieldMushrooms; /* API */ extern void initTiles(void); -extern void initPlayer(void); extern void addStartingMushrooms(void);