test/snake.c

changeset 37
8a8cc6725b48
parent 36
e26b4ac1661c
child 44
b3da4096c607
equal deleted inserted replaced
36:e26b4ac1661c 37:8a8cc6725b48
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 // addition and multiplication is more efficient testing for zero
33 // at an unnoticible cost of imprecision 33 // at an unnoticeable cost of imprecision
34 static unsigned last_fps = 0u; 34 static unsigned last_fps = 0u;
35 static unsigned debounce = 999u; 35 static unsigned debounce = 999u;
36 unsigned fps = 1000u; 36 unsigned fps = 1000u;
37 debounce += asc_context.elapsed_millis; 37 debounce += asc_context.elapsed_millis;
38 if (debounce >= 1000u) { 38 if (debounce >= 1000u) {
49 static void create_fps_counter(void) { 49 static void create_fps_counter(void) {
50 asc_set_font(asc_font(ASC_FONT_REGULAR, 24)); 50 asc_set_font(asc_font(ASC_FONT_REGULAR, 24));
51 asc_ink_rgb(255, 0, 0); 51 asc_ink_rgb(255, 0, 0);
52 AscSceneNode* node = asc_text(10, 10, "XXXXX FPS"); 52 AscSceneNode* node = asc_text(10, 10, "XXXXX FPS");
53 asc_scene_add_behavior(node, update_fps_counter); 53 asc_scene_add_behavior(node, update_fps_counter);
54 asc_scene_add(asc_window_active_ui, node); 54 asc_scene_add(&asc_window_active->ui, node);
55 }
56
57 static void update_score_counter(AscSceneNode *node) {
58 AscText *text = asc_text_data(node);
59
60 // tie to bottom left of the screen
61 if (asc_window_active->resized) {
62 asc_vec2i bottom_left = asc_window_active->dimensions;
63 text->position.x = bottom_left.x - text->dimension.width - 10;
64 text->position.y = bottom_left.y - text->dimension.height - 10;
65 asc_node_update_transform(text);
66 }
67 }
68
69 static void create_score_counter(void) {
70 asc_set_font(asc_font(ASC_FONT_BOLD, 14));
71 asc_ink_rgb(0, 255, 0);
72 AscSceneNode* node = asc_text(0, 0, "Score: 0");
73 asc_scene_add_behavior(node, update_score_counter);
74 asc_scene_add(&asc_window_active->ui, node);
55 } 75 }
56 76
57 int main(int argc, char** argv) { 77 int main(int argc, char** argv) {
58 asc_context_initialize(); 78 asc_context_initialize();
59 if (asc_has_error()) { 79 if (asc_has_error()) {
67 settings.title = "Snake"; 87 settings.title = "Snake";
68 88
69 AscWindow *window = asc_window_initialize(0, &settings); 89 AscWindow *window = asc_window_initialize(0, &settings);
70 asc_shader_initialize_predefined(); 90 asc_shader_initialize_predefined();
71 91
72 // create fps counter 92 // create UI elements
73 create_fps_counter(); 93 create_fps_counter();
94 create_score_counter();
95
74 96
75 // Main Loop 97 // Main Loop
76 do { 98 do {
77 // quit application on any error 99 // quit application on any error
78 if (asc_has_error()) { 100 if (asc_has_error()) {

mercurial