From cc28b4629d4106f51d3a3d8da1d5f9c57ae3aa11 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Mon, 18 Jan 2021 18:00:35 +0100 Subject: [PATCH] Use special crt routines for printing --- src/main.c | 157 ++++++++++++++++++++++------------------------------- 1 file changed, 66 insertions(+), 91 deletions(-) diff --git a/src/main.c b/src/main.c index dda480f..326a1b8 100644 --- a/src/main.c +++ b/src/main.c @@ -4,7 +4,7 @@ #define WIDTH 12 #define HEIGHT 18 -#define SPEED 20 +#define SPEED 4 const char* tetromino[7]; @@ -15,6 +15,9 @@ uint16_t score = 0; uint8_t cleared[HEIGHT-1] = {0}; +__sfr __at (0x1F) status; +__sfr __at (0x1E) key; + void construct_shapes() { tetromino[0] = "..X." "..X." @@ -97,17 +100,67 @@ uint8_t does_piece_fit(uint8_t shape, uint8_t rotation, int8_t px, uint8_t py) { return 1; } +void draw_part(uint8_t i) { + if (i == 9) { + put_string_fast("\033[0m"); + } else if (i == 8) { + put_string_fast("\033[33m"); + } else if (i > 0) { + put_string_fast("\033[9"); + put_char_fast('0'+i); + put_char_fast('m'); + } + + switch (i) { + case 0: + put_char_fast(' '); + break; + + case 8: + put_char_fast(0xCD); + break; + + case 9: + put_char_fast(0xB0); + break; + + default: + put_char_fast(0xDB); + break; + } +} + + uint8_t next_shape = 0; uint8_t current_shape = 0; uint8_t current_rotation = 0; int8_t current_x = WIDTH/2-2; uint8_t current_y = 0; +inline void draw_next_shape() { + // Draw next part + for (uint8_t y = 0; y < 4; ++y) { + put_string_fast("\033["); + put_char_fast('3' + y); + put_string_fast(";15H"); + + for (uint8_t x = 0; x < 4; ++x) { + if (tetromino[next_shape][rotate(x, y, 0)] == 'X') { + draw_part(next_shape + 1); + } else { + draw_part(0); + } + } + } +} + void new_shape() { current_shape = next_shape; do { next_shape = random() & 0x07; } while (next_shape == 7); + + draw_next_shape(); } void render() { @@ -128,54 +181,9 @@ void render() { } } -__sfr __at (0x02) cout; -void draw_part(uint8_t i) { - if (i == 9) { - cout = '\033'; - cout = '['; - cout = '0'; - cout = 'm'; - } else if (i == 8) { - cout = '\033'; - cout = '['; - cout = '3'; - cout = '3'; - cout = 'm'; - } else if (i > 0) { - cout = '\033'; - cout = '['; - cout = '9'; - cout = 48+i; - cout = 'm'; - } - - switch (i) { - case 0: - cout = ' '; - break; - - case 8: - cout = 0xCD; - break; - - case 9: - cout = 0xB0; - break; - - default: - cout = 0xDB; - break; - } -} - void draw() { // Move to the start of the field - cout = '\033'; - cout = '['; - cout = '2'; - cout = ';'; - cout = '2'; - cout = 'H'; + put_string_fast("\033[2;2H"); // Draw the screen for (uint8_t y = 0; y < HEIGHT; ++y) { @@ -183,56 +191,23 @@ void draw() { uint8_t i = screen[y*WIDTH + x]; draw_part(i); } - cout = '\n'; - cout = '\r'; - cout = ' '; + put_string_fast("\n\r "); } +} - // Draw next part - for (uint8_t y = 0; y < 4; ++y) { - cout = '\033'; - cout = '['; - cout = '3' + y; - cout = ';'; - cout = '1'; - cout = '5'; - cout = 'H'; - - for (uint8_t x = 0; x < 4; ++x) { - if (tetromino[next_shape][rotate(x, y, 0)] == 'X') { - draw_part(next_shape + 1); - } else { - draw_part(0); - } - } - } - - cout = '\033'; - cout = '['; - cout = '8'; - cout = ';'; - cout = '1'; - cout = '5'; - cout = 'H'; +inline void draw_score() { + put_string_fast("\033[8;15H"); + put_string_fast("\033[0m"); // Print the score uint32_t r = uint16_to_bcd(score); for (uint8_t j = 0; j < 6; ++j) { - cout = '0' + ((r >> (20 - j*4)) & 0xF); + put_char_fast('0' + ((r >> (20 - j*4)) & 0xF)); } - - - cout = '\033'; - cout = '['; - cout = '0'; - cout = 'm'; } uint8_t counter = SPEED; -__sfr __at (0x1F) status; -__sfr __at (0x1E) key; -__sfr __at (0xFF) debug; uint8_t loop() { // Check if a key has been pressed uint8_t bonus = 0; @@ -321,14 +296,11 @@ uint8_t loop() { if (cc) { score += 10*cc*cc + bonus; - } - - if (cc) { - debug = (score >> 8) & 0xFF; - debug = score & 0xFF; + draw_score(); } new_shape(); + draw_next_shape(); current_y = 0; current_x = WIDTH/2-2; current_rotation = 0; @@ -379,6 +351,9 @@ void main() { // Clear the screen put_string("\033[2J"); + draw_next_shape(); + draw_score(); + // @todo We need to be able to turn the cursor on/off while (loop());