Skip to content

agdktunnel: Fix keyboard, controller and Back input, and allocation bugs - #137

Merged
natetrost merged 1 commit into
android:mainfrom
natetrost:main
Nov 24, 2026
Merged

natetrost merged 1 commit into
android:mainfrom
natetrost:main

Conversation

@natetrost

Copy link
Copy Markdown
Contributor

Fix input that never reached the game, and a few memory bugs in agdktunnel:

  • NativeEngine::KeyEventCallback returned false for every key, so keyboard input (WASD, Enter, Escape) was ignored since the BaseGameFramework migration. It now cooks key down/up events for the keys the game uses. Repeated down events from holding a key are ignored (a held Escape would toggle the pause menu, a held Enter click again).
  • Game controller buttons and the left stick's directional keys did nothing in menus or for key steering: TunnelEngine reports our key codes (OURKEY_*) in key events, but ProcessCookedEvent mapped them again as Android key codes, turning every one into OURKEY_UNKNOWN. Cooked key events now always carry our key codes (the keyboard path maps once). Making them work exposed bugs in the controller code, also fixed:
    • Dpad buttons and stick directions shared one key state, so a centered stick released a held dpad button in the same poll (dpad steering did nothing, and a held dpad button repeated every frame in menus). They now have separate states.
    • Stick directions also arrive as key events, which switched play from analog stick steering to digital key steering. Stick key events are marked, and PlayScene only takes them in its menus (scenes opt in with Scene::WantsStickDirectionKeys); their key ups only reach a scene that got the key down.
    • A controller disconnecting with a button or the stick held left the key stuck down (and the ship drifting). Its input is released.
    • PlayScene no longer starts key steering from a key pressed in a menu.
  • Back always left the game, even during play (where it should open the pause menu): with predictive back (targetSdk 37 on Android 16), Back isn't delivered as a key event at all. The Activity now has an OnBackPressedCallback, enabled only while the current scene uses Back (Scene::HandlesBack: play and dialogs), that queues back presses for the game thread. Elsewhere (main menu) Back keeps the system behavior and its predictive back animation. If a forwarded press isn't used after all, the game asks the Activity to do what Back does without it (not once it is finishing). A Back key event, where the platform still sends one, is handled the same way. Pending back presses are cleared when the engine is destroyed, not when the next one is created (the new Activity can forward one first).
  • SceneManager: once a scene change is pending, the current scene gets no more input (a second key could request another scene); a replaced pending scene is deleted instead of leaked, and so is one pending at shutdown.
  • SimpleGeom destroys its buffers in its destructor but was copyable, so a copy destroyed the buffers the original still used. Copying is deleted, and it can be moved instead (the moved-from geometry is left empty). Its single buffer constructor is explicit. Destroying one after the renderer is shut down no longer creates a new renderer.
  • DataLoaderStateMachine freed its new[] allocated save file name with delete (undefined behavior); it is a std::string now (also making the class safely copyable), and a null save path is handled. AsciiArtToGeom freed its working rows with delete; it uses delete[]. The save path is passed as a const char* from the FilesystemManager's string (it is copied; the cast that removed const is gone).
  • Texture file buffers: TextureManager::LoadTexture leaked the buffer when loading the asset failed. LoaderScene's texture loader (the path the game uses) leaked it when an async load couldn't start or failed, past 16 textures, or if the loader was destroyed before creating the textures, and never finished loading if an asset had size 0. Both check the allocation and that the size fits in size_t (32-bit ABIs).
  • CreateFromASTCFile read the third byte of the 24-bit ASTC width and height with << 8 instead of << 16 (this only affects textures of 65536 pixels or more on a side), and only checked the low byte of the depth, so a 3D texture with a depth of 257 was loaded as 2D.

Fix input that never reached the game, and a few memory bugs in agdktunnel:

- NativeEngine::KeyEventCallback returned false for every key, so keyboard
  input (WASD, Enter, Escape) was ignored since the BaseGameFramework
  migration. It now cooks key down/up events for the keys the game uses.
  Repeated down events from holding a key are ignored (a held Escape
  would toggle the pause menu, a held Enter click again).
