@@ -70,6 +70,8 @@ std::vector<MiniGL::MousePressFct> MiniGL::m_mousePressFct;
7070std::vector<MiniGL::MouseMoveFct> MiniGL::m_mouseMoveFct;
7171std::vector<MiniGL::MouseWheelFct> MiniGL::m_mouseWheelFct;
7272GLFWwindow* MiniGL::m_glfw_window = nullptr ;
73+ bool MiniGL::m_vsync = false ;
74+ double MiniGL::m_lastTime;
7375
7476void MiniGL::bindTexture ()
7577{
@@ -468,14 +470,15 @@ void MiniGL::setClientSceneFunc (SceneFct func)
468470 scenefunc = func;
469471}
470472
471- void MiniGL::init (int argc, char **argv, const int width, const int height, const char *name)
473+ void MiniGL::init (const int width, const int height, const char *name, const bool vsync, const bool maximized )
472474{
473475 fovy = 60 ;
474476 znear = 0 .5f ;
475477 zfar = 1000 ;
476478
477479 m_width = width;
478480 m_height = height;
481+ m_vsync = vsync;
479482
480483 scenefunc = nullptr ;
481484
@@ -488,7 +491,13 @@ void MiniGL::init(int argc, char **argv, const int width, const int height, cons
488491 // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
489492 // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
490493
491- // glfwWindowHint(GLFW_DOUBLEBUFFER, GL_FALSE);
494+ if (maximized)
495+ glfwWindowHint (GLFW_MAXIMIZED, GLFW_TRUE);
496+
497+ if (m_vsync)
498+ glfwWindowHint (GLFW_DOUBLEBUFFER, GL_TRUE);
499+ else
500+ glfwWindowHint (GLFW_DOUBLEBUFFER, GL_FALSE);
492501 m_glfw_window = glfwCreateWindow (width, height, name, NULL , NULL );
493502 if (!m_glfw_window)
494503 {
@@ -525,6 +534,8 @@ void MiniGL::init(int argc, char **argv, const int width, const int height, cons
525534 glfwSetScrollCallback (m_glfw_window, mouseWheel);
526535
527536 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
537+
538+ m_lastTime = glfwGetTime ();
528539}
529540
530541void MiniGL::initTexture ()
@@ -604,12 +615,12 @@ void MiniGL::setClientDestroyFunc(DestroyFct func)
604615 destroyfunc = func;
605616}
606617
607- void MiniGL::addKeyFunc ( unsigned char k , std::function<void ()> const & func)
618+ void MiniGL::addKeyFunc ( int key, int modifiers , std::function<void ()> const & func)
608619{
609620 if (func == nullptr )
610621 return ;
611622 else
612- keyfunc.push_back ({ func, k });
623+ keyfunc.push_back ({ func, key, modifiers });
613624}
614625
615626void MiniGL::keyboard (GLFWwindow* window, int key, int scancode, int action, int mods)
@@ -621,6 +632,13 @@ void MiniGL::keyboard(GLFWwindow* window, int key, int scancode, int action, int
621632 return ;
622633 }
623634
635+ // execute user key functions
636+ for (int i = 0 ; i < keyfunc.size (); i++)
637+ {
638+ if ((key == keyfunc[i].key ) && (mods == keyfunc[i].modifiers ) && (action == GLFW_PRESS))
639+ keyfunc[i].fct ();
640+ }
641+
624642 if (key == GLFW_KEY_ESCAPE)
625643 {
626644 m_breakPointLoop = false ;
@@ -657,19 +675,14 @@ void MiniGL::char_callback(GLFWwindow* window, unsigned int codepoint)
657675 return ;
658676 }
659677
660- for (int i = 0 ; i < keyfunc.size (); i++)
661- {
662- if (codepoint == keyfunc[i].key )
663- keyfunc[i].fct ();
664- else if (codepoint == GLFW_KEY_1)
665- rotateX (-turnspeed);
666- else if (codepoint == GLFW_KEY_2)
667- rotateX (turnspeed);
668- else if (codepoint == GLFW_KEY_3)
669- rotateY (-turnspeed);
670- else if (codepoint == GLFW_KEY_4)
671- rotateY (turnspeed);
672- }
678+ if (codepoint == GLFW_KEY_1)
679+ rotateX (-turnspeed);
680+ else if (codepoint == GLFW_KEY_2)
681+ rotateX (turnspeed);
682+ else if (codepoint == GLFW_KEY_3)
683+ rotateY (-turnspeed);
684+ else if (codepoint == GLFW_KEY_4)
685+ rotateY (turnspeed);
673686}
674687
675688void MiniGL::setProjectionMatrix (int width, int height)
@@ -964,8 +977,8 @@ void MiniGL::drawSelectionRect()
964977 glOrtho (0 .0f , m_width, m_height, 0 .0f , -1 .0f , 1 .0f );
965978 glBegin (GL_LINE_LOOP);
966979 glColor3f (1 , 0 , 0 ); // Set the colour to red
967- glVertex2f (m_selectionStart[0 ], m_selectionStart[1 ]);
968- glVertex2f (m_selectionStart[0 ], mouse_pos_y_old);
980+ glVertex2f (static_cast <GLfloat>( m_selectionStart[0 ]), static_cast <GLfloat>( m_selectionStart[1 ]) );
981+ glVertex2f (static_cast <GLfloat>( m_selectionStart[0 ]), static_cast <GLfloat>( mouse_pos_y_old) );
969982 glVertex2f (mouse_pos_x_old, mouse_pos_y_old);
970983 glVertex2f (mouse_pos_x_old, m_selectionStart[1 ]);
971984 glEnd ();
@@ -980,22 +993,30 @@ void MiniGL::mainLoop()
980993{
981994 while (!glfwWindowShouldClose (m_glfw_window))
982995 {
983- glfwPollEvents ();
984-
985996 if (idlefunc != nullptr )
986997 idlefunc ();
987998
988- glPolygonMode (GL_FRONT_AND_BACK, drawMode);
989- viewport ();
999+ double currentTime = glfwGetTime ();
1000+ if (currentTime - m_lastTime >= 1.0 / 60.0 ) // render at maximum at 60 fps
1001+ {
1002+ glfwPollEvents ();
1003+
1004+ glPolygonMode (GL_FRONT_AND_BACK, drawMode);
1005+ viewport ();
1006+
1007+ if (scenefunc != nullptr )
1008+ scenefunc ();
9901009
991- if (scenefunc != nullptr )
992- scenefunc ();
1010+ if (selectionMode )
1011+ drawSelectionRect ();
9931012
994- if (selectionMode)
995- drawSelectionRect ();
1013+ if (m_vsync)
1014+ glfwSwapBuffers (m_glfw_window);
1015+ else
1016+ glFlush ();
9961017
997- glfwSwapBuffers (m_glfw_window) ;
998- // glFlush();
1018+ m_lastTime = currentTime ;
1019+ }
9991020 }
10001021
10011022 if (destroyfunc != nullptr )
@@ -1015,8 +1036,10 @@ void MiniGL::leaveMainLoop()
10151036
10161037void MiniGL::swapBuffers ()
10171038{
1018- glfwSwapBuffers (m_glfw_window);
1019- // glFlush();
1039+ if (m_vsync)
1040+ glfwSwapBuffers (m_glfw_window);
1041+ else
1042+ glFlush ();
10201043}
10211044
10221045void MiniGL::getWindowPos (int &x, int &y)
@@ -1039,6 +1062,19 @@ void MiniGL::setWindowSize(int w, int h)
10391062 glfwSetWindowSize (m_glfw_window, w, h);
10401063}
10411064
1065+ bool MiniGL::getWindowMaximized ()
1066+ {
1067+ return glfwGetWindowAttrib (m_glfw_window, GLFW_MAXIMIZED);
1068+ }
1069+
1070+ void MiniGL::setWindowMaximized (const bool b)
1071+ {
1072+ if (b)
1073+ glfwRestoreWindow (m_glfw_window);
1074+ else
1075+ glfwRestoreWindow (m_glfw_window);
1076+ }
1077+
10421078void MiniGL::breakPointMainLoop ()
10431079{
10441080 if (m_breakPointActive)
@@ -1052,8 +1088,10 @@ void MiniGL::breakPointMainLoop()
10521088 if (scenefunc != nullptr )
10531089 scenefunc ();
10541090
1055- glfwSwapBuffers (m_glfw_window);
1056- // glFlush();
1091+ if (m_vsync)
1092+ glfwSwapBuffers (m_glfw_window);
1093+ else
1094+ glFlush ();
10571095 glfwPollEvents ();
10581096 }
10591097 }
0 commit comments