added commandline arguments and deleted target
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,2 @@
|
|||||||
.clangd
|
.clangd
|
||||||
target/
|
target
|
||||||
|
|||||||
84
src/args.c
Normal file
84
src/args.c
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "args.h"
|
||||||
|
|
||||||
|
extern char* version;
|
||||||
|
|
||||||
|
static void handle_version() {
|
||||||
|
printf("Sanke version %s\n", version);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void set_width(Arguments* args, char* width_str) {
|
||||||
|
printf("width_str: %s\n", width_str);
|
||||||
|
int width = atoi(width_str);
|
||||||
|
if (width < 10) {
|
||||||
|
printf("Width must be between 10 and 50. Was %d.\n", width);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
args->width = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void set_height(Arguments* args, char* height_str) {
|
||||||
|
int height = atoi(height_str);
|
||||||
|
if (height < 10) {
|
||||||
|
printf("Width must be between 10 and 50. Was %d.\n", height);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
args->height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void set_speed(Arguments* args, char* speed_str) {
|
||||||
|
int squares_per_second = atoi(speed_str);
|
||||||
|
if (squares_per_second < 1 || squares_per_second > 1000) {
|
||||||
|
printf("Speed must be between 1 and 1000. Was %d.\n", squares_per_second);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
args->sleep_ms = 1000 / squares_per_second;
|
||||||
|
}
|
||||||
|
|
||||||
|
Arguments cmd_args(int argc, char** argv) {
|
||||||
|
Arguments args = {
|
||||||
|
.width = 15,
|
||||||
|
.height = 15,
|
||||||
|
.sleep_ms = 150
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int i = 1; i < argc; i++) {
|
||||||
|
if (
|
||||||
|
strcmp(argv[i], "--version") == 0
|
||||||
|
|| strcmp(argv[i], "-v") == 0
|
||||||
|
)
|
||||||
|
{
|
||||||
|
handle_version();
|
||||||
|
|
||||||
|
} else if (
|
||||||
|
strcmp(argv[i], "--width") == 0
|
||||||
|
|| strcmp(argv[i], "-w") == 0
|
||||||
|
)
|
||||||
|
{
|
||||||
|
set_width(&args, argv[i + 1]);
|
||||||
|
|
||||||
|
} else if (
|
||||||
|
strcmp(argv[i], "--height") == 0
|
||||||
|
|| strcmp(argv[i], "-h") == 0
|
||||||
|
)
|
||||||
|
{
|
||||||
|
set_height(&args, argv[i + 1]);
|
||||||
|
|
||||||
|
} else if (
|
||||||
|
strcmp(argv[i], "--speed") == 0
|
||||||
|
|| strcmp(argv[i], "-s") == 0
|
||||||
|
)
|
||||||
|
{
|
||||||
|
set_speed(&args, argv[i + 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return args;
|
||||||
|
}
|
||||||
12
src/headers/args.h
Normal file
12
src/headers/args.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef ARGS_H_
|
||||||
|
#define ARGS_H_
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
int sleep_ms;
|
||||||
|
} Arguments;
|
||||||
|
|
||||||
|
Arguments cmd_args(int argc, char** argv);
|
||||||
|
|
||||||
|
#endif // ARGS_H_
|
||||||
80
src/main.c
80
src/main.c
@@ -3,7 +3,6 @@
|
|||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "Board.h"
|
#include "Board.h"
|
||||||
@@ -11,21 +10,19 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "food.h"
|
#include "food.h"
|
||||||
#include "BoardPiece.h"
|
#include "BoardPiece.h"
|
||||||
|
#include "args.h"
|
||||||
|
|
||||||
#define TERMIOS 1
|
#define TERMIOS 1
|
||||||
#define DEBUG 0
|
#define DEBUG 0
|
||||||
|
|
||||||
const char* VERSION = "1.1.9";
|
const char* version = "1.1.9";
|
||||||
|
|
||||||
const char snake_vis = '#';
|
const char snake_vis = '#';
|
||||||
|
|
||||||
void cmd_args(int argc, char** argv);
|
|
||||||
void empty_stdin_buffer();
|
|
||||||
void get_int_or_minus_one(int* dst);
|
|
||||||
struct termios set_termios();
|
struct termios set_termios();
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
cmd_args(argc, argv);
|
Arguments args = cmd_args(argc, argv);
|
||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
@@ -35,8 +32,8 @@ int main(int argc, char** argv) {
|
|||||||
#endif // TERMIOS
|
#endif // TERMIOS
|
||||||
|
|
||||||
// Game model init
|
// Game model init
|
||||||
const int board_w = 15;
|
const int board_w = args.width;
|
||||||
const int board_h = 15;
|
const int board_h = args.height;
|
||||||
Board board = board_alloc(board_w, board_h);
|
Board board = board_alloc(board_w, board_h);
|
||||||
Snake snake = snake_alloc(board_w * board_h, 0, 0, 'd');
|
Snake snake = snake_alloc(board_w * board_h, 0, 0, 'd');
|
||||||
BoardPiece food = { .vis_char = '$' };
|
BoardPiece food = { .vis_char = '$' };
|
||||||
@@ -46,24 +43,11 @@ int main(int argc, char** argv) {
|
|||||||
// Screen init
|
// Screen init
|
||||||
system("clear");
|
system("clear");
|
||||||
|
|
||||||
int frame = 0;
|
#if DEBUG
|
||||||
int fps = 0;
|
long long frame = 0;
|
||||||
|
#endif // DEBUG
|
||||||
long second_start = time(NULL);
|
|
||||||
int frame_stamp = frame;
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
long second_check = time(NULL);
|
|
||||||
long elapsed_time = second_check - second_start;
|
|
||||||
if (elapsed_time >= 1) {
|
|
||||||
fps = frame - frame_stamp;
|
|
||||||
if (elapsed_time > 1) {
|
|
||||||
fps /= 2;
|
|
||||||
}
|
|
||||||
frame_stamp = frame;
|
|
||||||
second_start = time(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
system("clear");
|
system("clear");
|
||||||
|
|
||||||
// Process input
|
// Process input
|
||||||
@@ -85,7 +69,6 @@ int main(int argc, char** argv) {
|
|||||||
board_clear(&board);
|
board_clear(&board);
|
||||||
board_draw_snake(&board, &snake);
|
board_draw_snake(&board, &snake);
|
||||||
board_set_square(&board, food.x, food.y, food.vis_char);
|
board_set_square(&board, food.x, food.y, food.vis_char);
|
||||||
printf("FPS: %d\n", fps);
|
|
||||||
print_board(&board);
|
print_board(&board);
|
||||||
printf("Score: %d\n", score);
|
printf("Score: %d\n", score);
|
||||||
|
|
||||||
@@ -93,7 +76,6 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
if (snake_collides_with_tail(&snake)) {
|
if (snake_collides_with_tail(&snake)) {
|
||||||
printf("GAME OVER\n");
|
printf("GAME OVER\n");
|
||||||
printf("Final score: %d\n", score);
|
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,10 +90,10 @@ int main(int argc, char** argv) {
|
|||||||
printf("Frame: %d\n", frame);
|
printf("Frame: %d\n", frame);
|
||||||
snake_print_info(&snake);
|
snake_print_info(&snake);
|
||||||
board_piece_print_info(&food, "Food");
|
board_piece_print_info(&food, "Food");
|
||||||
|
frame++;
|
||||||
#endif // DEBUG
|
#endif // DEBUG
|
||||||
|
|
||||||
frame++;
|
sleep_ms(args.sleep_ms);
|
||||||
sleep_ms(150);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if TERMIOS
|
#if TERMIOS
|
||||||
@@ -121,48 +103,6 @@ int main(int argc, char** argv) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmd_args(int argc, char** argv) {
|
|
||||||
if (argc == 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
strcmp(argv[1], "--version") == 0
|
|
||||||
|| strcmp(argv[1], "-v") == 0
|
|
||||||
)
|
|
||||||
{
|
|
||||||
printf("Sanke version %s\n", VERSION);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void empty_stdin_buffer() {
|
|
||||||
char ch;
|
|
||||||
while ((ch = getchar()) != '\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_int_or_minus_one(int* dst) {
|
|
||||||
char iBuf[100];
|
|
||||||
char num[100];
|
|
||||||
size_t i = 0;
|
|
||||||
|
|
||||||
fgets(iBuf, 100, stdin);
|
|
||||||
|
|
||||||
while (!isdigit(iBuf[i])) {
|
|
||||||
if (iBuf[i] == '\0') {
|
|
||||||
*dst = -1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int j = 0; i < strlen(iBuf); i++, j++) {
|
|
||||||
num[j] = iBuf[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
*dst = atoi(num);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct termios set_termios() {
|
struct termios set_termios() {
|
||||||
struct termios attr;
|
struct termios attr;
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
#include <termios.h>
|
|
||||||
|
|
||||||
BIN
target/sanke
BIN
target/sanke
Binary file not shown.
Reference in New Issue
Block a user