-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_bar.cpp
102 lines (83 loc) · 2.31 KB
/
two_bar.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
#include <GL/glut.h>
const float boxSize = 200.0f;
const float dotRadius = 5.0f;
const float dotSpeed = 3.0f;
float dotX = -boxSize / 2 + dotRadius;
float dotY = -boxSize / 2 + dotRadius;
int dotDirection = 3;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-boxSize / 2, boxSize / 2, -boxSize / 2, boxSize / 2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(-boxSize / 2, -boxSize / 2);
glVertex2f(boxSize / 2, -boxSize / 2);
glVertex2f(boxSize / 2, boxSize / 2);
glVertex2f(-boxSize / 2, boxSize / 2);
glEnd();
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_POLYGON);
glVertex2f(dotX - dotRadius, dotY - dotRadius);
glVertex2f(dotX + dotRadius, dotY - dotRadius);
glVertex2f(dotX + dotRadius, dotY + dotRadius);
glVertex2f(dotX - dotRadius, dotY + dotRadius);
glEnd();
glutSwapBuffers();
}
void update(int value)
{
switch (dotDirection)
{
case 0:
dotX += dotSpeed;
if (dotX >= boxSize / 2 - dotRadius)
{
dotX = boxSize / 2 - dotRadius;
dotDirection = 1;
}
break;
case 1:
dotY -= dotSpeed;
if (dotY <= -boxSize / 2 + dotRadius)
{
dotY = -boxSize / 2 + dotRadius;
dotDirection = 2;
}
break;
case 2:
dotX -= dotSpeed;
if (dotX <= -boxSize / 2 + dotRadius)
{
dotX = -boxSize / 2 + dotRadius;
dotDirection = 3;
}
break;
case 3:
dotY += dotSpeed;
if (dotY >= boxSize / 2 - dotRadius)
{
dotY = boxSize / 2 - dotRadius;
dotDirection = 0;
}
break;
}
glutPostRedisplay();
glutTimerFunc(16, update, 0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1000, 1000);
glutCreateWindow("1 Sim B");
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glutDisplayFunc(display);
glutTimerFunc(0, update, 0);
glutMainLoop();
return 0;
}