fire/main.c

116 lines
2.1 KiB
C
Raw Normal View History

2019-12-12 22:08:35 +00:00
#include <stdlib.h>
2019-12-12 23:46:24 +00:00
#include "bool.h"
2019-12-13 03:27:13 +00:00
#include "output.h"
#include "draw.h"
#include "termbox.h"
#include "args.h"
2019-12-13 02:18:56 +00:00
#ifdef __OpenBSD__
#include "sys/types.h"
#else
2020-09-17 11:42:48 +00:00
#include "stdint.h"
#endif
2020-09-17 11:42:48 +00:00
#define VERSION "0.2.0"
2019-12-13 03:39:35 +00:00
2019-12-13 03:27:13 +00:00
// argument parsing (args.h)
char *argv0;
struct Options *opts;
2019-12-12 22:08:35 +00:00
int
2019-12-13 03:39:35 +00:00
main ( int argc, char *argv[] )
2019-12-12 22:08:35 +00:00
{
2019-12-13 21:15:29 +00:00
opts = (struct Options*) calloc(1, sizeof(struct Options*));
if (opts == NULL) {
2020-09-17 11:54:08 +00:00
EPRINT("fire: error: cannot ");
2019-12-13 21:15:29 +00:00
perror("calloc()");
}
// default args
2020-09-17 11:54:08 +00:00
opts->refresh_rate = 5;
opts->truecolor = FALSE;
size_t output_mode = TB_OUTPUT_NORMAL;
2019-12-13 21:15:29 +00:00
2019-12-13 03:39:35 +00:00
// argument parsing
argv0 = argv[0];
ARGBEGIN {
2019-12-13 21:15:29 +00:00
case 't':
2019-12-13 21:35:13 +00:00
output_mode = TB_OUTPUT_TRUECOLOR;
2019-12-13 21:15:29 +00:00
opts->truecolor = TRUE;
break;
case 'r':
opts->refresh_rate = atoi(ARGF());
break;
2019-12-13 03:39:35 +00:00
case 'V':
2020-09-17 11:54:08 +00:00
printf("%s %s\n", argv0, VERSION);
exit(EXIT_SUCCESS);
2019-12-13 03:39:35 +00:00
case 'h':
default:
2020-09-17 11:54:08 +00:00
printf("Usage: %s [-tVh] [-r rate]\n", argv0);
printf("Display a nice fiery animation.\n\n");
printf("ARGUMENTS:\n");
printf(" -r [rate] Change refresh rate. (default: 5)\n");
printf(" -t Enable truecolor. (Will not work on *rxvt)\n");
printf(" -h Display this help message and exit.\n");
printf(" -V Display version and exit.\n\n");
printf("(c) Kiëd Llaentenn, nullgemm\n");
printf("https://github.com/lptstr/fire\n");
exit(EXIT_SUCCESS);
2019-12-13 03:39:35 +00:00
} ARGEND
2019-12-12 23:46:24 +00:00
// initialize termbox
tb_init();
2019-12-13 21:35:13 +00:00
tb_select_output_mode(output_mode);
2019-12-12 23:46:24 +00:00
tb_clear();
2019-12-13 03:27:13 +00:00
struct buffer buf;
2019-12-13 04:03:58 +00:00
struct tb_event e;
2019-12-12 23:46:24 +00:00
2019-12-13 02:18:56 +00:00
// initialize drawing
init(&buf);
2019-12-12 22:08:35 +00:00
2019-12-13 02:18:56 +00:00
// animate
while (TRUE)
{
2019-12-13 03:27:13 +00:00
// clear the screen
2020-09-17 11:54:08 +00:00
//tb_clear();
2019-12-13 03:27:13 +00:00
// update framebuffer
2019-12-13 02:18:56 +00:00
dofire(&buf);
2019-12-13 03:27:13 +00:00
// draw framebuffer to terminal
2019-12-13 02:18:56 +00:00
tb_present();
2019-12-12 23:46:24 +00:00
2019-12-13 04:03:58 +00:00
// event handling
int err = (size_t) tb_peek_event(&e, opts->refresh_rate);
2019-12-13 04:03:58 +00:00
2019-12-13 04:08:35 +00:00
if (err < 0)
2019-12-13 04:03:58 +00:00
continue;
if (e.type == TB_EVENT_KEY)
{
2020-09-17 11:58:22 +00:00
switch (e.ch) {
case 'q':
goto cleanup;
default:
break;
}
2019-12-13 04:03:58 +00:00
switch (e.key)
{
2020-09-17 11:58:22 +00:00
case TB_KEY_CTRL_C:
case TB_KEY_CTRL_D:
goto cleanup;
2019-12-13 04:03:58 +00:00
default:
break;
}
}
}
2019-12-13 03:27:13 +00:00
2020-09-17 11:58:22 +00:00
cleanup:
2019-12-13 04:03:58 +00:00
cleanup(&buf);
2019-12-12 22:08:35 +00:00
return 0;
}