changeset 504:60e04709ce0f

Refactor window backbuffer to use integer as internal storage to simplify line handling and buffer drawing.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 02 Jun 2012 22:00:25 +0300
parents bac3f9af112c
children 12939e665e49
files ui.c ui.h
diffstat 2 files changed, 48 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/ui.c	Sat Jun 02 20:03:08 2012 +0300
+++ b/ui.c	Sat Jun 02 22:00:25 2012 +0300
@@ -194,6 +194,7 @@
     }
 }
 
+
 static BOOL nnwin_get_color(char const **s, int *col)
 {
     int val = 0;
@@ -216,7 +217,26 @@
 }
 
 
-#define QPUTCH(ch) th_vputch(&(win->line->buf), &(win->line->bufsize), &(win->line->len), ch)
+#define QPUTCH(ch) nnwin_putch(&(win->line->buf), &(win->line->bufsize), &(win->line->len), col, ch)
+static BOOL nnwin_putch(int **buf, size_t *bufsize, size_t *len, int color, char ch)
+{
+    if (*buf == NULL)
+        *bufsize = *len = 0;
+
+    if (*buf == NULL || *len + 1 >= *bufsize)
+    {
+        *bufsize += TH_BUFGROW;
+        *buf = (int *) th_realloc(*buf, *bufsize * sizeof(int));
+        if (*buf == NULL)
+            return FALSE;
+    }
+
+    (*buf)[*len] = ((unsigned char) ch) | color;
+    (*len)++;
+    
+    return TRUE;
+}
+
 
 int nnwin_print(nn_window_t *win, const char *fmt)
 {
@@ -238,26 +258,15 @@
             if (*s == '½')
             {
                 QPUTCH(*s);
-                QPUTCH(*s);
-                win->line->chlen++;
             }
             else
             {
                 if (!nnwin_get_color(&s, &col))
                     return -1;
-                
-                QPUTCH('½');
-
-                if (!th_growbuf(&(win->line->buf), &(win->line->bufsize), &(win->line->len), sizeof(int)))
-                    return -2;
-
-                memcpy(win->line->buf + win->line->len, &col, sizeof(int));
-                win->line->len += sizeof(int);
             }
         }
         else if (*s == '\n')
         {
-            QPUTCH(0);
             th_ringbuf_add(win->data, win->line);
             win->line = NULL;
             win->dirty = TRUE;
@@ -265,7 +274,6 @@
         else if (*s != '\r')
         {
             QPUTCH((unsigned char) *s == 255 ? ' ' : *s);
-            win->line->chlen++;
         }
 
         s++;
@@ -304,7 +312,7 @@
     int col = 0;
     while (*s)
     {
-        if (clip && getcurx(win) >= getmaxx(win))
+        if (clip && getcurx(win) >= scrWidth)
             return;
 
         if (*s == '½')
@@ -357,33 +365,32 @@
             nn_line_t *line = buf->data[offs];
             if (line != NULL)
             {
-                const char *s = line->buf;
-                int col = 0;
-                y -= 1 + (line->chlen / scrWidth);
+                const int *s = line->buf;
+                size_t pos;
+
+                y -= (line->len / scrWidth);
+                if (line->len % scrWidth != 0)
+                    y -= 1;
+
+                if (y < 0)
+                {
+                    size_t r;
+                    int x;
+                    for (r = -y, x = 0, pos = 0; r && pos < line->len; pos++)
+                    {
+                        if (++x >= scrWidth)
+                        {
+                            x = 0;
+                            r++;
+                        }
+                    }
+                    y = 0;
+                }
+
                 wmove(stdscr, y, 0);
 
-                while (*s)
-                {
-                    if (*s == '½')
-                    {
-                        s++;
-                        if (*s == '½')
-                        {
-                            waddch(stdscr, ((unsigned char) *s) | col);
-                            s++;
-                        }
-                        else
-                        {
-                            memcpy(&col, s, sizeof(int));
-                            s += sizeof(int);
-                        }
-                    }
-                    else
-                    {
-                        waddch(stdscr, ((unsigned char) *s) | col);
-                        s++;
-                    }
-                }
+                for (pos = 0; pos < line->len; pos++)
+                    waddch(stdscr, s[pos]);
             }
         }
 
--- a/ui.h	Sat Jun 02 20:03:08 2012 +0300
+++ b/ui.h	Sat Jun 02 22:00:25 2012 +0300
@@ -25,8 +25,8 @@
 
 typedef struct
 {
-    char *buf;
-    size_t chlen, len, bufsize;
+    int *buf;
+    size_t len, bufsize;
 } nn_line_t;