fire/main.c

101 lines
1.7 KiB
C
Raw Normal View History

2019-12-12 14:08:35 -08:00
#include <stdlib.h>
2019-12-12 15:46:24 -08:00
#include "bool.h"
2019-12-12 19:27:13 -08:00
#include "output.h"
#include "draw.h"
#include "types.h"
#include "termbox.h"
#include "args.h"
2019-12-12 18:18:56 -08:00
2019-12-13 13:36:16 -08:00
#define VERSION "0.2.0"
2019-12-12 19:39:35 -08:00
2019-12-12 19:27:13 -08:00
// argument parsing (args.h)
char *argv0;
struct Options *opts;
2019-12-12 14:08:35 -08:00
int
2019-12-12 19:39:35 -08:00
main ( int argc, char *argv[] )
2019-12-12 14:08:35 -08:00
{
2019-12-13 13:15:29 -08: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;
2019-12-13 13:35:13 -08:00
usize output_mode = TB_OUTPUT_NORMAL;
2019-12-13 13:15:29 -08:00
2019-12-12 19:39:35 -08:00
// argument parsing
argv0 = argv[0];
ARGBEGIN {
2019-12-13 13:15:29 -08:00
case 't':
2019-12-13 13:35:13 -08:00
output_mode = TB_OUTPUT_TRUECOLOR;
2019-12-13 13:15:29 -08:00
opts->truecolor = TRUE;
break;
case 'r':
opts->refresh_rate = atoi(ARGF());
break;
2019-12-12 19:39:35 -08: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 13:15:29 -08:00
PRINT("OPTIONS:\n\t-t\tenable true colors.\n");
PRINT("\t-V\tshow version and exit.\n");
2019-12-12 19:39:35 -08:00
PRINT("\t-h\tshow this help message and exit.\n\n");
exit(1);
} ARGEND
2019-12-12 15:46:24 -08:00
// initialize termbox
tb_init();
2019-12-13 13:35:13 -08:00
tb_select_output_mode(output_mode);
2019-12-12 15:46:24 -08:00
tb_clear();
2019-12-12 19:27:13 -08:00
struct buffer buf;
2019-12-12 20:03:58 -08:00
struct tb_event e;
2019-12-12 15:46:24 -08:00
2019-12-12 18:18:56 -08:00
// initialize drawing
init(&buf);
2019-12-12 14:08:35 -08:00
2019-12-12 18:18:56 -08:00
// animate
while (TRUE)
{
2019-12-12 19:27:13 -08:00
// clear the screen
2019-12-12 18:18:56 -08:00
tb_clear();
2019-12-12 19:27:13 -08:00
// update framebuffer
2019-12-12 18:18:56 -08:00
dofire(&buf);
2019-12-12 19:27:13 -08:00
// draw framebuffer to terminal
2019-12-12 18:18:56 -08:00
tb_present();
2019-12-12 15:46:24 -08:00
2019-12-12 20:03:58 -08:00
// event handling
2019-12-13 13:15:29 -08:00
int err = (usize) tb_peek_event(&e, opts->refresh_rate);
2019-12-12 20:03:58 -08:00
2019-12-12 20:08:35 -08:00
if (err < 0)
2019-12-12 20:03:58 -08:00
continue;
if (e.type == TB_EVENT_KEY)
{
switch (e.key)
{
case 0x03:
cleanup(&buf);
exit(0);
default:
break;
}
}
}
2019-12-12 19:27:13 -08:00
2019-12-12 20:03:58 -08:00
// perform cleanup
cleanup(&buf);
2019-12-12 15:46:24 -08:00
2019-12-12 14:08:35 -08:00
return 0;
}