I am making a port of Undetale to the CE using the CE-C compiler, and all is going "okay" I guess, minus the fact that I need to do a bit of optimizing sprite-wise because of how robust the art is. However, the original game runs at 30 frames per second, and I want to make it as accurate as possible, but the framerate fluctuates a bit, especially when things are going on onscreen, and I can't seem to find any relevant information on the toolchain docs. Any help is appreciated! Also, I don't know if I need to mention it but I feel it may be important info, but I'm using the gfx_SwapDraw() method because of how much stuff is moving around lmao. Another thing, if you have any advice from when you started out, that would be pretty cool too. Thanks!
Use clock() to limit the framerate.
The CE toolchain generally goes for a mechanism-not-policy approach to stuff, so there aren't any functions specifically for framerate limiting. With that being said, it's quite a common thing to want to do when using the toolchain, so I went ahead and wrote an example covering framerate limiting.

To summarize, you'll want to get the time that each loop iteration starts using clock(), determine how clock ticks you want each frame to take by dividing CLOCKS_PER_SECOND by your target frame rate, and then calling clock() again until the difference between the newest result and the one from the start of the frame is at least that number of clock ticks:

Code:
/* The framerate to limit to */
#define TARGET_FRAMERATE 20

/* The (approximate) amount of time each frame should take, in clock ticks */
#define TARGET_FRAME_TIME (CLOCKS_PER_SEC / TARGET_FRAMERATE)

    while (!os_GetCSC()) {
        /* Get the time that the frame starts. */
        clock_t frame_start = clock();

        /* Do all of your game logic and graphics here */

        /* Get how much time has elapsed since the start of the frame. */
        clock_t frame_time = clock() - frame_start;

        if (frame_time > TARGET_FRAME_TIME) {
            dbg_printf("Warning! Frame took too long (%li clock ticks). "
                       "The maximum frame time for %u FPS is %u clock ticks.\n",
                       frame_time, TARGET_FRAMERATE, TARGET_FRAME_TIME);
        }

        /* Wait for at least TARGET_FRAME_TIME to have passed since
           the start of the frame. */
        do {
            frame_time = clock() - frame_start;
        } while (frame_time < TARGET_FRAME_TIME);

        /* Note that there should not be any code below the above loop,
           since it would not be counted towards the frame time. */
    }


Note that this isn't exact; there are rounding errors resulting from the division for TARGET_FRAME_TIME, and the call to os_GetCSC() is not included in the frame time.
Thank you both for helping out, I appreciate it! I'm sorry I missed your example you linked, Google has yet again failed me.
slimefolf wrote:
I'm sorry I missed your example you linked, Google has yet again failed me.


Nah, don't worry about it - I wrote the example after you made your post, so it wouldn't have been possible for you to have found it to begin with.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement