ref: c41c9befad2e503e0abcb3f2d8521f2147647e7d
parent: 9b15b51c3f5dc2e67e1e123892b88d8b8f0c6d80
author: cancel <cancel@cancel.fm>
date: Sun Nov 25 02:04:00 EST 2018
Split tui and cli executables Now built as separate targets
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,9 @@
else
library_flags := -lncursesw
endif
-source_files := field.c sim.c main.c
+common_source_files := field.c sim.c
+tui_source_files := $(common_source_files) tui_main.c
+cli_source_files := $(common_source_files) cli_main.c
all: debug
@@ -18,13 +20,27 @@
build/debug build/release: | build
@mkdir $@
+.PHONY: debug_cli
+debug_cli: | build/debug
+ @cc $(basic_flags) $(debug_flags) $(sanitize_flags) $(cli_source_files) -o build/debug/orca $(library_flags)
+
+.PHONY: debug_ui
+debug_ui: | build/debug
+ @cc $(basic_flags) $(debug_flags) $(sanitize_flags) $(tui_source_files) -o build/debug/orca_ui $(library_flags)
+
.PHONY: debug
-debug: | build/debug
- @cc $(basic_flags) $(debug_flags) $(sanitize_flags) $(source_files) -o build/debug/acro $(library_flags)
+debug: debug_cli debug_ui
+.PHONY: release_cli
+release_cli: | build/release
+ @cc $(basic_flags) $(release_flags) $(cli_source_files) -o build/release/orca $(library_flags)
+
+.PHONY: release_tui
+release_tui: | build/release
+ @cc $(basic_flags) $(release_flags) $(tui_source_files) -o build/release/orca_tui $(library_flags)
+
.PHONY: release
-release: | build/release
- @cc $(basic_flags) $(release_flags) $(source_files) -o build/release/acro $(library_flags)
+release: release_cli release_tui
.PHONY: clean
clean:
--- /dev/null
+++ b/cli_main.c
@@ -1,0 +1,8 @@
+#include "base.h"
+#include "field.h"
+#include <unistd.h>
+
+int main() {
+ fprintf(stderr, "Not implemented\n");
+ return 0;
+}
--- a/main.c
+++ /dev/null
@@ -1,86 +1,0 @@
-#include "base.h"
-#include "field.h"
-#include <locale.h>
-#include <unistd.h>
-
-int main() {
- // Enable UTF-8 by explicitly initializing our locale before initializing
- // ncurses.
- setlocale(LC_ALL, "");
- // Initialize ncurses
- initscr();
- // Allow ncurses to control newline translation. Fine to use with any modern
- // terminal, and will let ncurses run faster.
- nonl();
- // Set interrupt keys (interrupt, break, quit...) to not flush. Helps keep
- // ncurses state consistent, at the cost of less responsive terminal
- // interrupt. (This will rarely happen.)
- intrflush(stdscr, FALSE);
- // Receive keyboard input immediately, and receive shift, control, etc. as
- // separate events, instead of combined with individual characters.
- raw();
- // Don't echo keyboard input
- noecho();
- // Also receive arrow keys, etc.
- keypad(stdscr, TRUE);
- // Hide the terminal cursor
- curs_set(0);
- // Don't block on calls like getch() -- have it ERR immediately if the user
- // hasn't typed anything. That way we can mix other timers in our code,
- // instead of being a slave only to terminal input.
- // nodelay(stdscr, TRUE);
-
- Field field;
- field_init_zeros(&field, 16, 16);
-
- printw("Type any character to fill it in an alternating grid, or\ntype '");
- attron(A_BOLD);
- printw("q");
- attroff(A_BOLD);
- printw("' to quit\n");
- refresh();
-
- char fill_char = '?';
- for (;;) {
- int ch = getch();
- clear();
- if (ch == 'q')
- break;
- // ncurses gives us ERR if there was no user input. We'll sleep for 0
- // seconds, so that we'll yield CPU time to the OS instead of looping as
- // fast as possible. This avoids battery drain/excessive CPU usage. There
- // are better ways to do this that waste less CPU, but they require doing a
- // little more work on each individual platform (Linux, Mac, etc.)
- if (ch == ERR) {
- sleep(0);
- continue;
- }
- // ncurses gives us the special value KEY_RESIZE if the user didn't
- // actually type anything, but the terminal resized. If that happens to us,
- // just re-use the fill character from last time.
- char new_fill_char;
- if (ch < CHAR_MIN || ch > CHAR_MAX || ch == KEY_RESIZE)
- new_fill_char = '?';
- else
- new_fill_char = (char)ch;
- int term_height = getmaxy(stdscr);
- int term_width = getmaxx(stdscr);
- assert(term_height >= 0 && term_width >= 0);
- if (new_fill_char != fill_char) {
- fill_char = new_fill_char;
- }
- field_fill_subrect(&field, 0, 0, field.height, field.width, '.');
- field_fill_subrect(&field, 1, 1, field.height - 2, field.width - 2,
- fill_char);
- field_debug_draw(stdscr, &field, 0, 0);
- field_copy_subrect(&field, &field, 0, 0, 4, 4, 8, 8);
- field_copy_subrect(&field, &field, 0, 0, 0, 0, 0, 0);
- field_debug_draw(stdscr, &field, field.height + 1, 0);
- field_copy_subrect(&field, &field, 6, 6, 9, 9, 30, 30);
- field_debug_draw(stdscr, &field, 0, field.width + 1);
- refresh();
- }
- field_deinit(&field);
- endwin();
- return 0;
-}
--- /dev/null
+++ b/tui_main.c
@@ -1,0 +1,86 @@
+#include "base.h"
+#include "field.h"
+#include <locale.h>
+#include <unistd.h>
+
+int main() {
+ // Enable UTF-8 by explicitly initializing our locale before initializing
+ // ncurses.
+ setlocale(LC_ALL, "");
+ // Initialize ncurses
+ initscr();
+ // Allow ncurses to control newline translation. Fine to use with any modern
+ // terminal, and will let ncurses run faster.
+ nonl();
+ // Set interrupt keys (interrupt, break, quit...) to not flush. Helps keep
+ // ncurses state consistent, at the cost of less responsive terminal
+ // interrupt. (This will rarely happen.)
+ intrflush(stdscr, FALSE);
+ // Receive keyboard input immediately, and receive shift, control, etc. as
+ // separate events, instead of combined with individual characters.
+ raw();
+ // Don't echo keyboard input
+ noecho();
+ // Also receive arrow keys, etc.
+ keypad(stdscr, TRUE);
+ // Hide the terminal cursor
+ curs_set(0);
+ // Don't block on calls like getch() -- have it ERR immediately if the user
+ // hasn't typed anything. That way we can mix other timers in our code,
+ // instead of being a slave only to terminal input.
+ // nodelay(stdscr, TRUE);
+
+ Field field;
+ field_init_zeros(&field, 16, 16);
+
+ printw("Type any character to fill it in an alternating grid, or\ntype '");
+ attron(A_BOLD);
+ printw("q");
+ attroff(A_BOLD);
+ printw("' to quit\n");
+ refresh();
+
+ char fill_char = '?';
+ for (;;) {
+ int ch = getch();
+ clear();
+ if (ch == 'q')
+ break;
+ // ncurses gives us ERR if there was no user input. We'll sleep for 0
+ // seconds, so that we'll yield CPU time to the OS instead of looping as
+ // fast as possible. This avoids battery drain/excessive CPU usage. There
+ // are better ways to do this that waste less CPU, but they require doing a
+ // little more work on each individual platform (Linux, Mac, etc.)
+ if (ch == ERR) {
+ sleep(0);
+ continue;
+ }
+ // ncurses gives us the special value KEY_RESIZE if the user didn't
+ // actually type anything, but the terminal resized. If that happens to us,
+ // just re-use the fill character from last time.
+ char new_fill_char;
+ if (ch < CHAR_MIN || ch > CHAR_MAX || ch == KEY_RESIZE)
+ new_fill_char = '?';
+ else
+ new_fill_char = (char)ch;
+ int term_height = getmaxy(stdscr);
+ int term_width = getmaxx(stdscr);
+ assert(term_height >= 0 && term_width >= 0);
+ if (new_fill_char != fill_char) {
+ fill_char = new_fill_char;
+ }
+ field_fill_subrect(&field, 0, 0, field.height, field.width, '.');
+ field_fill_subrect(&field, 1, 1, field.height - 2, field.width - 2,
+ fill_char);
+ field_debug_draw(stdscr, &field, 0, 0);
+ field_copy_subrect(&field, &field, 0, 0, 4, 4, 8, 8);
+ field_copy_subrect(&field, &field, 0, 0, 0, 0, 0, 0);
+ field_debug_draw(stdscr, &field, field.height + 1, 0);
+ field_copy_subrect(&field, &field, 6, 6, 9, 9, 30, 30);
+ field_debug_draw(stdscr, &field, 0, field.width + 1);
+ refresh();
+ }
+ field_deinit(&field);
+ endwin();
+ return 0;
+}