diff --git a/multiplatformLib.c b/multiplatformLib.c new file mode 100644 index 0000000000000000000000000000000000000000..2fa3abb6aa9512d629a07549cad038a955084aae --- /dev/null +++ b/multiplatformLib.c @@ -0,0 +1,47 @@ +#include "multiplatformLib.h" + +#include "structs.h" + +#ifdef __linux__ +#include<sys/ioctl.h> +#include<unistd.h> +#include<time.h> +#else +#include<Windows.h> +#endif + + + + +//This package is designed to do the basic operations like get char without waiting for enter or sleep +//and be able to compile on multiple OS like linux and windows. + + +struct Vec2i getWindowSize(){ + struct Vec2i size; + #ifdef __linux__ + struct winsize info; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &info); + size.x = info.ws_col; + size.y = info.ws_row; + #else + CONSOLE_SCREEN_BUFFER_INFO info; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); + size.x = info.srWindow.Right - info.srWindow.Left + 1; + size.y = info.srWindow.Bottom - info.srWindow.Top + 1; + #endif + return size; +} + +void unisleep(int milisec){ + #ifdef __linux__ + struct timespec ts; + ts.tv_sec = milisec / 1000; + ts.tv_nsec = (milisec % 1000) * 1000000; + nanosleep(&ts, NULL); + #else + Sleep(milisec); + #endif +} + + diff --git a/multiplatformLib.h b/multiplatformLib.h new file mode 100644 index 0000000000000000000000000000000000000000..bf6a5a39bf70be5a45c9f89857f9901069fbbfba --- /dev/null +++ b/multiplatformLib.h @@ -0,0 +1,10 @@ +#ifndef SNAKE_MULTIPLATFORM +#define SNAKE_MULTIPLATFORM +//include guard +#include "structs.h" + +struct Vec2i getWindowSize(); + +void unisleep(int milisec); + +#endif \ No newline at end of file diff --git a/snake.c b/snake.c index 0e0240e36692d30a58cd7dbf9f5c0993bca884b1..3d1dbaaef99e9cb64793e2e1ba169cfdca61cb3a 100644 --- a/snake.c +++ b/snake.c @@ -4,20 +4,15 @@ #include<locale.h> #include<ctype.h> -#ifdef __linux__ -#include<sys/ioctl.h> -#include<unistd.h> -#include<time.h> -#else -#include<Windows.h> -#endif +#include "structs.h" +#include "multiplatformLib.h" -//#define windows 1 +//There are lots of hints on https://infoc.eet.bme.hu/ -//typedef unsigned long long int pointer; //Not optimal - -//int isUnicodeEncoding = 0; +//#define windows 1 with #ifndef __linux__ solved... +//typedef unsigned long long int pointer; //Not optimal and not required. sometimes even the worst idea... +//-----------methods-------------- int isUnicodeEncoding(int set){ static int bl = 0; @@ -27,104 +22,6 @@ int isUnicodeEncoding(int set){ return bl; } -typedef enum Direcion{ - UP, - RIGHT, - DOWN, - LEFT, -}Direction; - -typedef struct Vec2i{ - int x; - int y; -}Pos; - -typedef struct snakeChain -{ - int num; - struct Vec2i pos; - struct snakeChain *next; - -}snakeChain; - -typedef union unichar{ - int isUnicone : 1; - struct{ - char c[4]; - }bytes; - -}unichar; -typedef struct chunk //struct stores 2 chars and it's color :D -{ - unichar chars[2]; - /* - struct{ - int fg : 3; //3 bit color codes. - int bg : 3; //red green blue black white and 3 other (idk these) - }color;*/ -}chunk; - -typedef struct state{ - struct Vec2i displaySize; - struct Vec2i displayPos; - int commands[2]; -}globalState; - -typedef struct linkedString{ - unichar value; - struct linkedString *next; -}linkedString; - -typedef struct chunkMatrix{ - chunk **matrix; - int width; - int height; -}Matrix; - -typedef struct food{ - Pos pos; - int rand; - struct food *next; -}food; - -typedef struct screenData{ - Pos pos; - Pos size; - int repeatMap; - int isXRepeat; - int isYRepeat; -}screenData; - -//-----------methods-------------- - - -struct Vec2i getWindowSize(){ - struct Vec2i size; - #ifdef __linux__ - struct winsize info; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &info); - size.x = info.ws_col; - size.y = info.ws_row; - #else - CONSOLE_SCREEN_BUFFER_INFO info; - GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); - size.x = info.srWindow.Right - info.srWindow.Left + 1; - size.y = info.srWindow.Bottom - info.srWindow.Top + 1; - #endif - return size; -} - -void unisleep(int milisec){ - #ifdef __linux__ - struct timespec ts; - ts.tv_sec = milisec / 1000; - ts.tv_nsec = (milisec % 1000) * 1000000; - nanosleep(&ts, NULL); - #else - Sleep(milisec); - #endif -} - /** * Only the first byte is required */ @@ -154,7 +51,7 @@ void printChar(unichar c){ } } -//fueoetoeoecsoeoe *next +//fueoetoeoecsoeoe *next //what to NOT do int readFile(FILE *file, Matrix *matrix){ int c, len, maxLineLen = 0, lineCount = (3,1), lineLen = 0; //lineCount = (3,1) ??? why?... i was just bored diff --git a/structs.h b/structs.h new file mode 100644 index 0000000000000000000000000000000000000000..3cd47f8d5152e2e7fe14342e183929d1a4ac453a --- /dev/null +++ b/structs.h @@ -0,0 +1,72 @@ +#ifndef SNAKE_STRUCTS +#define SNAKE_STRUCTS + +typedef enum Direcion{ + UP, + RIGHT, + DOWN, + LEFT, +}Direction; + +typedef struct Vec2i{ + int x; + int y; +}Pos; + +typedef struct snakeChain +{ + int num; + struct Vec2i pos; + struct snakeChain *next; + +}snakeChain; + +typedef union unichar{ + int isUnicone : 1; + struct{ + char c[4]; + }bytes; + +}unichar; +typedef struct chunk //struct stores 2 chars and it's color :D +{ + unichar chars[2]; + /* + struct{ + int fg : 3; //3 bit color codes. + int bg : 3; //red green blue black white and 3 other (idk these) + }color;*/ +}chunk; + +typedef struct state{ + struct Vec2i displaySize; + struct Vec2i displayPos; + int commands[2]; +}globalState; + +typedef struct linkedString{ + unichar value; + struct linkedString *next; +}linkedString; + +typedef struct chunkMatrix{ + chunk **matrix; + int width; + int height; +}Matrix; + +typedef struct food{ + Pos pos; + int rand; + struct food *next; +}food; + +typedef struct screenData{ + Pos pos; + Pos size; + int repeatMap; + int isXRepeat; + int isYRepeat; +}screenData; + +#endif \ No newline at end of file diff --git a/tests/a.c b/tests/a.c new file mode 100644 index 0000000000000000000000000000000000000000..a6f76c76c489012728aa63369d9c4688a23b425d --- /dev/null +++ b/tests/a.c @@ -0,0 +1,13 @@ +#include "a.h" + +#include "b.h" + +void addone(int i){ + test(i + 1); +} + +int main(int argc, char const *argv[]) +{ + /* code */ + return 0; +} diff --git a/tests/a.h b/tests/a.h new file mode 100644 index 0000000000000000000000000000000000000000..20071326f39e25d4ba8f2aac0ece7cc3643a7f23 --- /dev/null +++ b/tests/a.h @@ -0,0 +1,2 @@ + +void addOne(int i); \ No newline at end of file diff --git a/tests/b.c b/tests/b.c new file mode 100644 index 0000000000000000000000000000000000000000..ccf94771fa35ec36b3e01756d3ea01b864540c9e --- /dev/null +++ b/tests/b.c @@ -0,0 +1,14 @@ +#include<stdio.h> +#include "b.h" + +#include "a.h" + +void test(int i){ + printf("%d", i); +} + +int main(int argc, char const *argv[]) +{ + addOne(2); + return 0; +} diff --git a/tests/b.h b/tests/b.h new file mode 100644 index 0000000000000000000000000000000000000000..2db8f7cb08f3098b6ab3087c18f26d43d690a1df --- /dev/null +++ b/tests/b.h @@ -0,0 +1,2 @@ + +void test(int i); \ No newline at end of file diff --git a/xperiments.c b/xperiments.c index b153b6660597c1d7d6a37df8dba0521d6a319ab4..d78617a1093bcb0ae990ec6b81719c5b4f698699 100644 --- a/xperiments.c +++ b/xperiments.c @@ -1,5 +1,10 @@ #include<stdio.h> #include<stdlib.h> +#include<Windows.h> + +#ifndef __linux__ +#include<conio.h> +#endif int xp1(void) { @@ -42,11 +47,16 @@ void xp3(){ printf("%c", 0x00); } +void xp4(){ + Sleep(1000); + char c = _getch(); + printf("\nchar: %c\n", c); +} int main(int argc, char const *argv[]) { 12%5; - xp3(); + xp4(); return 0; } diff --git a/xperiments.exe b/xperiments.exe index 6560eb7cf592c2b5632184b58ef125ff98a0b991..f0fdab5db041fca0420a901d4f88436f2935bb97 100644 Binary files a/xperiments.exe and b/xperiments.exe differ