test/snake/snake.c

changeset 108
d619bf7dd87b
parent 105
2b7f92ff2c15
equal deleted inserted replaced
107:a35b39abe2b2 108:d619bf7dd87b
51 asc_texture_from_file(TEXTURE_SHIP, "ship.png"); 51 asc_texture_from_file(TEXTURE_SHIP, "ship.png");
52 asc_texture_from_file(TEXTURE_BACKDROP, "backdrop.png"); 52 asc_texture_from_file(TEXTURE_BACKDROP, "backdrop.png");
53 cxMempoolRegister(asc_active_glctx_mpool, tex2d, destroy_textures); 53 cxMempoolRegister(asc_active_glctx_mpool, tex2d, destroy_textures);
54 } 54 }
55 55
56 static void update_fps_counter(AscSceneNode *node) { 56 static void update_fps_counter(AscBehavior *behavior) {
57 AscSceneNode *node = behavior->node;
57 static uint64_t last_fps = 0; 58 static uint64_t last_fps = 0;
58 static uint64_t debounce = ASC_NANOS_SECOND - 1; 59 static uint64_t debounce = ASC_NANOS_SECOND - 1;
59 debounce += asc_context.frame_nanos; 60 debounce += asc_context.frame_nanos;
60 // only update text every second 61 // only update text every second
61 if (debounce >= ASC_NANOS_SECOND) { 62 if (debounce >= ASC_NANOS_SECOND) {
79 (int)bottom_right.y - (int)text_size.height - 10 80 (int)bottom_right.y - (int)text_size.height - 10
80 ); 81 );
81 } 82 }
82 } 83 }
83 84
84 static void scale_backdrop(AscSceneNode *node) { 85 static void scale_backdrop(AscBehavior *behavior) {
85 // scale the backdrop to the size of the window 86 // scale the backdrop to the size of the window
86 if (asc_active_window->resized) { 87 if (asc_active_window->resized) {
88 AscSceneNode *node = behavior->node;
87 asc_vec2u window_size = asc_active_window->dimensions; 89 asc_vec2u window_size = asc_active_window->dimensions;
88 // TODO: replace scale with asc_sprite_set_size() 90 // TODO: replace scale with asc_sprite_set_size()
89 asc_set_scale(node, (float)window_size.width, (float)window_size.height, 1); 91 asc_set_scale(node, (float)window_size.width, (float)window_size.height, 1);
90 // TODO: implement texture repetition 92 // TODO: implement texture repetition
91 } 93 }
92 } 94 }
93 95
94 static void create_backdrop(void) { 96 static void create_backdrop(void) {
95 AscSceneNode *node = asc_sprite(.texture = TEXTURE_BACKDROP); 97 AscSceneNode *node = asc_sprite(.texture = TEXTURE_BACKDROP);
96 asc_scene_add_behavior(node, scale_backdrop); 98 asc_behavior_add(node, .func = scale_backdrop);
97 asc_scene_add_node(BACKDROP_SCENE, node); 99 asc_scene_add_node(BACKDROP_SCENE, node);
98 } 100 }
99 101
100 static void create_fps_counter(void) { 102 static void create_fps_counter(void) {
101 asc_font(ASC_FONT_REGULAR, 12); 103 asc_font(ASC_FONT_REGULAR, 12);
102 asc_ink_rgba(224, 224, 224, 196); 104 asc_ink_rgba(224, 224, 224, 196);
103 AscSceneNode *node = asc_text(); 105 AscSceneNode *node = asc_text();
104 asc_scene_add_behavior(node, update_fps_counter); 106 asc_behavior_add(node, .func = update_fps_counter);
105 asc_add_ui_node(node); 107 asc_add_ui_node(node);
106 } 108 }
107 109
108 static void create_score_counter(void) { 110 static void create_score_counter(void) {
109 asc_font(ASC_FONT_BOLD, 16); 111 asc_font(ASC_FONT_BOLD, 16);

mercurial