Start section on controls

This commit is contained in:
Gwilym Kuiper 2022-10-13 20:52:11 +01:00
parent 2705c58a72
commit e666ebbd05
2 changed files with 24 additions and 0 deletions

View file

@ -11,3 +11,4 @@
- [Learn agb part I - pong](./pong/01_introduction.md) - [Learn agb part I - pong](./pong/01_introduction.md)
- [The Gba struct](./pong/02_the_gba_struct.md) - [The Gba struct](./pong/02_the_gba_struct.md)
- [Sprites](./pong/03_sprites.md) - [Sprites](./pong/03_sprites.md)
- [Controls](./pong/04_controls.md)

View file

@ -0,0 +1,23 @@
# Controls
In this section, we'll make the ball that we displayed in the last section move by pressing the D-Pad.
# The GBA controls
The GBA has 10 buttons we can read the state of, and this is the only way a player can directly control the game.
They are the 4 directions on the D-Pad, A, B, Start, Select, and the L and R triggers.
# Reading the button state
There are two ways to capture the button state in **agb**, interrupts and polling.
In most games, you will want to use polling, so that is what we will use now.
Interrupts will be covered in a later chapter.
To add button control to our game, we will need a [ButtonController](https://docs.rs/agb/latest/agb/input/struct.ButtonController.html).
Add this near the top of your main function:
```rust
let mut input = agb::input::ButtonController::new();
```
The button controller is not part of the `Gba` struct because it only allows for reading and not writing so does not need to be controlled by the borrow checker.