Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: The fps of the sfml window seems to be limited to 60fps  (Read 496 times)

0 Members and 1 Guest are viewing this topic.

Andrew_hh

  • Newbie
  • *
  • Posts: 2
    • View Profile
The fps of the sfml window seems to be limited to 60fps
« on: July 01, 2023, 06:58:05 pm »
No matter how I set it, the fps can't exceed 60.  setFramerateLimit works only value < 60 (not 0).
fps value output:
57.3579
65.9922
60.532
63.6517
61.9053
64.9473
61.2239
62.0806
61.2557
62.7817
61.5756
62.0621
63.4546
61.4655
63.3172
61.0191
63.2627
65.7592
61.859
62.3768
61.7566
60.3497
61.0456
61.2505
60.7367
61.6625
62.4637
60.7268
62.7435
64.1807
61.3576
61.8789
61.1131
60.5554
59.0211
 

code: (shader is not complex)

#include <SFML/Window.hpp>
#include <glad/gl.h>
#include <iostream>
#include <shader.h>
#include <chrono>

int main()
{

    sf::ContextSettings settings;
    settings.majorVersion = 3;
    settings.minorVersion = 3;
    settings.attributeFlags = sf::ContextSettings::Core;

    sf::Window window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, settings);
    window.setFramerateLimit(0);
    window.setVerticalSyncEnabled(true);

    window.setActive(true);

    // Load OpenGL functions, gladLoadGL returns the loaded version, 0 on error.
    int version = gladLoaderLoadGL();
    if (version == 0) {
        std::cout << "Failed to initialize OpenGL context" << std::endl;
        return EXIT_FAILURE;
    }

    Shader shader("vertex_shader.glsl", "fragment_shader.glsl");
    float vertices[] = {
        // positions         // colors
        0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,  // bottom right
        -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,  // bottom left
        0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f   // top
    };

    unsigned int VAO, VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *) 0);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *) (3 * sizeof(float)));
    glEnableVertexAttribArray(1);

    bool running = true;
    while (running) {
        auto start = std::chrono::high_resolution_clock::now();
    // window.draw, etc.


        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                running = false;
            } else if (event.type == sf::Event::Resized) {
                 glViewport(0, 0, event.size.width, event.size.height);
            }
        }

        // Render
        // Clear the colorbuffer
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        shader.use();
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        window.display();
        auto end = std::chrono::high_resolution_clock::now();
        auto fps = (float)1e9/(float)std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count();
        std::cout << fps << std::endl;
    }
    return 0;
}
 

Andrew_hh

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: The fps of the sfml window seems to be limited to 60fps
« Reply #1 on: July 01, 2023, 07:01:28 pm »
I know, it's because I set: window.setVerticalSyncEnabled(true);
fps excceed 60 after remoing it.

 

anything