Rev 2097 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1479 | jag | 1 | /* |
2 | * loom.cc |
||
2302 | jag | 3 | * DIN Is Noise is copyright (c) 2006-2025 Jagannathan Sampath |
1713 | jag | 4 | * DIN Is Noise is released under GNU Public License 2.0 |
1479 | jag | 5 | * For more information, please visit https://dinisnoise.org/ |
6 | */ |
||
964 | jag | 7 | #include "loom.h" |
8 | #include "log.h" |
||
9 | |||
10 | loom::loom () { |
||
11 | cell_size = 50; |
||
12 | seam = 0.9; |
||
13 | half_cell_size = cell_size / 2; |
||
14 | for (int i = 0, j = 1; i < sz; ++i) { |
||
15 | pattern0[i]=pattern[i]=j; |
||
16 | j=!j; |
||
17 | control[i]=1; |
||
18 | } |
||
19 | set_moveable (1); |
||
20 | } |
||
21 | |||
22 | void loom::draw () { |
||
23 | int x0 = posx, y0 = posy, x1 = 0, y1 = 0, x2 = 0, y2 = 0; |
||
24 | for (int i = 0; i < sz; ++i) { |
||
25 | y1 = y0 + half_cell_size; |
||
26 | y2 = y1 + half_cell_size; |
||
27 | if (control[i]) flip_pattern (); |
||
28 | for (int j = 0; j < sz; ++j) { |
||
29 | x1 = x0 + cell_size; |
||
30 | x2 = x1 - half_cell_size; |
||
31 | if (pattern[j] == 0) { |
||
32 | draw_rect (x0, y1, x1, y1, 0, 1, 1, 0); // horizontal |
||
33 | draw_rect (x2, y0, x2, y2, 1, 1, 0, 1); // vertical |
||
34 | } else { |
||
35 | draw_rect (x2, y0, x2, y2, 1, 1, 0, 1); // vertical |
||
36 | draw_rect (x0, y1, x1, y1, 0, 1, 1, 0); // horizontal |
||
37 | } |
||
38 | x0 = x1; |
||
39 | } |
||
40 | y0 = y2; |
||
41 | x0 = posx; |
||
42 | } |
||
43 | int szzs = sz * cell_size; |
||
44 | set_extents (posx, posy, posx + szzs, posy + szzs); |
||
45 | memcpy (pattern, pattern0, sz * sizeof(int)); |
||
46 | } |
||
47 | |||
48 | void loom::draw_rect (int x1, int y1, int x2, int y2, float r, float g, float b, int v) { |
||
49 | float w = seam * half_cell_size; |
||
50 | glColor3f (r, g, b); |
||
51 | if (v) { |
||
52 | glRectf (x1 - w, y1, x1 + w, y2); |
||
53 | } else { |
||
54 | glRectf (x1, y1 - w, x2, y1 + w); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | void loom::flip_pattern () { // to get one free pattern |
||
59 | for (int i = 0, p; i < sz; ++i) { |
||
60 | p = pattern[i]; |
||
61 | pattern[i]=!p; |
||
62 | } |
||
63 | } |