- Game controller buttons and the left stick's directional keys did
  nothing in menus or for key steering: TunnelEngine reports our key
  codes (OURKEY_*) in key events, but ProcessCookedEvent mapped them again
  as Android key codes, turning every one into OURKEY_UNKNOWN. Cooked key
  events now always carry our key codes (the keyboard path maps once).
  Making them work exposed bugs in the controller code, also fixed:
  - Dpad buttons and stick directions shared one key state, so a centered
    stick released a held dpad button in the same poll (dpad steering did
    nothing, and a held dpad button repeated every frame in menus). They
    now have separate states.
  - Stick directions also arrive as key events, which switched play from
    analog stick steering to digital key steering. Stick key events are
    marked, and PlayScene only takes them in its menus (scenes opt in with
    Scene::WantsStickDirectionKeys); their key ups only reach a scene that
    got the key down.
  - A controller disconnecting with a button or the stick held left the
    key stuck down (and the ship drifting). Its input is released.
  - PlayScene no longer starts key steering from a key pressed in a menu.
- Back always left the game, even during play (where it should open the
  pause menu): with predictive back (targetSdk 37 on Android 16), Back
  isn't delivered as a key event at all. The Activity now has an
  OnBackPressedCallback, enabled only while the current scene uses Back
  (Scene::HandlesBack: play and dialogs), that queues back presses for the
  game thread. Elsewhere (main menu) Back keeps the system behavior and
  its predictive back animation. If a forwarded press isn't used after
  all, the game asks the Activity to do what Back does without it (not
  once it is finishing). A Back key event, where the platform still sends
  one, is handled the same way. Pending back presses are cleared when the
  engine is destroyed, not when the next one is created (the new
  Activity can forward one first).
- SceneManager: once a scene change is pending, the current scene gets no
  more input (a second key could request another scene); a replaced
  pending scene is deleted instead of leaked, and so is one pending at
  shutdown.
- SimpleGeom destroys its buffers in its destructor but was copyable, so a
  copy destroyed the buffers the original still used. Copying is deleted,
  and it can be moved instead (the moved-from geometry is left empty). Its
  single buffer constructor is explicit. Destroying one after the renderer
  is shut down no longer creates a new renderer.
- DataLoaderStateMachine freed its new[] allocated save file name with
  delete (undefined behavior); it is a std::string now (also making the
  class safely copyable), and a null save path is handled. AsciiArtToGeom
  freed its working rows with delete; it uses delete[]. The save path is
  passed as a const char* from the FilesystemManager's string (it is
  copied; the cast that removed const is gone).
- Texture file buffers: TextureManager::LoadTexture leaked the buffer when
  loading the asset failed. LoaderScene's texture loader (the path the game
  uses) leaked it when an async load couldn't start or failed, past 16
  textures, or if the loader was destroyed before creating the textures,
  and never finished loading if an asset had size 0. Both check the
  allocation and that the size fits in size_t (32-bit ABIs).
- CreateFromASTCFile read the third byte of the 24-bit ASTC width and
  height with << 8 instead of << 16 (this only affects textures of 65536
  pixels or more on a side), and only checked the low byte of the depth,
  so a 3D texture with a depth of 257 was loaded as 2D.

Tested on a Pixel 10 Pro (Vulkan):
- Keyboard: S on the main menu focuses Play and Enter starts the game
  (before: nothing), Escape opens and closes the pause menu, and a long
  pressed Escape opens it once.
- Back during play opens the pause menu, and again closes it, with the
  game staying in front (before: the game went to the background). Back
  on the main menu still leaves the game; the callback is only enabled
  once play starts.
- With a temporary hook simulating a controller through
  PollGameController (no controller attached): a held dpad button steers
  the ship (before the fix: released in the same poll), the stick steers
  with analog joystick steering and its key events are filtered during
  play, stick down moves the pause menu selection once, B opens and
  closes the pause menu, and disconnecting with dpad and stick held
  releases them.
- A temporary static_assert check: SimpleGeom isn't copy constructible or
  assignable, and is nothrow move constructible and assignable.
- Activity recreations (night mode toggles) run without crashes.
- Debug and release both build.
@natetrost
natetrost merged commit b813b4e into android:main Nov 24, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant