0

I'm new to C++ and trying to make a simple update function.

I have built a "clock" which tracks time between frames as well as time since T0.

I also have a loops which every so many "clock" frames calls an update function.

void Engine::start()
{
    float count = 0;
    Clock clock;
    clock.startClock();
    while (true)
    {
        clock.Time(.001F);
        count += clock.deltaTime();
        if (count >= 1000) {
            update(clock.deltaTime());
            count = 0;
        }
    }
}

However this feels inelegant to me.

The clock code:

class Clock
{
private:    
public:
    float startTime=0;
    float nextTime=0;
    float currentTime=0;

    void startClock()
    {
        float startTime = 0;
        float currentTime = startTime;
    }
    float deltaTime()
    {
        return nextTime - currentTime;
    }
    void Time(float incrementTime)
    {
        currentTime = 0;
        nextTime = incrementTime;
    }
};

It works as written; I have a "counter" class which prints to the console every time the update function is called; however I'd like advice on how to improve this, to make it more elegant/efficient.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Dan
  • 45
  • 1
  • 5
  • 1
    What inefficiency do you see? What is inelegant about it? – Scott Hunter Mar 13 '20 at 15:17
  • That code looks OK to me. I have no idea what you mean.... – Jabberwocky Mar 13 '20 at 15:19
  • 1
    This should be on [codereview](https://codereview.stackexchange.com/) - that said, you are busy-waiting which is an expensive way of waiting for time to pass (but relevant in some cases). You could look at the standard function `std::this_thread::sleep_until()`. – Ted Lyngmo Mar 13 '20 at 15:21
  • 1
    Ill look into that, thanks Ted – Dan Mar 13 '20 at 15:39
  • Related: https://stackoverflow.com/a/50032992/4561887. Just make sure you also add a short sleep somewhere once per polling loop to give the processor back to the OS scheduler and other processes and threads periodically, or else it will unnecessarily suck up 100% of one cpu forever. – Gabriel Staples Mar 13 '20 at 15:43
  • If you want to be precise, where it says `count = 0;` it should be `count -= 1000;`, otherwise you will be losing part of the `count` value which will result in a systematic accumulated delay over time (although considering you are incrementing in steps of 0.001 it will be quite negligible, to be honest). – jdehesa Mar 13 '20 at 15:43
  • I think [this answer](https://stackoverflow.com/a/43373364) may be something like what you want. – jdehesa Mar 13 '20 at 15:48
  • @GabrielStaples That short sleep may not be needed. The thread could simply `yield()` which would do the same thing (cheaper) - and be a no-op in an environment where it's not needed at all. – Ted Lyngmo Mar 13 '20 at 16:11
  • 1
    True true. I know in an RTOS, reading from or writing to message-passing queues also performs an OS/scheduler-aware block, which yields until the queue is no longer empty/full, respectively. – Gabriel Staples Mar 13 '20 at 16:24

0 Answers0