1 /*
2 * trail.cc
3 * DIN Is Noise is copyright (c) 2006-2021 Jagannathan Sampath
4 * DIN Is Noise is released under GNU Public License 2.0
5 * For more information, please visit https://dinisnoise.org/
6 */
7
8 #include "dingl.h"
9 #include "trail.h"
10 #include "log.h"
11
alloc(int n)12 void trail_t::alloc (int n) {
13 if (n > n_tpts) {
14 n_tpts = n;
15 if (tpts) delete[] tpts;
16 tpts = new float [2 * n_tpts];
17 }
18 }
19
trail_t()20 trail_t::trail_t () {
21 cur = 0;
22 total = 0;
23 }
24
trail_t(int t)25 trail_t::trail_t (int t) {
26 cur = 0;
27 total = 0;
28 set (t);
29 }
30
set(int t)31 void trail_t::set (int t) {
32 if (t > total) alloc (t);
33 total = t;
34 if (total == 0) reset ();
35 }
36
reset()37 void trail_t::reset () {
38 cur = 0;
39 pts.x.clear ();
40 pts.y.clear ();
41 }
42
add(float x,float y)43 void trail_t::add (float x, float y) {
44 if (total && !(x == last.x && y == last.y)) {
45 last.x = x; last.y = y;
46 int dt = cur - total;
47 if (dt > -1) {
48 int npops = dt + 1;
49 for (int i = 0; i < npops; ++i) {
50 pts.x.pop_front ();
51 pts.y.pop_front ();
52 }
53 cur -= npops;
54 }
55 pts.x.push_back (x);
56 pts.y.push_back (y);
57 ++cur;
58 }
59 }
60
change(int delta)61 void trail_t::change (int delta) {
62 total += delta;
63 if (delta > 0)
64 alloc (total);
65 else {
66 if (total < 1) {
67 total = 0;
68 reset ();
69 }
70 }
71 }
72
draw()73 void trail_t::draw () {
74 if (total) {
75 int m = 0, n = 0;
76 float x, y;
77 for (std::list< float >::iterator xiter = pts.x.begin (), xjter = pts.x.end (), yiter = pts.y.begin (); xiter != xjter; ++xiter, ++yiter) {
78 x = *xiter;
79 y = *yiter;
80 tpts[m++] = x; tpts[m++]=y;
81 ++n;
82 }
83 glVertexPointer (2, GL_FLOAT, 0, tpts);
84 glDrawArrays (GL_LINE_STRIP, 0, n);
85 }
86 }
87
88 #ifdef __SVG__
write()89 void trail_t::write () {
90 dlog << "<polyline points=\"";
91 float x, y;
92 for (std::list< float >::iterator xiter = pts.x.begin (), xjter = pts.x.end (), yiter = pts.y.begin (); xiter != xjter; ++xiter, ++yiter) {
93 x = *xiter;
94 y = *yiter;
95 dlog << x << "," << y << ' ';
96 }
97 dlog << "\" style=\"stroke-width:1\" fill=\"none\" stroke=\"black\"/>" << endl;
98 }
99 #endif
100