-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseGfxApp.cpp
192 lines (153 loc) · 4.64 KB
/
BaseGfxApp.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
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
//
// BaseGfxApp.cpp
// Originally created by the CSci-3081W TAs.
//
#include "BaseGfxApp.h"
#include <iostream>
#include <assert.h>
BaseGfxApp* BaseGfxApp::s_currentApp = NULL;
bool BaseGfxApp::s_glutInitialized = false;
#define INIT_WIDTH 800
#define INIT_HEIGHT 600
BaseGfxApp::BaseGfxApp(int argc, char* argv[], int width, int height, int x, int y, int glutFlags, bool createGLUIWin, int gluiWinX, int gluiWinY) {
s_currentApp = this;
m_glui = NULL;
m_drag = false;
m_width = width;
m_height = height;
// Set window size and position
glutInitWindowSize(width, height);
glutInitWindowPosition(x, y);
glutInitDisplayMode(glutFlags);
if (! s_glutInitialized) {
glutInit(&argc, argv);
s_glutInitialized = true;
}
m_glutWindowHandle = glutCreateWindow("Graphics Window");
glutReshapeFunc(s_reshape);
glutKeyboardFunc(s_keyboard);
glutKeyboardUpFunc(s_keyboardup);
glutSpecialFunc(s_keyboardspecial);
glutSpecialUpFunc(s_keyboardspecialup);
glutMotionFunc(s_mousemotion);
glutPassiveMotionFunc(s_mousemotion);
glutMouseFunc(s_mousebtn);
glutDisplayFunc(s_draw);
glutIdleFunc(s_idle);
if (createGLUIWin) {
m_glui = GLUI_Master.create_glui("Controls", 0, gluiWinX, gluiWinY);
m_glui->set_main_gfx_window(m_glutWindowHandle);
// Note: if using a glut idle func, it may need to be registered with glui rather than glut.
GLUI_Master.set_glutIdleFunc(NULL);
}
}
BaseGfxApp::~BaseGfxApp() {
s_currentApp = NULL;
glutDestroyWindow(m_glutWindowHandle);
}
void BaseGfxApp::setCaption(const std::string& caption) {
glutSetWindowTitle(caption.c_str());
glutSetIconTitle(caption.c_str());
}
void BaseGfxApp::runMainLoop() {
glutMainLoop();
}
void BaseGfxApp::reshape(int width, int height) {
// This code essentially disables the ability to interactively resize the graphics window.
// BaseGfxApp defaults to a window that cannot be resized by dragging the corner with the mouse.
if (s_currentApp->width() != width || s_currentApp->height() != height) {
glutReshapeWindow( s_currentApp->width(), s_currentApp->height() );
}
}
void BaseGfxApp::renderOneFrame() {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
display();
glutSwapBuffers();
}
void BaseGfxApp::drawPixels(int start_x, int start_y, int width, int height, void const * const pixels) {
glRasterPos2i(start_x, start_y);
glDrawPixels(width, height, GL_RGBA, GL_FLOAT, pixels);
int err;
if ((err = glGetError()) != GL_NO_ERROR)
{
std::cerr << "GL is in an error state after call to glDrawPixels()" << std::endl;
std::cerr << "(GL error code " << err << ")\n";
assert(0);
}
}
int BaseGfxApp::width() const {
return m_width;
}
int BaseGfxApp::height() const {
return m_height;
}
void BaseGfxApp::s_reshape(int width, int height) {
s_currentApp->reshape(width, height);
}
void BaseGfxApp::s_keyboard(unsigned char c, int x, int y) {
s_currentApp->keyboard(c, x, y);
glutPostRedisplay();
}
void BaseGfxApp::s_keyboardspecial(int key, int x, int y) {
s_currentApp->keyboardSpecial(key, x, y);
glutPostRedisplay();
}
void BaseGfxApp::s_keyboardup(unsigned char c, int x, int y) {
s_currentApp->keyboardUp(c, x, y);
glutPostRedisplay();
}
void BaseGfxApp::s_keyboardspecialup(int key, int x, int y) {
s_currentApp->keyboardSpecialUp(key, x, y);
glutPostRedisplay();
}
void BaseGfxApp::s_mousemotion(int x, int y) {
if (s_currentApp->m_drag == true) {
s_currentApp->mouseDragged(x,y);
}
else {
s_currentApp->mouseMoved(x,y);
}
glutPostRedisplay();
}
void BaseGfxApp::s_mousebtn(int b, int s, int x, int y) {
if ((b==GLUT_LEFT_BUTTON) && (s==GLUT_UP)) {
s_currentApp->leftMouseUp(x,y);
s_currentApp->m_drag = false;
}
else if ((b==GLUT_LEFT_BUTTON) && (s==GLUT_DOWN)) {
s_currentApp->leftMouseDown(x,y);
s_currentApp->m_drag = true;
}
else if ((b==GLUT_RIGHT_BUTTON) && (s==GLUT_UP)) {
s_currentApp->rightMouseUp(x,y);
}
else if ((b==GLUT_RIGHT_BUTTON) && (s==GLUT_DOWN)) {
s_currentApp->rightMouseDown(x,y);
}
else if ((b==GLUT_MIDDLE_BUTTON) && (s==GLUT_UP)) {
s_currentApp->middleMouseUp(x,y);
}
else if ((b==GLUT_MIDDLE_BUTTON) && (s==GLUT_DOWN)) {
s_currentApp->middleMouseDown(x,y);
}
glutPostRedisplay();
}
void BaseGfxApp::s_draw() {
s_currentApp->renderOneFrame();
}
void BaseGfxApp::s_gluicallback(int controlID) {
s_currentApp->gluiControl(controlID);
}
void BaseGfxApp::s_idle() {
int timeSinceStart = glutGet(GLUT_ELAPSED_TIME);
int delta = timeSinceStart - s_currentApp->m_milliseconds;
if (delta > 0) {
s_currentApp->m_milliseconds = timeSinceStart;
s_currentApp->update(delta);
}
}
void BaseGfxApp::setWindowDimensions(int width, int height) {
m_height = height;
m_width = width;
glutReshapeWindow(m_width, m_height);
}