million-perfect-letters/src/ui.main.menu.a
4am 74ee53e015 demo [WIP] and sound stuff
sound routines now take list of acceptable keys to exit, with wildcard support

also lots of refactoring
2020-09-25 22:59:55 -04:00

197 lines
5.6 KiB
Plaintext

;license:MIT
;(c) 2020 by 4am
;
; main menu
;
; Public functions:
; - MainMenuEventLoop
;
; Codes returned by event handlers
kStayOnMainMenu = 0
kRequestedQuit = 1
kStartPlaying = 2
kMainMenuKeys ; must keep in sync with kMainMenuKeyHandlersLo/Hi arrays
; except for last byte ($00) which doesn't need an associated handler
!byte $8D ; Return
!byte $C3 ; C
!byte $E3 ; c
!byte $D3 ; S
!byte $F3 ; s
!byte $C4 ; D
!byte $E4 ; d
!byte $D1 ; Q
!byte $F1 ; q
!byte $9B ; Esc
!byte $00
kMainMenuKeyHandlersLo
!byte <MainMenuEventReturn
!byte <MainMenuEventC
!byte <MainMenuEventC
!byte <MainMenuEventS
!byte <MainMenuEventS
!byte <MainMenuEventD
!byte <MainMenuEventD
!byte <MainMenuEventQ
!byte <MainMenuEventQ
!byte <MainMenuEventQ
kMainMenuKeyHandlersHi
!byte >MainMenuEventReturn
!byte >MainMenuEventC
!byte >MainMenuEventC
!byte >MainMenuEventS
!byte >MainMenuEventS
!byte >MainMenuEventD
!byte >MainMenuEventD
!byte >MainMenuEventQ
!byte >MainMenuEventQ
!byte >MainMenuEventQ
;------------------------------------------------------------------------------
; MainMenuEventLoop
; display main menu
;
; in: C clear if screen is already cleared and title is already drawn
; (will happen if animated title screen runs to completion)
; C set if full screen clear & redraw is required
; (will happen if key is pressed during title screen, or
; if user returns to main menu from play or any other screen)
; out: C clear if user selected a world to play, and
; A = world ID
; C set if user selected quit
;------------------------------------------------------------------------------
MainMenuEventLoop
bcc +
jsr DrawMainMenuTitle
+ jsr DrawMainMenuText
bit CLEARKBD
LoopMusic
+LDADDR kMainMenuKeys
+ST16 $EE
+LDADDR MainMenuMusic
jsr PlaySound
bpl LoopMusic ; music played to completion, so start over
bit CLEARKBD
+ sta gLastKeyPressed
ldx #0
- ldy kMainMenuKeys, x
cpy gLastKeyPressed
beq +
inx
bne -
; /!\ execution never reaches here
brk
+ lda kMainMenuKeyHandlersLo, x
sta @j+1
lda kMainMenuKeyHandlersHi, x
sta @j+2
@j jsr $FDFD ; SMC
beq MainMenuEventLoop
rts
MainMenuEventReturn
jsr SelectWorld
bcs + ; in next event handler
ldx #kStartPlaying ; Z=0 so caller will exit main menu event loop
; C=0 so grand-caller will start playing
; A=world ID (from SelectWorld)
rts
MainMenuEventD
jsr RunDemo
bcs + ; always branches
MainMenuEventC
jsr AboutPage
+ ldx #kStayOnMainMenu ; Z=1 so caller will stay in main menu event loop
sec ; C=1 so caller will do a full screen refresh
rts
MainMenuEventS
lda gSoundPref
eor #$01
sta gSoundPref
jsr SavePrefs
jsr ReinitSoundAfterPrefChange
ldx #kStayOnMainMenu ; Z=1 so caller will stay in main menu event loop
clc ; C=0 so caller will only do a partial screen refresh
rts
MainMenuEventQ
ldx #kRequestedQuit ; Z=0 so caller will exit main menu event loop
sec ; C=1 so grand-caller will call MLI quit
rts
DrawMainMenuTitle
jsr Home
lda #1
sta GlobalLeftMargin
ldy #3
- ldx #1
lda sTitleLine1-2, y
jsr DrawLargeCharacter
inx
lda sTitleLine2-2, y
jsr DrawLargeCharacter
inx
lda sTitleLine3-2, y
jsr DrawLargeCharacter
iny
cpy #$0A
bne -
rts
DrawMainMenuText
+PRINT_AT sAsterisk, 2, 30
+PRINT_AT sVersion, 11, 26
+PRINT_AT sMainMenuPlay, 15, 10
+PRINT_AT sMainMenuSound, 16, 10
lda gSoundPref
beq @theSoundOfSilence
+LDADDR sMainMenuSoundOn
bne + ; always branches
@theSoundOfSilence
+LDADDR sMainMenuSoundOff
+ jsr DrawHeavySilkString
+PRINT_AT sMainMenuAbout, 17, 10
+PRINT_AT sMainMenuDemo, 18, 10
+PRINT_AT sMainMenuQuit, 19, 10
+PRINT_AT sMainMenuDisclaimer, 23, 0
rts
sAsterisk
!byte 1
!byte "*"
sVersion
!byte 4
!raw "V0.4"
sMainMenuPlay
!byte 20
!raw "RETURN.....PLAY GAME"
sMainMenuSound
!byte 18
!raw " S.....SOUND ("
sMainMenuSoundOn
!byte 4
!raw "ON) "
sMainMenuSoundOff
!byte 4
!raw "OFF)"
sMainMenuAbout
!byte 18
!raw " C.....CREDITS"
sMainMenuDemo
!byte 15
!raw " D.....DEMO"
sMainMenuQuit
!byte 15
!raw " Q.....QUIT"
sMainMenuDisclaimer
!byte 40
!raw "* NOT GUARANTEED, ACTUAL COUNT MAY VARY."