Simple Question:
What is the best way to set the framerate. Is it with GetTickCount() or QueryPerformanceCounter()...? Please can you give me a coded example of how to use whichever one within the 'Game Loop' ?

Advanced Question:
Is it possible to do something in the 'Waiting' part of the framerate calculation. Can you give me some examples of how this time could be utilised (theoretically) ?

Thanks...!
Elixir

Here is my attempt: (I answered my own post). Uncomment the file ops if you want to see printed info... also you can change the while (iNumSecs < 10) loop to something higher as it is what controls how long the Gameloop runs. Also the sleep function simulates the weight of the render loop. what are your thoughts...?

#include <iostream>
#include <iomanip>
#include <ctime>
#include <windows.h>

using namespace std;

int main()
{
    DWORD ticks = GetTickCount() / 1000;

    cout << ticks << "\n";

    ticks = 0;
    int iFrame = 0;
    int iFrameRate = 10;
    long iWaited = 0;
    long iWaitedOLD = 0;

   FILE * pFile;
   char buffer [100];

   pFile = fopen ("myfile.txt" , "w");
   if (pFile == NULL) perror ("Error opening file");

    strcpy_s(buffer, "This is a line of text\n");
    fputs(buffer, pFile);
    strcpy_s(buffer, "Second line of text\n");
    fputs(buffer, pFile);
    strcpy_s(buffer, "Third line of text\n");
    fputs(buffer, pFile);

    char s[] = "         ";
    int iNumSecs = 0;

    while (iNumSecs < 10)
    {
        if (ticks < (GetTickCount() / 1000)) 
        {
            cout << "iWaited Delta = " << (iWaited - iWaitedOLD) << endl;
            cout << "iWaited = " << iWaited << endl;
            iWaitedOLD = iWaited;
            iWaited = 0;

            //strcpy_s(buffer, "iWaited = ");
            //itoa(iWaited, s, 10);
            //strcat_s(buffer, s);
            //strcat_s(buffer, "\n");
            //fputs(buffer, pFile);

            cout << "ticks = " << ticks << "\n";
            ticks = GetTickCount() / 1000;
            iFrame = 0;
            iNumSecs += 1;

            //strcpy_s(buffer, "\n");
            //fputs(buffer, pFile);
        }

        if (iFrame < iFrameRate)
        {
            cout << "Render(" << iFrame << ")" << endl;

            Sleep(50);

            //strcpy_s(buffer, "Render(");
            //itoa(iFrame, s, 10);
            //strcat_s(buffer, s);
            //strcat_s(buffer, ")\n");
            //fputs(buffer, pFile);

            iFrame += 1;
        }
        else
            iWaited += 1;
    }

    fclose (pFile);
    cin.get();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.