55 asc_gl_context_add_cleanup_func(asc_active_glctx, destroy_textures); |
55 asc_gl_context_add_cleanup_func(asc_active_glctx, destroy_textures); |
56 } |
56 } |
57 |
57 |
58 static void update_fps_counter(AscBehavior *behavior) { |
58 static void update_fps_counter(AscBehavior *behavior) { |
59 AscSceneNode *node = behavior->node; |
59 AscSceneNode *node = behavior->node; |
|
60 // update text |
60 static uint64_t last_fps = 0; |
61 static uint64_t last_fps = 0; |
61 static uint64_t debounce = ASC_NANOS_SECOND - 1; |
62 uint64_t fps = asc_seconds(1) / asc_context.frame_nanos; |
62 debounce += asc_context.frame_nanos; |
63 if (fps != last_fps) { |
63 // only update text every second |
64 last_fps = fps; |
64 if (debounce >= ASC_NANOS_SECOND) { |
65 asc_text_printf(node, "%"PRIu64" FPS", fps); |
65 debounce = 0; |
66 } |
66 uint64_t fps = ASC_NANOS_SECOND; |
67 } |
67 fps /= asc_context.frame_nanos; |
68 |
68 if (fps != last_fps) { |
69 static void tie_fps_counter_to_corner(AscBehavior *behavior) { |
69 last_fps = fps; |
70 // TODO: this should be replaced with some sort of UI layout manager |
70 asc_text_printf(node, "%"PRIu64" FPS", fps); |
71 AscSceneNode *node = behavior->node; |
71 } |
72 if (asc_test_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED) || asc_active_window->resized) { |
72 } |
|
73 // tie to bottom right of the screen |
|
74 if (asc_test_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED) |
|
75 || asc_active_window->resized) { |
|
76 asc_vec2u bottom_right = asc_active_window->dimensions; |
73 asc_vec2u bottom_right = asc_active_window->dimensions; |
77 asc_vec2u text_size = ((AscText*)node)->dimension; |
74 asc_vec2u text_size = ((AscText*)node)->dimension; |
78 asc_set_position2d( |
75 asc_set_position2d( |
79 node, |
76 node, |
80 (int)bottom_right.x - (int)text_size.width - 10, |
77 (int)bottom_right.x - (int)text_size.width - 10, |
103 |
100 |
104 static void create_fps_counter(void) { |
101 static void create_fps_counter(void) { |
105 asc_font(ASC_FONT_REGULAR, 12); |
102 asc_font(ASC_FONT_REGULAR, 12); |
106 asc_ink_rgb(255, 255, 255); |
103 asc_ink_rgb(255, 255, 255); |
107 AscSceneNode *node = asc_text(.name = "FPS Counter"); |
104 AscSceneNode *node = asc_text(.name = "FPS Counter"); |
108 asc_behavior_add(node, .func = update_fps_counter); |
105 asc_behavior_add(node, .func = update_fps_counter, .interval = asc_seconds(1)); |
|
106 asc_behavior_add(node, .func = tie_fps_counter_to_corner); |
109 asc_add_ui_node(node); |
107 asc_add_ui_node(node); |
110 } |
108 } |
111 |
109 |
112 static void create_score_counter(void) { |
110 static void create_score_counter(void) { |
113 asc_font(ASC_FONT_BOLD, 16); |
111 asc_font(ASC_FONT_BOLD, 16); |