From b89e6d7abaf0f2acb662f04efb12bb2cd47bae74 Mon Sep 17 00:00:00 2001 From: MuedeHydra Date: Fri, 13 Dec 2024 18:06:49 +0100 Subject: [PATCH] add windows pipes --- src/kbhit_linux.c | 34 ++++++++++++ src/kbhit_linux.h | 10 ++++ src/main.c | 29 ++++++++-- src/pipes2.c | 135 ++++++++++++++++++++++++++++++++++++++++++++++ src/pipes2.h | 10 ++++ src/test_prog.c | 29 ++++++++++ src/test_prog.h | 2 + 7 files changed, 246 insertions(+), 3 deletions(-) create mode 100644 src/kbhit_linux.c create mode 100644 src/kbhit_linux.h create mode 100644 src/pipes2.c create mode 100644 src/pipes2.h create mode 100644 src/test_prog.c create mode 100644 src/test_prog.h diff --git a/src/kbhit_linux.c b/src/kbhit_linux.c new file mode 100644 index 0000000..58fd307 --- /dev/null +++ b/src/kbhit_linux.c @@ -0,0 +1,34 @@ +#ifdef __linux__ +#include +#include +#include +#include + +int kbhit(void) +{ + struct termios oldt, newt; + int ch; + int oldf; + + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + oldf = fcntl(STDIN_FILENO, F_GETFL, 0); + fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); + + ch = getchar(); + + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + fcntl(STDIN_FILENO, F_SETFL, oldf); + + if(ch != EOF) + { + ungetc(ch, stdin); + return 1; + } + + return 0; +} + +#endif diff --git a/src/kbhit_linux.h b/src/kbhit_linux.h new file mode 100644 index 0000000..448e6b1 --- /dev/null +++ b/src/kbhit_linux.h @@ -0,0 +1,10 @@ +#ifndef kbhit_linux_H_ +#define kbhit_linux_H_ + +// #ifndef _FILE_NAME_H_ +// #define _FILE_NAME_H_ + + // extern int kbhit(); + int kbhit(); + +#endif diff --git a/src/main.c b/src/main.c index 647a519..088ca11 100644 --- a/src/main.c +++ b/src/main.c @@ -24,6 +24,7 @@ Datum: 04.12.2024 #include #include #include + #include "kbhit_linux.h" #elif _WIN32 #include #include @@ -34,6 +35,8 @@ Datum: 04.12.2024 #include "Ramen_Physik.h" #include "test_prog.h" +#include "pipes2.h" + #ifdef __linux__ int getch(){ @@ -248,7 +251,9 @@ void run_programm(int programm, int width, int height, bool *used){ } // ist dazu da um "\n" aus dem buffer zu entfernen! - getchar(); + int c; + while ((c = getchar()) != '\n' && c != EOF) { } + // getchar(); switch (return_code) { case 10: used[programm] = 1; break; @@ -316,7 +321,25 @@ int main(int argc, char **argv){ print_menu(width, height, line); // liest die Tastatur aus - char key_input = (char)getch(); + // char key_input = (char)getch(); + + // timer updateten + int last_input = time(0); + + while (!kbhit()){ + if (last_input + 30 <= time(0)){ + pipes_2(width, height); + last_input = time(0); + print_menu(width, height, line); + } + } + + #ifdef __linux__ + char key_input = (char)getchar(); + #elif _WIN32 + char key_input = (char)getch(); + #endif + // verarbeitet die Tastatur eingabe switch (key_input) { @@ -334,7 +357,7 @@ int main(int argc, char **argv){ case 'q': run = false; break; case '?': print_help(); break; - // case 'p': pipes(); break; + case 'p': pipes_2(width, height); break; // windows case -32:{ diff --git a/src/pipes2.c b/src/pipes2.c new file mode 100644 index 0000000..aad87ff --- /dev/null +++ b/src/pipes2.c @@ -0,0 +1,135 @@ +#include +#include +#include +#include +#include + +// #include +// #include + +#ifdef __linux__ +#include "kbhit_linux.h" +#elif _WIN32 + #include + #include +#endif + +#define GRID_GROESSE_X 200 +#define GRID_GROESSE_Y 60 +#define SLEEP_TIME 10000 + +/* +int kbhit(void) +{ + struct termios oldt, newt; + int ch; + int oldf; + + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + oldf = fcntl(STDIN_FILENO, F_GETFL, 0); + fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); + + ch = getchar(); + + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + fcntl(STDIN_FILENO, F_SETFL, oldf); + + if(ch != EOF) + { + ungetc(ch, stdin); + return 1; + } + + return 0; +} +*/ + +void print_at(int x, int y, char c[4]) +{ + printf("\033[%d;%dH%s", y, x, c); +} + +void print_grid(char **grid){ + + for (int i = 0; i < GRID_GROESSE_Y; i++) { + for (int j = 0; j < GRID_GROESSE_X; j++) { + printf("%i", grid[i][j]); + // printf(" "); + } + printf("\n"); + } +} + +char print_pipe(int *x, int *y, int x_max, int y_max, int length, char direction, char direction_old){ + bool start = true; + for(int i = 0; i < length; i++){ + + if (start && direction != direction_old){ + switch (direction_old){ + case 0: *y -= 1; if (*y <= 0){ *y = y_max; } break; // up + case 1: *y += 1; if (*y >= y_max){ *y = 0; } break; // down + case 2: *x -= 1; if (*x <= 0){ *x = x_max; } break; // left + case 3: *x += 1; if (*x >= x_max){ *x = 0; } break; // rigth + } + if (direction == 2 && direction_old == 0 || direction == 1 && direction_old == 3){ + print_at(*x, *y, "┓"); + } else if (direction == 0 && direction_old == 2 || direction == 3 && direction_old == 1) { + print_at(*x, *y, "┗"); + } else if (direction == 3 && direction_old == 0 || direction == 1 && direction_old == 2) { + print_at(*x, *y, "┏"); + } else if (direction == 0 && direction_old == 3 || direction == 2 && direction_old == 1) { + print_at(*x, *y, "┛"); + } // else { printf("\nd = %i | d_o = %i\n", direction, direction_old); } + start = false; + } else { + switch (direction){ + case 0: *y -= 1; if (*y <= 0){ *y = y_max; } break; // up + case 1: *y += 1; if (*y >= y_max){ *y = 0; } break; // down + case 2: *x -= 1; if (*x <= 0){ *x = x_max; } break; // left + case 3: *x += 1; if (*x >= x_max){ *x = 0; } break; // rigth + } + if (direction <= 1) { + print_at(*x, *y, "┃"); + } else { + print_at(*x, *y, "━"); + } + } + fflush(stdout); + usleep(SLEEP_TIME); + } + return direction; +} + +int randome(int max){ + return rand() % max; +} + +void clear_terminal2(){ + printf("\e[1;1H\e[2J"); +} + +int pipes_2(int width, int height){ + int x_max = width; + int y_max =height; + int x = width / 2; + int y = height / 2; + char direction_old = 0; + int start_time = 0; + srand(time(0)); + + while(!kbhit()){ + if (start_time + 20 <= time(0)){ + clear_terminal2(); + start_time = time(0); + } + direction_old = print_pipe(&x, &y, x_max, y_max, randome(height / 2), randome(4), direction_old); + } + + // ist dazu da um "\n" aus dem buffer zu entfernen! + getchar(); + + return 0; +} diff --git a/src/pipes2.h b/src/pipes2.h new file mode 100644 index 0000000..bf73759 --- /dev/null +++ b/src/pipes2.h @@ -0,0 +1,10 @@ +#ifndef pipes2_H_ +#define pipes2_H_ + +// #ifndef _FILE_NAME_H_ +// #define _FILE_NAME_H_ + +// extern int pipes(int width, int height); +int pipes_2(int width, int height); + +#endif diff --git a/src/test_prog.c b/src/test_prog.c new file mode 100644 index 0000000..7377a65 --- /dev/null +++ b/src/test_prog.c @@ -0,0 +1,29 @@ +#include + + +/* +Photonics helper sub programm +Programmvorlage für die subprogramme + +Autor: Noah Balsinger, Thomas Zwicker +Version: 0.1 +Datum: 04.12.2024 +Änderungen: +0.1 04.12.2024 Dokument erstellt +*/ + + +int test_gruppe_programmname(){ + // Tasschenrechner beispiel + int x; + int y; + + printf("Bitte Zahl 1 eingeben: "); + scanf("%i", &x); + // printf("Bitte Zahl 2 eingeben: "); + // scanf("%i", &y); + + // printf("Die Summe von Zahl1 und Zahl2 ist: %i\n", x + y); + + return x; +} diff --git a/src/test_prog.h b/src/test_prog.h new file mode 100644 index 0000000..da216c5 --- /dev/null +++ b/src/test_prog.h @@ -0,0 +1,2 @@ + +int test_gruppe_programmname();