Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411594 Posts in 69386 Topics- by 58444 Members - Latest Member: FightingFoxGame

May 07, 2024, 12:46:26 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)game loops
Pages: 1 [2]
Print
Author Topic: game loops  (Read 4185 times)
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #20 on: August 05, 2010, 08:44:57 AM »

nothing cross platform though...

It's not hard to wrap it up in a platform neutral fashion.  I'd be surprised if popular game libraries didn't provide one.
Logged



What would John Carmack do?
mechacrash
Level 1
*


It's almost like I'm actually being productive!


View Profile
« Reply #21 on: August 05, 2010, 11:48:35 AM »

well... ive combined some information from all the examples here and across the web... and i have an ugly yet functional fix (gonna start making it nicer and less cluttered tonight).

Code:
#include <windows.h>
#include <iostream>
using namespace std;
int main(){
    timeBeginPeriod(1);
    double acc = 0.0;
    unsigned tick = timeGetTime();
    unsigned lasttick = timeGetTime();
    unsigned currtick = 0;
    unsigned fps = 0;
    unsigned second = (unsigned)(timeGetTime() / 1000.0);
    double update = 1000.0 / 60.0; // <- change 60.0 to whatever you want :)
    while(1){
        currtick = timeGetTime();
        if(acc >= update){
            acc -= update;
            lasttick = currtick;
            fps++;
            cout << "-";
        }
        if(second != (unsigned)(currtick / 1000.0)){
            second = (unsigned)(currtick / 1000.0);
            cout << "\n\nfps : " << fps << "\n";
            cout << "acc : " << acc << "\n\n";
            fps = 0;
        }
        if(tick != currtick){
            acc+=currtick - tick;
            tick = currtick;
        }
        if((update - acc) > 5){
            Sleep(5);
            tick = currtick;
        }
    }
    timeEndPeriod(0);
    cin.get();
    return 0;
}


it still requires winmm (-lwinmm for G++ linker command) for the high resolution timers on windows... also its not cross platform...

ANYWAY... it works... regardless of fps... so im happy XD

thanks everyone! and feel free to use my code if you actually think its decent Tongue
Logged

Linus
Level 0
***


View Profile
« Reply #22 on: August 05, 2010, 04:19:17 PM »

While the Boost docs seem down at the moment, meaning I can't check, I'm pretty sure that Boost::Thread or Boost::Timer features cross-platform high precision sleep functions using the different OS APIs.
Logged
Krux
Level 2
**



View Profile
« Reply #23 on: August 07, 2010, 10:10:32 AM »

you can make it event based. Start a thread that throws 60 draw events in every second. Draw your scene only when a draw event has came in.
Logged
Pages: 1 [2]
Print
Jump to:  

Theme orange-lt created by panic