changed some debugging functions to only compile when DEBUG is defined

This commit is contained in:
2026-06-25 23:20:02 +03:00
parent 8b3c52f580
commit c7b89aded5
6 changed files with 45 additions and 27 deletions

View File

@@ -26,7 +26,7 @@ fi
version=$(git rev-parse HEAD) version=$(git rev-parse HEAD)
oflag="-Og" oflag="-Og"
debug_flag="-ggdb" debug_flag="-ggdb -DDEBUG"
if [ $arg = "release" ]; then if [ $arg = "release" ]; then
oflag="-O3" oflag="-O3"

View File

@@ -1,7 +1,13 @@
#include <stdio.h>
#include "BoardPiece.h" #include "BoardPiece.h"
bool pieces_collide(BoardPiece* piece1, BoardPiece* piece2) {
return piece1->x == piece2->x && piece1->y == piece2->y;
}
#ifdef DEBUG
#include <stdio.h>
void board_piece_print_info(BoardPiece* piece, char* name) { void board_piece_print_info(BoardPiece* piece, char* name) {
printf("%s: {\n", name); printf("%s: {\n", name);
printf(" x: %d\n", piece->x); printf(" x: %d\n", piece->x);
@@ -10,6 +16,4 @@ void board_piece_print_info(BoardPiece* piece, char* name) {
printf("}\n"); printf("}\n");
} }
bool pieces_collide(BoardPiece* piece1, BoardPiece* piece2) { #endif // DEBUG
return piece1->x == piece2->x && piece1->y == piece2->y;
}

View File

@@ -108,21 +108,6 @@ void snake_move(Snake* snake, const int width, const int height) {
check_bounds(snake, width, height); check_bounds(snake, width, height);
} }
void snake_print_info(const Snake* snake) {
printf("snake: {\n");
printf(" parts: {\n");
for (size_t i = 0; i < snake->length; i++) {
const BoardPiece part = snake->parts[i];
printf(" { x: %d, y: %d }\n", part.x, part.y);
}
printf(" }\n");
printf(" max_length: %zu\n", snake->max_length);
printf(" length: %zu\n", snake->length);
printf(" dir: %c\n", snake->dir);
printf("}\n");
}
void snake_change_direction(Snake* snake, const char direction) { void snake_change_direction(Snake* snake, const char direction) {
snake->dir = direction; snake->dir = direction;
} }
@@ -211,3 +196,23 @@ void snake_add_part(Snake* snake) {
}; };
snake->length++; snake->length++;
} }
#ifdef DEBUG
void snake_print_info(const Snake* snake) {
printf("snake: {\n");
printf(" parts: {\n");
for (size_t i = 0; i < snake->length; i++) {
const BoardPiece part = snake->parts[i];
printf(" { x: %d, y: %d }\n", part.x, part.y);
}
printf(" }\n");
printf(" max_length: %zu\n", snake->max_length);
printf(" length: %zu\n", snake->length);
printf(" dir: %c\n", snake->dir);
printf("}\n");
}
#endif // DEBUG

View File

@@ -7,7 +7,12 @@ typedef struct {
char vis_char; char vis_char;
} BoardPiece; } BoardPiece;
void board_piece_print_info(BoardPiece* piece, char* name);
bool pieces_collide(BoardPiece* piece1, BoardPiece* piece2); bool pieces_collide(BoardPiece* piece1, BoardPiece* piece2);
#ifdef DEBUG
void board_piece_print_info(BoardPiece* piece, char* name);
#endif // DEBUG
#endif // BOARD_PIECE_H_ #endif // BOARD_PIECE_H_

View File

@@ -20,10 +20,15 @@ Snake snake_alloc(
); );
void snake_free(Snake* snake); void snake_free(Snake* snake);
void snake_move(Snake* snake, const int width, const int height); void snake_move(Snake* snake, const int width, const int height);
void snake_print_info(const Snake* snake);
void snake_change_direction(Snake* snake, const char direction); void snake_change_direction(Snake* snake, const char direction);
bool snake_collides(const Snake* snake, const BoardPiece* piece); bool snake_collides(const Snake* snake, const BoardPiece* piece);
bool snake_collides_with_tail(const Snake* snake); bool snake_collides_with_tail(const Snake* snake);
void snake_add_part(Snake* snake); void snake_add_part(Snake* snake);
#ifdef DEBUG
void snake_print_info(const Snake* snake);
#endif // DEBUG
#endif // SNAKE_H_ #endif // SNAKE_H_

View File

@@ -13,7 +13,6 @@
#include "utils.h" #include "utils.h"
#define TERMIOS 1 #define TERMIOS 1
#define DEBUG 0
#define MIN_SLEEP_TIME_MS 60 #define MIN_SLEEP_TIME_MS 60
@@ -44,8 +43,8 @@ int main(int argc, char** argv) {
// Screen init // Screen init
system("clear"); system("clear");
#if DEBUG #ifdef DEBUG
long long frame = 0; unsigned long long frame = 0;
#endif // DEBUG #endif // DEBUG
while (true) { while (true) {
@@ -89,7 +88,7 @@ int main(int argc, char** argv) {
} }
} }
#if DEBUG #ifdef DEBUG
printf("input: %c\n", input); printf("input: %c\n", input);
printf("Frame: %lld\n", frame); printf("Frame: %lld\n", frame);
printf("sleep_ms: %u\n", sleep_time); printf("sleep_ms: %u\n", sleep_time);