diff options
author | Erik Faye-Lund <erik.faye-lund@collabora.com> | 2020-04-16 13:20:48 +0200 |
---|---|---|
committer | Erik Faye-Lund <erik.faye-lund@collabora.com> | 2020-04-17 10:27:58 +0000 |
commit | d6e7b4fc48c4e8eeabf3e341413d587b09a75b2f (patch) | |
tree | 8ba3d39bb7334d6162e72be968c4f91409863513 | |
parent | 8209ea79ec6735ae100ca6dd1d941f0ab662f950 (diff) |
wglgears: fix rotation speed to 70 degrees per second
This copies the logic from glxgears, omitting the rotating-state,
because we have no such option (yet).
In order to do this, we also need to swap out our timer. In order to
keep things nice and easy, we can use timeGetTime(), but set the minumum
period before the event-loop.
And for the timing-calculation, this is a lot better with proper
resolution anyway.
Reviewed-by: Brian Paul <brianp@vmware.com>
-rw-r--r-- | src/wgl/wglgears.c | 67 |
1 files changed, 43 insertions, 24 deletions
diff --git a/src/wgl/wglgears.c b/src/wgl/wglgears.c index fa9efc8d..cc4f876b 100644 --- a/src/wgl/wglgears.c +++ b/src/wgl/wglgears.c @@ -41,12 +41,6 @@ #include <ctype.h> #include <math.h> -/* XXX this probably isn't very portable */ -#include <time.h> -#ifndef _MSC_VER -#include <unistd.h> -#endif - #ifndef M_PI #define M_PI 3.14159265 #endif /* !M_PI */ @@ -88,10 +82,10 @@ void usage(void) /* return current time (in seconds) */ -static int +static double current_time(void) { - return (int)time(NULL); + return timeGetTime() / 1000.0; } @@ -520,13 +514,49 @@ make_window(const char *name, int x, int y, int width, int height) SetFocus(hWnd); } +static void +draw_frame() +{ + static int frames = 0; + static double tRot0 = -1.0, tRate0 = -1.0; + double dt, t = current_time(); + + if (tRot0 < 0.0) + tRot0 = t; + dt = t - tRot0; + tRot0 = t; + + /* advance rotation for next frame */ + angle += 70.0 * dt; /* 70 degrees per second */ + if (angle > 3600.0) + angle -= 3600.0; + + draw(); + SwapBuffers(hDC); + + frames++; + + if (tRate0 < 0.0) + tRate0 = t; + if (t - tRate0 >= 5.0) { + GLfloat seconds = t - tRate0; + GLfloat fps = frames / seconds; + printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds, + fps); + fflush(stdout); + tRate0 = t; + frames = 0; + } +} static void event_loop(void) { MSG msg; - int t, t0 = current_time(); - int frames = 0; + + TIMECAPS tc; + timeGetDevCaps(&tc, sizeof(tc)); + timeBeginPeriod(tc.wPeriodMin); while(1) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { @@ -535,21 +565,10 @@ event_loop(void) DispatchMessage(&msg); } - angle += 2.0; - draw(); - SwapBuffers(hDC); - - /* calc framerate */ - t = current_time(); - frames++; - if (t - t0 >= 5.0) { - GLfloat s = t - t0; - GLfloat fps = frames / s; - printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, s, fps); - t0 = t; - frames = 0; - } + draw_frame(); } + + timeEndPeriod(tc.wPeriodMin); } |