add windows pipes

pipes2game
MuedeHydra 2024-12-13 18:06:49 +01:00
parent 7a93be6d29
commit b89e6d7aba
7 changed files with 246 additions and 3 deletions

34
src/kbhit_linux.c Normal file
View File

@ -0,0 +1,34 @@
#ifdef __linux__
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
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

10
src/kbhit_linux.h Normal file
View File

@ -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

View File

@ -24,6 +24,7 @@ Datum: 04.12.2024
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#include <termio.h> #include <termio.h>
#include "kbhit_linux.h"
#elif _WIN32 #elif _WIN32
#include <windows.h> #include <windows.h>
#include <conio.h> #include <conio.h>
@ -34,6 +35,8 @@ Datum: 04.12.2024
#include "Ramen_Physik.h" #include "Ramen_Physik.h"
#include "test_prog.h" #include "test_prog.h"
#include "pipes2.h"
#ifdef __linux__ #ifdef __linux__
int getch(){ 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! // ist dazu da um "\n" aus dem buffer zu entfernen!
getchar(); int c;
while ((c = getchar()) != '\n' && c != EOF) { }
// getchar();
switch (return_code) { switch (return_code) {
case 10: used[programm] = 1; break; case 10: used[programm] = 1; break;
@ -316,7 +321,25 @@ int main(int argc, char **argv){
print_menu(width, height, line); print_menu(width, height, line);
// liest die Tastatur aus // 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 // verarbeitet die Tastatur eingabe
switch (key_input) { switch (key_input) {
@ -334,7 +357,7 @@ int main(int argc, char **argv){
case 'q': run = false; break; case 'q': run = false; break;
case '?': print_help(); break; case '?': print_help(); break;
// case 'p': pipes(); break; case 'p': pipes_2(width, height); break;
// windows // windows
case -32:{ case -32:{

135
src/pipes2.c Normal file
View File

@ -0,0 +1,135 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <time.h>
// #include <termios.h>
// #include <fcntl.h>
#ifdef __linux__
#include "kbhit_linux.h"
#elif _WIN32
#include <windows.h>
#include <conio.h>
#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;
}

10
src/pipes2.h Normal file
View File

@ -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

29
src/test_prog.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
/*
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;
}

2
src/test_prog.h Normal file
View File

@ -0,0 +1,2 @@
int test_gruppe_programmname();