From 38a40eb2e43b29195edbfae044f5d3c59ed3c693 Mon Sep 17 00:00:00 2001 From: hkz Date: Fri, 18 Jul 2025 09:12:17 +0200 Subject: [PATCH] Fix logic --- src/game_graphics.c | 21 ++++++++++++++++++++- src/game_graphics.h | 1 + src/game_logic.c | 17 ++++++++--------- src/main.c | 11 ++++++++--- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/game_graphics.c b/src/game_graphics.c index 2c1d188..13f028b 100644 --- a/src/game_graphics.c +++ b/src/game_graphics.c @@ -8,6 +8,7 @@ #include "line_data.h" #include "game_logic.h" #include "tiles.h" +#include "monitor_subroutines.h" #define SCREEN_WIDTH_B 40 #define GRID_CELL_SIDE 35 @@ -45,13 +46,31 @@ void draw_game_background(void) { } +// This will draw directly to the front buffer +void ddraw_single_tile(uint8_t offset) { + uint8_t* grid = get_front_grid(); + if(!grid[offset]) return; // The tile is not there, nothing to do + + const uint8_t *tile_data = tiles + (TILE_WIDTH_BYTES * TILE_HEIGHT * (grid[offset] - 1)); + + uint8_t col = offset % GRID_SIDE; + uint8_t row = offset / GRID_SIDE; + + uint8_t delay = 0xFF; + + for(uint8_t h = 0; h < TILE_HEIGHT; h++) { + memcpy(front_buf + line_offset_map[TOP_OFFSET + 7 + (row * GRID_CELL_SIDE) + h] + LEFT_OFFSET_B + 1 + (col * GRID_CELL_SIDE/7), + tile_data + (TILE_WIDTH_BYTES * h), TILE_WIDTH_BYTES); + WAIT(48); + } +} + void draw_tiles(void) { uint8_t* grid = get_front_grid(); // Clear the grid so we'll be able to draw the boxes on clear_box(GRID_SIDE * (GRID_CELL_SIDE/7) + 1, (GRID_SIDE * GRID_CELL_SIDE) + 6, LEFT_OFFSET_B, TOP_OFFSET + 1, back_buf); - for (uint8_t tile = 0; tile < GRID_SIDE * GRID_SIDE; tile++) { if(grid[tile]) { const uint8_t *tile_data = tiles + (TILE_WIDTH_BYTES * TILE_HEIGHT * (grid[tile] - 1)); diff --git a/src/game_graphics.h b/src/game_graphics.h index 18d4653..fbcafed 100644 --- a/src/game_graphics.h +++ b/src/game_graphics.h @@ -5,5 +5,6 @@ void draw_game_background(void); void draw_tiles(void); +void ddraw_single_tile(uint8_t offset); #endif /* _GAME_GRAPHICS_HEADER_ */ diff --git a/src/game_logic.c b/src/game_logic.c index 3b532b0..5316a74 100644 --- a/src/game_logic.c +++ b/src/game_logic.c @@ -35,19 +35,18 @@ uint8_t *get_front_grid(void) { uint8_t add_random_tile(void) { uint16_t rand = lfsr_update(); - uint8_t tile_val = (rand & 0x000F) > 0x0D ? 2 : 1; // 90% chance of a tile of type 1 (a "2"), 10% of a type 2 (a "4") + uint8_t tile_val = (rand & 0x000F) > 0x0D ? 2 : 1; // ~90% chance of a tile of type 1 (a "2"), 10% of a type 2 (a "4") uint8_t rand_offset = rand >> 8; - uint8_t tile_placed = 0; - for (int8_t offset = 0; offset < GRID_SIDE * GRID_SIDE; offset++) { - if (!front_grid[(offset + rand_offset)%(GRID_SIDE * GRID_SIDE)]) { - front_grid[(offset + rand_offset)%(GRID_SIDE * GRID_SIDE)] = tile_val; - tile_placed = 1; - break; + for (uint8_t offset = 0; offset < GRID_SIDE * GRID_SIDE; offset++) { + uint8_t rand_off = (offset + rand_offset)%(GRID_SIDE * GRID_SIDE); + if (!front_grid[rand_off]) { + front_grid[rand_off] = tile_val; + return rand_off + 1; } } - return tile_placed; // Return 0 if we were not able to place the tile + return 0; // Return 0 if we were not able to place the tile, else we return (offset + 1) to indicate where the tile was placed } game_state step_game(step_direction dir) { @@ -123,7 +122,7 @@ game_state step_game(step_direction dir) { state.score += ((uint16_t)1) << back_grid[current_offset]; state.done += back_grid[current_offset] == 11 ? 1 : 0; break; - } + } else if (front_grid[sub_col_offset]) break; } } } diff --git a/src/main.c b/src/main.c index 76158ae..bcaecab 100644 --- a/src/main.c +++ b/src/main.c @@ -64,10 +64,15 @@ __task int main(void) { // If we have finished, break out of this loop if(state.done) break; - // Unable to add a tile. We lost!!! - if(!add_random_tile()) break; - + // Draw the moved sprites draw_tiles(); + + // Unable to add a tile. We lost!!! + uint8_t random_tile_off = add_random_tile(); + if(!random_tile_off) break; + + // Draw the new tile directly on the front buffer + ddraw_single_tile(random_tile_off - 1); } };