0

I wrote this cpp glfw program that runs 1546 fps

while my monitor refresh rate is 60Hz

how to pause my main loop until my monitor is ready to refresh?

#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <cstdio>
#include <time.h>

int main(void)
{
    time_t begin = time(NULL);
    GLFWwindow* window;

    /* Initialize the library */

    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK)
        printf("Error with glew");

    printf("%s", glGetString(GL_VERSION));
    float i = 0;
    bool s = true;
    unsigned int f = 0;

    /* Loop until the user closes the window */
    while (!(glfwWindowShouldClose(window)))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLES);

        GLdouble red = 255;
        GLdouble green = 0;
        GLdouble blue = 0;

        glColor3d(red, green, blue);

        glVertex2d( 0.0f,  i);
        glVertex2d(-0.5f, -i);
        glVertex2d( 0.5f, -i);

        glEnd();

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();

        if (s)
            i += 0.001f;
        else
            i -= 0.001f;

        if (i >= 1)
            s = false;

        if (i <= -1)
            s = true;

        glfwSwapInterval(0);
        ++f;
    }

    time_t end = time(NULL);
    printf("\nfps avarage = %d\nframes = %d\nseconds = %d", (int)(f / (end - begin)), f, (end - begin));
    glfwTerminate();
    return 0;
}

I get this as an output

4.5.13399 Compatibility Profile Context 15.201.1151.1008
fps avarage = 1546
frames = 3092
seconds = 2

I tried glfwWaitEvents() but it js wait for events

that's not what you want in 3d graphics programming

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

0 Answers0