Program Listing for File tick.cpp

Return to documentation for file (blam\tick.cpp)

// Tick Management                            //
// Controls and manages engine tick rate     //
// ---------------------------------------- //
// Copyright (c) Elaztek Studios 2013-2019 //

#include "core.h"

#include <iostream>
#include <string>

//TODO: Add this to matg
uint64_t max_tickrate = 40;
uint64_t ticks_this_second = 0;
LARGE_INTEGER last_second_time;

void doTick();

uint64_t Blam::Tick::getCurrentTickRate()
{
    //TODO: calculate average TPS
    //we're gonna return ticks this second in the meantime

    return ticks_this_second;
}

void Blam::Tick::updateLastTimestamp()
{
    QueryPerformanceCounter(&last_second_time);
}

//check if a second has passed in order to reset tick counter
bool hasSecondPassed()
{
    LARGE_INTEGER elapsed_microseconds; //elapsed time
    LARGE_INTEGER current_time; //the new time
    LARGE_INTEGER frequency;

    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&current_time);

    elapsed_microseconds.QuadPart = current_time.QuadPart - last_second_time.QuadPart;


    elapsed_microseconds.QuadPart *= 1000000;
    elapsed_microseconds.QuadPart /= frequency.QuadPart;

    if (elapsed_microseconds.QuadPart >= 1000000)
    {
        return true;
    }
    else
    {
        return false;
    }
}


void Blam::Tick::tick()
{
    //if a second has passed, reset this second's tick counter and last second timestamp
    if (hasSecondPassed())
    {
        ticks_this_second = 0;
        Blam::Tick::updateLastTimestamp();
    }

    if (max_tickrate > ticks_this_second)
    {
        doTick();
        ticks_this_second += 1;
    }
}

//put things you want to run based on game tick here
void doTick()
{
    std::cout << "doTick() was called (" + std::to_string(ticks_this_second) + ")" << std::endl;
}