2024-12-10 16:07:51 +01:00

277 lines
11 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#ifdef __linux__
#include <sys/ioctl.h>
#include <unistd.h>
#include <termio.h>
#elif _WIN32
#include <windows.h>
#include <conio.h>
#include <conio.h>
#endif
// import sub programme
#include "test_prog.h"
/*
Photonics helper main programm
Das main programm startet die sub programme der verschienene gruppen
Autor: Noah Balsinger, Thomas Zwicker
Version: 0.1
Datum: 04.12.2024
Änderungen:
0.1 04.12.2024 Dokument erstellt
0.2 09.12.2024 Willkommensnachricht
*/
#ifdef __linux__
int getch(){
int ch;
struct termios oldattr, newattr;
tcgetattr(STDIN_FILENO, &oldattr);
newattr = oldattr;
newattr.c_lflag &= ~ICANON;
newattr.c_lflag &= ~ECHO;
newattr.c_cc[VMIN] = 1;
newattr.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &newattr);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
return ch;
}
#endif
#ifdef __linux__
void get_terminal_size_linux(int argc, char **argv, int *width, int *height){
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
*height = w.ws_row;
*width = w.ws_col;
}
#elif _WIN32
void get_terminal_size_windows(int argc, char **argv, int *width, int *height){
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
*width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
*height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
}
#endif
void print_line(char line[100], int spaces, char color[5]){
for(int i = 0; i < spaces; i++){
printf(" ");
}
printf("\033[1%sm %s \033[0m\n", color, line);
// printf("%s\n", line);
}
void clear_terminal(){
#ifdef __linux__
printf("\e[1;1H\e[2J");
#elif _WIN32
system("cls");
#endif
}
void sleep_universal(int t){
#ifdef __linux__
sleep(t);
#elif _WIN32
Sleep(t * 1000);
#endif
}
void print_blank_lines(int lines){
for(int i = 0; i < lines; i++){
printf("\n");
}
}
void print_wellcome(int terminal_width, int terminal_height){
clear_terminal();
print_blank_lines((terminal_height - 10) / 2);
int spaces = (terminal_width - 83) / 2;
print_line("__ ___ _ _ _ ", spaces, ";37");
print_line("\\ \\ / (_) | | | _____ _ __ ___ _ __ ___ ___ _ __ _____ _ _ __ ___ ", spaces, ";37");
print_line(" \\ \\ /\\ / /| | | | |/ / _ \\| '_ ` _ \\| '_ ` _ \\ / _ \\ '_ \\ |_ / | | | '_ ` _ \\", spaces, ";37");
print_line(" \\ V V / | | | | < (_) | | | | | | | | | | | __/ | | | / /| |_| | | | | | | ", spaces, ";37");
print_line(" \\_/\\_/ |_|_|_|_|\\_\\___/|_| |_| |_|_| |_| |_|\\___|_| |_| /___|\\__,_|_| |_| |_| ", spaces, ";37");
print_line(" ", spaces, ";37");
print_line(" ____ _ _ _ _ _ _ _ __ ", spaces, ";37");
print_line("| _ \\| |__ ___ | |_| |__ ___ _ __ (_) ___ ___ | | | | ___| |/ _| ___ _ __ ", spaces, ";37");
print_line("| |_) | '_ \\ / _ \\| __| '_ \\ / _ \\| '_ \\| |/ __/ __| | |_| |/ _ \\ | |_ / _ \\ '__| ", spaces, ";37");
print_line("| __/| | | | (_) | |_| | | | (_) | | | | | (__\\__ \\ | _ | __/ | _| __/ | ", spaces, ";37");
print_line("|_| |_| |_|\\___/ \\__|_| |_|\\___/|_| |_|_|\\___|___/ |_| |_|\\___|_|_| \\___|_| ", spaces, ";37");
sleep_universal(2);
/*
printf("__ ___ _ _ _ ");
printf("\ \ / (_) | | | _____ _ __ ___ _ __ ___ ___ _ __ _____ _ _ __ ___ ");
printf(" \ \ /\ / /| | | | |/ / _ \| '_ ` _ \| '_ ` _ \ / _ \ '_ \ |_ / | | | '_ ` _ \ ");
printf(" \ V V / | | | | < (_) | | | | | | | | | | | __/ | | | / /| |_| | | | | | |");
printf(" \_/\_/ |_|_|_|_|\_\___/|_| |_| |_|_| |_| |_|\___|_| |_| /___|\__,_|_| |_| |_|");
printf(" ");
printf(" ____ _ _ _ _ _ _ _ __ ");
printf("| _ \| |__ ___ | |_| |__ ___ _ __ (_) ___ ___ | | | | ___| |/ _| ___ _ __ ");
printf("| |_) | '_ \ / _ \| __| '_ \ / _ \| '_ \| |/ __/ __| | |_| |/ _ \ | |_ / _ \ '__|");
printf("| __/| | | | (_) | |_| | | | (_) | | | | | (__\__ \ | _ | __/ | _| __/ | ");
printf("|_| |_| |_|\___/ \__|_| |_|\___/|_| |_|_|\___|___/ |_| |_|\___|_|_| \___|_| ");
*/
}
void print_game_over(int terminal_width, int terminal_height){
clear_terminal();
print_blank_lines((terminal_height - 5) / 2);
int spaces = (terminal_width - 50) / 2;
print_line(" ____ ", spaces, ";37");
print_line(" / ___| __ _ _ __ ___ ___ _____ _____ _ __ ", spaces, ";37");
print_line("| | _ / _` | '_ ` _ \\ / _ \\ / _ \\ \\ / / _ \\ '__| ", spaces, ";37");
print_line("| |_| | (_| | | | | | | __/ | (_) \\ V / __/ | ", spaces, ";37");
print_line(" \\____|\\__,_|_| |_| |_|\\___| \\___/ \\_/ \\___|_| ", spaces, ";37");
print_blank_lines(2);
print_line("Press a key to contine: ", spaces, ";37");
char key_input = (char)getch();
// getchar();
}
void print_menu(int terminal_width, int terminal_height, int line){
clear_terminal();
print_blank_lines((terminal_height - 11) / 2);
int spaces = (terminal_width - 41) / 2;
#ifdef __linux__
print_line("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓", spaces, (0 <= line && line < 1) ? ";31": ";37");
print_line("┃ Programm 1 ┃", spaces, (0 <= line && line < 1) ? ";31": ";37");
print_line("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫", spaces, (0 <= line && line < 2) ? ";31": ";37");
print_line("┃ Programm 1 ┃", spaces, (1 <= line && line < 2) ? ";31": ";37");
print_line("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫", spaces, (1 <= line && line < 3) ? ";31": ";37");
print_line("┃ Programm 1 ┃", spaces, (2 <= line && line < 3) ? ";31": ";37");
print_line("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫", spaces, (2 <= line && line < 4) ? ";31": ";37");
print_line("┃ Programm 1 ┃", spaces, (3 <= line && line < 4) ? ";31": ";37");
print_line("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫", spaces, (3 <= line && line < 5) ? ";31": ";37");
print_line("┃ ┃", spaces, (4 <= line && line < 5) ? ";31": ";37");
print_line("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛", spaces, (4 <= line && line < 5) ? ";31": ";37");
#elif _WIN32
print_line("+---------------------------------------+", spaces, (0 <= line && line < 1) ? ";31": ";37");
print_line("| Programm 1 |", spaces, (0 <= line && line < 1) ? ";31": ";37");
print_line("+---------------------------------------+", spaces, (0 <= line && line < 2) ? ";31": ";37");
print_line("| Programm 1 |", spaces, (1 <= line && line < 2) ? ";31": ";37");
print_line("+---------------------------------------+", spaces, (1 <= line && line < 3) ? ";31": ";37");
print_line("| Programm 1 |", spaces, (2 <= line && line < 3) ? ";31": ";37");
print_line("+---------------------------------------+", spaces, (2 <= line && line < 4) ? ";31": ";37");
print_line("| Programm 1 |", spaces, (3 <= line && line < 4) ? ";31": ";37");
print_line("+---------------------------------------+", spaces, (3 <= line && line < 5) ? ";31": ";37");
print_line("| |", spaces, (4 <= line && line < 5) ? ";31": ";37");
print_line("+---------------------------------------+", spaces, (4 <= line && line < 5) ? ";31": ";37");
#endif
}
void print_help(){
clear_terminal();
printf("? \t Diese Hilfe anzeigen\n");
printf("gg \t zum obersten Programm springen\n");
printf("G \t zum untersten Programm springen\n");
printf("j \t Ein Programm nach unten\n");
printf("k \t Ein Programm nach oben\n");
printf("Enter \t Um das ausgewählte Programm zu starten\n");
printf("q \t Um das Programm zu beenden\n");
printf("\nDrücke eine Taste um zurück zu kommen\n");
char key_input = (char)getch();
// getchar();
}
void get_terminal_size(int argc, char **argv, int *width, int *height){
do{
#ifdef __linux__
get_terminal_size_linux(argc, argv, width, height);
#elif _WIN32
get_terminal_size_windows(argc, argv, width, height);
#endif
if (*width < 100 || *height < 20){
clear_terminal();
printf("Terminal zu klein!");
// sleep_universal(1);
}
}while(*width < 100 || *height < 20);
}
int main(int argc, char **argv){
int width = 0;
int height = 0;
int line = 0;
bool run = true;
// Terminal grösse herauslesen
get_terminal_size(argc, argv, &width, &height);
// int x = test_gruppe_programmname();
printf ("lines %d\n", height);
printf ("columns %d\n", width);
// neues terminal öffnen (nur linux)
#ifdef __linux__
system("tput smcup");
#endif
// print wellcome message
print_wellcome(width, height);
// print_game_over(width, height);
// print_menu(width, height, line);
while(run){
get_terminal_size(argc, argv, &width, &height);
print_menu(width, height, line);
// char key_input = (char)getch();
char key_input = (char)0;
switch (key_input) {
case 'k': line--; break;
case 'j': line++; break;
case 'g': {
char key_input = (char)getch();
if (key_input == 'g'){
line = 0;
}
break;
}
case 'G': line = 4; break;
case '?': print_help(); break;
case 'q': run = false; break;
case '\n': printf(" Es wurde die Enter Taste gedrückt"); getchar(); break;
default: printf(" Es wurde die Taste: %c gedrückt", key_input);
}
if(line < 0){line = 4;}
if(line > 4){line = 0;}
// print_menu(width, height, line);
}
// zürück zum alten terminal (nur linux)
#ifdef __linux__
system("tput rmcup");
#endif
return 0;
}