notcurses/src/demo/grid.c

269 lines
7.9 KiB
C
Raw Normal View History

2019-11-25 02:01:25 +00:00
#include <unistd.h>
#include "demo.h"
2019-11-25 07:22:15 +00:00
static void
release_cells(struct ncplane* n,
cell* ul, cell* uc, cell* ur,
cell* cl, cell* cc, cell* cr,
cell* ll, cell* lc, cell* lr){
cell_release(n, ul);
cell_release(n, uc);
cell_release(n, ur);
cell_release(n, cl);
cell_release(n, cc);
cell_release(n, cr);
cell_release(n, ll);
cell_release(n, lc);
cell_release(n, lr);
}
static int
prep_cells2(struct ncplane* n,
cell* ul, cell* uc, cell* ur,
cell* cl, cell* cc, cell* cr,
cell* ll, cell* lc, cell* lr){
cell_init(ul);
cell_init(uc);
cell_init(cl);
cell_init(cr);
cell_init(ll);
cell_init(lc);
cell_init(lr);
cell_init(ur);
cell_init(cc);
int ret = 0;
ret |= cell_load(n, ul, "");
ret |= cell_load(n, uc, "");
ret |= cell_load(n, ur, "");
ret |= cell_load(n, cl, "");
ret |= cell_load(n, cc, "");
ret |= cell_load(n, cr, "");
ret |= cell_load(n, ll, "");
ret |= cell_load(n, lc, "");
ret |= cell_load(n, lr, "");
return ret;
}
2019-11-25 07:22:15 +00:00
static int
prep_cells(struct ncplane* n,
cell* ul, cell* uc, cell* ur,
cell* cl, cell* cc, cell* cr,
cell* ll, cell* lc, cell* lr){
cell_init(ul);
cell_init(uc);
cell_init(cl);
cell_init(cr);
cell_init(ll);
cell_init(lc);
cell_init(lr);
cell_init(ur);
cell_init(cc);
int ret = 0;
ret |= cell_load(n, ul, "");
ret |= cell_load(n, uc, "");
ret |= cell_load(n, ur, "");
ret |= cell_load(n, cl, "");
ret |= cell_load(n, cc, "");
ret |= cell_load(n, cr, "");
ret |= cell_load(n, ll, "");
ret |= cell_load(n, lc, "");
ret |= cell_load(n, lr, "");
return ret;
}
2019-11-25 02:18:02 +00:00
static int
2019-11-25 07:22:15 +00:00
gridswitch_demo(struct notcurses* nc, struct ncplane *n){
ncplane_erase(n);
2019-11-25 02:18:02 +00:00
int maxx, maxy;
notcurses_term_dim_yx(nc, &maxy, &maxx);
2019-11-25 02:18:02 +00:00
int rs = 256 / maxx;
int gs = 256 / (maxx + maxy);
int bs = 256 / maxy;
int x = 0;
int y = 0;
2019-11-25 07:22:15 +00:00
cell ul, ll, cl, cr, lc, lr, ur, uc, cc;
prep_cells(n, &ul, &uc, &ur, &cl, &cc, &cr, &ll, &lc, &lr);
2019-11-25 02:18:02 +00:00
if(ncplane_cursor_move_yx(n, y, x)){
return -1;
}
// top line
cell_set_fg_rgb(&ul, 255 - rs * y, 255 - gs * (x + y), 255 - bs * x);
cell_set_bg_rgb(&ul, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &ul);
2019-11-25 02:18:02 +00:00
for(x = 1 ; x < maxx - 1 ; ++x){
cell_set_fg_rgb(&uc, 255 - rs * y, 255 - gs * (x + y), 255 - bs * x);
cell_set_bg_rgb(&uc, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &uc);
2019-11-25 02:18:02 +00:00
}
cell_set_fg_rgb(&ur, 255 - rs * y, 255 - gs * (x + y), 255 - bs * x);
cell_set_bg_rgb(&ur, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &ur);
2019-11-25 02:18:02 +00:00
// center
for(y = 1 ; y < maxy - 1 ; ++y){
2019-11-25 07:22:15 +00:00
x = 0;
cell_set_fg_rgb(&cl, 255 - rs * y, 255 - gs * (x + y), 255 - bs * x);
cell_set_bg_rgb(&cl, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &cl);
2019-11-25 02:18:02 +00:00
for(x = 1 ; x < maxx - 1 ; ++x){
cell_set_fg_rgb(&cc, 255 - rs * y, 255 - gs * (x + y), 255 - bs * x);
cell_set_bg_rgb(&cc, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &cc);
2019-11-25 02:18:02 +00:00
}
cell_set_fg_rgb(&cr, 255 - rs * y, 255 - gs * (x + y), 255 - bs * x);
cell_set_bg_rgb(&cr, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &cr);
2019-11-25 02:18:02 +00:00
}
// bottom line
x = 0;
cell_set_fg_rgb(&ll, 255 - rs * y, 255 - gs * (x + y), 255 - bs * x);
cell_set_bg_rgb(&ll, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &ll);
2019-11-25 02:18:02 +00:00
for(x = 1 ; x < maxx - 1 ; ++x){
cell_set_fg_rgb(&lc, 255 - rs * y, 255 - gs * (x + y), 255 - bs * x);
cell_set_bg_rgb(&lc, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &lc);
2019-11-25 02:18:02 +00:00
}
cell_set_fg_rgb(&lr, 255 - rs * y, 255 - gs * (x + y), 255 - bs * x);
cell_set_bg_rgb(&lr, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &lr);
2019-11-25 02:18:02 +00:00
// render!
notcurses_render(nc);
2019-11-25 07:22:15 +00:00
release_cells(n, &ul, &uc, &ur, &cl, &cc, &cr, &ll, &lc, &lr);
nanosleep(&demodelay, NULL);
2019-11-25 02:18:02 +00:00
return 0;
}
2019-11-25 02:10:54 +00:00
static int
2019-11-25 07:22:15 +00:00
gridinv_demo(struct notcurses* nc, struct ncplane *n){
ncplane_erase(n);
2019-11-25 02:10:54 +00:00
int maxx, maxy;
notcurses_term_dim_yx(nc, &maxy, &maxx);
2019-11-25 02:10:54 +00:00
int rs = 256 / maxx;
int gs = 256 / (maxx + maxy);
int bs = 256 / maxy;
int x = 0;
int y = 0;
2019-11-25 07:22:15 +00:00
cell ul, ll, cl, cr, lc, lr, ur, uc, cc;
prep_cells2(n, &ul, &uc, &ur, &cl, &cc, &cr, &ll, &lc, &lr);
2019-11-25 02:10:54 +00:00
if(ncplane_cursor_move_yx(n, y, x)){
return -1;
}
// top line
cell_set_fg_rgb(&ul, 0, 0, 0);
cell_set_bg_rgb(&ul, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &ul);
2019-11-25 02:10:54 +00:00
for(x = 1 ; x < maxx - 1 ; ++x){
cell_set_fg_rgb(&uc, 0, 0, 0);
cell_set_bg_rgb(&uc, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &uc);
2019-11-25 02:10:54 +00:00
}
cell_set_fg_rgb(&ur, 0, 0, 0);
cell_set_bg_rgb(&ur, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &ur);
2019-11-25 02:10:54 +00:00
// center
for(y = 1 ; y < maxy - 1 ; ++y){
2019-11-25 07:22:15 +00:00
x = 0;
cell_set_fg_rgb(&cl, 0, 0, 0);
cell_set_bg_rgb(&cl, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &cl);
2019-11-25 02:10:54 +00:00
for(x = 1 ; x < maxx - 1 ; ++x){
cell_set_fg_rgb(&cc, 0, 0, 0);
cell_set_bg_rgb(&cc, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &cc);
2019-11-25 02:10:54 +00:00
}
cell_set_fg_rgb(&cr, 0, 0, 0);
cell_set_bg_rgb(&cr, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &cr);
2019-11-25 02:10:54 +00:00
}
// bottom line
x = 0;
cell_set_fg_rgb(&ll, 0, 0, 0);
cell_set_bg_rgb(&ll, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &ll);
2019-11-25 02:10:54 +00:00
for(x = 1 ; x < maxx - 1 ; ++x){
cell_set_fg_rgb(&lc, 0, 0, 0);
cell_set_bg_rgb(&lc, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &lc);
2019-11-25 02:10:54 +00:00
}
cell_set_fg_rgb(&lr, 0, 0, 0);
cell_set_bg_rgb(&lr, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 07:22:15 +00:00
ncplane_putc(n, &lr);
2019-11-25 02:10:54 +00:00
// render!
notcurses_render(nc);
2019-11-25 07:22:15 +00:00
release_cells(n, &ul, &uc, &ur, &cl, &cc, &cr, &ll, &lc, &lr);
nanosleep(&demodelay, NULL);
2019-11-25 07:22:15 +00:00
return gridswitch_demo(nc, n);
2019-11-25 02:10:54 +00:00
}
2019-11-25 02:01:25 +00:00
// red across, blue down, green from UL to LR
int grid_demo(struct notcurses* nc){
int maxx, maxy;
notcurses_term_dim_yx(nc, &maxy, &maxx);
2019-11-25 02:01:25 +00:00
int rs = 256 / maxx;
int gs = 256 / (maxx + maxy);
int bs = 256 / maxy;
int y, x;
struct ncplane* n = notcurses_stdplane(nc);
2019-11-25 03:00:35 +00:00
ncplane_erase(n);
2019-11-25 02:01:25 +00:00
cell ul, ll, cl, cr, lc, lr, ur, uc, cc;
2019-11-25 07:22:15 +00:00
prep_cells(n, &ul, &uc, &ur, &cl, &cc, &cr, &ll, &lc, &lr);
2019-11-25 02:01:25 +00:00
ncplane_cursor_move_yx(n, 0, 0);
y = 0;
// top line
2019-11-25 02:10:54 +00:00
x = 0;
cell_set_bg_rgb(&ul, y, y, y);
cell_set_bg_rgb(&uc, y, y, y);
cell_set_bg_rgb(&ur, y, y, y);
cell_set_fg_rgb(&ul, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 02:01:25 +00:00
ncplane_putc(n, &ul);
for(x = 1 ; x < maxx - 1 ; ++x){
cell_set_fg_rgb(&uc, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 02:01:25 +00:00
ncplane_putc(n, &uc);
}
cell_set_fg_rgb(&ur, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 02:01:25 +00:00
ncplane_putc(n, &ur);
// center
for(y = 1 ; y < maxy - 1 ; ++y){
2019-11-25 07:22:15 +00:00
x = 0;
cell_set_bg_rgb(&cl, y, y, y);
cell_set_bg_rgb(&cc, y, y, y);
cell_set_bg_rgb(&cr, y, y, y);
cell_set_fg_rgb(&cl, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 02:01:25 +00:00
ncplane_putc(n, &cl);
for(x = 1 ; x < maxx - 1 ; ++x){
cell_set_fg_rgb(&cc, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 02:01:25 +00:00
ncplane_putc(n, &cc);
}
cell_set_fg_rgb(&cr, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 02:01:25 +00:00
ncplane_putc(n, &cr);
}
// bottom line
2019-11-25 02:10:54 +00:00
x = 0;
cell_set_bg_rgb(&ll, y, y, y);
cell_set_bg_rgb(&lc, y, y, y);
cell_set_bg_rgb(&lr, y, y, y);
cell_set_fg_rgb(&ll, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 02:01:25 +00:00
ncplane_putc(n, &ll);
for(x = 1 ; x < maxx - 1 ; ++x){
cell_set_fg_rgb(&lc, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 02:01:25 +00:00
ncplane_putc(n, &lc);
}
cell_set_fg_rgb(&lr, 255 - rs * x, 255 - gs * (x + y), 255 - bs * y);
2019-11-25 02:01:25 +00:00
ncplane_putc(n, &lr);
// render!
notcurses_render(nc);
2019-11-25 07:22:15 +00:00
release_cells(n, &ul, &uc, &ur, &cl, &cc, &cr, &ll, &lc, &lr);
nanosleep(&demodelay, NULL);
2019-11-25 07:22:15 +00:00
return gridinv_demo(nc, n);
2019-11-25 02:01:25 +00:00
}