fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Contrib / ComplexSceneManager / Passive / OSGCSMPassiveWindow.cpp
blob131cc588b47569170696c2d42aa473506513d6ed
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGCSMPassiveWindow.h"
49 #include "OSGCSMPassiveWinClearOp.h"
51 #include "OSGPassiveWindow.h"
52 #include "OSGComplexSceneManager.h"
53 #include "OSGGLUT.h"
55 OSG_BEGIN_NAMESPACE
57 // Documentation for this class is emitted in the
58 // OSGCSMPassiveWindowBase.cpp file.
59 // To modify it, please change the .fcd file (OSGCSMPassiveWindow.fcd) and
60 // regenerate the base file.
62 /***************************************************************************\
63 * Class variables *
64 \***************************************************************************/
66 bool CSMPassiveWindow::_bGLUTInitialized = false;
67 CSMPassiveWindow *CSMPassiveWindow::_pPassiveWindow = NULL;
69 /***************************************************************************\
70 * Class methods *
71 \***************************************************************************/
73 void CSMPassiveWindow::initMethod(InitPhase ePhase)
75 Inherited::initMethod(ePhase);
77 if(ePhase == TypeObject::SystemPost)
83 void CSMPassiveWindow::csmGlutKeyHandler(UChar8 key,
84 Int32 x,
85 Int32 y )
87 switch(key)
89 case 27:
91 glutKeyboardFunc (NULL);
92 glutReshapeFunc (NULL);
93 // glutDisplayFunc (NULL);
94 glutMouseFunc (NULL);
95 glutMotionFunc (NULL);
96 glutIdleFunc (NULL);
98 ComplexSceneManager::the()->terminate();
100 osgExit();
102 exit(0);
104 break;
106 default:
107 ComplexSceneManager::the()->key(x,
109 CSMKeyData::ButtonDown,
110 key);
111 break;
115 void CSMPassiveWindow::csmGlutReshapeHandler(Int32 w,
116 Int32 h)
118 glViewport(0, 0,
119 w * _pPassiveWindow->getViewportScale()[0],
120 h * _pPassiveWindow->getViewportScale()[1]);
122 if(_pPassiveWindow->getViewportScale()[0] < 1.0 ||
123 _pPassiveWindow->getViewportScale()[1] < 1.0 )
125 glScissor(0, 0,
126 w * _pPassiveWindow->getViewportScale()[0],
127 h * _pPassiveWindow->getViewportScale()[1]);
129 glEnable(GL_SCISSOR_TEST);
131 else
133 glDisable(GL_SCISSOR_TEST);
136 _pPassiveWindow->reshape(w, h);
138 glutPostRedisplay();
141 void CSMPassiveWindow::csmGlutFrameHandler(void)
143 if(_pPassiveWindow->getClearOp() != NULL)
145 _pPassiveWindow->getClearOp()->execute(_pPassiveWindow);
147 else
149 glClearColor(0.2, 0.2, 0.5, 0.0);
150 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
153 ComplexSceneManager::the()->frame();
155 Thread::getCurrentChangeList()->commitChangesAndClear();
157 if(_pPassiveWindow->getClearOp() != NULL)
159 _pPassiveWindow->getClearOp()->postDraw(_pPassiveWindow);
162 glutSwapBuffers();
165 void CSMPassiveWindow::csmGlutMouseHandler(Int32 iButton,
166 Int32 iState,
167 Int32 x,
168 Int32 y )
170 _pPassiveWindow->mouse(iButton,
171 iState,
172 1 | (glutGetModifiers() << 1),
177 void CSMPassiveWindow::csmGlutMouseMotionHandler(Int32 x,
178 Int32 y)
180 _pPassiveWindow->motion(x, y, 1 | (glutGetModifiers() << 1));
184 /***************************************************************************\
185 * Instance methods *
186 \***************************************************************************/
188 /*-------------------------------------------------------------------------*\
189 - private -
190 \*-------------------------------------------------------------------------*/
192 /*----------------------- constructors & destructors ----------------------*/
194 CSMPassiveWindow::CSMPassiveWindow(void) :
195 Inherited ( ),
196 _iGlutWinId(-1)
200 CSMPassiveWindow::CSMPassiveWindow(const CSMPassiveWindow &source) :
201 Inherited (source),
202 _iGlutWinId( -1)
206 CSMPassiveWindow::~CSMPassiveWindow(void)
210 void CSMPassiveWindow::terminateGLContext(void)
212 if(_pWindow != NULL)
214 _pWindow->terminate();
217 glutDestroyWindow(_iGlutWinId);
220 /*----------------------------- class specific ----------------------------*/
222 void CSMPassiveWindow::changed(ConstFieldMaskArg whichField,
223 UInt32 origin,
224 BitVector details)
226 Inherited::changed(whichField, origin, details);
229 void CSMPassiveWindow::dump( UInt32 ,
230 const BitVector ) const
232 SLOG << "Dump CSMPassiveWindow NI" << std::endl;
235 bool CSMPassiveWindow::init(void)
237 if(_bGLUTInitialized == false)
239 Int32 argc = 1;
240 const Char8 *argv[] = { "testCSM-Passive" };
242 glutInit(&argc, const_cast<Char8 **>(argv));
245 UInt32 uiDisplayMode = (GLUT_RGBA |
246 GLUT_DEPTH |
247 GLUT_DOUBLE |
248 GLUT_STENCIL |
249 GLUT_MULTISAMPLE);
251 #if 0
252 if(_pVSCWindow->stereo() == true)
254 uiDisplayMode |= GLUT_STEREO;
256 #endif
258 OSG::PassiveWindowUnrecPtr pPWindow = OSG::PassiveWindow::create();
260 if(pPWindow == NULL)
261 return false;
263 glutInitDisplayMode(uiDisplayMode);
265 if(this->getXPos() > 0.f && this->getYPos() > 0.f)
267 glutInitWindowPosition(Int32(this->getXPos()),
268 Int32(this->getYPos()));
271 if(this->getXSize() > 0.f && this->getYSize() > 0.f)
273 glutInitWindowSize(Int32(this->getXSize()),
274 Int32(this->getYSize()));
277 _iGlutWinId = glutCreateWindow("OpenSG - CSM");
279 // pGLUTWindow->setGlutId(iWinId);
281 if(this->getXSize() > 0.f && this->getYSize() > 0.f)
283 pPWindow->resize(Int32(this->getXSize()),
284 Int32(this->getYSize()));
287 if(ComplexSceneManager::the() != NULL)
288 ComplexSceneManager::the()->setMainloop(glutMainLoop);
290 #if 0
291 if(_pVSCWindow->getXSize () < 0.f &&
292 _pVSCWindow->getYSize () < 0.f &&
293 _pVSCWindow->getDecorEnabled() == false)
295 glutFullScreen();
297 #endif
299 _pWindow = pPWindow;
300 _pPassiveWindow = this;
302 if(_bGLUTInitialized == false)
304 _bGLUTInitialized = true;
306 // _pFirstWindow = this;
308 glutKeyboardFunc (csmGlutKeyHandler );
309 // glutVisibilityFunc(vscGlutVisHandler );
310 glutReshapeFunc (csmGlutReshapeHandler );
311 glutDisplayFunc (csmGlutFrameHandler );
312 glutMouseFunc (csmGlutMouseHandler );
313 glutMotionFunc (csmGlutMouseMotionHandler);
314 glutIdleFunc (csmGlutFrameHandler );
317 pPWindow->init();
319 #if 0
320 OSG::IndentFileOutStream outFileStream("/tmp/window.osg");
322 if(outFileStream)
324 std::cerr << "STARTING PRINTOUT:" << std::endl;
326 OSG::OSGWriter writer(outFileStream, 4);
328 writer.write(_pOSGWindow);
330 outFileStream.close();
332 #endif
334 Inherited::init();
336 // pPWindow->activate();
338 return true;
341 OSG_END_NAMESPACE