test/snake/snake.c

changeset 205
d1e44c861426
parent 204
be5cf64b5c29
equal deleted inserted replaced
204:be5cf64b5c29 205:d1e44c861426
113 .text = "Score: 0" 113 .text = "Score: 0"
114 ); 114 );
115 asc_ui_add_node(node); 115 asc_ui_add_node(node);
116 } 116 }
117 117
118 static AscSceneNode *create_spaceship(void) { 118 enum MoveDirection {
119 MOVE_UP,
120 MOVE_LEFT,
121 MOVE_DOWN,
122 MOVE_RIGHT
123 };
124
125 static asc_transform rotations[4];
126 static asc_vec2f directions[4];
127
128 typedef struct {
129 AscSceneNode *sprite;
130 enum MoveDirection direction;
131 } Spaceship;
132
133 static void turn_left(Spaceship *spaceship) {
134 spaceship->direction = (spaceship->direction + 1) % 4;
135 asc_node_set_rotation(spaceship->sprite, rotations[spaceship->direction]);
136 }
137
138 static void turn_right(Spaceship *spaceship) {
139 spaceship->direction = (spaceship->direction + 3) % 4;
140 asc_node_set_rotation(spaceship->sprite, rotations[spaceship->direction]);
141 }
142
143 static Spaceship *create_spaceship(void) {
144 // TODO: this all belongs somewhere else, this is just quickly hacked into here for testing
145 asc_transform_identity(rotations[MOVE_UP]);
146 asc_transform_roll(rotations[MOVE_LEFT], asc_rad(-90));
147 asc_transform_roll(rotations[MOVE_RIGHT], asc_rad(90));
148 asc_transform_roll(rotations[MOVE_DOWN], asc_rad(180));
149 directions[MOVE_UP] = ASC_VEC2F(0, -1);
150 directions[MOVE_LEFT] = ASC_VEC2F(-1, 0);
151 directions[MOVE_DOWN] = ASC_VEC2F(0, 1);
152 directions[MOVE_RIGHT] = ASC_VEC2F(1, 0);
119 AscSceneNode *sprite = asc_sprite( 153 AscSceneNode *sprite = asc_sprite(
120 .name = "Player", 154 .name = "Player",
121 .texture = TEXTURE_SHIP, 155 .texture = TEXTURE_SHIP,
122 .x = 250, 156 .x = 250,
123 .y = 300, 157 .y = 300,
125 .height = 64, 159 .height = 64,
126 .origin_x = 32, 160 .origin_x = 32,
127 .origin_y = 32, 161 .origin_y = 32,
128 ); 162 );
129 asc_scene_add_node(MAIN_SCENE, sprite); 163 asc_scene_add_node(MAIN_SCENE, sprite);
130 return sprite; 164 // TODO: this is never freed at the moment
165 Spaceship *ship = cxZallocDefault(sizeof(Spaceship));
166 ship->sprite = sprite;
167 return ship;
131 } 168 }
132 169
133 static asc_rect update_viewport_for_window_resize(asc_vec2u window_size) { 170 static asc_rect update_viewport_for_window_resize(asc_vec2u window_size) {
134 // Compute scaling and offsets 171 // Compute scaling and offsets
135 unsigned viewport_size, offset_x = 0, offset_y = 0; 172 unsigned viewport_size, offset_x = 0, offset_y = 0;
190 // create UI elements 227 // create UI elements
191 create_fps_counter(); 228 create_fps_counter();
192 create_score_counter(); 229 create_score_counter();
193 230
194 // create spaceship 231 // create spaceship
195 AscSceneNode *spaceship = create_spaceship(); 232 Spaceship *spaceship = create_spaceship();
196 233
197 // Main Loop 234 // Main Loop
198 do { 235 do {
199 // quit application on any error 236 // quit application on any error
200 if (asc_has_error()) { 237 if (asc_has_error()) {
205 asc_context_quit(); 242 asc_context_quit();
206 } 243 }
207 244
208 // player rotation 245 // player rotation
209 if (asc_key_pressed(ASC_KEY(LEFT))) { 246 if (asc_key_pressed(ASC_KEY(LEFT))) {
210 asc_node_roll_deg(spaceship, -90); 247 turn_left(spaceship);
211 asc_node_update_transform(spaceship); 248 }
249 if (asc_key_pressed(ASC_KEY(RIGHT))) {
250 turn_right(spaceship);
212 } 251 }
213 252
214 // debug-key for clearing the shader registry 253 // debug-key for clearing the shader registry
215 if (asc_key_pressed(ASC_KEY(S))) { 254 if (asc_key_pressed(ASC_KEY(S))) {
216 asc_shader_clear_registry(); 255 asc_shader_clear_registry();

mercurial