Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "windows.h"
- #include "math.h"
- #pragma comment(lib, "winmm.lib")
- #define clamp(x, a, b) (max(min(x, b), a))
- LARGE_INTEGER __qpcFreq;
- void initTimer()
- {
- QueryPerformanceFrequency(&__qpcFreq);
- timeBeginPeriod(1);
- }
- void doneTimer()
- {
- timeEndPeriod(1);
- }
- double getTime()
- {
- LARGE_INTEGER counter;
- QueryPerformanceCounter(&counter);
- return (double) counter.QuadPart / (double) __qpcFreq.QuadPart;
- }
- class World
- {
- public:
- void Update(double dt)
- {
- // двигаешь всё с dt
- }
- void Draw()
- {
- // ...
- }
- };
- const double MIN_DT = 0.001;
- const double MAX_DT = 0.1;
- const double REQUIRED_FPS = 100;
- const double REQUIRED_DT = 1.0 / REQUIRED_FPS;
- int _tmain(int argc, _TCHAR* argv[])
- {
- initTimer();
- World world = World();
- double prevTime = getTime();
- for(;;)
- {
- double curTime = getTime();
- double dt = clamp(curTime - prevTime, MIN_DT, MAX_DT);
- prevTime = curTime;
- world.Update(dt);
- world.Draw();
- double timePassed = clamp(getTime() - curTime, 0.0, 0.5);
- if (timePassed < REQUIRED_DT) Sleep((int)(1000.0 * (REQUIRED_DT - timePassed) + 0.5));
- }
- doneTimer();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement