fire/main.c

106 lines
1.8 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) {
PRINT("fire: error: cannot ");
perror("calloc()");
}
// default args
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':
PRINT("%s %s\n", argv0, VERSION);
exit(0);
case 'h':
default:
PRINT("fire %s\n(c) Kied Llaentenn and contributors\n", VERSION);
PRINT("https://github.com/lptstr/fire\n");
PRINT("\nUSAGE:\n\t$ fire\n\n");
2019-12-13 21:15:29 +00:00
PRINT("OPTIONS:\n\t-t\tenable true colors.\n");
PRINT("\t-V\tshow version and exit.\n");
2019-12-13 03:39:35 +00:00
PRINT("\t-h\tshow this help message and exit.\n\n");
exit(1);
} 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
2019-12-13 02:18:56 +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)
{
switch (e.key)
{
case 0x03:
cleanup(&buf);
exit(0);
default:
break;
}
}
}
2019-12-13 03:27:13 +00:00
2019-12-13 04:03:58 +00:00
// perform cleanup
cleanup(&buf);
2019-12-12 23:46:24 +00:00
2019-12-12 22:08:35 +00:00
return 0;
}