commit b30a5d9d32d3ef31e2e2a59c1d2960c35449d21a
parent 4ddd895e355373d473d6c3988f9cac30db26b055
Author: Wilson Gheen <wilson@wilsonrgheen.com>
Date: Sat, 17 Dec 2022 04:27:33 -0600
Split printing functions to a header file
Diffstat:
3 files changed, 41 insertions(+), 25 deletions(-)
diff --git a/life.c b/life.c
@@ -8,6 +8,8 @@
#include <sys/stat.h>
#include <unistd.h>
+#include "print_box.h"
+
#define WIDTH(x) sizeof(x[0]) * 8
#define MAX_ROWS 24
@@ -15,33 +17,13 @@
static uint64_t gosper[MAX_ROWS] = {0x0000000800000000, 0x0000001400000000, 0x0030001a18000000, 0x0028001b18000000, 0x6604001a00000000, 0x6924001400000000, 0x0604000800000000, 0x0028000000000000, 0x0030000000000000};
#define BOARD gosper
-void print_bw(int x, int y, uint64_t ch) {
- tb_set_cell(x, y, ch, TB_BLACK, TB_WHITE);
-}
-
-void print_bl(int x, int y, uint64_t ch) {
- tb_set_cell(x, y, ch, TB_BLACK, TB_BLACK);
-}
-
void print_board(uint64_t board[], int rows, int cols) {
- int x, y;
- print_bl(0, 0, 0x250f); //heavy box corner down and right
- for(x=1; x <= cols*2; x++)
- print_bl(x, 0, 0x2501); //heavy box horizontal line
- print_bl((cols*2)+1, 0, 0x2513); //heavy box corner down and left
- for(y=1; y <= rows; y++) {
- print_bl(0, y, 0x2503); //heavy box vertical line
- print_bl(x, y, 0x2503); //heavy box vertical line
- }
- print_bl(x, y, 0x251b); //heavy box corner up and left
- while(--x>0)
- print_bl(x, y, 0x2501); //heavy box horizontal line
- print_bl(0, y, 0x2517); //heavy box corner up and right
+ print_box(rows, cols*2);
- for(y=0; y < rows; y++) {
+ for(int y=0; y < rows; y++) {
uint64_t leftBit = 1ULL << (cols - 1);
uint64_t bit=leftBit;
- for(x=1; x <= cols*2; bit=bit>>1, x+=2) {
+ for(int x=1; x <= cols*2; bit=bit>>1, x+=2) {
if (board[y] & bit) {
print_bw(x, y+1, ' ');
print_bw(x+1, y+1, ' ');
@@ -51,7 +33,7 @@ void print_board(uint64_t board[], int rows, int cols) {
tb_present();
}
-uint64_t *get_next_state(uint64_t *newBoard, uint64_t board[], int rows, int cols) {
+void get_next_state(uint64_t *newBoard, uint64_t board[], int rows, int cols) {
uint64_t m[MAX_ROWS];
for(int r=0; r < rows; r++) {
uint64_t newRow = board[r];
@@ -126,7 +108,7 @@ size_t load_board_from_file(uint64_t **board, char *filename) {
int main() {
/* uint64_t *board = BOARD;*/
uint64_t *board = 0;
- char *filename = "gliderShootsImmortalFlower";
+ char *filename = "mbytes";
const size_t rows = MAX_ROWS;
const size_t cols = sizeof board[0] * 8;
diff --git a/mbytes b/mbytes
Binary files differ.
diff --git a/print_box.h b/print_box.h
@@ -0,0 +1,34 @@
+void print_bw(int x, int y, uint64_t ch) {
+ tb_set_cell(x, y, ch, TB_BLACK, TB_WHITE);
+}
+
+void print_bl(int x, int y, uint64_t ch) {
+ tb_set_cell(x, y, ch, TB_BLACK, TB_BLACK);
+}
+
+void print_box_offset(int rows, int cols, int offx, int offy, int clear) {
+ int x,y;
+ print_bl(offx, offy, 0x250f); //heavy box corner down and right
+ for(x = offx + 1; x <= cols+offx; x++)
+ print_bl(x, offy, 0x2501); //heavy box horizontal line
+ print_bl(cols + offx + 1, offy, 0x2513); //heavy box corner down and left
+ for(y = offy + 1; y <= rows + offy; y++) {
+ print_bl(offx, y, 0x2503); //heavy box vertical line
+ print_bl(x, y, 0x2503); //heavy box vertical line
+ }
+ print_bl(x, y, 0x251b); //heavy box corner up and left
+ while(--x > offx)
+ print_bl(x, y, 0x2501); //heavy box horizontal line
+ print_bl(offx, y, 0x2517); //heavy box corner up and right
+}
+
+void print_box(int rows, int cols) {
+ print_box_offset(rows, cols, 0, 0, 1);
+}
+
+void print_msg(const char *fmt, ...) {
+ va_list vl;
+ va_start(vl, fmt);
+ tb_printf_inner(2, 10, TB_BLUE, TB_DEFAULT, NULL, fmt, vl);
+ va_end(vl);
+}