Files
snakePlus/source/game_input.cpp
T
phillip df9b859601 Apply clang-tidy style fixes to audio and input code
Use explicit != 0U comparisons for bitmask checks, static_cast instead
of C-style casts, <cstdint>/<cstdio> instead of the C headers, and
puts() instead of iprintf() for plain strings.
2026-09-03 22:21:51 +02:00

92 lines
2.6 KiB
C++

#include "game_input.h"
#include <bitset>
#include <cstdint>
#include <cstdio>
bool handle_input_frame(InputContext &ctx) {
scanKeys();
const uint32_t keys = keysDown();
if ((keys != 0U) && *ctx.internals) {
consoleSelect(ctx.topScreen);
std::string key_str = "\x1b[6;0Hkeys = " + std::bitset<16>(keys).to_string();
puts(key_str.c_str());
}
if (((keys & KEY_START) != 0U) && ((keys & KEY_L) != 0U)) {
return false;
}
if ((keys != 0U) && ((keys & KEY_SELECT) != 0U)) {
*ctx.internals = !(*ctx.internals);
if (*ctx.internals) {
setBackdropColor(RGB15(0, 0, 7));
} else {
setBackdropColor(RGB15(0, 0, 0));
consoleSelect(ctx.topScreen);
consoleClear();
}
}
if (!ctx.state->paused && !ctx.state->gameOver) {
if ((keys != 0U) && ((keys & KEY_UP) != 0U)) {
ctx.state->snakeDirection = 0;
draw_game(*ctx.state, *ctx.renderAssets);
}
if ((keys != 0U) && ((keys & KEY_RIGHT) != 0U)) {
ctx.state->snakeDirection = 1;
draw_game(*ctx.state, *ctx.renderAssets);
}
if ((keys != 0U) && ((keys & KEY_DOWN) != 0U)) {
ctx.state->snakeDirection = 2;
draw_game(*ctx.state, *ctx.renderAssets);
}
if ((keys != 0U) && ((keys & KEY_LEFT) != 0U)) {
ctx.state->snakeDirection = 3;
draw_game(*ctx.state, *ctx.renderAssets);
}
}
if (ctx.state->paused) {
if ((keys != 0U) && ((keys & KEY_X) != 0U)) {
audio_toggle_music(*ctx.audio);
}
if ((keys != 0U) && ((keys & KEY_Y) != 0U)) {
ctx.state->hardMode = !ctx.state->hardMode;
ctx.init_game();
draw_game(*ctx.state, *ctx.renderAssets);
}
}
if ((keys != 0U) && ((keys & KEY_R) != 0U)) {
lcdSwap();
}
if ((keys != 0U) && ((keys & KEY_START) != 0U)) {
if (!ctx.state->gameOver) {
ctx.state->paused = !ctx.state->paused;
if (ctx.state->paused) {
consoleSelect(ctx.topScreen);
audio_play_pause(*ctx.audio);
audio_pause_music_if_enabled(*ctx.audio);
puts("\x1b[11;12HPaused");
} else {
audio_resume_music_if_enabled(*ctx.audio);
audio_play_unpause(*ctx.audio);
consoleSelect(ctx.topScreen);
consoleClear();
}
} else {
ctx.init_game();
}
}
return true;
}