test/snake/snake.c

changeset 256
60014484121c
parent 249
2522fbeccede
equal deleted inserted replaced
255:0e0a0bf4f7e4 256:60014484121c
56 56
57 static asc_transform rotations[4]; 57 static asc_transform rotations[4];
58 static asc_vec2i directions[4]; 58 static asc_vec2i directions[4];
59 59
60 typedef struct { 60 typedef struct {
61 asc_col4f color; 61 asc_color color;
62 enum MoveDirection direction; 62 enum MoveDirection direction;
63 enum MoveDirection target_direction; 63 enum MoveDirection target_direction;
64 /** 64 /**
65 * The speed in tiles per second. 65 * The speed in tiles per second.
66 */ 66 */
141 asc_behavior_add(node, .func = backdrop_scale); 141 asc_behavior_add(node, .func = backdrop_scale);
142 asc_scene_add_node(BACKDROP_SCENE, node); 142 asc_scene_add_node(BACKDROP_SCENE, node);
143 143
144 // also add a frame for the main scene 144 // also add a frame for the main scene
145 // add this to the UI layer so that the border size does not scale 145 // add this to the UI layer so that the border size does not scale
146 AscSceneNode *frame = asc_rectangle(.thickness = 2, .color = ASC_RGB(66, 142, 161)); 146 AscSceneNode *frame = asc_rectangle(.thickness = 2, .color = ASC_RGBi(66, 142, 161));
147 asc_behavior_add(frame, .func = main_scene_frame_scale); 147 asc_behavior_add(frame, .func = main_scene_frame_scale);
148 asc_ui_add_node(frame); 148 asc_ui_add_node(frame);
149 } 149 }
150 150
151 static void fps_counter_update(AscBehavior *behavior) { 151 static void fps_counter_update(AscBehavior *behavior) {
168 } 168 }
169 169
170 static void fps_counter_create(void) { 170 static void fps_counter_create(void) {
171 AscSceneNode *node = asc_text( 171 AscSceneNode *node = asc_text(
172 .name = "FPS Counter", 172 .name = "FPS Counter",
173 .color = ASC_RGB(255, 255, 255), 173 .color = ASC_RGB(1, 1, 1),
174 .font = asc_font(ASC_FONT_REGULAR, 12), 174 .font = asc_font(ASC_FONT_REGULAR, 12),
175 ); 175 );
176 asc_behavior_add(node, .func = fps_counter_update, .interval = asc_seconds(1)); 176 asc_behavior_add(node, .func = fps_counter_update, .interval = asc_seconds(1));
177 asc_behavior_add(node, fps_counter_tie_to_corner); 177 asc_behavior_add(node, fps_counter_tie_to_corner);
178 asc_ui_add_node(node); 178 asc_ui_add_node(node);
185 unsigned x = coords.x, y = coords.y; 185 unsigned x = coords.x, y = coords.y;
186 asc_ptr_cast(AscRectangle, tile, game_field->nodes[x][y]); 186 asc_ptr_cast(AscRectangle, tile, game_field->nodes[x][y]);
187 int old_owner = game_field->tile_data[x][y] & GAME_FIELD_TILE_OWNER_MASK; 187 int old_owner = game_field->tile_data[x][y] & GAME_FIELD_TILE_OWNER_MASK;
188 if (player == NULL) { 188 if (player == NULL) {
189 asc_clear_flag(game_field->tile_data[x][y], GAME_FIELD_TILE_OWNER_MASK); 189 asc_clear_flag(game_field->tile_data[x][y], GAME_FIELD_TILE_OWNER_MASK);
190 tile->color = asc_col_itof(ASC_RGB(16, 50, 160)); 190 tile->color = ASC_RGBi(16, 50, 160);
191 } else { 191 } else {
192 asc_set_flag_masked(game_field->tile_data[x][y], GAME_FIELD_TILE_OWNER_MASK, player->number); 192 asc_set_flag_masked(game_field->tile_data[x][y], GAME_FIELD_TILE_OWNER_MASK, player->number);
193 tile->color = player->color; 193 tile->color = player->color;
194 } 194 }
195 int new_owner = game_field->tile_data[x][y] & GAME_FIELD_TILE_OWNER_MASK; 195 int new_owner = game_field->tile_data[x][y] & GAME_FIELD_TILE_OWNER_MASK;
203 for (unsigned x = 0; x < GAME_FIELD_SIZE; x++) { 203 for (unsigned x = 0; x < GAME_FIELD_SIZE; x++) {
204 for (unsigned y = 0; y < GAME_FIELD_SIZE; y++) { 204 for (unsigned y = 0; y < GAME_FIELD_SIZE; y++) {
205 AscSceneNode *tile = asc_rectangle( 205 AscSceneNode *tile = asc_rectangle(
206 .x = x*GAME_FIELD_TILE_SIZE, .y = y*GAME_FIELD_TILE_SIZE, .filled = true, .thickness = 2, 206 .x = x*GAME_FIELD_TILE_SIZE, .y = y*GAME_FIELD_TILE_SIZE, .filled = true, .thickness = 2,
207 .width = GAME_FIELD_TILE_SIZE, .height = GAME_FIELD_TILE_SIZE, 207 .width = GAME_FIELD_TILE_SIZE, .height = GAME_FIELD_TILE_SIZE,
208 .color = ASC_RGB(16, 50, 160), 208 .color = ASC_RGBi(16, 50, 160),
209 .border_color = ASC_RGB(20, 84, 128), 209 .border_color = ASC_RGBi(20, 84, 128),
210 ); 210 );
211 211
212 game_field->tile_data[x][y] = GAME_FIELD_TILE_EXISTS_FLAG; 212 game_field->tile_data[x][y] = GAME_FIELD_TILE_EXISTS_FLAG;
213 game_field->nodes[x][y] = tile; 213 game_field->nodes[x][y] = tile;
214 214
257 asc_shader_upload_model_matrix(s, node); 257 asc_shader_upload_model_matrix(s, node);
258 258
259 // Bind texture 259 // Bind texture
260 asc_texture_bind(TEXTURE_PLAYER, shader->map_albedo, 0); 260 asc_texture_bind(TEXTURE_PLAYER, shader->map_albedo, 0);
261 asc_texture_bind(TEXTURE_PLAYER_COLOR_MAP, shader->map_color, 1); 261 asc_texture_bind(TEXTURE_PLAYER_COLOR_MAP, shader->map_color, 1);
262 asc_shader_upload_col4f(shader->color, player->color); 262 asc_shader_upload_color(shader->color, player->color);
263 asc_mesh_draw_triangle_strip(&sprite->mesh); 263 asc_mesh_draw_triangle_strip(&sprite->mesh);
264 } 264 }
265 265
266 static void player_move(AscBehavior *behavior) { 266 static void player_move(AscBehavior *behavior) {
267 AscSceneNode *node = behavior->node; 267 AscSceneNode *node = behavior->node;
368 asc_scene_add_node(MAIN_SCENE, node); 368 asc_scene_add_node(MAIN_SCENE, node);
369 Player *player = asc_scene_node_allocate_data(node, sizeof(Player)); 369 Player *player = asc_scene_node_allocate_data(node, sizeof(Player));
370 player_position(player, 12, 8); 370 player_position(player, 12, 8);
371 player->speed = 3.f; // start with 3 tiles/sec 371 player->speed = 3.f; // start with 3 tiles/sec
372 player->number = 1; 372 player->number = 1;
373 player->color = ASC_RGB_F(1, 0, 0); 373 player->color = ASC_RGB(1, 0, 0);
374 player->trace = cxLinkedListCreateSimple(sizeof(asc_vec2i)); 374 player->trace = cxLinkedListCreateSimple(sizeof(asc_vec2i));
375 node->draw_func = player_draw; 375 node->draw_func = player_draw;
376 node->user_data_free_func = (cx_destructor_func2)player_destroy; 376 node->user_data_free_func = (cx_destructor_func2)player_destroy;
377 asc_behavior_add(node, player_move); 377 asc_behavior_add(node, player_move);
378 return player; 378 return player;
450 -GAME_FIELD_TILE_SIZE, 450 -GAME_FIELD_TILE_SIZE,
451 (GAME_FIELD_SIZE+2)*GAME_FIELD_TILE_SIZE, 451 (GAME_FIELD_SIZE+2)*GAME_FIELD_TILE_SIZE,
452 (GAME_FIELD_SIZE+2)*GAME_FIELD_TILE_SIZE 452 (GAME_FIELD_SIZE+2)*GAME_FIELD_TILE_SIZE
453 ), 453 ),
454 .viewport_clear = true, 454 .viewport_clear = true,
455 .clear_color = ASC_RGB(0, 32, 16), 455 .clear_color = ASC_RGBi(0, 32, 16),
456 .viewport_update_func = main_scene_viewport_update 456 .viewport_update_func = main_scene_viewport_update
457 ); 457 );
458 458
459 // backdrop for letterbox/pillarbox 459 // backdrop for letterbox/pillarbox
460 backdrop_create(); 460 backdrop_create();

mercurial