-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.cpp
More file actions
221 lines (198 loc) · 6.71 KB
/
graphics.cpp
File metadata and controls
221 lines (198 loc) · 6.71 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// $Id: graphics.cpp,v 1.6 2019-05-15 18:02:12-07 - - $
#include <iostream>
using namespace std;
#include <GL/freeglut.h>
#include "graphics.h"
#include "util.h"
int window::width = 640; // in pixels
int window::height = 480; // in pixels
int window::pixels = 4; // in pixels
vector<object> window::objects;
size_t window::selected_obj = 0;
mouse window::mus;
// Implementation of object functions.
object::object (shared_ptr<shape> pshape_, vertex center_,
rgbcolor color_):
pshape(pshape_), center(center_), color(color_) {
}
void object::draw() {
pshape->draw (center, color);
}
void object::move (GLfloat delta_x, GLfloat delta_y) {
center.xpos += delta_x;
center.ypos += delta_y;
}
// Implementation of mouse functions.
void mouse::state (int button, int state) {
switch (button) {
case GLUT_LEFT_BUTTON: left_state = state; break;
case GLUT_MIDDLE_BUTTON: middle_state = state; break;
case GLUT_RIGHT_BUTTON: right_state = state; break;
}
}
void mouse::draw() {
static rgbcolor color ("green");
ostringstream text;
text << "(" << xpos << "," << window::height - ypos << ")";
if (left_state == GLUT_DOWN) text << "L";
if (middle_state == GLUT_DOWN) text << "M";
if (right_state == GLUT_DOWN) text << "R";
if (entered == GLUT_ENTERED) {
void* font = GLUT_BITMAP_HELVETICA_18;
glColor3ubv (color.ubvec);
glRasterPos2i (10, 10);
auto ubytes = reinterpret_cast<const GLubyte*>
(text.str().c_str());
glutBitmapString (font, ubytes);
}
}
// Executed when window system signals to shut down.
void window::close() {
DEBUGF ('g', sys_info::execname() << ": exit ("
<< sys_info::exit_status() << ")");
exit (sys_info::exit_status());
}
// Executed when mouse enters or leaves window.
void window::entry (int mouse_entered) {
DEBUGF ('g', "mouse_entered=" << mouse_entered);
window::mus.entered = mouse_entered;
if (window::mus.entered == GLUT_ENTERED) {
DEBUGF ('g', sys_info::execname() << ": width=" << window::width
<< ", height=" << window::height);
}
glutPostRedisplay();
}
// Called to display the objects in the window.
void window::display() {
glClear (GL_COLOR_BUFFER_BIT);
for (auto& object: window::objects) object.draw();
mus.draw();
glutSwapBuffers();
}
// Called when window is opened and when resized.
void window::reshape (int width_, int height_) {
DEBUGF ('g', "width=" << width_ << ", height=" << height_);
window::width = width_;
window::height = height_;
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (0, window::width, 0, window::height);
glMatrixMode (GL_MODELVIEW);
glViewport (0, 0, window::width, window::height);
glClearColor (0.25, 0.25, 0.25, 1.0);
glutPostRedisplay();
}
// Executed when a regular keyboard key is pressed.
void window::keyboard (GLubyte key, int x, int y) {
enum {BS = 8, TAB = 9, ESC = 27, SPACE = 32, DEL = 127};
DEBUGF ('g', "key=" << unsigned (key) << ", x=" << x << ", y=" << y);
window::mus.set (x, y);
switch (key) {
case 'Q': case 'q': case ESC:
window::close();
break;
case 'H': case 'h':
//move_selected_object (
objects[selected_obj].move(-pixels, 0.0);
break;
case 'J': case 'j':
//move_selected_object (
objects[selected_obj].move(0.0, -pixels);
break;
case 'K': case 'k':
//move_selected_object (
objects[selected_obj].move(0.0, pixels);
break;
case 'L': case 'l':
//move_selected_object (
objects[selected_obj].move(pixels, 0.0);
break;
case 'N': case 'n': case SPACE: case TAB:
++selected_obj;
if(selected_obj>objects.size()){
selected_obj = 0;
}
break;
case 'P': case 'p': case BS:
{
--selected_obj;
unsigned int zero = 0;
if(selected_obj<zero){
selected_obj = objects.size()-1;
}
break;
}
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
//select_object (key - '0');
selected_obj = key - '0';
break;
default:
cerr << unsigned (key) << ": invalid keystroke" << endl;
break;
}
glutPostRedisplay();
}
// Executed when a special function key is pressed.
void window::special (int key, int x, int y) {
DEBUGF ('g', "key=" << key << ", x=" << x << ", y=" << y);
window::mus.set (x, y);
switch (key) {
case GLUT_KEY_LEFT: //move_selected_object (-1, 0); break;
case GLUT_KEY_DOWN: //move_selected_object (0, -1); break;
case GLUT_KEY_UP: //move_selected_object (0, +1); break;
case GLUT_KEY_RIGHT: //move_selected_object (+1, 0); break;
case GLUT_KEY_F1: //select_object (1); break;
case GLUT_KEY_F2: //select_object (2); break;
case GLUT_KEY_F3: //select_object (3); break;
case GLUT_KEY_F4: //select_object (4); break;
case GLUT_KEY_F5: //select_object (5); break;
case GLUT_KEY_F6: //select_object (6); break;
case GLUT_KEY_F7: //select_object (7); break;
case GLUT_KEY_F8: //select_object (8); break;
case GLUT_KEY_F9: //select_object (9); break;
case GLUT_KEY_F10: //select_object (10); break;
case GLUT_KEY_F11: //select_object (11); break;
case GLUT_KEY_F12: //select_object (12); break;
default:
cerr << unsigned (key) << ": invalid function key" << endl;
break;
}
glutPostRedisplay();
}
void window::motion (int x, int y) {
DEBUGF ('g', "x=" << x << ", y=" << y);
window::mus.set (x, y);
glutPostRedisplay();
}
void window::passivemotion (int x, int y) {
DEBUGF ('g', "x=" << x << ", y=" << y);
window::mus.set (x, y);
glutPostRedisplay();
}
void window::mousefn (int button, int state, int x, int y) {
DEBUGF ('g', "button=" << button << ", state=" << state
<< ", x=" << x << ", y=" << y);
window::mus.state (button, state);
window::mus.set (x, y);
glutPostRedisplay();
}
void window::main () {
static int argc = 0;
glutInit (&argc, nullptr);
glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize (window::width, window::height);
glutInitWindowPosition (128, 128);
glutCreateWindow (sys_info::execname().c_str());
glutCloseFunc (window::close);
glutEntryFunc (window::entry);
glutDisplayFunc (window::display);
glutReshapeFunc (window::reshape);
glutKeyboardFunc (window::keyboard);
glutSpecialFunc (window::special);
glutMotionFunc (window::motion);
glutPassiveMotionFunc (window::passivemotion);
glutMouseFunc (window::mousefn);
DEBUGF ('g', "Calling glutMainLoop()");
glutMainLoop();
}