From 271ec55924819ae27d4f8349cc8f60e60411aad0 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 16 Apr 2021 19:33:41 +0200 Subject: Add initial input handling --- src/main.c | 88 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 54 insertions(+), 34 deletions(-) diff --git a/src/main.c b/src/main.c index 6dcbf64..28239cc 100644 --- a/src/main.c +++ b/src/main.c @@ -302,51 +302,71 @@ u32 profile_stop() { return (TIMER_DATA_3 << 16) | TIMER_DATA_2; } -int main(void) { - DISP_CONTROL = DISP_MODE_3 | DISP_BG_2; - - draw_fill_rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, COLOR_GREY); - - int side = 60; - int line = 35; - int height = side * 0.5; - int x = SCREEN_WIDTH / 2 - height / 2; - int y = SCREEN_HEIGHT / 2; - - // Draw red triangle. - draw_line(x + height - 1, y - side / 2, x, y - 1, COLOR_RED); - draw_line(x + height - 1, y + side / 2, x, y + 1, COLOR_RED); - draw_line(x + height - 1, y - side / 2 + 1, x, y, COLOR_RED); - draw_line(x + height - 1, y + side / 2 - 1, x, y, COLOR_RED); +// +// Input handling. +// - // Draw white triangle. - draw_line(x, y - side / 2, x, y + side / 2, COLOR_WHITE); - draw_line(x + 1, y - side / 2, x + height, y - 1, COLOR_WHITE); - draw_line(x + 1, y + side / 2, x + height, y + 1, COLOR_WHITE); +// Memory address for key input register +#define KEY_INPUTS *((vu16*) 0x04000130) - // Draw white line at triangle tip. - draw_line(x + height, y - side / 2, x + height, y + side / 2, COLOR_WHITE); - draw_line(x + height + 1, y - side / 2, x + height + 1, y + side / 2, COLOR_WHITE); +// Alias for key pressing bits. +#define KEY_A (1 << 0) +#define KEY_B (1 << 1) +#define KEY_SELECT (1 << 2) +#define KEY_START (1 << 3) +#define KEY_RIGHT (1 << 4) +#define KEY_LEFT (1 << 5) +#define KEY_UP (1 << 6) +#define KEY_DOWN (1 << 7) +#define KEY_R (1 << 8) +#define KEY_L (1 << 9) - // Double triangle line. - draw_line(x - 1, y - side / 2, x - 1, y + side / 2, COLOR_WHITE); - draw_line(x + 1, y - side / 2 + 1, x + height, y, COLOR_WHITE); - draw_line(x + 1, y + side / 2 - 1, x + height, y, COLOR_WHITE); +// Check if the given key/button is currently pressed. +#define KEY_PRESSED(key) (~(KEY_INPUTS) & key) - // Draw white lines. - draw_line(x - line, y, x, y, COLOR_WHITE); - draw_line(x + height, y, x + height + line, y, COLOR_WHITE); - draw_line(x - line, y + 1, x, y + 1, COLOR_WHITE); - draw_line(x + height, y + 1, x + height + line, y + 1, COLOR_WHITE); +int main(void) { + DISP_CONTROL = DISP_MODE_3 | DISP_BG_2; - put_text(SCREEN_WIDTH / 2 - 8 * 10 / 2, 125, COLOR_RED, "0xbadd10de"); + draw_fill_rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, COLOR_GREY); int frame_counter = 0; while(true) { wait_vsync(); if (frame_counter++ > 30) { frame_counter = 0; - flip_page(); + } + + if (KEY_PRESSED(KEY_DOWN)) { + put_text(8, 8, COLOR_RED, "down"); + } + else if (KEY_PRESSED(KEY_UP)) { + put_text(8, 8, COLOR_RED, "up"); + } + else if (KEY_PRESSED(KEY_LEFT)) { + put_text(8, 8, COLOR_RED, "left"); + } + else if (KEY_PRESSED(KEY_RIGHT)) { + put_text(8, 8, COLOR_RED, "right"); + } + else if (KEY_PRESSED(KEY_A)) { + put_text(8, 8, COLOR_RED, "A"); + } + else if (KEY_PRESSED(KEY_B)) { + put_text(8, 8, COLOR_RED, "B"); + } + else if (KEY_PRESSED(KEY_START)) { + put_text(8, 8, COLOR_RED, "start"); + } + else if (KEY_PRESSED(KEY_SELECT)) { + put_text(8, 8, COLOR_RED, "select"); + } + else if (KEY_PRESSED(KEY_L)) { + put_text(8, 8, COLOR_RED, "L"); + } + else if (KEY_PRESSED(KEY_R)) { + put_text(8, 8, COLOR_RED, "R"); + } else { + draw_fill_rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, COLOR_GREY); } }; -- cgit v1.2.1