1 #include "main.h"
2 #include <cstdlib>
3 #include <iostream>
4
pulser(struct notcurses * nc,struct ncplane * ncp,const struct timespec * ts,void * curry)5 auto pulser(struct notcurses* nc, struct ncplane* ncp __attribute__ ((unused)),
6 const struct timespec* ts __attribute__ ((unused)), void* curry) -> int {
7 auto pulsestart = static_cast<struct timespec*>(curry);
8 if(notcurses_render(nc)){
9 return -1;
10 }
11 struct timespec now;
12 clock_gettime(CLOCK_MONOTONIC, &now);
13 auto delta = timespec_to_ns(&now) - timespec_to_ns(pulsestart);
14 if(delta > 250000000){
15 return 1;
16 }
17 return 0;
18 }
19
fadeaborter(struct notcurses * nc,struct ncplane * ncp,const struct timespec * ts,void * curry)20 auto fadeaborter(struct notcurses* nc, struct ncplane* ncp,
21 const struct timespec* ts, void* curry) -> int {
22 (void)nc;
23 (void)ncp;
24 (void)ts;
25 (void)curry;
26 return 1;
27 }
28
29 TEST_CASE("Fade") {
30 auto nc_ = testing_notcurses();
31 if(!nc_){
32 return;
33 }
34 struct ncplane* n_ = notcurses_stdplane(nc_);
35 REQUIRE(n_);
36 if(!notcurses_canfade(nc_)){
37 CHECK(0 == notcurses_stop(nc_));
38 return;
39 }
40 REQUIRE(0 == ncplane_cursor_move_yx(n_, 0, 0));
41 unsigned dimy, dimx;
42 ncplane_dim_yx(n_, &dimy, &dimx);
43 nccell c = NCCELL_CHAR_INITIALIZER('*');
44 unsigned rgb = 0xffffffu;
45 CHECK(!ncplane_set_scrolling(n_, true));
46 for(unsigned y = 0 ; y < dimy ; ++y){
47 for(unsigned x = 0 ; x < dimx ; ++x){
48 rgb -= 32;
49 if(rgb < 32){
50 rgb = 0xffffffu;
51 }
52 nccell_set_fg_rgb(&c, rgb);
53 nccell_set_bg_rgb8(&c, rgb & 0xff, (rgb >> 16u) & 0xff, (rgb >> 8u) & 0xff);
54 CHECK(0 < ncplane_putc(n_, &c));
55 }
56 }
57
58 SUBCASE("FadeOut") {
59 struct timespec ts;
60 ts.tv_sec = 0;
61 ts.tv_nsec = 250000000;
62 CHECK(0 == ncplane_fadeout(n_, &ts, nullptr, nullptr));
63 CHECK(0 == notcurses_render(nc_));
64 }
65
66 SUBCASE("FadeIn") {
67 struct timespec ts;
68 ts.tv_sec = 0;
69 ts.tv_nsec = 250000000;
70 CHECK(0 == ncplane_fadein(n_, &ts, nullptr, nullptr));
71 CHECK(0 == notcurses_render(nc_));
72 }
73
74 SUBCASE("FadeOutAbort") {
75 struct timespec ts;
76 ts.tv_sec = 0;
77 ts.tv_nsec = 250000000;
78 CHECK(0 < ncplane_fadeout(n_, &ts, fadeaborter, nullptr));
79 CHECK(0 == notcurses_render(nc_));
80 }
81
82 SUBCASE("FadeInAbort") {
83 struct timespec ts;
84 ts.tv_sec = 0;
85 ts.tv_nsec = 250000000;
86 CHECK(0 < ncplane_fadein(n_, &ts, fadeaborter, nullptr));
87 CHECK(0 == notcurses_render(nc_));
88 }
89
90 SUBCASE("Pulse") {
91 struct timespec ts;
92 ts.tv_sec = 0;
93 ts.tv_nsec = 75000000;
94 ncplane_erase(n_);
95 ncplane_set_fg_rgb(n_, 0xffd700);
96 CHECK(0 < ncplane_printf_aligned(n_, dimy - 1, NCALIGN_LEFT, "pulllllllse"));
97 CHECK(0 < ncplane_printf_aligned(n_, dimy - 1, NCALIGN_CENTER, "pulllllllse"));
98 CHECK(0 < ncplane_printf_aligned(n_, dimy - 1, NCALIGN_RIGHT, "pulllllllse"));
99 struct timespec pulsestart;
100 clock_gettime(CLOCK_MONOTONIC, &pulsestart);
101 CHECK(0 < ncplane_pulse(n_, &ts, pulser, &pulsestart));
102 CHECK(0 == notcurses_render(nc_));
103 }
104
105 // drive fadeout with the more flexible api
106 SUBCASE("FadeOutFlexible") {
107 auto nctx = ncfadectx_setup(n_);
108 REQUIRE(nctx);
109 auto maxiter = ncfadectx_iterations(nctx);
110 CHECK(0 < maxiter);
111 for(int i = 0 ; i < maxiter ; ++i){
112 CHECK(0 == ncplane_fadeout_iteration(n_, nctx, i, nullptr, nullptr));
113 }
114 ncfadectx_free(nctx);
115 CHECK(0 == notcurses_render(nc_));
116 }
117
118 SUBCASE("FadeOutFlexibleAbort") {
119 auto nctx = ncfadectx_setup(n_);
120 REQUIRE(nctx);
121 auto maxiter = ncfadectx_iterations(nctx);
122 CHECK(0 < maxiter);
123 for(int i = 0 ; i < maxiter ; ++i){
124 CHECK(0 < ncplane_fadeout_iteration(n_, nctx, i, fadeaborter, nullptr));
125 }
126 ncfadectx_free(nctx);
127 CHECK(0 == notcurses_render(nc_));
128 }
129
130 // drive fadein with the more flexible api
131 SUBCASE("FadeInFlexible") {
132 auto nctx = ncfadectx_setup(n_);
133 REQUIRE(nctx);
134 auto maxiter = ncfadectx_iterations(nctx);
135 CHECK(0 < maxiter);
136 for(int i = 0 ; i < maxiter ; ++i){
137 CHECK(0 == ncplane_fadein_iteration(n_, nctx, i, nullptr, nullptr));
138 }
139 ncfadectx_free(nctx);
140 CHECK(0 == notcurses_render(nc_));
141 }
142
143 SUBCASE("FadeInFlexibleAbort") {
144 auto nctx = ncfadectx_setup(n_);
145 REQUIRE(nctx);
146 auto maxiter = ncfadectx_iterations(nctx);
147 CHECK(0 < maxiter);
148 for(int i = 0 ; i < maxiter ; ++i){
149 CHECK(0 < ncplane_fadein_iteration(n_, nctx, i, fadeaborter, nullptr));
150 }
151 ncfadectx_free(nctx);
152 CHECK(0 == notcurses_render(nc_));
153 }
154
155 CHECK(0 == notcurses_stop(nc_));
156
157 }
158