Skip to navigation

Keyboard: GetMenuOption

Name: GetMenuOption [Show more] Type: Subroutine Category: Keyboard Summary: Scan the keyboard for a menu entry number, highlight the choice, show the SPACE bar message and return the choice number
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * MainLoop (Part 1 of 6) calls GetMenuOption * MainLoop (Part 2 of 6) calls GetMenuOption * MainLoop (Part 3 of 6) calls GetMenuOption * MainLoop (Part 5 of 6) calls GetMenuOption

Arguments: X The number of entries in the menu
Returns: X The chosen option, zero-based (so the first option is 0, the second option is 1, and so on)
.GetMenuOption LDY #0 \ Set W = 0, to indicate that we have not yet chosen an STY W \ option from the menu STX U \ Store the number of entries in U .mopt1 JSR CheckRestartKeys \ Check whether the restart keys are being pressed, and \ if they are, restart the game (the restart keys are \ SHIFT and right arrow) LDY U \ We now loop through each valid menu option for this \ menu and check whether the relevant key is being \ pressed, so we set a loop counter in U to start from \ the menu size and loop down to zero .mopt2 STY V \ Store the loop counter in V so we can retrieve it \ below IF _ACORNSOFT OR _4TRACKS LDX menuKeys,Y \ Fetch the key number for menu option Y ELIF _SUPERIOR OR _REVSPLUS LDX menuKeysSup,Y \ Fetch the key number for menu option Y ENDIF JSR ScanKeyboard \ Scan the keyboard to see if this key is being pressed BEQ mopt3 \ If this key is being pressed, jump to mopt3 LDY V \ Retrieve the value of the loop counter DEY \ Decrement the loop counter to scan for the next menu \ option BPL mopt2 \ Loop back to check the key for the next option BMI mopt1 \ Loop back to mopt1 to keep checking through the option \ keys (this BMI is effectively a JMP as we just passed \ through a BPL) .mopt3 LDY V \ Set Y to the menu option that was chosen BNE mopt4 \ If Y is non-zero, jump to mopt4 to process the choice \ If we get here, SPACE was pressed LDA W \ If W = 0 then we have not yet chosen an option from BEQ mopt1 \ the menu, so jump back to mopt1 to keep checking for \ key presses, as SPACE is only a valid key press when \ we have chosen an option \ If we get here then we have already chosen an option \ from the menu, and SPACE has been pressed LDA #152 \ Poke the mode 7 conceal character into screen memory, STA row24_column5 \ to hide row 24 from column 5 onwards, i.e. hide the \ "PRESS SPACE BAR TO CONTINUE" prompt LDX G \ Set X = G - 1, to return as the zero-based choice DEX \ number RTS \ Return from the subroutine .mopt4 STY G \ Set G to the number of the choice we just made LDA W \ If W is non-zero, jump to mopt5 to skip the following BNE mopt5 \ three instructions LDX #30 \ Set X = 30 to pass to PrintToken below STX W \ Set W = 30, so W is now non-zero and denotes that we \ have made a choice JSR PrintToken \ Print token 30 ("PRESS SPACE BAR TO CONTINUE" in cyan \ at column 5, row 24) .mopt5 \ We now work our way through the menu, setting each \ entry's background colour according to the choice made \ (the chosen entry is set to red, while other entries \ are set to blue) LDX #0 \ Set an offset counter in X to step through the screen \ address of the on-screen number for each menu entry, \ starting at an offset of 0 (the offset is added to \ row18_column5 in the loop below) LDY #1 \ Set a counter in Y to count through the menu entries .mopt6 LDA #132 \ Set A to the mode 7 control code for blue, to set as \ the background colour for the unselected menu entries CPY G \ If Y <> G then this is not the chosen entry, so skip BNE mopt7 \ the following instruction to leave A as blue LDA #129 \ Set A to the mode 7 control code for red, to set as \ the background colour for the selected menu entry .mopt7 STA row18_column5,X \ Poke the colour in A into screen memory at offset X \ from column 5 on row 18, which is the screen address \ of the number for the first menu entry (so this sets \ the background colour of the current entry to A) TXA \ Set X = X + 80 CLC \ ADC #80 \ so X now points to the next menu entry, as 80 is two TAX \ lines of mode 7 characters, and the menu entries are \ shown on every other line INY \ Increment the option counter in Y CPY U \ If Y <= U then loop back to set the background colour BCC mopt6 \ for the next option, until we have done all of them BEQ mopt6 BNE mopt1 \ Jump back to mopt1 to keep checking for key presses, \ so we can change the option, or press SPACE to lock in \ our choice (this BNE is effectively a JMP as we just \ passed through a BEQ)