From 4f8fde9eb5ccba385b1f89742e2e3dcfd0738244 Mon Sep 17 00:00:00 2001 From: violette Date: Tue, 13 May 2025 00:48:04 -0400 Subject: [PATCH] fix: now working on glibc fixed a bug were stdin was set to nonblocking and never reset back to blocking. This caused STDOUT to become nonblock, and for some reason glibc cannot deal with it, but every other libc could. uh. --- Makefile | 2 +- main.c | 107 +++++++++++++++++++++++++++++-------------------------- 2 files changed, 58 insertions(+), 51 deletions(-) diff --git a/Makefile b/Makefile index 4334cef..1540438 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS = -lm build: - cc ./main.c $(CFLAGS) -o ./cube + cc ./main.c $(CFLAGS) -DVBUF -o ./cube run: build ./cube diff --git a/main.c b/main.c index e67f532..562e42d 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,7 @@ * ---------------------------------------------------------------------------- */ +#include #include #include #include @@ -17,7 +18,6 @@ #include #include "json.h" -#define VBUF 1 #define SCREEN_WIDTH 80 #define SCREEN_HEIGHT 32 #define CUBE_WIDTH 10 @@ -71,7 +71,7 @@ double interpolationStep = 0; double zBuffer[SCREEN_HEIGHT * SCREEN_WIDTH]; char output[SCREEN_HEIGHT * SCREEN_WIDTH]; #ifdef VBUF -char buf[SCREEN_HEIGHT * SCREEN_WIDTH + 30 * 20]; // stdout buffer +char buf[SCREEN_HEIGHT * SCREEN_WIDTH * 7 * 2]; // stdout buffer #endif static volatile char shouldBreak = 1; @@ -94,7 +94,7 @@ struct Quaternions mult(struct Quaternions q, double x, double y, double z) { //p = q * p * qbar - struct Quaternions res; + struct Quaternions res; res.w = 0; res.x = x * (SQ(q.w) + SQ(q.x) - SQ(q.y) - SQ(q.z)) @@ -154,7 +154,7 @@ chooseColor(char c) return " "; default: //unused, but issues no warning - return ""; + return "\033[0m"; } } @@ -181,29 +181,29 @@ chooseMainFace() } void -printFrontFace(struct json_object_element_s *elt) +printFrontFace(struct json_object_element_s *elt, int row) { - printf("Contacts:\r\n"); + printf("\033[%dCContacts:\r\n", row); struct json_object_element_s *obj = json_value_as_object(elt->value)->start; while (obj != NULL) { struct json_string_s *str = json_value_as_string(obj->value); - printf(" %s: %s\r\n", obj->name->string, str->string); + printf("\033[%dC%s: %s\r\n", row + 4, obj->name->string, str->string); obj = obj->next; } } void -printLefttFace(struct json_object_element_s *elt) +printLefttFace(struct json_object_element_s *elt, int row) { - printf("Skills:\r\n"); + printf("\033[%dCSkills:\r\n", row); struct json_array_element_s *arr = json_value_as_array(elt->value)->start; while (arr != NULL) { struct json_object_element_s *obj = json_value_as_object(arr->value)->start; - printf(" %s\r\n", json_value_as_string(obj->value)->string); + printf("\033[%dC%s\r\n", row + 4, json_value_as_string(obj->value)->string); struct json_array_element_s *arr2 = json_value_as_array(obj->next->value)->start; while (arr2 != NULL) { struct json_string_s *str = json_value_as_string(arr2->value); - printf(" %s\r\n", str->string); + printf("\033[%dC%s\r\n", row + 8, str->string); arr2 = arr2->next; } arr = arr->next; @@ -211,21 +211,21 @@ printLefttFace(struct json_object_element_s *elt) } void -printRightFace(struct json_object_element_s *elt) +printRightFace(struct json_object_element_s *elt, int row) { - printf("Languages:\r\n"); + printf("\033[%dCLanguages:\r\n", row); struct json_array_element_s *arr = json_value_as_array(elt->value)->start; while (arr != NULL) { struct json_object_element_s *obj = json_value_as_object(arr->value)->start; - printf(" %s: %s / 5\r\n", json_value_as_string(obj->value)->string, json_value_as_number(obj->next->value)->number); + printf("\033[%dC%s: %s / 5\r\n", row + 4, json_value_as_string(obj->value)->string, json_value_as_number(obj->next->value)->number); arr = arr->next; } } void -printBottomFace(struct json_object_element_s *elt) +printBottomFace(struct json_object_element_s *elt, int row) { - printf("Professional experience:\r\n"); + printf("\033[%dCProfessional experience:\r\n", row); struct json_array_element_s *arr = json_value_as_array(elt->value)->start; while (arr != NULL) { struct json_object_element_s *obj = json_value_as_object(arr->value)->start; @@ -235,9 +235,9 @@ printBottomFace(struct json_object_element_s *elt) struct json_string_s *to = json_value_as_string(obj->next->next->next->value); struct json_array_element_s *descr = json_value_as_array(obj->next->next->next->next->value)->start; - printf(" %s: %s ; %s -> %s\r\n", json_value_as_string(company->value)->string, position->string, from->string, to->string); + printf("\033[%dC%s: %s ; %s -> %s\r\n", row + 4, json_value_as_string(company->value)->string, position->string, from->string, to->string); while (descr != NULL) { - printf(" %s\r\n", json_value_as_string(descr->value)->string); + printf("\033[%dC%s\r\n", row + 8, json_value_as_string(descr->value)->string); descr = descr->next; } printf("\r\n"); @@ -246,13 +246,13 @@ printBottomFace(struct json_object_element_s *elt) } void -printBackFace(struct json_object_element_s *elt) +printBackFace(struct json_object_element_s *elt, int row) { - printf("Projects:\r\n"); + printf("\033[%dCProjects:\r\n", row); struct json_object_element_s *obj = json_value_as_object(elt->value)->start; //ignore personnal projects - printf(" - This cube! its made using Quaternion for 3d rotation, " - "from scratch (except the CV text), in C\r\n"); + printf("\033[%dC- This cube! its made using Quaternion for 3d rotation, " + "from scratch (except the CV text), in C\r\n", row + 4); printf("\r\n"); struct json_array_element_s *arr = json_value_as_array(obj->next->value)->start; while (arr != NULL) { @@ -261,9 +261,9 @@ printBackFace(struct json_object_element_s *elt) struct json_array_element_s *descr = json_value_as_array(json_value_as_object(arr->value)->start->next->value)->start; struct json_number_s *year = json_value_as_number(json_value_as_object(arr->value)->start->next->next->next->value); - printf(" - %s, %s\r\n", json_value_as_string(descr->value)->string, year->number); + printf("\033[%dC- %s, %s\r\n", row + 4, json_value_as_string(descr->value)->string, year->number); while (descr != NULL) { - printf(" o %s\r\n", json_value_as_string(descr->value)->string); + printf("\033[%dCo %s\r\n", row + 8, json_value_as_string(descr->value)->string); descr = descr->next; } printf("\r\n"); @@ -272,9 +272,9 @@ printBackFace(struct json_object_element_s *elt) } void -printTopFace(struct json_object_element_s *elt) +printTopFace(struct json_object_element_s *elt, int row) { - printf("Education:\r\n"); + printf("\033[%dCEducation:\r\n", row); struct json_array_element_s *arr = json_value_as_array(elt->value)->start; while (arr != NULL) { struct json_object_element_s *obj = json_value_as_object(arr->value)->start; @@ -289,18 +289,18 @@ printTopFace(struct json_object_element_s *elt) struct json_string_s *to = json_value_as_string(obj->next->next->next->next->next->value); - printf(" %s: %s -> %s \r\n", univ_name->string, from->string, to->string); + printf("\033[%dC%s: %s -> %s \r\n", row + 4, univ_name->string, from->string, to->string); if (track == NULL || strcmp(track->string, "None") == 0) - printf(" %s, %s\r\n", degree->string, major->string); + printf("\033[%dC%s, %s\r\n", row + 8, degree->string, major->string); else - printf(" %s %s, %s\r\n", degree->string, major->string, track->string); + printf("\033[%dC%s %s, %s\r\n", row + 8, degree->string, major->string, track->string); printf("\r\n"); arr = arr->next; } } void -printCV(char face, struct json_object_element_s *r) +printCV(char face, struct json_object_element_s *r, int row) { struct json_object_element_s start = *r; struct json_object_element_s *next = &start; @@ -310,25 +310,25 @@ printCV(char face, struct json_object_element_s *r) for (; k < face; ++k) next = next->next; - char *res; + char *res; switch (face) { case FACE_FRONT: - printFrontFace(next); + printFrontFace(next, row); break; case FACE_LEFT: - printLefttFace(next); + printLefttFace(next, row); break; case FACE_RIGHT: - printRightFace(next); + printRightFace(next, row); break; case FACE_BOTTOM: - printBottomFace(next); + printBottomFace(next, row); break; case FACE_BACK: - printBackFace(next); + printBackFace(next, row); break; case FACE_TOP: - printTopFace(next); + printTopFace(next, row); break; default: break; @@ -340,14 +340,21 @@ printAscii(struct json_value_s *json) { char face = chooseMainFace(); struct json_object_element_s *root = json_value_as_object(json)->start; - printCV(face, root); - for (int k = 0; k < SCREEN_WIDTH * SCREEN_HEIGHT; ++k) - printf("%s", k % SCREEN_WIDTH ? chooseColor(output[k]) : "\r\n"); - printf("\r\n"); -#ifdef VBUF + struct winsize w; + ioctl(0, TIOCGWINSZ, &w); + + printCV(face, root, w.ws_row); + + printf("\033[%d;%dH", (int)((w.ws_row - SCREEN_HEIGHT) * 1.5 / 2), (w.ws_col - SCREEN_WIDTH) / 2); + + for (int k = 0; k < SCREEN_WIDTH * SCREEN_HEIGHT; ++k) { + if (k % SCREEN_WIDTH) + printf("%s", chooseColor(output[k])); + else + printf("\r\n\033[%dC", w.ws_row); + } fflush(stdout); -#endif } void @@ -363,10 +370,9 @@ rotateCube(double cubeX, double cubeY, double cubeZ, char ch) int screenY = (int)(SCREEN_HEIGHT / 2) + floor(((y) * K1 / 2) * invZ); //TODO luminescence - if (screenX > SCREEN_WIDTH || screenX < 0) - return; + if (screenX > SCREEN_WIDTH || screenX < 0) return; - int idx = screenY * SCREEN_WIDTH + screenX; + int idx = screenY * SCREEN_WIDTH + screenX; if (idx >= 0 && idx < SCREEN_WIDTH * SCREEN_HEIGHT) if (zBuffer[idx] < invZ) { zBuffer[idx] = invZ; @@ -379,6 +385,7 @@ intHandler(int unused) { shouldBreak = 0; tcsetattr(STDIN_FILENO, TCSANOW, &originalTerm); + printf("\033[2J\033[?1049l"); // escape fullscreen mode } void @@ -496,12 +503,11 @@ handleAngle(char *input) char getInput(char *keyboardInput) { - fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); + fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK); char c = getchar(); if (c == '\033') { if ((c = getchar()) == '[') switch (c = getchar()) { - //the real value case 'A': c = 'W'; break; @@ -521,7 +527,7 @@ getInput(char *keyboardInput) *keyboardInput = c; while ((c = getchar()) != '\n' && c != EOF) {} //clean stdin // - fcntl(STDIN_FILENO, F_SETFL, ~O_NONBLOCK); + fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) & ~O_NONBLOCK); handleAngle(keyboardInput); return c; } @@ -547,6 +553,7 @@ readJson() int main() { + printf("\033[?1049h\033[H"); // enter fullscreen mode char keyboardInput = 0; signal(SIGINT, intHandler); tcgetattr(STDIN_FILENO, &originalTerm); @@ -583,7 +590,7 @@ main() } } - printf("\x1b[2J"); + printf("\033[1;1H\033[2J"); printAscii(json); getInput(&keyboardInput); usleep(1000000 / 60); // 60fps max