-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglStupidSnakeGlut.cpp
164 lines (138 loc) · 3.26 KB
/
glStupidSnakeGlut.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <iostream>
#include <list>
#include <sys/time.h>
void drawFrame();
void initPositions();
const int nPoints = 32;
int wX = 256 << 1;
int wY = 256 << 1;
int count = -1;
bool bDown = false; //is left mouse button down?
timeval last_frame_time;
double frame_time = 1000000.0 / 60.0;
int range = 16;
struct pVect {
short x,y;
pVect(short px, short py) : x(px),y(py) { }
pVect(const pVect &in) : x(in.x), y(in.y) { }
void copy(const pVect& in) {x = in.x; y = in.y;}
void pos() {
if (x < 0) x *= -1;
if (y < 0) y *= -1;
if (x >= wX) x = (wX << 1) - 1 - x;
if (y >= wY) y = (wY << 1) - y - 1;
}
};
pVect bVect(128,128); //bVect is the mouse coords
std::list<pVect> pList;
double diff_in_micros(timeval &a, timeval &b)
{
double result = a.tv_sec - b.tv_sec;
result *= 1000000;
result += a.tv_usec - b.tv_usec;
return result;
}
void initPositions() {
srand(time(0));
pVect input(128,128);
pList.push_back(input);
for (int i = 0; i < nPoints - 1; i++) {
//input.copy(*pList.end());
input.x += rand() % 4 - 2;
input.y += rand() % 4 - 2;
input.pos();
pList.push_back(input);
}
}
void changeSize(int w, int h)
{
wX = w;
wY = h;
if (wX <= 0) wX = 1;
if (wY <= 0) wY = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, wX, wY);
gluOrtho2D(0,wX,wY,0); //swap around x axis to make windows and openGL coords match
}
void drawFrame() {
timeval t;
gettimeofday(&t, 0);
if (diff_in_micros(t, last_frame_time) < frame_time) return;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(5.0);
glBegin(GL_LINE_STRIP);
glVertex2s(1,1);
glVertex2s(wX-1,1);
glVertex2s(wX-1,wY-1);
glVertex2s(1,wY-1);
glVertex2s(1,1);
glEnd();
int j,k;
float color = 0.0;
glLineWidth(3.0);
glBegin(GL_LINE_STRIP);
for (std::list<pVect>::iterator iter = pList.begin();
iter != pList.end();
++iter, color += (1.0 / (float)nPoints)) {
j = iter->x;
k = iter->y;
glColor3f(1.0 - color,0.0,color);
glVertex2s(iter->x,iter->y);
}
glEnd();
pList.pop_front();
pVect foo(pList.back());
if (bDown) {
range++;
foo.x += (rand() % range) * (foo.x - bVect.x < 0 ? 1 : -1);
foo.y += (rand() % range) * (foo.y - bVect.y < 0 ? 1 : -1);
} else {
range = 16;
foo.x += (rand() % 32) - 16;
foo.y += (rand() % 32) - 16;
}
foo.pos();
pList.push_back(foo);
glutSwapBuffers();
gettimeofday(&last_frame_time, 0);
}
void processKeys(unsigned char key, int x, int y)
{
if (key == 27) exit(0);
}
void processMouseClicks(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) bDown = state == GLUT_DOWN;
}
void processMouseMovement(int x, int y)
{
bVect.x = x;
bVect.y = y;
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(wX, wY);
glutCreateWindow("GLUT Tutorial");
initPositions();
gettimeofday(&last_frame_time, 0);
glutDisplayFunc(drawFrame);
glutReshapeFunc(changeSize);
glutMotionFunc(processMouseMovement);
glutPassiveMotionFunc(processMouseMovement);
glutMouseFunc(processMouseClicks);
glutIdleFunc(drawFrame);
glutMainLoop();
return 1;
}