From 400e3421b123c0092988df3422f96cc210d33f7b Mon Sep 17 00:00:00 2001
From: Paul Duncan <pabs@pablotron.org>
Date: Wed, 9 Jan 2019 06:17:33 -0500
Subject: add SET_TILE

---
 src/text/draw.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

(limited to 'src')

diff --git a/src/text/draw.c b/src/text/draw.c
index e6584bb..762fb8f 100644
--- a/src/text/draw.c
+++ b/src/text/draw.c
@@ -6,7 +6,10 @@
 #include "util.h"
 #include "draw.h"
 
-static char print_buf[(SOK_LEVEL_MAX_WIDTH + 1) * SOK_LEVEL_MAX_HEIGHT + 1];
+static char buf[(SOK_LEVEL_MAX_WIDTH + 1) * SOK_LEVEL_MAX_HEIGHT + 1];
+
+#define SET_TILE(ctx, pos, c) \
+  buf[(pos).y * ((ctx)->level.size.x + 1) + (pos).x] = (c)
 
 static bool
 draw_on_size(
@@ -17,12 +20,10 @@ draw_on_size(
   UNUSED(ctx);
   UNUSED(data);
 
-  // fprintf(stderr, "size: x = %u, y = %u\n", size.x, size.y);
-
-  memset(print_buf, ' ', sizeof(print_buf));
-  print_buf[(size.x + 1) * size.y + 1] = '\0';
+  memset(buf, ' ', sizeof(buf));
+  buf[(size.x + 1) * size.y + 1] = '\0';
   for (size_t i = 0; i < size.y; i++) {
-    print_buf[(i + 1) * (size.x + 1) - 1] = '\n';
+    buf[(i + 1) * (size.x + 1) - 1] = '\n';
   }
 
   return true;
@@ -36,7 +37,7 @@ draw_on_home(
   void * const data
 ) {
   UNUSED(data);
-  print_buf[pos.y * (ctx->level.size.x + 1) + pos.x] = has_goal ? '+' : '@';
+  SET_TILE(ctx, pos, has_goal ? '+' : '@');
   return true;
 }
 
@@ -46,9 +47,8 @@ draw_on_wall(
   const sok_pos_t pos,
   void * const data
 ) {
-  // fprintf(stderr, "wall: x = %u, y = %u\n", pos.x, pos.y);
   UNUSED(data);
-  print_buf[pos.y * (ctx->level.size.x + 1) + pos.x] = '#';
+  SET_TILE(ctx, pos, '#');
   return true;
 }
 
@@ -62,7 +62,7 @@ draw_on_goal(
 ) {
   UNUSED(data);
   const char c = has_player ? '+' : (has_box ? '*' : '.');
-  print_buf[pos.y * (ctx->level.size.x + 1) + pos.x] = c;
+  SET_TILE(ctx, pos, c);
   return true;
 }
 
@@ -74,7 +74,7 @@ draw_on_box(
   void * const data
 ) {
   UNUSED(data);
-  print_buf[pos.y * (ctx->level.size.x + 1) + pos.x] = has_goal ? '*' : '$';
+  SET_TILE(ctx, pos, has_goal ? '*' : '$');
   return true;
 }
 
@@ -104,7 +104,7 @@ draw(
     "%s\n"            // level
     "%zu%s> ",        // console
     level->pack, level->name, level_num,
-    print_buf,
+    buf,
     ctx->num_moves, sok_ctx_is_done(ctx) ? " (won!)" : ""
   );
 }
-- 
cgit v1.2.3