Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 1 of 1

Thread: Why my FPS calculation is wrong?

  1. #1
    Junior Member Newbie
    Join Date
    May 2018
    Posts
    5

    Why my FPS calculation is wrong?

    The result is a number that always increases while the time passes.

    The code below is wrong
    Code :
    	double seconds = 1;
    	int frames = 0;
     
    	/*----------------------------------Main Game Loop----------------------------------*/
    	while (!glfwWindowShouldClose(window))
    	{
     
    		double start = glfwGetTime();
     
    		std::cout << frames / seconds << std::endl;
     
    		//Handle Events.
    		EventHandler(window);
     
    		//Clear The Screen.
    		renderer->Clear(0, 0, 0);
     
    		sp->Render(renderer, screen_width, screen_height);
     
    		sp->Move(speed * (glfwGetTime() / 1000), left, right, up, down);
     
    		//Update the Renderer.
    		renderer->Update(window);
     
    		double end = glfwGetTime();
     
    		frames++;
     
    		seconds = end - start;
     
     
    	}
    	/*----------------------------------Main Game Loop----------------------------------*/

    Well, actually i figured out that glfwGetTime() returns time in seconds and secondly i made this algorithm:
    Code :
    	Sprite *sp = new Sprite("Resources/Images/cherno.png", 100, 100);
     
    	double time = 0;
    	int frames = 0;
    	int fps = 0;
     
    	/*----------------------------------Main Game Loop----------------------------------*/
    	while (!glfwWindowShouldClose(window))
    	{
     
    		double start = glfwGetTime();
     
    		std::cout << fps << std::endl;
     
    		if (time >= 1)
    		{
    			fps    = frames;
    			frames = 0;
    			time   = 0;
    		}
     
     
    		//Handle Events.
    		EventHandler(window);
     
    		//Clear The Screen.
    		renderer->Clear(0, 0, 0);
     
    		sp->Render(renderer, screen_width, screen_height);
     
    		sp->Move(speed * (glfwGetTime() / 1000), left, right, up, down);
     
    		//Update the Renderer.
    		renderer->Update(window);
     
     
    		frames++;
     
     
     
    		double end = glfwGetTime();
    		time      += end - start;
     
     
    	}
    	/*----------------------------------Main Game Loop----------------------------------*/

    I believe this works, but i would appreciate your advice.
    Last edited by babaliaris; 05-14-2018 at 06:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •