shithub: orca

Download patch

ref: f4ff0e71ad64edef5f74412cbc069414a69e9f92
parent: 7616d6c6994eb0acad51913de5b75ab74fb3d03d
author: cancel <cancel@cancel.fm>
date: Sun Nov 25 02:39:41 EST 2018

Add basic printing of Field to stdout

--- a/base.h
+++ b/base.h
@@ -1,9 +1,9 @@
 #pragma once
 #include <assert.h>
 #include <limits.h>
-#include <ncurses.h>
 #include <stdbool.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
--- a/cli_main.c
+++ b/cli_main.c
@@ -2,7 +2,15 @@
 #include "field.h"
 #include <unistd.h>
 
-int main() {
-  fprintf(stderr, "Not implemented\n");
+int main(int argc, char** argv) {
+  (void)argc;
+  (void)argv;
+  Field field;
+  field_init_fill(&field, 32, 32, '.');
+  field_fill_subrect(&field, 1, 1, field.height - 2, field.width - 2, 'a');
+  field_fill_subrect(&field, 2, 2, field.height - 4, field.width - 4, 'b');
+  field_fill_subrect(&field, 3, 3, field.height - 6, field.width - 6, '.');
+  field_fput(&field, stdout);
+  field_deinit(&field);
   return 0;
 }
--- a/field.c
+++ b/field.c
@@ -124,23 +124,26 @@
   f->buffer[y * f_width + x] = term;
 }
 
-void field_debug_draw(WINDOW* win, Field* f, int offset_y, int offset_x) {
-  enum { Line_buffer_count = 4096 };
-  cchar_t line_buffer[Line_buffer_count];
-  wchar_t wcs[2];
-  wcs[1] = '\0';
+void field_fput(Field* f, FILE* stream) {
+  enum { Column_buffer_count = 4096 };
+  char out_buffer[Column_buffer_count];
   size_t f_height = f->height;
   size_t f_width = f->width;
   Term* f_buffer = f->buffer;
-  if (f_width > Line_buffer_count)
+  if (f_width > Column_buffer_count - 2)
     return;
   for (size_t iy = 0; iy < f_height; ++iy) {
     Term* row_p = f_buffer + f_width * iy;
     for (size_t ix = 0; ix < f_width; ++ix) {
-      wcs[0] = row_p[ix];
-      setcchar(line_buffer + ix, wcs, A_NORMAL, 0, NULL);
+      char c = row_p[ix];
+      if (c >= '#' && c <= '~') {
+        out_buffer[ix] = c;
+      } else {
+        out_buffer[ix] = '!';
+      }
     }
-    move(iy + offset_y, offset_x);
-    wadd_wchnstr(win, line_buffer, (int)f_width);
+    out_buffer[f_width] = '\n';
+    out_buffer[f_width + 1] = '\0';
+    fputs(out_buffer, stream);
   }
 }
--- a/field.h
+++ b/field.h
@@ -11,4 +11,5 @@
                         Term fill_char);
 Term field_peek(Field* f, U32 y, U32 x);
 void field_poke(Field* f, U32 y, U32 x, Term term);
-void field_debug_draw(WINDOW* win, Field* f, int offset_y, int offset_x);
+
+void field_fput(Field* f, FILE* stream);
--- a/sim.c
+++ b/sim.c
@@ -2,4 +2,5 @@
 #include "sim.h"
 
 void orca_run(Field* f) {
+  (void)f;
 }
--- a/tui_main.c
+++ b/tui_main.c
@@ -1,6 +1,7 @@
 #include "base.h"
 #include "field.h"
 #include <locale.h>
+#include <ncurses.h>
 #include <unistd.h>
 
 int main() {
@@ -72,12 +73,12 @@
     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_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_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);
+    // field_debug_draw(stdscr, &field, 0, field.width + 1);
     refresh();
   }
   field_deinit(&field);