Controlling FPS

edited November 2011 in Help request
Hi again!

Mr google:
My game runs approximately between 40 and 220 FPS.

How I can do to make this run a maximum of 30 FPS?

And how do I get to run at 60 FPS?

I:
mi juego corre aproximadamente a entre 40 y 220 FPS.

¿Cómo puedo hacer para que este corra como máximo a 30 FPS?

¿Y cómo hago para que corra a 60 FPS?

Ideas? :)

Comments

  • edited November 2011
    So, have you tried turning VSync on?

    My advise is to always use VSync, this way you won't get tearing.
    If it doesn't work, it might be your graphics driver that are overriding your application decision.

    As for your logic code, you can cap the main clock at 60FPS via config, or better, create your own clock at 30FPS and register your update function to it. However I wouldn't really worry about that unless you're doing a lot of expensive stuff and can't reach 60 FPS when letting things run on the core clock with VSync on.
  • edited November 2011
    Mi problema es que si pongo un clock andando a 30 FPS, el tiempo que me lleve lo que la instrucción del reloj ejecute se descontarán de ahí...
    My problem is that if I put a clock at 30 FPS, the time it takes to "update" will reduce the real FPS

    Pseudo-code solution 1 (simple, but not very effective)
    OnUpdate()
    {
        it = getActualTime(); // <--- I can't find a precise function
    
        engine.Update();      // <--- Process a lot of things
        ft = getActualTime(); 
        
        delay(1000/fps - (ft-it));
    }
    

    I thinking also in a complex second solution, but for now, i want to make solution 1... how can i do this using orx functions?

    Edit: i'm created a complex solution, but i also need the functions:
    getTime and Delay with hight presition, from orx
  • edited November 2011
    orx functions I could find are inaccurate. (Accuracy in seconds)

    EDIT: "control + z" to my comment. orx does have precision!
  • edited November 2011
    Simple solution:
    It works! but at 10 fps less than expected. https://forum.orx-project.org/uploads/legacy/fbfiles/files/FPS.zip

    Use:

    Creating fps controller in a class
    class MyClass
    {
    private:
        Fps::FpsAbstract* fpsControl;
    public:
        MyClass()
        {
            fpsControl = new Fps::FpsSimple();
            fpsControl->SetFps(60); // Default: 30
        }
    };
    

    Using fps controller
    // Main clock update function
    void orxFASTCALL StandAlone::Update( const orxCLOCK_INFO* clockInfo, void* context )
    {
        myClass.fpsControl->OnFrameInit();
    
        //
        // MY CODE HERE
        //     
    
        myClass.fpsControl->OnFrameEnd();
    }
    
    
    FPS 1.1K
  • edited November 2011
    Well, actually you don't need to call anything. That's why you get a parameter to your Update callback.

    The structure orxCLOCK_INFO contains, among other info, both the time elapsed since the initialization of the clock and the current delta time (ie. your current frame duration).

    You can even apply modifiers to a clock to either consolidate the DT (ie. slowing down time instead of stuttering), fix the DT (for a lockstep scheme, for example) or do time stretching.

    You should *always* use the info given by the clock calling you if you want time coherency.

    Any time spent in your update function is taken into account in the whole frame DT.

    If you want no tearing and work in VSync mode at 60FPS, the whole frame should take a maximum of 16.6ms. You can see very precisely where time is spent in both orx and your code by using the profiler version of orx and enabling the profiler's display:
    In config:
    
    [Render]
    ShowProfiler = true
    

    If you want to profile your own section of code, you have to use the orxPROFILER_PUSH_MARKER()/orxPROFILER_POP_MARKER() macros and define __orxPROFILER__.
  • edited November 2011
    The structure orxCLOCK_INFO contains, among other info, both the time elapsed since the initialization of the clock and the current delta time (ie. your current frame duration).
    You can even apply modifiers to a clock to either consolidate the DT (ie. slowing down time instead of stuttering), fix the DT (for a lockstep scheme, for example) or do time stretching.

    could you give me examples? :) I would be very helpful ;)
    If you want no tearing and work in VSync mode at 60FPS, the whole frame should take a maximum of 16.6ms.

    I'm using vsync... mmm what if i want a variable frame rate?
    If you want to profile your own section of code, you have to use the orxPROFILER_PUSH_MARKER()/orxPROFILER_POP_MARKER() macros and define __orxPROFILER__.

    I'll read about it, to use it ;)

    PD: Excuse the question, but do you know a community of "orx" in spanish? because I have difficulties to express what I think, because I don't speak english.

    New question added
    Can you adapt my FpsAbstract/FpsSimple classes to work with orxCLOCK_INFO ?

    Simple solution correction (only tested on Windows)
    FpsSimple.cpp
    #include "FpsSimple.h"
    
    #define ST2D(n) static_cast<orxDOUBLE>(n)
    
    #define FPSCORRECTION 12
    #ifdef FPSCORRECTION
    #define FPS GetFps() + FPSCORRECTION
    #else
    #define FPS GetFps()
    #endif
    
    namespace Engine
    {
    	namespace Fps
    	{
    		FpsSimple::FpsSimple()
    		{
    			t = ST2D(0);
    		}
    
    		void FpsSimple::OnFrameInit()
    		{
    			t = orxSystem_GetTime();
    		}
    
    		void FpsSimple::OnFrameEnd()
    		{
    			t = orxSystem_GetTime() - t;
    			orxDOUBLE delay = ST2D(1)/orx2D(FPS);
    			if(delay > t)
    				orxSystem_Delay(delay - t);
    		}
    	};
    };
    
  • edited November 2011
    luciano wrote:
    could you give me examples? :) I would be very helpful ;)

    Well the first example would be to look at the tutorial #2 named Clock. It shows how to do time stretching. As for fixed/consolidated DT I could write a simple example later, sure.

    With default setting, orx is using a consolidated core clock with a lower limit of 10 Hz. That means that the clock will always use the real time and update as fast as possible most of the time, except when too much work is done and the actual framerate drops under 10 FPS. In that case, it'll always send a DT of 0.1s which will result in actual slowdown of the game.

    Note also that the physics simulation is using an accumulated DT to prevent as much as possible divergences in the simulation.
    I'm using vsync... mmm what if i want a variable frame rate?

    Well, in that case I need to ask you why? :)
    For most games (with the exception of lockstepped network games) you shouldn't pay attention to the framerate in the first place.
    When your game is done, or close to be done, if you really can't make it at the native 60FPS (in most cases), you should consider capping it at 30FPS.
    Beside this, I'm not sure why you should care about the game's framerate. :)
    PD: Excuse the question, but do you know a community of "orx" in spanish? because I have difficulties to express what I think, because I don't speak english.

    I understand your pain and that's a good question. I'm sure there are some other native Spanish speakers that came over the time, not sure if there are any around still. You can start a thread on the general forum looking for some.
    If you're enough people, I could even open a new Spanish section as I did for Chinese.
    New question added
    Can you adapt my FpsAbstract/FpsSimple classes to work with orxCLOCK_INFO ?

    Well, that's the thing, you don't need your class at all. If you're only goal is to limit at 60FPS and VSync doesn't do anything, it means that your graphics driver is preventing you from doing it. In which case you need to either update your graphics driver and/or go into its setup application and make sure that VSync can be toggled by programs.
  • edited November 2011
    mmm very interesting ... I had never seen the clocks example.

    then what do I do?
    1) I have to make a new clock to 60hz / 30 hz
    or
    2) modify the core clock frequency? <- I need examples

    what is the core clock default frequency?
  • edited November 2011
    Check the file SettingsTemplate.ini, section clock. :)

    But I'm asking again, for what reason do you want to fix your framerate at 60 Hz?
  • edited November 2011
    I want to make the fixed framerate, I do not care if the framerate is 30, 60 or 100.
    I told you "60" because that's what I'm used to. And 60 is much nicer than 30:) and do not see much difference with higher fps.

    60 hz??? jaja.... misstake! 60hz/1000 :p
  • edited November 2011
    I need a text that describes the operation of orx philosophically, because I see is very different to everything! :P i'm going crazy! jaja
    [Clock1]
    Frequency = 100

    [Clock2]
    Frequency = 50

    i understand this, but, i don't know how change orx core clock frecuency :P and, what is the core clock default frequency?
  • edited November 2011
    Well then simply use the Clock section as seen in the SettingTemplate.ini config. :)

    But again, having a fixed of varying framerate shouldn't matter at all as long as you're using the DT stored in the orxCLOCK_INFO structure.
  • edited November 2011
    As seen in CreationTemplate.ini:
    [Clock1]
    Frequency = 50
    

    This defines a clock named Clock1 with an actual update frequency of 50Hz.

    As seen in SettingTemplate.ini:
    [Clock]
    MainClockFrequency = 60
    

    This limits your core clock at 60 Hz.
  • edited November 2011
    And the core clock frequency's default value is 0, which means update as fast as possible, which provides with the smoothest experience for the user, whether VSync is turned on or off.
  • edited November 2011
    But again, having a fixed of varying framerate shouldn't matter at all as long as you're using the DT stored in the orxCLOCK_INFO structure.

    i will cry! jaja i don't understand :S I think I need a break..

    I leave you with one last question: Do you have a game made with orx? so that I can use as reference and fully understand the use of orx... :side:

    (In addition to the tutorials.)
  • edited November 2011
    And the core clock frequency's default value is 0, which means update as fast as possible, which provides with the smoothest experience for the user, whether VSync is turned on or off.

    This is cool! but, when you are on the move an object, it accelerates uncontrollably? because it happens to me :(

    so, does this happens to
    In which case you need to either update your graphics driver and/or go into its setup application and make sure that VSync can be toggled by programs.
    ?

    :(

    if this happens because of my video card, in another PC, will run correctly?
  • edited November 2011
    :)

    It's explained in the clock tutorial.

    For example let's say you want to fill a gauge at the rate of X percent per second.

    In your code you'll write:
    void orxFASTCALL Update(const orxCLOCK_INFO *_pstClockInfo)
    {
      FillGauge(X * _pstClockInfo->fDT);
    }
    

    No matter what your framerate is, your gauge will still fill at X percent per second.

    And for games, there are a few out there, but only one with open source code I think. You want to check Mushroom Stew in this thread: https://forum.orx-project.org/discussion/2514#Comment_2832
  • edited November 2011
    When you move an object, you should use the orxObject_SetSpeed() function, especially if you want collisions on it as setting its position will make Box2D unhappy. :)

    If you don't care about lasting collisions, then you'd do something like:
    void orxFASTCALL Update(const orxCLOCK_INFO *_pstClockInfo)
    {
      orxVECTOR vPos;
    
      orxObject_GetPosition(MyObject, &vPos);
      vPos.fX += MySpeed * _pstClockInfo->fDT;
      orxObject_SetPos(MyObject, &vPos);
    }
    

    This will give it a constant speed of value 'MySpeed' on the X axis.
  • edited November 2011
    And of course it's the same for applying acceleration:
    orxVector_Mulf(&vTemp, &vAcceleration, _pstClockInfo->fDT);
    orxVector_Add(&vNewSpeed, &vCurrentSpeed, &vTemp);
    
  • edited November 2011
    It's explained in the clock tutorial.

    Tomorrow I will read it in detail, thanks :)
    void orxFASTCALL Update(const orxCLOCK_INFO *_pstClockInfo)
    {
      FillGauge(X * _pstClockInfo->fDT);
    }
    
    void orxFASTCALL Update(const orxCLOCK_INFO *_pstClockInfo) 
    { 
      orxVECTOR vPos; 
     
      orxObject_GetPosition(MyObject, &vPos); 
      vPos.fX += MySpeed * _pstClockInfo->fDT; 
      orxObject_SetPos(MyObject, &vPos); 
    }
    
    orxVector_Mulf(&vTemp, &vAcceleration, _pstClockInfo->fDT);
    orxVector_Add(&vNewSpeed, &vCurrentSpeed, &vTemp);
    

    Man! It's just what I need! :) :) !!

    Jeje i don't understand English well, but I understand c++ code... Why not talk to me in c + +? :p I'm kidding

    Tomorrow will prove these things, and tomorrow is the big day of trying to compile orx, we'll see what happens.

    Thank you very much for the answers!
  • edited November 2011
    My pleasure! :)

    I'm glad the explanation went through in the end. I'll try code sooner next time! ;)
  • edited December 2011
    void orxFASTCALL Update(const orxCLOCK_INFO *_pstClockInfo)
    {
    orxVECTOR vPos;

    orxObject_GetPosition(MyObject, &vPos);
    vPos.fX += MySpeed * _pstClockInfo->fDT;
    orxObject_SetPos(MyObject, &vPos);
    }

    I'm used this, and i don't like how it works :( my object moves "MySpeed" pixels per second, it's really paused :(... is this because i'm using core clock?
  • edited December 2011
    I'm sorry, I don't understand what you mean by it's really paused.

    That was an exemple to show how to use a DT, for object's speed you should just really call orxObject_SetSpeed(), not orxObject_SetPos().
  • edited February 2012
    Which unit is the speed (orxObject_SetSpeed(asdf))? pixels / frame, pixels / seconds, or what?

    paused = not constant
  • edited February 2012
    You can consider the unit to be be pixels/s.

    All the distances are expressed in "world units" which turns out to be identical to pixels provided that your viewport covers your full display area and you don't have a zoom on your camera. :)
  • edited February 2012
    Okas! :)
  • edited March 2012
    Do you think that implementing FPS control in an orx game running on an android device could save battery? :)
  • edited March 2012
    It might yes, it's a trade off between fluidity/smoothness and battery saving.
    I think the best way is to experience it and see if your battery really goes down too fast when running your game and using another game as reference. :)
  • edited March 2012
    yes, it's true... Mmmm maybe I do a "battery saving mode" in my games, only if the frame rate control makes a difference.
Sign In or Register to comment.