-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangles.cpp
134 lines (116 loc) · 2.09 KB
/
triangles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include "../../../tmp/glcanvas.hpp"
using namespace std;
using namespace cnv;
int W = 640, H = 480;
bool PAUSED = false;
// typedef unsigned int color_t;
void triangle(float x1, float y1, float x2, float y2, float x3, float y3, color_t c = 0x000000ff)
{
line(x1, y1, x2, y2, c);
line(x2, y2, x3, y3, c);
line(x3, y3, x1, y1, c);
}
void triangle_fill(float x1, float y1, float x2, float y2, float x3, float y3, color_t c=0x000000ff)
{
glBegin(GL_TRIANGLE_FAN);
color(c);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glEnd();
}
color_t randcolor(int op)
{
return hexcolor(rand()%256, rand()%256, rand()%256, op);
}
void key(unsigned char key, int mx, int my)
{
switch(key)
{
case 27: // Esc
exit(0);
case 13: // Enter
PAUSED = !PAUSED;
}
}
class Triangle
{
public:
int x1, y1, x2, y2, x3, y3;
int alpha;
int delay;
int speed;
bool fading = false;
color_t col;
Triangle()
{
randcoords();
}
void randcoords()
{
x1 = rand()%(W-100)+50;
y1 = rand()%(H-100)+50;
x2 = x1+rand()%101-50;
y2 = y1+rand()%101-50;
x3 = x1+rand()%101-50;
y3 = y1+rand()%101-50;
col = randcolor(0);
alpha = 0;
delay = rand()%500;
speed = rand()%5+1;
}
void opacity()
{
if(delay)
{
delay--;
return;
}
if(alpha >= 255)
fading = true;
else if(alpha <= 0 && fading)
{
fading = false;
randcoords();
}
if(fading)
{
alpha -= speed;
if(alpha > 0)
col-=speed;
}
else
{
alpha += speed;
if(alpha < 255)
col += speed;
}
}
};
const int COUNT = 512;
Triangle figures[COUNT];
void draw()
{
clear(255, 255, 255);
for(int i = 0; i < COUNT; i++)
triangle_fill(figures[i].x1, figures[i].y1, figures[i].x2, figures[i].y2, figures[i].x3, figures[i].y3, figures[i].col);
glutSwapBuffers();
}
void tick(int)
{
if (!PAUSED)
for(int i = 0; i < COUNT; i++)
figures[i].opacity();
glutPostRedisplay();
glutTimerFunc(10, tick, 1);
}
int main()
{
srand(time(0));
window(W, H);
glutDisplayFunc(draw);
glutKeyboardFunc(key);
tick(0);
glutMainLoop();
}