Skip to content

Instantly share code, notes, and snippets.

@jyi2ya
Created June 4, 2022 16:19
Show Gist options
  • Select an option

  • Save jyi2ya/ef4faec520f4ebfb1dea7c0eae0c30e2 to your computer and use it in GitHub Desktop.

Select an option

Save jyi2ya/ef4faec520f4ebfb1dea7c0eae0c30e2 to your computer and use it in GitHub Desktop.
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
int kbhit() {
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;
}
#define FRAME_CLOCK ((double)CLOCKS_PER_SEC / (double)60)
int main(void) {
clock_t next_refresh = 0;
static char frames[2][255][255];
double cnt = 0;
int height = 24;
int width = 80;
int current_frame_id = 0;
printf("\e[H\e[2J");
for (;;) {
int i, j;
next_refresh += FRAME_CLOCK;
if (kbhit() && getchar() == '\n') {
cnt += 5;
if (cnt > height)
cnt = height;
} else {
cnt -= 0.3;
if (cnt < 0.000001)
cnt = 0.000001;
}
#define f(x) (-((x) * (x) / (width * width / cnt)) + cnt)
for (i = 0; i < height; ++i)
for (j = 0; j < width; ++j)
frames[current_frame_id][i][j] = ((height - i) <= f(j)) ?
'#' : ' ';
while (clock() < next_refresh)
;
for (i = 0; i < height; ++i)
for (j = 0; j < width; ++j)
if (frames[current_frame_id][i][j] != frames[current_frame_id ^ 1][i][j])
printf("\e[%d;%dH%c", i + 1, j + 1, frames[current_frame_id][i][j]);
printf("\e[%d;%dH", height, width);
current_frame_id ^= 1;
fflush(stdout);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment