#include "Menu.h" #include "GameUtil.h" #include "string.h" #define MAX_PAGE 10 #define NR_ON_PAGE 3 static int curr_page; static int curr_select; static int page_count; static game_page_t pages[MAX_PAGE]; static uint8_t board[height][width]; static int key_state[4]; static int running; static void draw(); void menu_init() { curr_page = 0; curr_select = 0; page_count = 0; running = 0; memset(key_state, 0, sizeof(key_state)); memset(pages, 0, sizeof(pages)); memset(board, 0, sizeof(board)); } void menu_add_game (game_entry_t game) { int i; page_count = 0; for (i = 0; i < MAX_PAGE; i++) { page_count++; if (pages[i].count != NR_ON_PAGE) { pages[i].game[pages[i].count++] = game; return ; } } } void menu_update() { if (running) { int ret = pages[curr_page].game[curr_select].update_fn(); if (ret) { running = 0; } /* update the game and go away */ return ; } int right_key = press_once(key_state, KEY_RIGHT); int left_key = press_once(key_state, KEY_LEFT); int up_key = press_once(key_state, KEY_UP); int down_key = press_once(key_state, KEY_DOWN); if (left_key && curr_select == NR_ON_PAGE) { /* prev page */ if (curr_page > 0) curr_page--; } else if (right_key && curr_select == NR_ON_PAGE) { /* next page */ if (curr_page < page_count - 1) curr_page++; } else if (right_key) { /* start selection, next tick will run the game */ if (curr_select < pages[curr_page].count) { int ret = pages[curr_page].game[curr_select].init_fn(); if (!ret) { running = 1; return ; } } } else if (down_key) { /* go down in menu */ if (curr_select < NR_ON_PAGE) curr_select++; } else if (up_key) { /* go up in menu */ if (curr_select > 0) curr_select--; } draw(); } static void draw_ico (int row, int col, uint8_t ico[3][4]) { int i, j; for (i = row; i < row + 3; i++) for (j = col; j < col + 4; j++) board[i][j] = ico[i - row][j - col]; } static void draw() { int i; memset(board, 0, sizeof(board)); for (i = 0; i < pages[curr_page].count; i++) draw_ico(1 + 4 * i, 1, pages[curr_page].game[i].ico); // board[13][1] = board[13][2] = 1; // board[14][1] = board[14][2] = 1; board[2 + curr_select * 4][6] = 1; if (curr_select == NR_ON_PAGE) board[13][6] = 1; draw_board(board); }