WorldSim  inDev
2D tile-based sandbox RPG with procedurally generated fantasy world simulator 🌏
Driver_GLHooks.hpp
Go to the documentation of this file.
1 /* WorldSim: Driver_GLHooks.hpp
2  #include "Driver_GLHooks.hpp"
3 
4 OpenGL hooks. Catches OpenGL events and translates them for use with internal engine.
5 If DirectX support is implemented, then we would do the same thing with DX events.
6 
7 */
8 //void glutMouseWheelFunc(void*);
9 
10 void shutDown(int signal = 0); // Tidy up the game and shutdown.
11 
12 
13 
14 static void GL_mouseMove(const int mouseX, int mouseY);
15 static inline void GL_mouseClick (const int clickType, const int state, int mouseX, int mouseY);
16 void GL_display();
17 void GL_idle();
18 static void GL_reshape(const int WIDTH, const int HEIGHT);
19 static void GL_keyboardUpEvent(const unsigned char key, const int x, const int y);
20 static void GL_specialFunc(const int key, const int x, const int y);
21 static void GL_mouseWheel (const int /* wheel */, const int /* direction */, const int /* x */, const int /* y */);
22 static void GL_specialUpFunc(const int key, const int x, const int y);
23 static void GL_keyboardEvent(const unsigned char key, const int x, const int y);
24 
25 void GL_init(int nArgs, char ** arg)
26 {
27 
28  /* Set the window position to that defined by the player. This must be put before glutInit because otherwise it will override user preferences provided by the -geometry command line arg. */
29  glutInitWindowPosition(0,0);
30  /* Set the window size to that defined by the player. This must be put before glutInit because otherwise it will override user preferences provided by the -geometry command line arg. */
31  glutInitWindowSize(RESOLUTIONX,RESOLUTIONY);
32  /* Glut needs the pointer to nArgs because it extracts args intended for GLUT. glutInit should be run before doing anything with the args. GLUT will initialist itself here. If GLUT cannot set up, the program could be terminated with an error message.
33  Useful arg: -geometry 100x200+10+10 will set window 10 pixel in from the top left corner, and make the window 100px wide and 200px high. */
34  glutInit(&nArgs, arg);
35 
36  /* Set display mode to single or double buffering, and RGBA colour mode. */
37  if(DOUBLE_BUFFERING==true)
38  { glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); }
39  else
40  { glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); }
41 
42 
43  /* Set the window title. */
44  /* NOTE: For some reason this needs to be after initDisplayMode or we get crashes with certain compilation options. Might be unrelated though. */
45 
46  std::string windowTitle = G_WINDOW_TITLE+" "+DataTools::toString(VERSION)+" "+DataTools::toString(__DATE__)+" "+DataTools::toString(__TIME__)+" "+DataTools::toString(COMPILE_COUNT);
47 
48  glutCreateWindow(windowTitle.c_str());
49 
50  // Load an icon
51  // Stolen from: https://stackoverflow.com/questions/12748103/how-to-change-freeglut-main-window-icon-in-c
52 #ifdef WILDCAT_WINDOWS
53  HWND hwnd = FindWindow(NULL, windowTitle.c_str()); //probably you can get the window handler in different way..
54  HANDLE icon = LoadImage(NULL, "Textures/icon.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE | LR_COLOR);
55  SendMessage(hwnd, (UINT)WM_SETICON, ICON_BIG, (LPARAM)icon);
56  // MAXIMUS THE WINDOW
57  if (MAXIMISE_WINDOW)
58  {
59  ShowWindow(hwnd, SW_SHOWMAXIMIZED);
60  }
61 
62  // CHECK FREE RAM
63  // The game uses a lot of RAM, so it's useful to know how much we have to work with.
64  // Stolen from https://msdn.microsoft.com/en-us/library/windows/desktop/aa366589%28v=vs.85%29.aspx
65 
66  MEMORYSTATUSEX statex;
67  statex.dwLength = sizeof (statex);
68 
69  GlobalMemoryStatusEx (&statex);
70  std::cout<<"There is "<<statex.ullTotalPhys/1024/1024<<" MB of RAM.\n";
71  std::cout<<"There is "<<statex.ullAvailPhys/1024/1024<<" MB of free RAM.\n";
72 
73  std::cout<<"There is "<<statex.ullTotalPageFile/1024/1024<<" MB of paging file.\n";
74  std::cout<<"There is "<<statex.ullAvailPageFile/1024/1024<<" MB of free paging file.\n";
75 
76  std::cout<<"There is "<<statex.ullTotalVirtual/1024/1024<<" MB of virtual memory.\n";
77  std::cout<<"There is "<<statex.ullAvailVirtual/1024/1024<<" MB of free virtual memory.\n";
78 
79  // Local maps currently need about 2MB of RAM each but this is likely to significantly increase
80  // in some cases we might want to make use of virtual memory as well as RAM
81  MAP_CACHE_SIZE = statex.ullAvailPhys/1024/1024/5;
82  if ( MAP_CACHE_SIZE < 12 )
83  {
84  MAP_CACHE_SIZE = 12;
85  std::cout<<"Warning, free RAM may be too low.\n";
86  }
87  std::cout<<"Setting map cache to "<<MAP_CACHE_SIZE<<" maps.\n";
88 #endif
89 
90  // Automatically set the ncores to the OS reported amount of cores (includes logical and physical)
91 #ifdef AUTO_SET_CORES
92  N_CORES = std::thread::hardware_concurrency();
93  if (N_CORES>MAX_CORES)
94  { N_CORES=MAX_CORES; }
95 #endif
96 
97  /* Set perspective? */
98  /* Set window and call reshape. */
99  //glutSetWindow(glutGetWindow());
100  //glutReshapeWindow(RESOLUTIONX,RESOLUTIONY);
101  //glViewport(0, 0, RESOLUTIONX, RESOLUTIONY);
102 
103  /* Initialise GLEW. OpenGL Extension Wrangler Library. Provides extended functions like mipmap generation. Not entirely stable. Must be called after OpenGL is intialised. */
104  /* BUG: Glew seems to crash my netbook when generating mipmaps even though it claims to load properly. My guess is that it expects ^2 textures. */
105  //GLenum err = glewInit();
106  //if (GLEW_OK != err)
107  //{ fprintf(stderr, "GLEW didn't load. Error: %s\n", glewGetErrorString(err));
108  //} fprintf(stdout, "GLEW version: %s\n", glewGetString(GLEW_VERSION));
109 
110  /* Performance tweaks. Hints can be GL_FASTEST, GL_NICEST, or GL_DONT_CARE. */
111  glHint(GL_FOG_HINT, GL_FASTEST);
112  glHint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, GL_FASTEST); /* GL version 2.0 or greater only. */ /* GLEW */
113  glHint(GL_GENERATE_MIPMAP_HINT, GL_FASTEST); /* GL version 1.4 or greater only. */ /* GLEW */
114  glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST);
115  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
116  glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST);
117  glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
118  glHint(GL_TEXTURE_COMPRESSION_HINT, GL_FASTEST); /* GL version 1.3 or greater only. */ /* GLEW */
119 
120  /* Define OpenGL features. */
121  glDisable(GL_DEPTH_TEST);
122  glDisable(GL_LIGHTING);
123  glEnable (GL_BLEND); /* Allows transparency */
124  glEnable(GL_TEXTURE_2D);
125 
126  glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); /* Transparency function */
127  glShadeModel(GL_FLAT); /* GL_FLAT or GL_SMOOTH Stops shit from blurring. Alternative is GL_SMOOTH. */
128 
129  /* I don't think this actually does anything. */
130  glDisable(GL_ALPHA_TEST);
131  glDisable(GL_AUTO_NORMAL);
132  //glDisable(GL_CLIP_PLANE); /* GLEW */
133  glDisable(GL_COLOR_LOGIC_OP);
134  glDisable(GL_COLOR_MATERIAL);
135  glDisable(GL_COLOR_SUM); /* GLEW */
136  glDisable(GL_COLOR_TABLE); /* GLEW */
137  glDisable(GL_DITHER);
138  glDisable(GL_FOG);
139  glDisable(GL_HISTOGRAM); /* GLEW */
140  glDisable(GL_LIGHTING);
141  glDisable(GL_LINE_SMOOTH);
142  glDisable(GL_POINT_SMOOTH);
143  glDisable(GL_POINT_SPRITE); /* GLEW */
144  glDisable(GL_POLYGON_SMOOTH);
145  glDisable(GL_SCISSOR_TEST);
146  glDisable(GL_STENCIL_TEST);
147  glDisable(GL_TEXTURE_1D);
148  glDisable(GL_TEXTURE_3D); /* GLEW */
149 
150 
151  Renderer::setTextureMode();
152 
153 
154  /* OPENGL VERSION? */
155  std::cout<<"OpenGL version: "<<glGetString(GL_VERSION)<<".\n";
156 
157  /* Function pointers for OpenGL to call. */
158  glutDisplayFunc(GL_display);
159  glutReshapeFunc(GL_reshape);
160  glutMouseFunc(GL_mouseClick);
161  glutIdleFunc(GL_idle);
162  glutPassiveMotionFunc(GL_mouseMove);
163  glutMotionFunc(GL_mouseMove);
164  glutKeyboardFunc(GL_keyboardEvent);
165  glutKeyboardUpFunc(GL_keyboardUpEvent);
166  glutSpecialFunc(GL_specialFunc);
167  glutSpecialUpFunc(GL_specialUpFunc);
168  glutMouseWheelFunc(GL_mouseWheel);
169 
170  /* Set screen background to violet. */
171  //glClearColor(127,0,255,255);
172  /* Set current colour to white. Alpha 255 is fully opaque. Alpha 0 is fully transparent. */
173  glColor4ub(255, 255, 255, 255);
174 
175  RENDER_NEXT_FRAME=true;
176 
177 }
178 
179 
180 // Important special keys:
181 // Arrows
182 
183 static void GL_specialUpFunc(const int key, const int x, const int y)
184 {
185  //std::cout<<"s Key: "<<(int)key<<".\n";
186  playerKeypressTimer.start();
187  globalKeyboard.specialKeyUp(key);
188 
189  if ( globalKeyboard.isPressed(Keyboard::LEFT_CTRL) == false && globalKeyboard.isPressed(Keyboard::RIGHT_CTRL) == false)
190  {
191  globalMouse.ctrlPressed=false;
192  }
193  RENDER_NEXT_FRAME=true;
194 }
195 static void GL_specialFunc(const int key, const int x, const int y)
196 {
197  playerKeypressTimer.start();
198  globalKeyboard.specialKeyDown(key);
199 
200  if ( globalKeyboard.isPressed(Keyboard::LEFT_CTRL) || globalKeyboard.isPressed(Keyboard::RIGHT_CTRL))
201  {
202  globalMouse.ctrlPressed=true;
203  }
204 
206  {
208  }
209  else if (activeMenu == MENU_WORLDSIMULATOR )
210  {
212  }
213 
214  globalGuiManager.keyboardEvent(&globalKeyboard);
215  RENDER_NEXT_FRAME=true;
216 }
217 
218 static void GL_reshape(const int WIDTH, const int HEIGHT)
219 {
220  /* Called whenever window size changes. */
221  /* Update global window coordinates. */
222  RESOLUTIONX=WIDTH;
223  RESOLUTIONY=HEIGHT;
224 
225  /* Update projection matrix */
226  glMatrixMode(GL_PROJECTION);
227  glLoadIdentity();
228  /* Viewport should match window dimensions. */
229  glViewport(0, 0,RESOLUTIONX, RESOLUTIONY);
230  gluOrtho2D(0.0, RESOLUTIONX, 0.0, RESOLUTIONY);
231 
232  /* Go back to default matrix mode. */
233  glMatrixMode(GL_MODELVIEW);
234  //glLoadIdentity();
235 
236  /* Update GUI coords. */
237  menuTitle.setPanel(0,0,RESOLUTIONX,RESOLUTIONY);
239  menuOptions.setPanel(0,0,RESOLUTIONX,RESOLUTIONY);
241  menuLoadGame.setPanel(0,0,RESOLUTIONX,RESOLUTIONY);
249  RENDER_NEXT_FRAME=true;
250 }
251 
252 
253 unsigned char lastKey=0;
255 
256 
257 static void GL_keyboardUpEvent(const unsigned char key, const int x, const int y)
258 {
259  playerKeypressTimer.start();
260 
261  if ( key==Keyboard::LEFT_CTRL || key==Keyboard::RIGHT_CTRL)
262  {
263  globalMouse.ctrlPressed=false;
264  }
265 
266  globalKeyboard.keyUp(key);
267  waitingForKeyPress=true;
268  RENDER_NEXT_FRAME=true;
269  //mainRender.keyUp(key,x,y); waitingForKeyPress=true;
270 }
271 
272 static void GL_keyboardEvent(const unsigned char key, const int x, const int y)
273 {
274  //std::cout<<"Key: "<<(int)key<<".\n";
275  playerKeypressTimer.start();
276  globalKeyboard.keyDown(key);
277 
278  if(key==Keyboard::TILDE) // SHIFT+TILDE
279  {
281 
282  if ( HOTKEYS_ENABLED )
283  {
284  std::cout<<"Debug hotkeys: ENABLED.\n";
285  }
286  else
287  {
288  std::cout<<"Debug hotkeys: DISABLED.\n";
289  }
290 
291  }
292 
293 
294 
295  if ( activeMenu == MENU_TITLE )
296  {
298  }
299  else if (activeMenu == MENU_OPTIONS )
300  {
301  menuOptions.keyboardEvent(&globalKeyboard);
302  }
303  else if (activeMenu == MENU_LOADGAME )
304  {
305  menuLoadGame.keyboardEvent(&globalKeyboard);
306  }
307  else if (activeMenu == MENU_WORLDGENERATOR )
308  {
310  }
311  else if (activeMenu == MENU_WORLDSIMULATOR )
312  {
314  }
315  else if (activeMenu == MENU_ADVENTUREMODE )
316  {
318  }
319 
320  if ( key==Keyboard::LEFT_CTRL || key==Keyboard::RIGHT_CTRL)
321  {
322  globalMouse.ctrlPressed=true;
323  }
324 
325  // NOTE:
326  // It seems globalGUIManager is not active for anything except preventing accidental
327  // Hotkey input. In future all gui controls should pass through globalGUIManager.
328  // It should be possible to add guiManagers to globalGUIManager.
329 
330  //if ( keyboardInterfaceManager.keyboardEventAll(&globalKeyboard) )
331  if ( globalGuiManager.keyboardEvent(&globalKeyboard) )
332  //if ( globalGuiManager.stealKeyboard() )
333  {
334  //std::cout<<"GL steal\n";
335  }
336  else if ( HOTKEYS_ENABLED == true && globalGuiManager.stealKeyboard() == false )
337  {
338  //std::cout<<"Key:"<<key<<".\n";
339  // if(key==Keyboard::EQUALS)
340  // {
341  // std::cout<<"LOGIC up.\n";
342  // U_LOGIC_PER_SECOND/=2;
343  // }
344  // if(key==Keyboard::MINUS)
345  // {
346  // std::cout<<"LOGIC down.\n";
347  // U_LOGIC_PER_SECOND*=2;
348  //}
349  // if(key==Keyboard::COMMA)
350  // {
351  // std::cout<<"Logic step.\n";
352  // FORCE_LOGIC_FRAME=true;
353  // }
354  // if(key==Keyboard::PERIOD)
355  // {
356  // std::cout<<"Logic paused.\n";
357  // PAUSE_LOGIC=!PAUSE_LOGIC;
358  // }
359 
360  if(key==Keyboard::CTRL_C) /* CTRL_C is value 3 and works like normal keypress. */
361  {
362  // If we exit with CTRL+C, don't erase save data.
363  CLEAN_SAVES_ON_EXIT=false;
364  QUIT_FLAG=true;
365  }
366 
367  // /* Change framerate to once per second. */
368  // if(key==Keyboard::R || key==Keyboard::r)
369  // {
370 
371  // SLOW_FRAMERATE_ACTIVE = !SLOW_FRAMERATE_ACTIVE;
372  // //UFRAMERATE = 1000000/2;
373 
374  // /* Previous setting was to disable rendering completely,
375  // but I decided to change it to render once per second. */
376 
377  // //DONT_RENDER = !DONT_RENDER;
378  // //glClear(GL_COLOR_BUFFER_BIT);
379  // }
380  // if(key==Keyboard::S || key==Keyboard::s)
381  // {
382  // LIMIT_LOGIC = !LIMIT_LOGIC;
383  // if(LIMIT_LOGIC==true)
384  // { std::cout<<"Logic limited.\n"; }
385  // else
386  // { std::cout<<"Logic unlimited.\n"; }
387  // }
388 
389  }
390  RENDER_NEXT_FRAME=true;
391 }
392 
393 long int logicLateness=0;
394 long int frameLateness=0;
395 
396 void GL_idle()
397 {
398  if(QUIT_FLAG==true)
399  {
400  shutDown();
401  }
402 
403  pollRateTimer.update();
404  if(pollRateTimer.totalUSeconds>UPOLLSPERSECOND)
405  {
406  pollRateTimer.start();
407  //world.keyboardEvent();
408  //world.mouseClick();
409  //world.aiTurn();
410  pollRateTimer.init();
411  }
412 
413  animationTimer.update();
414  if(animationTimer.totalUSeconds>UANIMATIONSPERSECOND)
415  {
416  animationTimer.start();
417 
419 
420 
421  animationTimer.init();
422  }
423 
424  // if(LIMIT_FRAMERATE==true && BUSY_WAIT==true)
425  // {
426 
427  // frameRateTimer.update();
428  // if(frameRateTimer.totalUSeconds<UFRAMERATE && frameRateTimer.totalUSeconds+BUSY_WAIT_USEC>=UFRAMERATE)
429  // {
430  // //std::cout<<"bw.\n";
431  // while(frameRateTimer.totalUSeconds<UFRAMERATE)
432  // {
433  // frameRateTimer.update();
434  // }
435  // }
436  // }
437 
439  {
440  frameRateTimer.update();
441  if(frameRateTimer.totalUSeconds+frameLateness>=SLOW_FRAMERATE)
442  {
443  /* Adjust for late frames by a maximum of half of the time between frames. */
444  //frameLateness = frameRateTimer.totalUSeconds-SLOW_FRAMERATE;
445  if ( frameLateness > SLOW_FRAMERATE/2 )
446  {
447  //frameLateness = SLOW_FRAMERATE/2;
448  }
449  glutPostRedisplay();
450  //GL_display();
451  //frameRateTimer.init();
452  //frameRateTimer.start();
453  }
454  }
455  else if(LIMIT_FRAMERATE==true)
456  {
457  frameRateTimer.update();
458  if(frameRateTimer.totalUSeconds+frameLateness>=UFRAMERATE)
459  {
460  /* Adjust for late frames by a maximum of half of the time between frames. */
461  //frameLateness = frameRateTimer.totalUSeconds-UFRAMERATE;
462  if ( frameLateness > UFRAMERATE/2 )
463  {
464  //frameLateness = UFRAMERATE/2;
465  }
466  glutPostRedisplay();
467  //GL_display();
468  //frameRateTimer.init();
469  //frameRateTimer.start();
470  }
471  }
472  else
473  { glutPostRedisplay(); }
474 
475  /* Logic goes last to prevent hanging the rendering. */
476  /* This is still a problem though. We need to make sure logic is only done when rendering doesn't need to be done soon. Preferably logic should be checked immediately after a render. */
477 
478  if(PAUSE_LOGIC==true)
479  {
480  logicLateness=0;
481  logicRateTimer.start();
482  logicRateTimer.init();
483  }
484 
485  if(FORCE_LOGIC_FRAME==true)
486  {
487  FORCE_LOGIC_FRAME=false;
488  logicRateTimer.update();
489  logicLateness=0;
490 
491  logicTickManager.tickAll();
492  logicRateTimer.start();
493  logicRateTimer.init();
494 
495  }
496  else if(LIMIT_LOGIC==true && PAUSE_LOGIC==false)
497  {
498  logicRateTimer.update();
499 
501  {
503 
504  logicTickManager.tickAll();
505  logicRateTimer.start();
506  logicRateTimer.init();
507  }
508 
509  }
510  else if (PAUSE_LOGIC==false)
511  { logicTickManager.tickAll();
512  }
513 
514  /* Use up some free time between frames to do calculations */
515  idleManager.tickAll();
516 
517  /* relinquish CPU. Alpha freeglut does this automatically (?) */
518  playerKeypressTimer.update();
519  if(RELINQUISH_CPU==true && LIMIT_LOGIC==true && world.ticksBacklog<=0 && playerKeypressTimer.fullSeconds > 0.5 && NO_BACKLOG)
520  {
521 #ifdef WILDCAT_WINDOWS
522  MsgWaitForMultipleObjects( 0, NULL, FALSE, RELINQUISH_CPU_TIMEOUT, QS_ALLINPUT ); /* parameter 4 is milliseconds ie 1000 = 1 second. */
523 #endif
524  }
525 
526 
527 }
528 
531 
532 double rollingAverage=0;
533 
535 {
536  // lock for entire scope
537  //std::lock_guard<std::mutex> guard(render_mutex);
538 
539  if ( DONT_RENDER )
540  { return; }
541 
542  float frameTime = 0;
543 
544  if (OUTPUT_FRAMERATE)
545  {
546  frameRateTimer.update();
547  frameTime = frameRateTimer.fullSeconds;
548  }
549 
551  {
552  frameRateTimer.update();
553 
554  if(frameRateTimer.totalUSeconds+frameLateness>=SLOW_FRAMERATE)
555  {
556  /* Adjust for late frames by a maximum of 1/4 of the time between frames. */
558  if ( frameLateness > SLOW_FRAMERATE/4 )
559  {
561  }
562  frameRateTimer.init();
563  frameRateTimer.start();
564  }
565  }
566  else if(LIMIT_FRAMERATE)
567  {
568  frameRateTimer.update();
569  if(frameRateTimer.totalUSeconds+frameLateness>=UFRAMERATE)
570  {
571  /* Adjust for late frames by a maximum of 1/4 of the time between frames. */
572  frameLateness = frameRateTimer.totalUSeconds-UFRAMERATE;
573  if ( frameLateness > UFRAMERATE/3 )
574  {
576  }
577  //frameLateness = 0;
578 
579  frameRateTimer.init();
580  frameRateTimer.start();
581  }
582  else
583  {
584  return;
585  }
586  }
587 
588 
589 
590 
591 
592  if( OUTPUT_FRAMERATE )
593  {
594 
595  /* Timer must be done like this because OpenGL threads shit which can hang the app but won't show up if the timer is checked directly after OpenGL calls. Maybe the framerate smoother could learn from this. */
596  //debugTimer.update();
597  //std::cout<<"Seconds per frame rolling average: "<<debugTimer.fullSeconds<<".\n";
598 
599  aFrameTime[iFrameTime] = frameTime;
600  ++iFrameTime;
602  {
603  float totalFrameTime = 0;
604  for (int i=0;i<OUTPUT_FRAMERATE_SAMPLE_SIZE;++i)
605  {
606  totalFrameTime+=aFrameTime[i];
607  }
608  totalFrameTime/=OUTPUT_FRAMERATE_SAMPLE_SIZE;
609  std::cout<<"SPF: "<<totalFrameTime<<". ";
610  float frameRate = 1/totalFrameTime;
611  std::cout<<"FPS: "<<frameRate<<".\n";
612 
613  iFrameTime=0;
614  }
615  //debugTimer.start();
616 
617  }
618 
619  if ( LAZY_RENDERING==false || RENDER_NEXT_FRAME )
620  {
621  RENDER_NEXT_FRAME=false;
622 
623  glClear(GL_COLOR_BUFFER_BIT);
624 
625  // NEW SYSTEM FOR MENU MANAGEMENT.
626  // No more complicated hierarchies or menu managers. Just one global variable.
627  // Submenus are still contained within their parent menu.
628 
629  if ( activeMenu == MENU_TITLE )
630  {
631  menuTitle.render();
632  }
633  else if (activeMenu == MENU_OPTIONS )
634  {
636  }
637  else if (activeMenu == MENU_LOADGAME )
638  {
640  }
641  else if (activeMenu == MENU_WORLDGENERATOR )
642  {
644  }
645  else if (activeMenu == MENU_WORLDSIMULATOR )
646  {
648  }
649  else if (activeMenu == MENU_ADVENTUREMODE )
650  {
652  }
653 
654  // Render everything that wants to render.
655  displayInterfaceManager.renderAll();
656 
657  if(DOUBLE_BUFFERING==true)
658  { glutSwapBuffers(); }
659  else
660  { glFlush(); }
661  }
662  }
663 
664 
665 static void GL_mouseWheel (const int wheel, const int direction, const int _x, const int _y)
666 {
667 
668  /* Get window size. */
669  GLint windowDimensions[4];
670  glGetIntegerv( GL_VIEWPORT, windowDimensions );
671  /* Change mouse coordinates to match poly coordinates. (Bottom left origin).*/
672  //_y=windowDimensions[3]-_y;
673  /* Update globalMouse pointer coordinates. */
674  globalMouse.move(_x,windowDimensions[3]-_y);
675 
676  if(direction==1)
677  { globalMouse.isWheelUp=true; globalMouse.isWheelDown=false; }
678  else if (direction==-1)
679  { globalMouse.isWheelDown=true; globalMouse.isWheelUp=false; }
680 
681  /* Send mouse event to all MouseInterface objects. */
682  //mouseInterfaceManager.mouseEventAll(&globalMouse);
683 
684  if ( activeMenu == MENU_TITLE )
685  {
687  }
688  else if (activeMenu == MENU_OPTIONS )
689  {
691  }
692  else if (activeMenu == MENU_LOADGAME )
693  {
695  }
696  else if (activeMenu == MENU_WORLDGENERATOR )
697  {
699  }
700  else if (activeMenu == MENU_WORLDSIMULATOR )
701  {
703  }
704  else if (activeMenu == MENU_ADVENTUREMODE )
705  {
707  }
708  RENDER_NEXT_FRAME=true;
709 }
710 
711 
712 static inline void GL_mouseClick (const int clickType, const int state, int mouseX, int mouseY)
713 {
714  RENDER_NEXT_FRAME=true;
715  /* Remove negative mouse values. */
716  if(mouseX<0) { mouseX=0; }
717  if(mouseY<0) { mouseY=0; }
718  /* Get window size. */
719  GLint windowDimensions[4];
720  glGetIntegerv( GL_VIEWPORT, windowDimensions );
721  /* Change mouse coordinates to match poly coordinates. (Bottom left origin).*/
722  mouseY=windowDimensions[3]-mouseY;
723 
724  /* Mouse click types. */
725  if(state==GLUT_DOWN)
726  {
727  if(clickType==GLUT_LEFT_BUTTON) { globalMouse.isLeftClick=true; globalMouse.isLeftDown=true; }
728  else if(clickType==GLUT_MIDDLE_BUTTON) { globalMouse.isMiddleClick=true; }
729  else if(clickType==GLUT_RIGHT_BUTTON) { globalMouse.isRightClick=true; }
730  }
731  else if(state==GLUT_UP)
732  {
733  if(clickType==GLUT_LEFT_BUTTON) { globalMouse.isLeftClick=false; globalMouse.isLeftDown=false; if (EMULATE_2_BUTTON_MIDDLE_CLICK) {globalMouse.isMiddleClick=false;} }
734  else if(clickType==GLUT_MIDDLE_BUTTON) { globalMouse.isMiddleClick=false; }
735  else if(clickType==GLUT_RIGHT_BUTTON) { globalMouse.isRightClick=false; if (EMULATE_2_BUTTON_MIDDLE_CLICK) {globalMouse.isMiddleClick=false;} }
736  }
737 
738  if ( EMULATE_2_BUTTON_MIDDLE_CLICK && globalMouse.isLeftClick && globalMouse.isRightClick )
739  {
740  globalMouse.isMiddleClick=true;
741  globalMouse.isLeftClick=false;
742  globalMouse.isRightClick=false;
743  }
744  /* Update globalMouse pointer coords. */
745  globalMouse.move(mouseX,mouseY);
746 
747  /* Send mouse event to all MouseInterface objects. */
748  //mouseInterfaceManager.mouseEventAll(&globalMouse);
749 
750  if ( activeMenu == MENU_TITLE )
751  {
753  }
754  else if ( activeMenu == MENU_OPTIONS )
755  {
757  }
758  else if (activeMenu == MENU_LOADGAME )
759  {
761  }
762  else if (activeMenu == MENU_WORLDGENERATOR )
763  {
765  }
766  else if (activeMenu == MENU_WORLDSIMULATOR )
767  {
769  }
770  else if (activeMenu == MENU_ADVENTUREMODE )
771  {
773  }
774 }
775 
776 
777 /* Called if the mouse moves within the window. */
778 static void GL_mouseMove(const int mouseX, int mouseY)
779 {
780  /* Get window size. */
781  GLint windowDimensions[4];
782  glGetIntegerv( GL_VIEWPORT, windowDimensions );
783  /* Change mouse coordinates to match poly coordinates. (Bottom left origin).*/
784  mouseY=windowDimensions[3]-mouseY;
785  /* Update globalMouse pointer coordinates. */
786  globalMouse.move(mouseX,mouseY);
787 
788  /* Send mouse event to all MouseInterface objects. */
789  //mouseInterfaceManager.mouseEventAll(&globalMouse);
790 
791  if ( activeMenu == MENU_TITLE )
792  {
794  }
795  else if (activeMenu == MENU_OPTIONS )
796  {
798  }
799  else if (activeMenu == MENU_LOADGAME )
800  {
802  }
803  else if (activeMenu == MENU_WORLDGENERATOR )
804  {
806  }
807  else if (activeMenu == MENU_WORLDSIMULATOR )
808  {
810  }
811  else if (activeMenu == MENU_ADVENTUREMODE )
812  {
814  }
815  RENDER_NEXT_FRAME=true;
816 }
bool mouseEvent(Mouse *_mouse)
Definition: Menu_LoadGame.hpp:67
bool keyboardEvent(Keyboard *_keyboard)
Definition: Menu_Title.hpp:160
Menu_Options menuOptions
Definition: Driver.cpp:54
Definition: Driver_Settings_Enums.hpp:23
bool keyboardEvent(Keyboard *_keyboard)
Definition: Menu_AdventureMode.hpp:997
const int OUTPUT_FRAMERATE_SAMPLE_SIZE
Definition: Driver_Settings.cpp:212
Definition: Driver_Settings_Enums.hpp:21
int U_LOGIC_PER_SECOND
Definition: Driver_Settings.cpp:227
const int UANIMATIONSPERSECOND
Definition: Driver_Settings.cpp:229
const std::string VERSION
Definition: Driver_Settings.cpp:91
long int frameLateness
Definition: Driver_GLHooks.hpp:394
void render()
Definition: Menu_Options.hpp:27
Menu_AdventureMode menuAdventureMode
Definition: Driver.cpp:67
bool QUIT_FLAG
Definition: Driver_Settings.cpp:307
int UFRAMERATE
Definition: Driver_Settings.cpp:225
Menu_WorldSimulator menuWorldSimulator
Definition: Driver.cpp:64
long long unsigned int ticksBacklog
Definition: World.hpp:86
World world
Definition: Driver.cpp:45
void eventResize()
Definition: Menu_Options.hpp:41
const bool RELINQUISH_CPU
Definition: Driver_Settings.cpp:41
int CURRENT_ANIMATION_FRAME
Definition: Driver_Settings.cpp:230
const bool DOUBLE_BUFFERING
Definition: Driver_Settings.cpp:233
Definition: Driver_Settings_Enums.hpp:20
void eventResize()
Definition: Menu_Title.hpp:57
bool PAUSE_LOGIC
Definition: Driver_Settings.cpp:314
void render()
Definition: Menu_AdventureMode.hpp:678
bool keyboardEvent(Keyboard *_keyboard)
Definition: Menu_WorldSimulator.hpp:480
bool mouseEvent(Mouse *_mouse)
Definition: Menu_Options.hpp:70
Menu_WorldGenerator menuWorldGenerator
Definition: Driver.cpp:61
bool NO_BACKLOG
Definition: Driver_Settings.cpp:276
bool waitingForKeyPress
Definition: Driver_GLHooks.hpp:254
void render()
Definition: Menu_LoadGame.hpp:26
GUI_Manager globalGuiManager
Definition: Driver_GlobalObjects.hpp:102
const std::string G_WINDOW_TITLE
Definition: Driver_Settings.cpp:94
bool HOTKEYS_ENABLED
Definition: Driver_Settings.cpp:272
Mouse globalMouse
Definition: Driver_GlobalObjects.hpp:59
bool LIMIT_LOGIC
Definition: Driver_Settings.cpp:312
double aFrameTime[OUTPUT_FRAMERATE_SAMPLE_SIZE]
Definition: Driver_GLHooks.hpp:529
Menu_Title menuTitle
Definition: Driver.cpp:51
unsigned short int N_CORES
Definition: Driver_Settings.cpp:61
void render()
Definition: Menu_Title.hpp:109
unsigned char lastKey
Definition: Driver_GLHooks.hpp:253
const bool OUTPUT_FRAMERATE
Definition: Driver_Settings.cpp:211
Definition: Driver_Settings_Enums.hpp:24
bool mouseEvent(Mouse *_mouse)
Definition: Menu_WorldGenerator.hpp:592
bool CLEAN_SAVES_ON_EXIT
Definition: Driver_Settings.cpp:274
void eventResize()
Definition: Menu_WorldGenerator.hpp:363
unsigned int RESOLUTIONY
Definition: Driver_Settings.cpp:96
Keyboard globalKeyboard
Definition: Driver_GlobalObjects.hpp:61
const int UPOLLSPERSECOND
Definition: Driver_Settings.cpp:226
Timer logicRateTimer
Definition: Driver_GlobalObjects.hpp:66
bool SLOW_FRAMERATE_ACTIVE
Definition: Driver_Settings.cpp:224
long int logicLateness
Definition: Driver_GLHooks.hpp:393
int SLOW_FRAMERATE
Definition: Driver_Settings.cpp:222
void render()
Definition: Menu_WorldSimulator.hpp:271
void GL_display()
Definition: Driver_GLHooks.hpp:534
const bool LAZY_RENDERING
Definition: Driver_Settings.cpp:239
Definition: Driver_Settings_Enums.hpp:22
int iFrameTime
Definition: Driver_GLHooks.hpp:530
bool EMULATE_2_BUTTON_MIDDLE_CLICK
Definition: Driver_Settings.cpp:317
Menu_LoadGame menuLoadGame
Definition: Driver.cpp:57
const std::string COMPILE_COUNT
Definition: CompileCount.hpp:12
enumMenu activeMenu
Definition: Driver_Settings_Enums.hpp:29
void shutDown(int signal=0)
Definition: Driver.cpp:155
void eventResize()
Definition: Menu_WorldSimulator.hpp:756
void eventResize()
Definition: Menu_AdventureMode.hpp:1815
bool mouseEvent(Mouse *_mouse)
Definition: Menu_AdventureMode.hpp:1418
Timer frameRateTimer
Definition: Driver_GlobalObjects.hpp:64
Timer animationTimer
Definition: Driver_GlobalObjects.hpp:68
LogicTickManager logicTickManager
Definition: Driver_GlobalObjects.hpp:107
const unsigned short int MAX_CORES
Definition: Driver_Settings.cpp:62
void render()
Definition: Menu_WorldGenerator.hpp:493
unsigned int RESOLUTIONX
Definition: Driver_Settings.cpp:96
const bool LIMIT_FRAMERATE
Definition: Driver_Settings.cpp:214
void GL_init(int nArgs, char **arg)
Definition: Driver_GLHooks.hpp:25
Timer pollRateTimer
Definition: Driver_GlobalObjects.hpp:65
IdleTickManager idleManager
Definition: Driver_GlobalObjects.hpp:111
double rollingAverage
Definition: Driver_GLHooks.hpp:532
const unsigned int RELINQUISH_CPU_TIMEOUT
Definition: Driver_Settings.cpp:42
unsigned int MAP_CACHE_SIZE
Definition: Driver_Settings.cpp:308
Timer playerKeypressTimer
Definition: Driver_GlobalObjects.hpp:69
bool RENDER_NEXT_FRAME
Definition: Driver_Settings.cpp:240
DisplayInterfaceManager displayInterfaceManager
Definition: Driver_GlobalObjects.hpp:86
Definition: Driver_Settings_Enums.hpp:25
bool mouseEvent(Mouse *_mouse)
Definition: Menu_WorldSimulator.hpp:567
bool FORCE_LOGIC_FRAME
Definition: Driver_Settings.cpp:313
void eventResize()
Definition: Menu_LoadGame.hpp:55
bool mouseEvent(Mouse *_mouse)
Definition: Menu_Title.hpp:117
void GL_idle()
Definition: Driver_GLHooks.hpp:396
bool DONT_RENDER
Definition: Driver_Settings.cpp:311
bool keyboardEvent(Keyboard *_keyboard)
Definition: Menu_WorldGenerator.hpp:550
const bool MAXIMISE_WINDOW
Definition: Driver_Settings.cpp:97