test/snake.c

changeset 65
9c44c55d327a
parent 63
e3cacdd636e4
equal deleted inserted replaced
64:f18dc427f86f 65:9c44c55d327a
45 } 45 }
46 } 46 }
47 } 47 }
48 48
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_font(ASC_FONT_REGULAR, 24);
51 asc_ink_rgb(255, 0, 0); 51 asc_ink_rgb(255, 0, 0);
52 AscSceneNode* node = asc_text( .x = 10, .y = 10 ); 52 AscSceneNode* node = asc_text( .x = 10, .y = 10 );
53 asc_scene_add_behavior(node, update_fps_counter); 53 asc_scene_add_behavior(node, update_fps_counter);
54 asc_add_ui_node(node); 54 asc_add_ui_node(node);
55 } 55 }
56 56
57 static void update_score_counter(AscSceneNode *node) { 57 static void update_score_counter(AscSceneNode *node) {
58 // tie to bottom right of the screen 58 // tie to bottom right of the screen
59 if (asc_window_active->resized) { 59 if (asc_active_window->resized) {
60 asc_vec2i bottom_right = asc_window_active->dimensions; 60 asc_vec2i bottom_right = asc_active_window->dimensions;
61 asc_vec2i scale = asc_get_scale2d(node); 61 asc_vec2i scale = asc_get_scale2d(node);
62 asc_set_position2d( 62 asc_set_position2d(
63 node, 63 node,
64 bottom_right.x - scale.width - 10, 64 bottom_right.x - scale.width - 10,
65 bottom_right.y - scale.height - 10 65 bottom_right.y - scale.height - 10
66 ); 66 );
67 } 67 }
68 } 68 }
69 69
70 static void create_score_counter(void) { 70 static void create_score_counter(void) {
71 asc_set_font(asc_font(ASC_FONT_BOLD, 14)); 71 asc_font(ASC_FONT_BOLD, 14);
72 asc_ink_rgb(0, 255, 0); 72 asc_ink_rgb(0, 255, 0);
73 AscSceneNode* node = asc_text(.text = "Score: 0" ); 73 AscSceneNode* node = asc_text(.text = "Score: 0" );
74 asc_scene_add_behavior(node, update_score_counter); 74 asc_scene_add_behavior(node, update_score_counter);
75 asc_add_ui_node(node); 75 asc_add_ui_node(node);
76 } 76 }
85 85
86 // create window 86 // create window
87 AscWindowSettings settings; 87 AscWindowSettings settings;
88 asc_window_settings_init_defaults(&settings); 88 asc_window_settings_init_defaults(&settings);
89 settings.title = "Snake"; 89 settings.title = "Snake";
90 AscWindow *window = asc_window_initialize(0, &settings); 90 asc_window_initialize(0, &settings);
91 91
92 // create UI elements 92 // create UI elements
93 create_fps_counter(); 93 create_fps_counter();
94 create_score_counter(); 94 create_score_counter();
95 95
96 // Main Loop 96 // Main Loop
97 do { 97 do {
98 // quit application on any error 98 // quit application on any error
99 if (asc_has_error()) { 99 if (asc_has_error()) {
100 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 100 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
101 "Fatal Error", asc_get_error(), window->window); 101 "Fatal Error", asc_get_error(),
102 asc_active_window->window);
102 asc_clear_error(); 103 asc_clear_error();
103 asc_context_quit(); 104 asc_context_quit();
104 } 105 }
105 106
106 // quit application on ESC key press 107 // quit application on ESC key press

mercurial