diff options
author | KatolaZ <katolaz@freaknet.org> | 2019-07-26 22:13:33 +0100 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2019-07-26 22:13:33 +0100 |
commit | 0adbc2a776c36b736bb5acb7184ba559d9428bc4 (patch) | |
tree | eee749a383e9220d12b21a6d74e881400c64b765 /gramscii.c | |
parent | 8f25f7b9abb68e2efbc732a69f09d795ad79433c (diff) |
towards dynamic screen management
Diffstat (limited to 'gramscii.c')
-rw-r--r-- | gramscii.c | 67 |
1 files changed, 44 insertions, 23 deletions
@@ -33,6 +33,13 @@ #include "arg.h" +typedef struct{ + int sz; + int lst; + char *s; +} line_t; + + #define MOVE 0x00 #define BOX 0x01 #define ARROW 0x02 @@ -71,9 +78,10 @@ #define MIN(x,y) (x) < (y) ? (x) : (y) #define MAX(x,y) (x) > (y) ? (x) : (y) -/** #define DEBUG 1 **/ +#define DEBUG 1 -char **screen; +line_t *screen; +int num_lines; int WIDTH, HEIGHT; int state; @@ -113,7 +121,7 @@ struct termios t1, t2, t3; void dump_lines(){ int i; for (i=0; i<HEIGHT; i++){ - printf("%s\n", screen[i]); + printf("%s\n", screen[i].s); } } @@ -179,7 +187,7 @@ void status_bar(){ else printf(" *%s*", fname ); #ifdef DEBUG - printf(" '%d' ", screen[y][x]); + printf(" '%d' ", screen[y].s[x]); #endif printf("\033[0m"); fflush(stdout); @@ -232,13 +240,24 @@ void show_cursor(){ fflush(stdout); } -void set_cur(char c){ - screen[y][x] = c; -} -void set_xy(int x, int y, char c){ +void set_xy(int _x, int _y, char c){ /* FIXME: check if x and y are valid!!!! */ - screen[y][x] = c; + if (screen[_y].sz < _x + 2){ + screen[_y].sz = (_x +2) * 2; + screen[_y].s = realloc(screen[_y].s, screen[_y].sz * sizeof(char)); + } + while (screen[_y].lst<_x){ + screen[_y].s[screen[_y].lst] = BG; + screen[_y].lst += 1; + } + screen[_y].s[_x] = c; + if (_x == screen[_y].lst) + screen[_y].s[_x+1] = '\0'; +} + +void set_cur(char c){ + set_xy(x, y, c); } void draw_xy(int x, int y, char c){ @@ -254,7 +273,7 @@ void update_current(){ if (silent) return; printf("\033[%d'%df",y+1,x+1); - putchar(screen[y][x]); + putchar(screen[y].s[x]); fflush(stdout); } @@ -282,7 +301,7 @@ void erase_box(int x1, int y1, char c){ void erase_screen(){ int i; for(i=0;i<HEIGHT; i++) - erase_line(screen[i]); + erase_line(screen[i].s); } void check_bound(){ @@ -311,7 +330,7 @@ void redraw(){ return; printf("\033[2J\033[1;1H"); for (i=0;i<HEIGHT;i++){ - fprintf(stdout,"%s\n",screen[i]); + fprintf(stdout,"%s\n",screen[i].s); } status_bar(); show_cursor(); @@ -774,7 +793,7 @@ void write_file(FILE *fc){ return; } for (i=0; i<HEIGHT; i++){ - fprintf(fout, "%s\n", screen[i]); + fprintf(fout, "%s\n", screen[i].s); } fclose(fout); modified = 0; @@ -800,10 +819,10 @@ void load_file(FILE *fc){ get_string(fc, "Load file: ", newfname, 255); if ((fin=fopen(newfname, "r")) != NULL){ i = 0; - while((fgets(screen[i], WIDTH+2, fin)) != NULL && i<HEIGHT) - screen[i++][WIDTH-1]='\0'; + while((fgets(screen[i].s, WIDTH+2, fin)) != NULL && i<HEIGHT) + screen[i++].s[WIDTH-1]='\0'; for(;i<HEIGHT; i++){ - erase_line(screen[i]); + erase_line(screen[i].s); } fclose(fin); } @@ -872,24 +891,26 @@ void init_screen(){ WIDTH=80; HEIGHT=24; } - screen = malloc(HEIGHT * sizeof(char *)); + screen = malloc(HEIGHT * sizeof(line_t)); + num_lines = HEIGHT; if (screen == NULL){ perror("allocating screen"); exit(1); } for (i=0; i<HEIGHT; i++){ - screen[i] = malloc((WIDTH+1) * sizeof(char)); - if (screen[i] == NULL){ - perror("allocating screen[i]"); + screen[i].sz = WIDTH+1; + screen[i].s = malloc((screen[i].sz) * sizeof(char)); + if (screen[i].s == NULL){ + perror("allocating screen[i].s"); exit(1); } - memset(screen[i], ' ', WIDTH * sizeof(char)); - screen[i][WIDTH]='\0'; + memset(screen[i].s, BG, screen[i].sz); + screen[i].lst = WIDTH; + screen[i].s[screen[i].lst+1]='\0'; } reset_styles(); } - void init(){ signal(SIGHUP, cleanup); |