diff --git a/multiplatformLib b/multiplatformLib new file mode 100755 index 0000000000000000000000000000000000000000..7092324e35bf0f781904e8b5d5dd68549738d064 Binary files /dev/null and b/multiplatformLib differ diff --git a/multiplatformLib.c b/multiplatformLib.c index 3184913b7f93c960915bf6b4790a7e3b84124bb9..4fea72bc007fb32d1e096fb2e651ee68908fd99c 100644 --- a/multiplatformLib.c +++ b/multiplatformLib.c @@ -19,32 +19,48 @@ //and be able to compile on multiple OS like linux and windows. //Init some platform dependent configurations + +void refreshScreen(void){ + #ifdef __linux__ + //TODO + #endif +} + +//Init platform specific IO void initMultiplatform(void){ #ifdef __linux__ - struct termios info; // This is from stackoverflow - tcgetattr(0, &info); /* get current terminal attirbutes; 0 is the file descriptor for stdin */ - info.c_lflag &= ~ICANON; /* disable canonical mode */ - info.c_cc[VMIN] = 0; /* don't wait for keypress, return EOF instead */ - info.c_cc[VTIME] = 0; /* no timeout */ - tcsetattr(0, TCSANOW, &info); /* set immediately */ + struct termios info; + tcgetattr(0, &info); //get attr + info.c_lflag &= ~ICANON; //turn off canonical mode + info.c_lflag &= ~ECHO; + tcsetattr(0, TCSANOW, &info); //set attr + setbuf(stdin, NULL); //??? #endif } +#ifdef __linux__ +//linux equivalent conio _kbhit +int _kbhit(){ + int kbhits; + ioctl(0, FIONREAD, &kbhits); + return kbhits; +} +#endif //returns the char or EOF int getNextChar(void){ - #ifdef __linux__ - return getchar(); - #else if(_kbhit()){ + #ifdef __linux__ + return getchar(); + #else return _getch(); - } + #endif + } else{ return EOF; } - #endif } - +//Returns with the size of the terminal struct Vec2i getWindowSize(void){ struct Vec2i size; #ifdef __linux__ @@ -61,6 +77,7 @@ struct Vec2i getWindowSize(void){ return size; } +//sleem void unisleep(int milisec){ #ifdef __linux__ struct timespec ts; @@ -74,6 +91,7 @@ void unisleep(int milisec){ //NOT TRUE MOD FUNCTION +//makes a false modulus what is always positive int mod(int i, int base, int repeat){ if(i < 0){ if(repeat){ diff --git a/multiplatformLib.h b/multiplatformLib.h index dc8bbbd83378879e502ab4f650b2d2a6edfa2823..27b81464b589e8172a618e2c83d3187f03a0103d 100644 --- a/multiplatformLib.h +++ b/multiplatformLib.h @@ -7,6 +7,8 @@ struct Vec2i getWindowSize(void); void initMultiplatform(void); +void refreshScreen(void); + int getNextChar(void); void unisleep(int milisec); diff --git a/snake.c b/snake.c index 31802d4f7ff3cb15a4b70a82928ceb2ecb2120ff..d02acc9db181dd3a741692c0b9da55512f8cec73 100644 --- a/snake.c +++ b/snake.c @@ -1,3 +1,4 @@ +#include "debugmalloc.h" #include<stdio.h> #include<stdlib.h> #include<string.h> @@ -9,7 +10,6 @@ #include "multiplatformLib.h" //debugmalloc from infoc -#include "debugmalloc.h" #define green "\e[0;32m" #define blue "\e[0;34m" @@ -17,7 +17,6 @@ //There are lots of hints on https://infoc.eet.bme.hu/ //#define windows 1 with #ifndef __linux__ solved... -//typedef unsigned long long int pointer; //Not optimal and not required. sometimes even the worst idea... //-----------methods-------------- @@ -72,7 +71,9 @@ int readFile(FILE *file, Matrix *matrix){ if(next == NULL){ return EOF; } - memset(next, 0, sizeof(linkedString)); //TODO remove after debugging + #ifdef DEBUG + memset(next, 0, sizeof(linkedString)); //fill it with zeros. only in debug mode + #endif next->next = 0; while (c == '\r') { @@ -235,12 +236,12 @@ void updateScreen(Matrix *map, screenData *scrDat, snakeChain *head, Direction d if(do_update){ if(scrDat->size.x < map->width){ - scrDat->pos.x = mod((head->pos.x + scrDat->size.x) / 2 ,map->width, scrDat->repeatMap); + scrDat->pos.x = mod((head->pos.x + scrDat->size.x) - scrDat->size.x / 2 ,map->width, scrDat->repeatMap); }else{ scrDat->pos.x = 0; } if(scrDat->size.y < map->height){ - scrDat->pos.y = mod((head->pos.y + scrDat->size.y) / 2 ,map->height, scrDat->repeatMap); + scrDat->pos.y = mod((head->pos.y + scrDat->size.y) - scrDat->size.y / 2 ,map->height, scrDat->repeatMap); }else{ scrDat->pos.y = 0; } @@ -322,7 +323,12 @@ void getControl(screenData *scrDat, Direction *direction){ default: next = NONE; - continue; //we don't have to registrane a non command... + continue; //we don't have to registrate a non command... + } + + if(*direction == NONE){ + *direction = next; + continue; } if(next == *direction){ @@ -397,10 +403,10 @@ int updateSnake(Matrix *map, screenData *scrDat, Direction d, snakeChain *head, switch (d) { case UP: - pos.y++; + pos.y--; break; case DOWN: - pos.y--; + pos.y++; break; case LEFT: pos.x--; @@ -433,6 +439,7 @@ int updateSnake(Matrix *map, screenData *scrDat, Direction d, snakeChain *head, if(isNotWall(map->matrix[pos.x][pos.y])){ int isFood = map->matrix[pos.x][pos.y].isFood; + head->pos = pos; snakeChain *snake = head; Pos tmp_pos1 = head->pos, tmp_pos2; while (snake->next != 0) @@ -442,13 +449,13 @@ int updateSnake(Matrix *map, screenData *scrDat, Direction d, snakeChain *head, if(snake != head && snake->pos.x == head->pos.x && snake->pos.y == head->pos.y){ if(snake->next != NULL){ if(canBite){ - snakeChain *current = snake; - snake = snake->next; + snakeChain *current = snake, *tmp_snek = snake; + tmp_snek = tmp_snek->next; current->next = NULL; - while (snake != NULL) + while (tmp_snek != NULL) { - current = snake; - snake = snake->next; + current = tmp_snek; + tmp_snek = tmp_snek->next; free(current); } } @@ -468,7 +475,7 @@ int updateSnake(Matrix *map, screenData *scrDat, Direction d, snakeChain *head, tmp_pos1 = tmp_pos2; c.data.chars[0].bytes.c[0] = '>'; - c.data.chars[0].bytes.c[0] = '<'; + c.data.chars[1].bytes.c[0] = '<'; if(snake->next == NULL && isFood){ isFood = false; snake->next = malloc(sizeof(snake)); @@ -490,12 +497,12 @@ int updateSnake(Matrix *map, screenData *scrDat, Direction d, snakeChain *head, if(tmp_pos2.x != -1){ chunk c; c.data.chars[0].bytes.c[0] = ' '; - c.data.chars[0].bytes.c[0] = ' '; + c.data.chars[1].bytes.c[0] = ' '; print(c, tmp_pos2, scrDat, map->width, map->height); //set this to air. } chunk c; c.data.chars[0].bytes.c[0] = '('; - c.data.chars[0].bytes.c[0] = ')'; + c.data.chars[1].bytes.c[0] = ')'; printf(blue); print(c, head->pos, scrDat, map->width, map->height); //TODO direction snake printf("\e[0m"); @@ -577,7 +584,7 @@ int tick(Matrix *map, screenData *scrDat, snakeChain *snake, Direction *d, int f if(*d == NONE){ chunk c; c.data.chars[0].bytes.c[0] = '('; - c.data.chars[0].bytes.c[0] = ')'; + c.data.chars[1].bytes.c[0] = ')'; printf(blue); print(c, snake->pos, scrDat, map->width, map->height); printf("\e[0m"); @@ -737,8 +744,15 @@ int main(int argc, char const *argv[]) } */ +/** + * debugger main function + * in debug mode, starts the core with correct params + * in release mode, it will use user input files. + */ int main(int argc, char const *argv[]) { + int walltest; //warning: unused variable ‘walltest’ [-Wunused-variable] + //2 + 3; //... this does nothing... #ifdef DEBUG int ret; @@ -751,5 +765,6 @@ int main(int argc, char const *argv[]) return ret; // így szép. #else return core(argc, argv); + debugmalloc_dump(); #endif } \ No newline at end of file diff --git a/structs b/structs new file mode 100755 index 0000000000000000000000000000000000000000..7092324e35bf0f781904e8b5d5dd68549738d064 Binary files /dev/null and b/structs differ diff --git a/tests/getchtest b/tests/getchtest index 8847179621ab0587769a6822d90a5daa8013cea4..d01b3db8c37b90ca69239fd55a83601deec5fd7f 100755 Binary files a/tests/getchtest and b/tests/getchtest differ diff --git a/tests/getchtest.c b/tests/getchtest.c index 44c0403afe42f50805f684115f733284cfb96e5b..a07bef197614d9f8cebe1d7b3902c9d0c5b36276 100644 --- a/tests/getchtest.c +++ b/tests/getchtest.c @@ -2,22 +2,46 @@ #include<stdio.h> #include<termios.h> #include<unistd.h> +#include<sys/select.h> +#include<sys/ioctl.h> + +int _kbhit(){ + int kbhits; + ioctl(0, FIONREAD, &kbhits); + return kbhits; +} int main(int argc, char const *argv[]) { - struct termios info; // This is from stackoverflow - tcgetattr(0, &info); /* get current terminal attirbutes; 0 is the file descriptor for stdin */ - info.c_lflag &= ~ICANON; /* disable canonical mode */ - info.c_cc[VMIN] = 0; /* don't wait for keypress, return EOF instead */ - info.c_cc[VTIME] = 0; /* no timeout */ - tcsetattr(0, TCSANOW, &info); /* set immediately */ + //struct termios info; // This is from stackoverflow + //tcgetattr(0, &info); /* get current terminal attirbutes; 0 is the file descriptor for stdin */ + //info.c_lflag &= ~ICANON; /* disable canonical mode */ + //info.c_cc[VMIN] = 1; /* don't wait for keypress, return EOF instead */ + //info.c_cc[VTIME] = 1; /* no timeout */ + //info.c_cc[ECHO] = 0; //Turn off echo + //tcsetattr(0, TCSANOW, &info); /* set immediately */ - int c = 0; + //initscr(); + //noecho(); + //cbreak(); + //scrollok(stdscr, true); + //nodelay(stdscr, true); + struct termios info; + tcgetattr(0, &info); //get attr + info.c_lflag &= ~ICANON; //turn off canonical mode + info.c_lflag &= ~ECHO; + tcsetattr(0, TCSANOW, &info); //set attr + setbuf(stdin, NULL); //??? + + + + int c = 0; sleep(1); + int l = _kbhit(); c = getchar(); - printf("\nint: %d\nchar: %c\n", c, c); + printf("\nint: %d\nchar: %c\nkbhit: %d\n", c, c, l); sleep(5); c = 0; diff --git a/tests/jumpto b/tests/jumpto new file mode 100755 index 0000000000000000000000000000000000000000..ff9bba3d82e9467de877d5e615bc3d52d1939a6f Binary files /dev/null and b/tests/jumpto differ diff --git a/tests/test.o b/tests/test.o new file mode 100755 index 0000000000000000000000000000000000000000..ce3da2cad65d74f56554df2517752fec10540da2 Binary files /dev/null and b/tests/test.o differ