[AD] yet another timer patch |
[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
Sorry for generating more spam but no one was in #allegro-dev.
This patch adds an "error" field to the timer event and adds this error field, event overhead, and total timer overhead to the printouts for exnew_timer. I figure it is useful for timer profiling and delta timing for games.
--ryan
Index: src/timernu.c
===================================================================
--- src/timernu.c (revision 10074)
+++ src/timernu.c (working copy)
@@ -128,8 +128,8 @@
timer->counter -= interval;
while (timer->counter <= 0) {
+ timer_handle_tick(timer);
timer->counter += timer->speed_secs;
- timer_handle_tick(timer);
}
if ((timer->counter > 0) && (timer->counter < new_delay))
@@ -352,6 +352,7 @@
event->timer.type = ALLEGRO_EVENT_TIMER;
event->timer.timestamp = al_current_time();
event->timer.count = this->count;
+ event->timer.error = -this->counter;
_al_event_source_emit_event(&this->es, event);
}
}
Index: include/allegro5/events.h
===================================================================
--- include/allegro5/events.h (revision 10074)
+++ include/allegro5/events.h (working copy)
@@ -198,6 +198,7 @@
{
_AL_EVENT_HEADER(struct ALLEGRO_TIMER);
long count;
+ double error;
} ALLEGRO_TIMER_EVENT;
Index: examples/exnew_timer.c
===================================================================
--- examples/exnew_timer.c (revision 10074)
+++ examples/exnew_timer.c (working copy)
@@ -22,6 +22,8 @@
double min_diff, max_diff, second_spread;
double second;
double timer_events;
+ double timer_error;
+ double timestamp;
} ex;
/* Initialize the example. */
@@ -30,6 +32,7 @@
ex.FPS = 50;
ex.first_tick = true;
+ /* TODO: use fixed width font */
ex.myfont = a5font_load_font("font.tga", 0);
if (!ex.myfont) {
allegro_message("font.tga not found");
@@ -69,9 +72,15 @@
static void draw(void)
{
int h, y, i;
+ double cur_time, event_overhead, total_error;
+
+ cur_time = al_current_time();
+ event_overhead = cur_time - ex.timestamp;
+ total_error = event_overhead + ex.timer_error;
h = a5font_text_height(ex.myfont);
al_clear(al_map_rgb_f(1, 1, 1));
+
print(0, 0, "%.9f target for %.0f Hz Timer", 1.0 / ex.FPS, ex.FPS);
print(0, h, "%.9f now", ex.this_time - ex.prev_time);
print(0, 2 * h, "%.9f accum over one second",
@@ -79,6 +88,9 @@
print(0, 3 * h, "%.9f min", ex.min_diff);
print(0, 4 * h, "%.9f max", ex.max_diff);
print(300, 3.5 * h, "%.9f (max - min)", ex.second_spread);
+ print(300, 4.5 * h, "%.9f (timer error)", ex.timer_error);
+ print(300, 5.5 * h ,"%.9f (event overhead)", event_overhead);
+ print(300, 6.5 * h, "%.9f (total error)" , total_error);
y = 240;
for (i = 0; i < 4; i++) {
@@ -88,7 +100,7 @@
}
/* Called a fixed amount of times per second. */
-static void tick(void)
+static void tick(ALLEGRO_TIMER_EVENT* timer_event)
{
int i;
@@ -112,6 +124,8 @@
if (duration > ex.max_diff) ex.max_diff = duration;
ex.accum_time += duration;
ex.timer_events++;
+ ex.timer_error = timer_event->error;
+ ex.timestamp = timer_event->timestamp;
}
draw();
@@ -144,7 +158,7 @@
/* Is it time for the next timer tick? */
case ALLEGRO_EVENT_TIMER:
- tick();
+ tick(&event.timer);
break;
}
}