Created
June 15, 2016 07:17
-
-
Save idimiter/429afe77f0c58b68b969f4363f7a40a6 to your computer and use it in GitHub Desktop.
Having some fun with the bash escape sequences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* bash_fun.c | |
* | |
* Dimitar T. Dimitrov | |
* 15.06.2016 | |
* | |
*/ | |
#include <sys/ioctl.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
static const double PI = 3.1415926535; | |
int kbhit() { | |
struct timeval tv = { 0L, 0L }; | |
fd_set fds; | |
FD_ZERO(&fds); | |
FD_SET(0, &fds); | |
return select(1, &fds, NULL, NULL, &tv); | |
} | |
void clearScreen() { | |
printf("\ec"); | |
} | |
void putCursor(int x, int y) { | |
printf("\e[%d;%dH", x, y); | |
} | |
void putPixel(int x, int y, int color) | |
{ | |
putCursor(x, y); | |
printf("\e[%dm*", color); | |
} | |
void circle(int x, int y, int r, int color) { | |
int i, angle, x1, y1; | |
for(i = 0; i < 360; i++) { | |
angle = i; | |
x1 = r * cos(angle * PI / 180); | |
y1 = r * sin(angle * PI / 180); | |
putPixel(x + x1, y + y1, color); | |
} | |
} | |
int main(int ac, char **av, char **ev) { | |
struct winsize w; | |
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); // Get terminal size | |
printf("lines %d\n", w.ws_row); | |
printf("columns %d\n", w.ws_col); | |
while (!kbhit()) { | |
clearScreen(); | |
circle(20, w.ws_col * 0.5f, 10, 34); | |
circle(10, w.ws_col * 0.5f, 5, 33); | |
circle(30, w.ws_col * 0.5f, 5, 35); | |
circle(20, w.ws_col * 0.5f - 10, 5, 36); | |
circle(20, w.ws_col * 0.5f + 10, 5, 37); | |
usleep(16670); // ~ 60 fps | |
} | |
putCursor(w.ws_row, 0); | |
printf("\e[0m"); // Reset color | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment