[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
On Jan 31, 2008 5:23 AM, Peter Wang <
novalazy@xxxxxxxxxx> wrote:
On 2008-01-30, Ryan Dickie <
goalieca@xxxxxxxxxx> wrote:
>
> I changed counter to double (only used internally to determine how long to
> rest) but left count as long. There's no reason to touch it.
>
> Just to keep things organized i've attached two patches. The first
> time.patch will change al_rest() and al_current_time() to doubles. I've also
> moved the events.h timestamp change into there. Also cleaned up the ^M's
> and patches the latest wtime.c. The second patch 'periodic' will change the
> periodic timer to double (windows version excluded).
>
> -- Ryan Dickie
So the switch to doubles has been decided? My first reaction was no, but the
1000/60 example is convincing.
Peter
> Index: demo/src/UFO.cpp
> ===================================================================
> --- demo/src/UFO.cpp (revision 9954)
> +++ demo/src/UFO.cpp (working copy)
> @@ -19,10 +19,10 @@
> p->die();
> my_play_sample(RES_COLLISION);
> return false;
> - }
> + }
>
> - if (al_current_time() > (unsigned long)nextShot) {
> - nextShot = al_current_time() + SHOT_SPEED;
> + if ( (al_current_time() * 1000) > nextShot) {
Delete the extra space.
> + nextShot = (int)(al_current_time() * 1000) + SHOT_SPEED;
> ResourceManager& rm = ResourceManager::getInstance();
> Player *p = (Player *)rm.getData(RES_PLAYER);
> float px = p->getX();
> @@ -71,7 +71,7 @@
> points = 500;
> ufo = true;
>
> - nextShot = al_current_time() + SHOT_SPEED;
> + nextShot = (int)(al_current_time() * 1000.0) + SHOT_SPEED;
>
> ResourceManager& rm = ResourceManager::getInstance();
> bitmaps[0] = (ALLEGRO_BITMAP *)rm.getData(RES_UFO0);
> Index: demo/src/Player.cpp
> ===================================================================
> --- demo/src/Player.cpp (revision 9954)
> +++ demo/src/Player.cpp (working copy)
> @@ -63,9 +63,10 @@
> default:
> shotRate = INT_MAX;
> break;
> - }
> - if (((unsigned long)(lastShot+shotRate) < al_current_time()) && input->b1()) {
> - lastShot = al_current_time();
> + }
> +
> + if ( (lastShot+shotRate) < (al_current_time() * 1000) && input->b1()) {
ditto.
> Index: demo/src/logic.cpp
> ===================================================================
> --- demo/src/logic.cpp (revision 9954)
> +++ demo/src/logic.cpp (working copy)
> @@ -9,12 +9,12 @@
> const int MAX_UFO_TIME = 50000;
>
> bool logic(int step)
> -{
> - if (lastUFO < 0) lastUFO = al_current_time();
> +{
> + if (lastUFO < 0) lastUFO = (long)(al_current_time() * 1000);
>
> - if (canUFO && (al_current_time() > (unsigned long)(lastUFO+MIN_UFO_TIME))) {
> + if (canUFO && ( al_current_time() * 1000 > (lastUFO+MIN_UFO_TIME))) {
ditto.
> Index: src/unix/utime.c
> ===================================================================
> --- src/unix/utime.c (revision 9954)
> +++ src/unix/utime.c (working copy)
> @@ -17,12 +17,11 @@
>
>
> #include <sys/time.h>
> -#include <sys/select.h>
> +#include <math.h>
>
> #include "allegro5/altime.h"
>
>
> -
> /* Marks the time Allegro was initialised, for al_current_time(). */
> static struct timeval initial_time;
>
> @@ -39,17 +38,16 @@
>
>
> /* al_current_time:
> - * Return the current time, in microseconds, since some arbitrary
> + * Return the current time, with microsecond resolution, since some arbitrary
> * point in time.
> */
Delete "with microsecond resolution".
> -unsigned long al_current_time(void)
> +double al_current_time(void)
> {
> struct timeval now;
> -
> - gettimeofday(&now, NULL);
> -
> - return ((now.tv_sec - initial_time.tv_sec) * 1000 +
> - (now.tv_usec - initial_time.tv_usec) / 1000);
> + gettimeofday(&now, 0); //null = timezone afaik
Put that gettimeofday line back how it used to be.
> Index: include/allegro5/altime.h
> ===================================================================
> --- include/allegro5/altime.h (revision 9954)
> +++ include/allegro5/altime.h (working copy)
> @@ -11,17 +11,18 @@
>
>
> /* Function: al_current_time
> - * Return the number of milliseconds since the Allegro library was
> + * Return the number of seconds since the Allegro library was
> * initialised. The return value is undefined if Allegro is uninitialised.
> + * microsecond resolution.
> */
> -AL_FUNC(unsigned long, al_current_time, (void));
> +AL_FUNC(double, al_current_time, (void));
What do you think about not using the Allegro initialisation time as the base?
I only did that to delay the overflow of a 32-bit integer.
> Index: include/allegro5/events.h
> ===================================================================
> --- include/allegro5/events.h (revision 9954)
> +++ include/allegro5/events.h (working copy)
> @@ -142,7 +142,7 @@
> #define _AL_EVENT_HEADER(srctype) \
> ALLEGRO_EVENT_TYPE type; \
> srctype *source; \
> - unsigned long timestamp; \
> + double timestamp; \
> signed int _refcount; /* internal */ \
> union ALLEGRO_EVENT *_next; /* internal */ \
> union ALLEGRO_EVENT *_next_free /* internal */
Fix the spaces.
> --- src/unix/utimernu.c (revision 9954)
> +++ src/unix/utimernu.c (working copy)
> @@ -25,24 +25,18 @@
> #include "allegro5/internal/aintern_events.h"
>
>
> -
> -/* readability typedefs */
> -typedef long msecs_t;
> -typedef long usecs_t;
> -
> -
> /* forward declarations */
> -static usecs_t timer_thread_handle_tick(usecs_t interval);
> +static double timer_thread_handle_tick(double interval);
> static void timer_handle_tick(ALLEGRO_TIMER *this);
>
> -
> + double new_delay = 0.032768;
Delete that.
> struct ALLEGRO_TIMER
> {
> ALLEGRO_EVENT_SOURCE es;
> bool started;
> - usecs_t speed_usecs;
> + double speed_secs;
> long count;
> - long counter; /* counts down to zero=blastoff */
> + double counter; /* counts down to zero=blastoff */
I've cleaned up the patches as requested.
I have no real opinion as to whether al_current_time() should return seconds since 1970 or seconds since allegro started. The current method returns the same value on all platforms which may or may not be useful. I suppose it might be nice to remove a chunk of code. I do think that al_current_time() is a long method name and that al_time() "read: allegro time" might be easier on the fingers.
-- Ryan Dickie
Index: demo/src/UFO.cpp
===================================================================
--- demo/src/UFO.cpp (revision 9954)
+++ demo/src/UFO.cpp (working copy)
@@ -19,10 +19,10 @@
p->die();
my_play_sample(RES_COLLISION);
return false;
- }
+ }
- if (al_current_time() > (unsigned long)nextShot) {
- nextShot = al_current_time() + SHOT_SPEED;
+ if ((al_current_time() * 1000) > nextShot) {
+ nextShot = (int)(al_current_time() * 1000) + SHOT_SPEED;
ResourceManager& rm = ResourceManager::getInstance();
Player *p = (Player *)rm.getData(RES_PLAYER);
float px = p->getX();
@@ -71,7 +71,7 @@
points = 500;
ufo = true;
- nextShot = al_current_time() + SHOT_SPEED;
+ nextShot = (int)(al_current_time() * 1000.0) + SHOT_SPEED;
ResourceManager& rm = ResourceManager::getInstance();
bitmaps[0] = (ALLEGRO_BITMAP *)rm.getData(RES_UFO0);
Index: demo/src/Player.cpp
===================================================================
--- demo/src/Player.cpp (revision 9954)
+++ demo/src/Player.cpp (working copy)
@@ -63,9 +63,10 @@
default:
shotRate = INT_MAX;
break;
- }
- if (((unsigned long)(lastShot+shotRate) < al_current_time()) && input->b1()) {
- lastShot = al_current_time();
+ }
+
+ if ((lastShot+shotRate) < (al_current_time() * 1000) && input->b1()) {
+ lastShot = (int) (al_current_time() * 1000);
float realAngle = -angle;
float bx = x + radius * cos(realAngle);
float by = y + radius * sin(realAngle);
@@ -88,7 +89,7 @@
}
if (input->cheat()) {
- al_rest(250);
+ al_rest(0.250);
std::list<Entity *>::iterator it;
for (it = entities.begin(); it != entities.end(); it++) {
Entity *e = *it;
Index: demo/src/a5teroids.cpp
===================================================================
--- demo/src/a5teroids.cpp (revision 9954)
+++ demo/src/a5teroids.cpp (working copy)
@@ -42,7 +42,7 @@
for (;;) {
player->load();
- al_rest(500);
+ al_rest(0.500);
play_sample(title_music, 255, 128, 1000, 1);
int choice = do_menu();
@@ -53,7 +53,7 @@
}
stop_sample(title_music);
- al_rest(250);
+ al_rest(0.250);
lastUFO = -1;
canUFO = true;
@@ -61,7 +61,7 @@
play_sample(game_music, 255, 128, 1000, 1);
int step = 0;
- long start = al_current_time();
+ long start = (long) (al_current_time() * 1000);
for (;;) {
if (entities.size() <= 0) {
@@ -74,8 +74,8 @@
if (!logic(step))
break;
render(step);
- al_rest(10);
- long end = al_current_time();
+ al_rest(0.010);
+ long end = (long) (al_current_time() * 1000);
step = end - start;
start = end;
}
Index: demo/src/GUI.cpp
===================================================================
--- demo/src/GUI.cpp (revision 9954)
+++ demo/src/GUI.cpp (working copy)
@@ -16,12 +16,12 @@
if (ud < 0 && selected) {
selected--;
my_play_sample(RES_FIRELARGE);
- al_rest(200);
+ al_rest(0.200);
}
else if (ud > 0 && selected < (widgets.size()-1)) {
selected++;
my_play_sample(RES_FIRELARGE);
- al_rest(200);
+ al_rest(0.200);
}
if (input->b1()) {
if (!widgets[selected]->activate())
@@ -42,7 +42,7 @@
widgets[i]->render(i == selected);
}
al_flip_display();
- al_rest(10);
+ al_rest(0.010);
}
}
Index: demo/src/logic.cpp
===================================================================
--- demo/src/logic.cpp (revision 9954)
+++ demo/src/logic.cpp (working copy)
@@ -9,12 +9,12 @@
const int MAX_UFO_TIME = 50000;
bool logic(int step)
-{
- if (lastUFO < 0) lastUFO = al_current_time();
+{
+ if (lastUFO < 0) lastUFO = (long)(al_current_time() * 1000);
- if (canUFO && (al_current_time() > (unsigned long)(lastUFO+MIN_UFO_TIME))) {
+ if (canUFO && (al_current_time() * 1000 > (lastUFO+MIN_UFO_TIME))) {
int r = rand() % (MAX_UFO_TIME-MIN_UFO_TIME);
- if (r <= step || (al_current_time() > (unsigned long)(lastUFO+MAX_UFO_TIME))) {
+ if (r <= step || (al_current_time() * 1000 > (lastUFO+MAX_UFO_TIME))) {
canUFO = false;
UFO *ufo;
float x, y, dx, dy;
@@ -58,8 +58,8 @@
for (it = entities.begin(); it != entities.end(); it++) {
Entity *e = *it;
if (!e->logic(step)) {
- if (e->isUFO()) {
- lastUFO = al_current_time();
+ if (e->isUFO()) {
+ lastUFO = (long)(al_current_time() * 1000);
canUFO = true;
}
delete e;
Index: src/win/wtime.c
===================================================================
--- src/win/wtime.c (revision 9959)
+++ src/win/wtime.c (working copy)
@@ -36,10 +36,10 @@
-unsigned long al_current_time(void)
+double al_current_time(void)
{
/* XXX: Maybe use QueryPerformanceCounter? */
- return (timeGetTime() - _al_win_initial_time);
+ return (double) (timeGetTime() - _al_win_initial_time) / 1000;
}
@@ -47,9 +47,9 @@
/* al_rest:
* Rests the specified amount of milliseconds.
*/
-void al_rest(long msecs)
+void al_rest(double seconds)
{
- ASSERT(msecs >= 0);
+ ASSERT(seconds >= 0);
- Sleep(msecs);
+ Sleep((DWORD)(seconds * 1000.0));
}
Index: src/unix/utime.c
===================================================================
--- src/unix/utime.c (revision 9954)
+++ src/unix/utime.c (working copy)
@@ -17,12 +17,11 @@
#include <sys/time.h>
-#include <sys/select.h>
+#include <math.h>
#include "allegro5/altime.h"
-
/* Marks the time Allegro was initialised, for al_current_time(). */
static struct timeval initial_time;
@@ -39,17 +38,16 @@
/* al_current_time:
- * Return the current time, in microseconds, since some arbitrary
+ * Return the current time since some arbitrary
* point in time.
*/
-unsigned long al_current_time(void)
+double al_current_time(void)
{
struct timeval now;
-
- gettimeofday(&now, NULL);
-
- return ((now.tv_sec - initial_time.tv_sec) * 1000 +
- (now.tv_usec - initial_time.tv_usec) / 1000);
+ gettimeofday(&now, NULL);
+ double time = (double) (now.tv_sec - initial_time.tv_sec)
+ + (double) (now.tv_usec - initial_time.tv_usec) * 1.0e-6;
+ return time;
}
@@ -57,14 +55,11 @@
/* al_rest:
* Make the caller rest for some time.
*/
-void al_rest(long msecs)
+void al_rest(double seconds)
{
-#ifdef ALLEGRO_MACOSX
- usleep(msecs * 1000);
-#else
- struct timeval timeout;
- timeout.tv_sec = 0;
- timeout.tv_usec = msecs * 1000;
- select(0, NULL, NULL, NULL, &timeout);
-#endif
+ struct timespec timeout;
+ double fsecs = floor(seconds);
+ timeout.tv_sec = (time_t) fsecs;
+ timeout.tv_nsec = (suseconds_t) ((seconds - fsecs) * 1e9);
+ nanosleep(&timeout, 0);
}
Index: include/allegro5/altime.h
===================================================================
--- include/allegro5/altime.h (revision 9954)
+++ include/allegro5/altime.h (working copy)
@@ -11,17 +11,18 @@
/* Function: al_current_time
- * Return the number of milliseconds since the Allegro library was
+ * Return the number of seconds since the Allegro library was
* initialised. The return value is undefined if Allegro is uninitialised.
+ * microsecond resolution.
*/
-AL_FUNC(unsigned long, al_current_time, (void));
+AL_FUNC(double, al_current_time, (void));
/* Function: al_rest
- * Waits for the specified number of milliseconds.
+ * Waits for the specified number seconds.
*/
-AL_FUNC(void, al_rest, (long msecs));
+AL_FUNC(void, al_rest, (double seconds));
Index: examples/exnew_mouse_events.c
===================================================================
--- examples/exnew_mouse_events.c (revision 9954)
+++ examples/exnew_mouse_events.c (working copy)
@@ -50,7 +50,7 @@
al_clear(al_map_rgb(0, 0, 0));
al_draw_bitmap(cursor, mx, my, 0);
al_flip_display();
- al_rest(5);
+ al_rest(0.005);
}
while(1);
done:
Index: examples/exnew_bitmap_target.c
===================================================================
--- examples/exnew_bitmap_target.c (revision 9954)
+++ examples/exnew_bitmap_target.c (working copy)
@@ -38,7 +38,7 @@
static void draw(void)
{
double dt = 0;
- double t = al_current_time() / 1000.0;
+ double t = al_current_time();
if (last_time > 0) {
dt = t - last_time;
}
@@ -84,7 +84,7 @@
dy = 63;
int frames = 0;
- double start = al_current_time() / 1000.0;
+ double start = al_current_time();
do {
/* Check for ESC key or close button event and quit in either case. */
if (!al_event_queue_is_empty(queue)) {
@@ -105,7 +105,7 @@
}
}
draw();
- print(0, 0, "FPS: %.1f", frames / (al_current_time() / 1000.0 - start));
+ print(0, 0, "FPS: %.1f", frames / (al_current_time() - start));
if (al_get_new_bitmap_flags() & ALLEGRO_FORCE_LOCKING) {
print(0, a5font_text_height(myfont), "using forced bitmap locking");
}
Index: examples/exnew_lockscreen.c
===================================================================
--- examples/exnew_lockscreen.c (revision 9954)
+++ examples/exnew_lockscreen.c (working copy)
@@ -15,7 +15,7 @@
((char *)locked.data)[i] = (i / locked.pitch) * 255 / 99;
al_unlock_bitmap(bitmap);
al_flip_display();
- al_rest(5000);
+ al_rest(5.0);
return 0;
}
END_OF_MAIN()
Index: examples/exnew_fs_resize.c
===================================================================
--- examples/exnew_fs_resize.c (revision 9954)
+++ examples/exnew_fs_resize.c (working copy)
@@ -28,10 +28,10 @@
}
redraw(picture);
- al_rest(2500);
+ al_rest(2.5);
al_resize_display(800, 600);
redraw(picture);
- al_rest(2500);
+ al_rest(2.5);
return 0;
}
Index: examples/exnew_bitmap.c
===================================================================
--- examples/exnew_bitmap.c (revision 9954)
+++ examples/exnew_bitmap.c (working copy)
@@ -15,7 +15,7 @@
}
al_draw_bitmap(bitmap, 0, 0, 0);
al_flip_display();
- al_rest(1000);
+ al_rest(5.0);
return 0;
}
END_OF_MAIN()
Index: examples/exnew_opengl.c
===================================================================
--- examples/exnew_opengl.c (revision 9954)
+++ examples/exnew_opengl.c (working copy)
@@ -12,7 +12,7 @@
void draw_opengl(void)
{
- double ms = al_current_time() / 1000.0;
+ double ms = al_current_time();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Index: examples/exnew_icon.c
===================================================================
--- examples/exnew_icon.c (revision 9954)
+++ examples/exnew_icon.c (working copy)
@@ -32,7 +32,7 @@
{
al_set_display_icon(i & 1 ? icon2 : icon1);
al_flip_display();
- al_rest(500);
+ al_rest(1.0);
}
return 0;
}
Index: examples/exnew_mouse.c
===================================================================
--- examples/exnew_mouse.c (revision 9954)
+++ examples/exnew_mouse.c (working copy)
@@ -28,7 +28,7 @@
al_clear(al_map_rgb(0, 0, 0));
al_draw_bitmap(cursor, msestate.x, msestate.y, 0);
al_flip_display();
- al_rest(5);
+ al_rest(0.005);
} while (!al_key_down(&kbdstate, ALLEGRO_KEY_ESCAPE)
&& !msestate.buttons);
Index: examples/exnew_resize.c
===================================================================
--- examples/exnew_resize.c (revision 9954)
+++ examples/exnew_resize.c (working copy)
@@ -23,7 +23,7 @@
ALLEGRO_EVENT event;
ALLEGRO_KEYBOARD *keyboard;
- long last_resize;
+ double last_resize;
int rs = 100;
/* Initialize Allegro and create an event queue. */
@@ -59,7 +59,7 @@
break;
}
}
- if (al_current_time() - last_resize > 100)
+ if (al_current_time() - last_resize > 0.1)
{
int s;
last_resize = al_current_time();
Index: examples/exnew_blend.c
===================================================================
--- examples/exnew_blend.c (revision 9954)
+++ examples/exnew_blend.c (working copy)
@@ -174,7 +174,7 @@
static void tick(void)
{
/* Count frames during the last second. */
- double t = al_current_time() / 1000.0;
+ double t = al_current_time();
if (t >= ex.last_second + 1) {
ex.last_second += 1;
ex.fps = ex.frames_accum;
Index: examples/exnewapi.c
===================================================================
--- examples/exnewapi.c (revision 9954)
+++ examples/exnewapi.c (working copy)
@@ -17,7 +17,6 @@
ALLEGRO_EVENT event;
ALLEGRO_EVENT_QUEUE *events;
int quit = 0;
- int ticks = 0, last_rendered = 0, start_ticks;
int fps_accumulator = 0, fps_time = 0;
double fps = 0;
int FPS = 500;
@@ -93,7 +92,6 @@
al_install_keyboard();
al_register_event_source(events, (ALLEGRO_EVENT_SOURCE *)al_get_keyboard());
- start_ticks = al_current_time();
for (i = 0; i < 3; i++) {
al_set_current_display(display[i]);
@@ -110,8 +108,8 @@
al_draw_line(0, 0, 100, 0, al_map_rgba_f(1, 1, 0, 0.5));
al_draw_line(0, 0, 0, 100, al_map_rgba_f(1, 1, 0, 0.5));
- long start = al_current_time();
- long last_move = al_current_time();
+ double start = al_current_time();
+ double last_move = al_current_time();
int frames = 0;
while (!quit) {
@@ -148,7 +146,7 @@
}
while (last_move < al_current_time()) {
- last_move++;
+ last_move += 0.001;
x += dx;
if (x == 0)
dx = 1;
@@ -186,7 +184,7 @@
}
}
- printf("fps=%f\n", (float)(frames * 1000) / (float)(al_current_time()-start));
+ printf("fps=%f\n", (double)frames / (al_current_time()-start));
al_destroy_bitmap(picture);
al_destroy_bitmap(mask);
Index: examples/exnew_membmp.c
===================================================================
--- examples/exnew_membmp.c (revision 9954)
+++ examples/exnew_membmp.c (working copy)
@@ -37,8 +37,8 @@
static void test(ALLEGRO_BITMAP *bitmap, A5FONT_FONT *font, char *message)
{
long frames = 0;
- long start_time = al_current_time();
- int fps = 0;
+ double start_time = al_current_time();
+ double fps = 0;
for (;;) {
if (key_down()) {
@@ -60,15 +60,13 @@
print(font, message, 0, 0);
char second_line[100];
- sprintf(second_line, "%d FPS", fps);
+ sprintf(second_line, "%.1f FPS", fps);
print(font, second_line, 0, a5font_text_height(font)+5);
al_flip_display();
frames++;
- long now = al_current_time();
- long mseconds = now - start_time;
- fps = mseconds > 10 ? 1000 * frames / mseconds : 0;
+ fps = (double)frames / (al_current_time() - start_time);
}
}
Index: examples/exnew_lockbitmap.c
===================================================================
--- examples/exnew_lockbitmap.c (revision 9954)
+++ examples/exnew_lockbitmap.c (working copy)
@@ -51,7 +51,7 @@
al_draw_bitmap(bitmap, 0, 0, 0);
al_flip_display();
- al_rest(5000);
+ al_rest(5.0);
return 0;
}
END_OF_MAIN()
Index: include/allegro5/events.h
===================================================================
--- include/allegro5/events.h (revision 9954)
+++ include/allegro5/events.h (working copy)
@@ -142,7 +142,7 @@
#define _AL_EVENT_HEADER(srctype) \
ALLEGRO_EVENT_TYPE type; \
srctype *source; \
- unsigned long timestamp; \
+ double timestamp; \
signed int _refcount; /* internal */ \
union ALLEGRO_EVENT *_next; /* internal */ \
union ALLEGRO_EVENT *_next_free /* internal */
@@ -210,7 +210,7 @@
*
* > ALLEGRO_EVENT_TYPE type;
* > ALLEGRO_EVENT_SOURCE * any.source;
- * > unsigned long any.timestamp;
+ * > double any.timestamp;
*
* By examining the type field you can then access type-specific fields. The
* any.source field tells you which event source generated that particular
only in patch2:
unchanged:
--- src/unix/utimernu.c (revision 9954)
+++ src/unix/utimernu.c (working copy)
@@ -25,24 +25,18 @@
#include "allegro5/internal/aintern_events.h"
-
-/* readability typedefs */
-typedef long msecs_t;
-typedef long usecs_t;
-
-
/* forward declarations */
-static usecs_t timer_thread_handle_tick(usecs_t interval);
+static double timer_thread_handle_tick(double interval);
static void timer_handle_tick(ALLEGRO_TIMER *this);
struct ALLEGRO_TIMER
{
ALLEGRO_EVENT_SOURCE es;
bool started;
- usecs_t speed_usecs;
+ double speed_secs;
long count;
- long counter; /* counts down to zero=blastoff */
+ double counter; /* counts down to zero=blastoff */
};
@@ -91,27 +85,18 @@
}
#endif
- struct timeval old_time;
- struct timeval new_time;
- struct timeval delay;
- usecs_t interval = 0x8000;
+ double old_time = al_current_time();
+ double new_time;
+ double interval = 0.032768;
- gettimeofday(&old_time, NULL);
-
while (!_al_thread_should_stop(self)) {
- /* Go to sleep for a short time. `select' is more accurate than
- * `usleep' (or even `nanosleep') on my Linux system.
- */
- delay.tv_sec = interval / 1000000L;
- delay.tv_usec = interval % 1000000L;
- select(0, NULL, NULL, NULL, &delay);
+ al_rest(interval);
_al_mutex_lock(&timer_thread_mutex);
{
/* Calculate actual time elapsed. */
- gettimeofday(&new_time, NULL);
- interval = ((new_time.tv_sec - old_time.tv_sec) * 1000000L
- + (new_time.tv_usec - old_time.tv_usec));
+ new_time = al_current_time();
+ interval = new_time - old_time;
old_time = new_time;
/* Handle a tick. */
@@ -128,9 +113,9 @@
* returns the duration that the timer thread should try to sleep
* next time.
*/
-static usecs_t timer_thread_handle_tick(usecs_t interval)
+static double timer_thread_handle_tick(double interval)
{
- usecs_t new_delay = 0x8000;
+ double new_delay = 0.032768;
unsigned int i;
for (i = 0; i < _al_vector_size(&active_timers); i++) {
@@ -140,7 +125,7 @@
timer->counter -= interval;
while (timer->counter <= 0) {
- timer->counter += timer->speed_usecs;
+ timer->counter += timer->speed_secs;
timer_handle_tick(timer);
}
@@ -161,9 +146,9 @@
/* al_install_timer: [primary thread]
* Create a new timer object.
*/
-ALLEGRO_TIMER* al_install_timer(msecs_t speed_msecs)
+ALLEGRO_TIMER* al_install_timer(double speed_secs)
{
- ASSERT(speed_msecs > 0);
+ ASSERT(speed_secs > 0);
{
ALLEGRO_TIMER *timer = _AL_MALLOC(sizeof *timer);
@@ -173,7 +158,7 @@
_al_event_source_init(&timer->es);
timer->started = false;
timer->count = 0;
- timer->speed_usecs = speed_msecs * 1000;
+ timer->speed_secs = speed_secs;
timer->counter = 0;
_al_register_destructor(timer, (void (*)(void *)) al_uninstall_timer);
@@ -220,7 +205,7 @@
ALLEGRO_TIMER **slot;
this->started = true;
- this->counter = this->speed_usecs;
+ this->counter = this->speed_secs;
slot = _al_vector_alloc_back(&active_timers);
*slot = this;
@@ -285,11 +270,11 @@
/* al_timer_get_speed: [primary thread]
* Return this timer's speed.
*/
-msecs_t al_timer_get_speed(ALLEGRO_TIMER *this)
+double al_timer_get_speed(ALLEGRO_TIMER *this)
{
ASSERT(this);
- return this->speed_usecs / 1000;
+ return this->speed_secs;
}
@@ -297,19 +282,19 @@
/* al_timer_set_speed: [primary thread]
* Change this timer's speed.
*/
-void al_timer_set_speed(ALLEGRO_TIMER *this, msecs_t new_speed_msecs)
+void al_timer_set_speed(ALLEGRO_TIMER *this, double new_speed_secs)
{
ASSERT(this);
- ASSERT(new_speed_msecs > 0);
+ ASSERT(new_speed_secs > 0);
_al_mutex_lock(&timer_thread_mutex);
{
if (this->started) {
- this->counter -= this->speed_usecs;
- this->counter += new_speed_msecs * 1000;
+ this->counter -= this->speed_secs;
+ this->counter += new_speed_secs;
}
- this->speed_usecs = new_speed_msecs * 1000;
+ this->speed_secs = new_speed_secs;
}
_al_mutex_unlock(&timer_thread_mutex);
}
only in patch2:
unchanged:
--- include/allegro5/timer.h (revision 9954)
+++ include/allegro5/timer.h (working copy)
@@ -26,15 +26,17 @@
/* Macros: conversion macros
- * AL_SECS_TO_MSECS - seconds to milliseconds
- * AL_BPS_TO_MSECS - beats per second to milliseconds
- * AL_BPM_TO_MSECS - beats per minute to milliseconds
+ * AL_USECS_TO_SECS - microseconds to seconds
+ * AL_MSECS_TO_SECS - milliseconds to seconds
+ * AL_BPS_TO_MSECS - beats per second to seconds
+ * AL_BPM_TO_MSECS - beats per minute to seconds
*
* These macros convert from various time units into milliseconds.
*/
-#define ALLEGRO_SECS_TO_MSECS(x) ((long)(x) * 1000)
-#define ALLEGRO_BPS_TO_MSECS(x) (1000 / (long)(x))
-#define ALLEGRO_BPM_TO_MSECS(x) ((60 * 1000) / (long)(x))
+#define ALLEGRO_USECS_TO_SECS(x) (x / 1000000)
+#define ALLEGRO_MSECS_TO_SECS(x) (x / 1000)
+#define ALLEGRO_BPS_TO_SECS(x) (1.0 / x)
+#define ALLEGRO_BPM_TO_SECS(x) (60 / x)
/* Type: ALLEGRO_TIMER
@@ -47,14 +49,14 @@
/* Function: al_install_timer
* Install a new timer. If successful, a pointer to a new timer object is
- * returned, otherwise NULL is returned. SPEED_MSECS is in milliseconds per
+ * returned, otherwise NULL is returned. SPEED_SECS is in seconds per
* "tick", and must be positive. The new timer is initially stopped.
*
* The system driver must be installed before this function can be called.
*
* Usage note: typical granularity is on the order of milliseconds.
*/
-AL_FUNC(ALLEGRO_TIMER*, al_install_timer, (long speed_msecs));
+AL_FUNC(ALLEGRO_TIMER*, al_install_timer, (double speed_secs));
/* Function: al_uninstall_timer
@@ -92,7 +94,7 @@
/* Function: al_timer_get_speed
* Return the timer's speed.
*/
-AL_FUNC(long, al_timer_get_speed, (ALLEGRO_TIMER *timer));
+AL_FUNC(double, al_timer_get_speed, (ALLEGRO_TIMER *timer));
/* Function: al_timer_set_speed
@@ -100,11 +102,11 @@
* incremented when it is started. This can be done when the timer is
* started or stopped. If the timer is currently running, it is made to
* look as though the speed change occured precisely at the last tick.
- * SPEED_MSECS is in milliseconds per "tick", and must be positive.
+ * SPEED_SECS is in seconds per "tick", and must be positive.
*
* Usage note: typical granularity is on the order of milliseconds.
*/
-AL_FUNC(void, al_timer_set_speed, (ALLEGRO_TIMER *timer, long speed_msecs));
+AL_FUNC(void, al_timer_set_speed, (ALLEGRO_TIMER *timer, double speed_secs));
/* Function: al_timer_get_count
only in patch2:
unchanged:
--- examples/exnew_events.c (revision 9954)
+++ examples/exnew_events.c (working copy)
@@ -448,9 +448,9 @@
* automatically increment a counter every n milliseconds. They no longer
* run user-defined callbacks.
*/
- timer_a = al_install_timer(100);
- timer_b = al_install_timer(1000);
- timer_c = al_install_timer(5000);
+ timer_a = al_install_timer(0.100);
+ timer_b = al_install_timer(1.000);
+ timer_c = al_install_timer(5.000);
if (!timer_a || !timer_b || !timer_c) {
fatal_error("al_install_timer");
}