#ifndef GAME_UTIL_H #define GAME_UTIL_H #include "Driver.h" #define KEY_LEFT 1 #define KEY_RIGHT 2 #define KEY_UP 3 #define KEY_DOWN 0 typedef struct { int r; int c; } pos_t; static inline void uscrn_to_dscrn (uint8_t uscrn[side][side], uint8_t *dscrn) { int i, j; for (i = 0; i < side; i++) for (j = 0; j < side; j++) dscrn[i] |= ((!!uscrn[side - j - 1][side - i - 1]) << j); } static inline void board_to_scrn (uint8_t board[height][width], uint8_t *scrn) { int i, j; uint8_t uscrn1[side][side]; uint8_t uscrn2[side][side]; for (i = 0; i < height; i++) { for (j = 0; j < width; j++) { if (i < side) uscrn1[i][j] = board[i][j]; else uscrn2[i - side][j] = board[i][j]; } } uscrn_to_dscrn(uscrn1, scrn); uscrn_to_dscrn(uscrn2, scrn + side); } static inline void draw_board (uint8_t board[height][width]) { uint8_t scrn[16] = {0}; board_to_scrn(board, scrn); driver_draw(scrn); } static inline int inside (int i, int j) { return i >= 0 && i < height && j >= 0 && j < width; } static inline uint32_t rand (uint32_t *seed) { uint32_t bit = (((*seed) >> 0) ^ ((*seed) >> 2) ^ ((*seed) >> 3) ^ ((*seed) >> 5) ) & 1; return (*seed) = ((*seed) >> 1) | (bit << 15); } static inline int press_once (int *key_state, int key) { if (driver_getkey(key) && !key_state[key]) { key_state[key] = 1; return 1; } else if (key_state[key] && !driver_getkey(key)) { key_state[key] = 0; } return 0; } #endif