mirror of
https://github.com/AngelJumbo/lavat.git
synced 2024-11-16 04:36:40 +00:00
change the number of balls in runtime with m and l (no more malloc(sizeof(ball)*nballs) :C )
This commit is contained in:
parent
be6adee51b
commit
aa408d0b9f
61
lavat.c
61
lavat.c
@ -7,6 +7,9 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define MIN_NBALLS 5
|
||||||
|
#define MAX_NBALLS 20
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
@ -22,7 +25,10 @@ static short speedMult = 5;
|
|||||||
static short rim = 0;
|
static short rim = 0;
|
||||||
static short contained = 0;
|
static short contained = 0;
|
||||||
static float radius = 100;
|
static float radius = 100;
|
||||||
|
static int maxX,maxY;
|
||||||
|
static int speed;
|
||||||
|
|
||||||
|
void event_handler(struct tb_event event);
|
||||||
int parse_options(int argc, char *argv[]);
|
int parse_options(int argc, char *argv[]);
|
||||||
void print_help();
|
void print_help();
|
||||||
|
|
||||||
@ -35,7 +41,8 @@ int main(int argc, char *argv[]) {
|
|||||||
rim = 0;
|
rim = 0;
|
||||||
|
|
||||||
time_t t;
|
time_t t;
|
||||||
Ball *balls = malloc(sizeof(Ball) * nballs);
|
//Ball *balls = malloc(sizeof(Ball) * nballs);
|
||||||
|
Ball balls[MAX_NBALLS] = {0};
|
||||||
|
|
||||||
struct tb_event event = {0};
|
struct tb_event event = {0};
|
||||||
|
|
||||||
@ -45,10 +52,10 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
tb_hide_cursor();
|
tb_hide_cursor();
|
||||||
|
|
||||||
int maxX = tb_width();
|
maxX = tb_width();
|
||||||
int maxY = tb_height() * 2;
|
maxY = tb_height() * 2;
|
||||||
speedMult = 11 - speedMult;
|
speedMult = 11 - speedMult;
|
||||||
int speed = (((1 / (float)(maxX + maxY)) * 1000000) + 10000) * speedMult;
|
speed = (((1 / (float)(maxX + maxY)) * 1000000) + 10000) * speedMult;
|
||||||
radius = (radius * radius + (float)(maxX * maxY)) / 15000;
|
radius = (radius * radius + (float)(maxX * maxY)) / 15000;
|
||||||
|
|
||||||
int margin = contained ? radius * 10 : 0;
|
int margin = contained ? radius * 10 : 0;
|
||||||
@ -65,7 +72,7 @@ int main(int argc, char *argv[]) {
|
|||||||
custom2 = custom + 1;
|
custom2 = custom + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < nballs; i++) {
|
for (int i = 0; i < MAX_NBALLS; i++) {
|
||||||
balls[i].x = rand() % (maxX - 2 * margin) + margin;
|
balls[i].x = rand() % (maxX - 2 * margin) + margin;
|
||||||
balls[i].y = rand() % (maxY - 2 * margin) + margin;
|
balls[i].y = rand() % (maxY - 2 * margin) + margin;
|
||||||
balls[i].dx = (rand() % 2 == 0) ? -1 : 1;
|
balls[i].dx = (rand() % 2 == 0) ? -1 : 1;
|
||||||
@ -145,6 +152,20 @@ int main(int argc, char *argv[]) {
|
|||||||
tb_clear();
|
tb_clear();
|
||||||
|
|
||||||
tb_peek_event(&event, 10);
|
tb_peek_event(&event, 10);
|
||||||
|
|
||||||
|
event_handler(event);
|
||||||
|
|
||||||
|
if (event.key == TB_KEY_CTRL_C || event.key == TB_KEY_ESC ||
|
||||||
|
event.ch == 'q' || event.ch == 'Q')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
tb_shutdown();
|
||||||
|
|
||||||
|
//free(balls);
|
||||||
|
}
|
||||||
|
|
||||||
|
void event_handler(struct tb_event event){
|
||||||
switch (event.ch) {
|
switch (event.ch) {
|
||||||
case '-':
|
case '-':
|
||||||
case '_':
|
case '_':
|
||||||
@ -160,17 +181,24 @@ int main(int argc, char *argv[]) {
|
|||||||
speed = (((1 / (float)(maxX + maxY)) * 1000000) + 10000) * speedMult;
|
speed = (((1 / (float)(maxX + maxY)) * 1000000) + 10000) * speedMult;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'm':
|
||||||
|
case 'M':
|
||||||
|
if(nballs+1<=MAX_NBALLS){
|
||||||
|
nballs++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'l':
|
||||||
|
case 'L':
|
||||||
|
if(nballs-1>=MIN_NBALLS){
|
||||||
|
nballs--;
|
||||||
}
|
}
|
||||||
if (event.key == TB_KEY_CTRL_C || event.key == TB_KEY_ESC ||
|
|
||||||
event.ch == 'q' || event.ch == 'Q')
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
tb_shutdown();
|
|
||||||
|
|
||||||
free(balls);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int set_color(short *var, char *optarg) {
|
int set_color(short *var, char *optarg) {
|
||||||
|
|
||||||
if (strcmp(optarg, "red") == 0) {
|
if (strcmp(optarg, "red") == 0) {
|
||||||
@ -230,10 +258,10 @@ int parse_options(int argc, char *argv[]) {
|
|||||||
|
|
||||||
case 'b':
|
case 'b':
|
||||||
nballs = atoi(optarg);
|
nballs = atoi(optarg);
|
||||||
if (nballs > 20 || nballs < 2) {
|
if (nballs > MAX_NBALLS || nballs < MIN_NBALLS) {
|
||||||
|
|
||||||
printf("Invalid number of metaballs, only values between 2 and 20 are"
|
printf("Invalid number of metaballs, only values between %i and %i are"
|
||||||
"allowed\n");
|
"allowed\n", MIN_NBALLS,MAX_NBALLS);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -282,7 +310,10 @@ void print_help() {
|
|||||||
" -k <COLOR> Set the color of the rim if there is one."
|
" -k <COLOR> Set the color of the rim if there is one."
|
||||||
" Available colours: red, blue, yellow, green, cyan and magenta. \n"
|
" Available colours: red, blue, yellow, green, cyan and magenta. \n"
|
||||||
" -b <NBALLS> Set the number of metaballs in the simulation, "
|
" -b <NBALLS> Set the number of metaballs in the simulation, "
|
||||||
"from 2 to 20. (default: 10)\n"
|
"from %i to %i. (default: 10)\n"
|
||||||
|
" Increase or decrease the number of balls in run"
|
||||||
|
|
||||||
|
"time with the m and l keys.\n"
|
||||||
" -F <CHARS> Allows for a custom set of chars to be used\n"
|
" -F <CHARS> Allows for a custom set of chars to be used\n"
|
||||||
" Only ascii symbols are supported for now, "
|
" Only ascii symbols are supported for now, "
|
||||||
"wide/unicode chars may appear broken.\n"
|
"wide/unicode chars may appear broken.\n"
|
||||||
@ -291,5 +322,5 @@ void print_help() {
|
|||||||
" a bigger radius than the default one.\n"
|
" a bigger radius than the default one.\n"
|
||||||
" -h Print help.\n"
|
" -h Print help.\n"
|
||||||
"(Tip: Zoom out in your terminal before running the program to get a "
|
"(Tip: Zoom out in your terminal before running the program to get a "
|
||||||
"better resolution of the lava).\n");
|
"better resolution of the lava).\n",MIN_NBALLS,MAX_NBALLS);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user