1 #include "ContextGLUT.hh"
2 #include <bias_config.h>
3 #include <Gui/biasgl.h>
6 #include <Utils/GlewInitWrapper.hh>
7 #include <Utils/GlutInitWrapper.hh>
9 #ifdef GLUT_TIME_MEASURE
10 #include <Base/Debug/TimeMeasure.hh>
12 unsigned int timeMeasureCount_ = 0;
13 unsigned int outputCount_ = 0;
14 unsigned int outputIntervall = 1;
15 unsigned int MEASURE_INTERVALL = 1;
16 unsigned int BIAS::ContextGLUT::measurementsCount_ = 0;
17 std::vector<double> BIAS::ContextGLUT::fpsMeasures;
25 std::vector<ContextGLUT*> registeredGlutWindows;
27 void SetRegisteredContext() {
28 RegisteredContext = NULL;
29 for(
unsigned int i=0; i<registeredGlutWindows.size(); i++) {
30 if(registeredGlutWindows[i]->GetGlutWindowID()==glutGetWindow()) {
31 RegisteredContext = registeredGlutWindows[i];
40 struct MouseActionStruct
42 int lastLeftX, lastLeftY;
43 int lastRightX, lastRightY;
44 int lastMiddleX, lastMiddleY;
50 bool listeningForLeftDoubleClick;
51 bool receivedLeftDoubleClick;
53 bool listeningForRightDoubleClick;
54 bool receivedRightDoubleClick;
55 bool rightButtonWasUp;
58 #define DOUBLE_CLICK_MS 200
63 SetRegisteredContext();
65 if(RegisteredContext->
control_==NULL) {
70 if(!(MouseAction.leftDown && MouseAction.rightDown)) {
71 if(MouseAction.leftDown) {
73 MouseAction.lastLeftY, y);
74 MouseAction.lastLeftX=x;
75 MouseAction.lastLeftY=y;
77 if(MouseAction.rightDown) {
80 MouseAction.lastRightY, y);
81 MouseAction.lastRightX=x;
82 MouseAction.lastRightY=y;
86 MouseAction.lastRightY, y);
87 MouseAction.lastLeftX=x;
88 MouseAction.lastLeftY=y;
89 MouseAction.lastRightX=x;
90 MouseAction.lastRightY=y;
93 if(MouseAction.middleDown) {
95 MouseAction.lastMiddleY, y);
96 MouseAction.lastMiddleX=x;
97 MouseAction.lastMiddleY=y;
106 SetRegisteredContext();
108 if(RegisteredContext->
control_==NULL) {
131 void leftDoubleClickListener(
int) {
132 MouseAction.listeningForLeftDoubleClick =
false;
133 if(MouseAction.receivedLeftDoubleClick)
137 if(!MouseAction.leftButtonWasUp)
141 MouseAction.lastLeftY);
147 void rightDoubleClickListener(
int) {
148 MouseAction.listeningForRightDoubleClick =
false;
149 if(MouseAction.receivedRightDoubleClick)
153 if(!MouseAction.rightButtonWasUp)
157 MouseAction.lastRightY);
166 SetRegisteredContext();
169 case GLUT_LEFT_BUTTON :
174 if(!MouseAction.listeningForLeftDoubleClick) {
175 MouseAction.leftDown =
true;
176 MouseAction.lastLeftX = x;
177 MouseAction.lastLeftY = y;
178 MouseAction.receivedLeftDoubleClick =
false;
179 MouseAction.leftButtonWasUp =
false;
180 MouseAction.listeningForLeftDoubleClick =
true;
182 leftDoubleClickListener, 0);
185 MouseAction.receivedLeftDoubleClick =
true;
186 int xDiff = MouseAction.lastLeftX - x;
187 int yDiff = MouseAction.lastLeftY - y;
188 if(xDiff == 0 && yDiff == 0) {
200 MouseAction.leftButtonWasUp =
true;
201 MouseAction.leftDown =
false;
208 case GLUT_RIGHT_BUTTON :
213 if(!MouseAction.listeningForRightDoubleClick) {
214 MouseAction.rightDown =
true;
215 MouseAction.lastRightX = x;
216 MouseAction.lastRightY = y;
217 MouseAction.receivedRightDoubleClick =
false;
218 MouseAction.rightButtonWasUp =
false;
219 MouseAction.listeningForRightDoubleClick =
true;
221 rightDoubleClickListener, 0);
224 MouseAction.receivedRightDoubleClick =
true;
225 int xDiff = MouseAction.lastRightX - x;
226 int yDiff = MouseAction.lastRightY - y;
227 if(xDiff == 0 && yDiff == 0) {
238 MouseAction.rightButtonWasUp =
true;
239 MouseAction.rightDown =
false;
245 case GLUT_MIDDLE_BUTTON :
249 MouseAction.lastMiddleX = x;
250 MouseAction.lastMiddleY = y;
251 MouseAction.middleDown =
true;
253 case GLUT_UP : MouseAction.middleDown =
false;
break;
263 SetRegisteredContext();
271 SetRegisteredContext();
278 SetRegisteredContext();
320 case GLUT_KEY_PAGE_UP:
323 case GLUT_KEY_PAGE_DOWN:
346 SetRegisteredContext();
388 case GLUT_KEY_PAGE_UP:
391 case GLUT_KEY_PAGE_DOWN:
414 SetRegisteredContext();
420 SetRegisteredContext();
421 #ifdef GLUT_TIME_MEASURE
422 timeMeasure_.
Start();
426 RegisteredContext->
Render();
428 #ifdef GLUT_TIME_MEASURE
431 if(timeMeasureCount_ > MEASURE_INTERVALL) {
432 double fps = double(timeMeasureCount_*1000)/timeMeasure_.
GetUserTime();
433 if(ContextGLUT::measurementsCount_<ContextGLUT::fpsMeasures.size())
434 ContextGLUT::fpsMeasures[BIAS::ContextGLUT::measurementsCount_++] = fps;
436 if(outputCount_ == outputIntervall) {
437 stringstream fpsString;
438 fpsString<<
"user time: "<<fps<<
"fps";
439 glutSetWindowTitle(fpsString.str().c_str());
441 if(fps>100) outputIntervall = (
unsigned int) (fps/50.0);
446 timeMeasureCount_ = 0;
447 timeMeasure_.
Reset();
461 ContextGLUT(
int x0,
int y0,
int width,
int height,
462 const std::string& name,
463 const char* initDisplayString,
464 int* commandLineArgc,
465 char** commandLineArgv)
468 RegisteredContext =
this;
469 registeredGlutWindows.push_back(
this);
474 if(registeredGlutWindows.size() == 1)
483 const std::string& name,
484 const char* initDisplayString,
485 int* commandLineArgc,
486 char** commandLineArgv)
489 if (gamemodestring.size()==0)
490 gamemodestring =
"1024x768:32";
492 RegisteredContext =
this;
493 registeredGlutWindows.push_back(
this);
498 if(registeredGlutWindows.size() == 1)
509 RegisteredContext = NULL;
531 int* commandLineArgc,
532 char** commandLineArgv)
534 if(commandLineArgc == NULL || commandLineArgv == NULL) {
539 if (initDisplayString == NULL) {
542 glutInitDisplayMode(GLUT_DOUBLE
549 glutInitDisplayString(initDisplayString);
555 const std::string& name)
558 BIASERR(
"ContextGLUT can only keep track of single window\n");
561 glutInitWindowPosition(x0, y0);
562 glutInitWindowSize(width, height);
563 windowID_ = glutCreateWindow(name.c_str());
577 if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE) != 0)
593 RegisteredContext =
this;
612 void oneShotTimer(
int interval)
619 RegisteredContext->
SetTimer(interval);
627 if(!first && !active) {
631 glutTimerFunc(interval, &oneShotTimer, interval);
645 BIASERR(
"ContextGLUT can only keep track of single window\n");
648 glutGameModeString(gamemodestring.c_str());
649 if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
650 BIASWARN(
"entering glut gamemode");
655 BIASERR(
"Full Screen Mode not available: " << gamemodestring);
virtual void InitContext_(const char *initDisplayString=NULL, int *commandLineArgc=NULL, char **commandLineArgv=NULL)
Initialization of glut library, initial display state and registration of callbacks.
bool LeftMouseDoubleClicked(int x, int y, int m)
bool LeftMouseSingleClicked(int x, int y)
ContextGLUT(int x0=0, int y0=0, int width=400, int height=600, const std::string &name="ContextGLUT Window", const char *initDisplayString=NULL, int *commandLineArgc=NULL, char **commandLineArgv=NULL)
void RegisterCallbacks_() const
virtual void CreateWindow_(int x0, int y0, int width, int height, const std::string &name)
Creates a glut window and registers all known callbacks to it.
bool StandardKeyUp(unsigned char, int, int)
unsigned int doubleClickInterval_
bool RightMouseDoubleClicked(int x, int y)
virtual void Redisplay()
Demands a rendering from the Context;.
void Reshape_(int width, int height)
bool LeftMouseDown(int x, int y)
virtual int Render()
Simple rendering routine.
bool LeftMouseMoved(int x1, int x2, int y1, int y2)
bool TimerExpired()
Always informs both camera and scenes!
ControlInterface * control_
friend void Reshape_(int width, int height)
bool SpecialKeyUp(int key)
void SpecialKeyFunc_(int key, int x, int y)
static GlutInitWrapper * GetInstance()
bool StandardKeyPressed(unsigned char, int, int)
friend void StandardKeyFunc_(unsigned char key, int x, int y)
friend void MouseButtonFunc_(int button, int state, int x, int y)
friend void SpecialKeyFunc_(int key, int x, int y)
virtual void SetTimer(unsigned int interval)
activate timer with intervall in ms
bool RightMouseSingleClicked(int x, int y)
bool LeftMouseUp(int x, int y)
virtual void DeactivateTimer()
deactivate timer
virtual void MakeGLContextCurrent()
try to make this OpenGl context current
void EnterGameMode_(const std::string gamemodestring)
bool LeftAndRightMouseMoved(int x1, int x2, int y1, int y2)
virtual void DestroyWindow_()
friend void MouseMotionFunc_(int x, int y)
void StandardKeyFunc_(unsigned char key, int x, int y)
bool WindowReshape(int width, int height)
void SpecialKeyUpFunc_(int key, int x, int y)
bool MiddleMouseMoved(int x1, int x2, int y1, int y2)
bool MouseMoved(int x, int y)
void MouseButtonFunc_(int button, int state, int x, int y)
friend void SpecialKeyUpFunc_(int key, int x, int y)
void StandardKeyUpFunc_(unsigned char key, int x, int y)
std::vector< ContextGLUT * >::iterator registrationIterator_
bool SpecialKeyPressed(int key)
friend void MousePassiveMotionFunc_(int x, int y)
bool RightMouseMoved(int x1, int x2, int y1, int y2)
friend void StandardKeyUpFunc_(unsigned char key, int x, int y)
double GetUserTime() const
return user time (=system usage time) in msec JW For Win32: user-time is the sum over all processes o...
class TimeMeasure contains functions for timing real time and cpu time.
void MouseMotionFunc_(int x, int y)
void MousePassiveMotionFunc_(int x, int y)
Base for all classes creating interface between GL and "window manager".