00001 #include "window.h"
00002
00007 #include <glutm/master.h>
00008 #include <glt/buffer.h>
00009
00010 #include <fstream>
00011 #include <cstdio>
00012 using namespace std;
00013
00015
00016 const unsigned char GlutWindow::_tickVal = 255;
00017
00018 GlutWindow::GlutWindow(const std::string &title,const int width,const int height,const int x,const int y,const unsigned int displayMode)
00019 : _leftButtonMenu(GLUT_LEFT_BUTTON),
00020 _rightButtonMenu(GLUT_RIGHT_BUTTON),
00021 _fileIndex(0),
00022 _idle(false),
00023 _tick(0),
00024 _tickPending(false),
00025 _windowID(-1),
00026 _shiftModifier(false),
00027 _ctrlModifier(false),
00028 _altModifier(false),
00029 _x(x),
00030 _y(y),
00031 _width(width),
00032 _height(height),
00033 _displayMode(displayMode),
00034 _gameMode(false),
00035 _title(title),
00036 _mouseEvents(false),
00037 _mouseX(0),
00038 _mouseY(0),
00039 _windowSaved(false),
00040 _windowX(0),
00041 _windowY(0),
00042 _windowWidth(0),
00043 _windowHeight(0)
00044 {
00045 _swapBuffers = (displayMode & GLUT_DOUBLE)!=0;
00046 }
00047
00048 GlutWindow::~GlutWindow()
00049 {
00050 }
00051
00052 void
00053 GlutWindow::open()
00054 {
00055 GlutMaster::openWindow(this);
00056 }
00057
00058 void
00059 GlutWindow::close()
00060 {
00061 if (_windowID!=-1)
00062 GlutMaster::closeWindow(this);
00063 }
00064
00065 void
00066 GlutWindow::grab(const GlutWindow &window)
00067 {
00068
00069
00070
00071
00072 _windowID = window._windowID;
00073
00074 if (_windowID!=-1)
00075 GlutMaster::openWindow(this);
00076 }
00077
00078 void
00079 GlutWindow::enterGameMode()
00080 {
00081 if (!_windowSaved)
00082 {
00083 _windowSaved = true;
00084
00085 _windowX = _x;
00086 _windowY = _y;
00087 _windowWidth = _width;
00088 _windowHeight = _height;
00089 GlutMaster::fullScreen(this);
00090 }
00091 }
00092
00093 void
00094 GlutWindow::leaveGameMode()
00095 {
00096 if (_windowSaved)
00097 {
00098 _windowSaved = false;
00099 position(_windowX,_windowY);
00100 reshape(_windowWidth,_windowHeight);
00101 }
00102 }
00103
00104 void
00105 GlutWindow::toggleGameMode()
00106 {
00107 if (_windowSaved)
00108 leaveGameMode();
00109 else
00110 enterGameMode();
00111 }
00112
00113 void
00114 GlutWindow::position(int x,int y)
00115 {
00116 GlutMaster::positionWindow(this,x,y);
00117 }
00118
00119 void
00120 GlutWindow::reshape(int width,int height)
00121 {
00122 GlutMaster::reshapeWindow(this,width,height);
00123 }
00124
00125 void
00126 GlutWindow::setCursor(int cursor)
00127 {
00128 GlutMaster::setCursor(this,cursor);
00129 }
00130
00131 void
00132 GlutWindow::setTimer(unsigned int msecs,unsigned char val)
00133 {
00134 GlutMaster::setTimer(this,msecs,(uint32(val)<<24)|_windowID);
00135 }
00136
00137 void
00138 GlutWindow::setTick(unsigned int msec)
00139 {
00140 GlutMaster::setTick(this,msec);
00141 }
00142
00143 void
00144 GlutWindow::setIdle(bool idle)
00145 {
00146 GlutMaster::setIdle(this,idle);
00147 }
00148
00149 void
00150 GlutWindow::postRedisplay()
00151 {
00152 GlutMaster::postRedisplay(this);
00153 }
00154
00155 void
00156 GlutWindow::swapBuffers()
00157 {
00158 GlutMaster::swapBuffers(this);
00159 }
00160
00162
00163 void
00164 GlutWindow::setSwapBuffers(const bool swap)
00165 {
00166 _swapBuffers = swap;
00167 }
00168
00169 bool
00170 GlutWindow::getIdle() const
00171 {
00172 return _idle;
00173 }
00174
00175 unsigned int
00176 GlutWindow::getTick() const
00177 {
00178 return _tick;
00179 }
00180
00181 bool
00182 GlutWindow::getSwapBuffers() const
00183 {
00184 return _swapBuffers;
00185 }
00186
00187 int &GlutWindow::windowID() { return _windowID; }
00188 const int &GlutWindow::windowID() const { return _windowID; }
00189
00190 bool GlutWindow::getOpen() const { return _windowID!=-1; }
00191
00192 int GlutWindow::getX() const { return GlutMaster::get(this,GLUT_WINDOW_X); }
00193 int GlutWindow::getY() const { return GlutMaster::get(this,GLUT_WINDOW_Y); }
00194 int GlutWindow::getWidth() const { return _width; }
00195 int GlutWindow::getHeight() const { return _height; }
00196
00197 int GlutWindow::getBufferSize() const { return GlutMaster::get(this,GLUT_WINDOW_BUFFER_SIZE); }
00198 int GlutWindow::getStencilSize() const { return GlutMaster::get(this,GLUT_WINDOW_STENCIL_SIZE); }
00199 int GlutWindow::getDepthSize() const { return GlutMaster::get(this,GLUT_WINDOW_DEPTH_SIZE); }
00200 int GlutWindow::getRedSize() const { return GlutMaster::get(this,GLUT_WINDOW_RED_SIZE); }
00201 int GlutWindow::getGreenSize() const { return GlutMaster::get(this,GLUT_WINDOW_GREEN_SIZE); }
00202 int GlutWindow::getBlueSize() const { return GlutMaster::get(this,GLUT_WINDOW_BLUE_SIZE); }
00203 int GlutWindow::getAlphaSize() const { return GlutMaster::get(this,GLUT_WINDOW_ALPHA_SIZE); }
00204
00205 int GlutWindow::getAccumRedSize() const { return GlutMaster::get(this,GLUT_WINDOW_ACCUM_RED_SIZE); }
00206 int GlutWindow::getAccumGreenSize() const { return GlutMaster::get(this,GLUT_WINDOW_ACCUM_GREEN_SIZE); }
00207 int GlutWindow::getAccumBlueSize() const { return GlutMaster::get(this,GLUT_WINDOW_ACCUM_BLUE_SIZE); }
00208 int GlutWindow::getAccumAlphaSize() const { return GlutMaster::get(this,GLUT_WINDOW_ACCUM_ALPHA_SIZE); }
00209
00210 bool GlutWindow::getDoubleBuffer() const { return GlutMaster::get(this,GLUT_WINDOW_DOUBLEBUFFER)!=0; }
00211 bool GlutWindow::getRGBA() const { return GlutMaster::get(this,GLUT_WINDOW_RGBA)!=0; }
00212 bool GlutWindow::getStereo() const { return GlutMaster::get(this,GLUT_WINDOW_STEREO)!=0; }
00213
00214 bool GlutWindow::getTopLevel() const { return GlutMaster::get(this,GLUT_WINDOW_PARENT)==0; }
00215 int GlutWindow::getNumChildren() const { return GlutMaster::get(this,GLUT_WINDOW_NUM_CHILDREN); }
00216
00217 int GlutWindow::getColormapSize() const { return GlutMaster::get(this,GLUT_WINDOW_COLORMAP_SIZE); }
00218 int GlutWindow::getNumSamples() const { return GlutMaster::get(this,GLUT_WINDOW_NUM_SAMPLES); }
00219 int GlutWindow::getCursor() const { return GlutMaster::get(this,GLUT_WINDOW_CURSOR); }
00220
00221 bool GlutWindow::getShiftModifier() const { return _shiftModifier; }
00222 bool GlutWindow::getCtrlModifier() const { return _ctrlModifier; }
00223 bool GlutWindow::getAltModifier() const { return _altModifier; }
00224
00226
00227 void GlutWindow::OnPreDisplay()
00228 {
00229 }
00230
00231 void GlutWindow::OnDisplay()
00232 {
00233 }
00234
00235 void GlutWindow::OnPostDisplay()
00236 {
00237 if (_swapBuffers)
00238 swapBuffers();
00239 }
00240
00241 void GlutWindow::OnTick()
00242 {
00243 postRedisplay();
00244 }
00245
00246 void GlutWindow::OnIdle()
00247 {
00248 postRedisplay();
00249 }
00250
00251 void GlutWindow::OnPreTimer(unsigned char val)
00252 {
00253 if (val==_tickVal)
00254 {
00255 if (_mouseEvents)
00256 {
00257 OnMotion(_mouseX,_mouseY);
00258 _mouseEvents = false;
00259 }
00260
00261 _tickPending = false;
00262 OnTick();
00263 GlutMaster::setTick(this,_tick);
00264 }
00265 }
00266
00267 void GlutWindow::OnTimer(unsigned char val)
00268 {
00269 }
00270
00271 void GlutWindow::OnReshape(int w, int h)
00272 {
00273 _width = w;
00274 _height = h;
00275 _viewport.set(w,h);
00276 postRedisplay();
00277 }
00278
00279 void GlutWindow::OnOpen() {}
00280 void GlutWindow::OnClose() {}
00281
00282 void GlutWindow::OnMotion(int x, int y) {}
00283 void GlutWindow::OnMouse(int button, int state, int x, int y) {}
00284 void GlutWindow::OnPassiveMotion(int x, int y) {}
00285 void GlutWindow::OnEntry(int state) {}
00286 void GlutWindow::OnVisibility(int visible) {}
00287
00288 void GlutWindow::OnKeyboard(unsigned char key, int x, int y)
00289 {
00290 switch (key)
00291 {
00292 case 'q':
00293 case 'Q':
00294 case 27:
00295 close();
00296 return;
00297
00298 case '\t':
00299 toggleGameMode();
00300 break;
00301 }
00302 }
00303
00304 void GlutWindow::OnKeyboardUp(unsigned char key, int x, int y)
00305 {
00306 }
00307
00308 void GlutWindow::OnSpecial(int key, int x, int y)
00309 {
00310 switch (key)
00311 {
00312 case GLUT_KEY_F9: captureRGB (nextFilename()); break;
00313 case GLUT_KEY_F10: captureStencil(nextFilename()); break;
00314 case GLUT_KEY_F11: captureZBuffer(nextFilename()); break;
00315
00316 default:
00317 break;
00318 }
00319 }
00320
00321 void GlutWindow::OnSpecialUp(int key, int x, int y)
00322 {
00323 }
00324
00325 void GlutWindow::OnMenu(int value)
00326 {
00327 switch (value)
00328 {
00329 case MENU_CLOSE:
00330 close();
00331 break;
00332
00333 case MENU_CAPTURE_RGB: captureRGB (nextFilename()); break;
00334 case MENU_CAPTURE_STENCIL: captureStencil(nextFilename()); break;
00335 case MENU_CAPTURE_ZBUFFER: captureZBuffer(nextFilename()); break;
00336
00337 default:
00338 break;
00339 }
00340 }
00341
00342 bool
00343 GlutWindow::captureRGB(const std::string &filename)
00344 {
00345
00346
00347 OnDisplay();
00348
00349
00350
00351 GltFrameBufferRGB rgb;
00352 rgb.read();
00353
00354 #ifdef GLT_PNG
00355
00356 string file(filename + ".png");
00357 ofstream os(file.c_str(),ios::binary);
00358 rgb.writePNG(os);
00359 #else
00360
00361 string file(filename + ".ppm");
00362 ofstream os(file.c_str(),ios::binary);
00363 rgb.writePPM(os);
00364 #endif
00365
00366 return true;
00367 }
00368
00369 bool
00370 GlutWindow::captureZBuffer(const std::string &filename)
00371 {
00372
00373
00374
00375
00376
00377 OnDisplay();
00378
00379
00380
00381 GltFrameBufferZUint z;
00382 z.read();
00383
00384
00385
00386 string file(filename + ".ppm");
00387 ofstream os(file.c_str(),ios::binary);
00388 z.writePPM(os);
00389
00390 return true;
00391 }
00392
00393 bool
00394 GlutWindow::captureStencil(const std::string &filename)
00395 {
00396
00397
00398
00399
00400
00401 OnDisplay();
00402
00403
00404
00405 GltFrameBufferStencilUbyte s;
00406 s.read();
00407
00408
00409
00410 string file(filename + ".ppm");
00411 ofstream os(file.c_str(),ios::binary);
00412 s.writePPM(os);
00413
00414 return true;
00415 }
00416
00417 string
00418 GlutWindow::nextFilename()
00419 {
00420 char filename[9];
00421 sprintf(filename,"%08d",_fileIndex++);
00422 return string(filename);
00423 }