27 |
27 |
28 #include <ascension/ascension.h> |
28 #include <ascension/ascension.h> |
29 #include <cx/printf.h> |
29 #include <cx/printf.h> |
30 |
30 |
31 static void update_fps_counter(AscSceneNode *node) { |
31 static void update_fps_counter(AscSceneNode *node) { |
32 // addition and multiplication is more efficient testing for zero |
32 static uint64_t last_fps = 0; |
33 // at an unnoticeable cost of imprecision |
33 static uint64_t debounce = ASC_NANOS_SECOND - 1; |
34 static unsigned last_fps = 0u; |
34 debounce += asc_context.frame_nanos; |
35 static unsigned debounce = 999u; |
35 // only update text every seconds |
36 unsigned fps = 1000u; |
36 if (debounce >= ASC_NANOS_SECOND) { |
37 debounce += asc_context.elapsed_millis; |
|
38 if (debounce >= 1000u) { |
|
39 debounce = 0; |
37 debounce = 0; |
40 fps /= asc_context.elapsed_millis; |
38 uint64_t fps = ASC_NANOS_SECOND; |
|
39 fps /= asc_context.frame_nanos; |
41 if (fps != last_fps) { |
40 if (fps != last_fps) { |
42 last_fps = fps; |
41 last_fps = fps; |
43 snprintf(asc_text_data(node)->text, 9, "%u FPS", fps); |
42 snprintf(asc_text_data(node)->text, 11, "%"PRIu64" FPS", fps); |
44 asc_node_update(node); |
43 asc_node_update(node); |
45 } |
44 } |
46 } |
45 } |
47 } |
46 } |
48 |
47 |
49 static void create_fps_counter(void) { |
48 static void create_fps_counter(void) { |
50 asc_set_font(asc_font(ASC_FONT_REGULAR, 24)); |
49 asc_set_font(asc_font(ASC_FONT_REGULAR, 24)); |
51 asc_ink_rgb(255, 0, 0); |
50 asc_ink_rgb(255, 0, 0); |
52 AscSceneNode* node = asc_text(10, 10, "XXXXX FPS"); |
51 AscSceneNode* node = asc_text(10, 10, "XXXXXXX FPS"); |
53 asc_scene_add_behavior(node, update_fps_counter); |
52 asc_scene_add_behavior(node, update_fps_counter); |
54 asc_scene_add(&asc_window_active->ui, node); |
53 asc_scene_add(&asc_window_active->ui, node); |
55 } |
54 } |
56 |
55 |
57 static void update_score_counter(AscSceneNode *node) { |
56 static void update_score_counter(AscSceneNode *node) { |