31 #include <ascension/2d.h> |
31 #include <ascension/2d.h> |
32 |
32 |
33 #include <cx/printf.h> |
33 #include <cx/printf.h> |
34 |
34 |
35 enum Textures2d { |
35 enum Textures2d { |
36 TEX_SHIP = 0, |
36 TEX_PLAYER = 0, |
37 TEX_BACKDROP, |
37 TEX_BACKDROP, |
38 TEX2D_COUNT |
38 TEX2D_COUNT |
39 }; |
39 }; |
40 static AscTexture tex2d[TEX2D_COUNT]; |
40 static AscTexture tex2d[TEX2D_COUNT]; |
41 #define TEXTURE_SHIP &tex2d[TEX_SHIP] |
41 #define TEXTURE_PLAYER &tex2d[TEX_PLAYER] |
42 #define TEXTURE_BACKDROP &tex2d[TEX_BACKDROP] |
42 #define TEXTURE_BACKDROP &tex2d[TEX_BACKDROP] |
43 |
43 |
44 #define BACKDROP_SCENE asc_window_scene(0) |
44 #define BACKDROP_SCENE asc_window_scene(0) |
45 #define MAIN_SCENE asc_window_scene(1) |
45 #define MAIN_SCENE asc_window_scene(1) |
|
46 |
|
47 #define HUD_WIDTH 400 |
46 |
48 |
47 enum MoveDirection { |
49 enum MoveDirection { |
48 MOVE_UP, |
50 MOVE_UP, |
49 MOVE_LEFT, |
51 MOVE_LEFT, |
50 MOVE_DOWN, |
52 MOVE_DOWN, |
64 asc_vec2i new_position; |
66 asc_vec2i new_position; |
65 bool reset_position; |
67 bool reset_position; |
66 } Player; |
68 } Player; |
67 |
69 |
68 static const unsigned game_field_size = 16; |
70 static const unsigned game_field_size = 16; |
69 static const unsigned game_field_tile_size = 32; |
71 static const unsigned game_field_tile_size = 64; |
70 |
72 |
71 static void globals_init(void) { |
73 static void globals_init(void) { |
72 asc_transform_identity(rotations[MOVE_UP]); |
74 asc_transform_identity(rotations[MOVE_UP]); |
73 asc_transform_roll(rotations[MOVE_LEFT], asc_rad(-90)); |
75 asc_transform_roll(rotations[MOVE_LEFT], asc_rad(-90)); |
74 asc_transform_roll(rotations[MOVE_RIGHT], asc_rad(90)); |
76 asc_transform_roll(rotations[MOVE_RIGHT], asc_rad(90)); |
83 asc_texture_destroy(tex2d, TEX2D_COUNT); |
85 asc_texture_destroy(tex2d, TEX2D_COUNT); |
84 } |
86 } |
85 |
87 |
86 static void textures_init(void) { |
88 static void textures_init(void) { |
87 asc_texture_init_2d(tex2d, TEX2D_COUNT); |
89 asc_texture_init_2d(tex2d, TEX2D_COUNT); |
88 asc_texture_from_file(TEXTURE_SHIP, "ship.png"); |
90 asc_texture_from_file(TEXTURE_PLAYER, "player.png"); |
89 asc_texture_from_file(TEXTURE_BACKDROP, "backdrop.png"); |
91 asc_texture_from_file(TEXTURE_BACKDROP, "backdrop.png"); |
90 asc_gl_context_add_cleanup_func(asc_active_glctx, textures_destroy); |
92 asc_gl_context_add_cleanup_func(asc_active_glctx, textures_destroy); |
91 } |
93 } |
92 |
94 |
93 static void backdrop_scale(AscBehavior *behavior) { |
95 static void backdrop_scale(AscBehavior *behavior) { |
98 asc_sprite_set_size(sprite, window_size); |
100 asc_sprite_set_size(sprite, window_size); |
99 } |
101 } |
100 } |
102 } |
101 |
103 |
102 static void backdrop_create(void) { |
104 static void backdrop_create(void) { |
|
105 const float scale = asc_active_window->ui_scale; |
103 AscSceneNode *node = asc_sprite( |
106 AscSceneNode *node = asc_sprite( |
104 .texture = TEXTURE_BACKDROP, |
107 .texture = TEXTURE_BACKDROP, |
105 .texture_scale_mode = ASC_TEXTURE_SCALE_REPEAT, |
108 .texture_scale_mode = ASC_TEXTURE_SCALE_REPEAT, |
|
109 .texture_scale_x = scale, .texture_scale_y = scale, |
106 ); |
110 ); |
107 asc_behavior_add(node, .func = backdrop_scale); |
111 asc_behavior_add(node, .func = backdrop_scale); |
108 asc_scene_add_node(BACKDROP_SCENE, node); |
112 asc_scene_add_node(BACKDROP_SCENE, node); |
109 } |
113 } |
110 |
114 |
212 } |
216 } |
213 |
217 |
214 static Player *player_create(void) { |
218 static Player *player_create(void) { |
215 AscSceneNode *sprite = asc_sprite( |
219 AscSceneNode *sprite = asc_sprite( |
216 .name = "Player", |
220 .name = "Player", |
217 .texture = TEXTURE_SHIP, |
221 .texture = TEXTURE_PLAYER, |
218 .width = game_field_tile_size, |
222 .width = game_field_tile_size, |
219 .height = game_field_tile_size, |
223 .height = game_field_tile_size, |
220 .origin_x = game_field_tile_size / 2, |
224 .origin_x = game_field_tile_size / 2, |
221 .origin_y = game_field_tile_size / 2, |
225 .origin_y = game_field_tile_size / 2, |
222 ); |
226 ); |
248 } |
252 } |
249 |
253 |
250 static asc_rect main_scene_viewport_update(asc_vec2u window_size) { |
254 static asc_rect main_scene_viewport_update(asc_vec2u window_size) { |
251 |
255 |
252 // margins |
256 // margins |
253 const unsigned margin = 10; |
257 const unsigned margin = 16; |
254 |
258 |
255 // space for score, power-ups, etc. |
259 // space for score, power-ups, etc. |
256 const unsigned left_area = (unsigned) (asc_active_window->ui_scale*200); |
260 const unsigned left_area = (unsigned) (asc_active_window->ui_scale*HUD_WIDTH); |
257 |
261 |
258 // calculate how many pixels need to be removed from width and height |
262 // calculate how many pixels need to be removed from width and height |
259 const unsigned rw = 2*margin + left_area; |
263 const unsigned rw = 2*margin + left_area; |
260 const unsigned rh = 2*margin; |
264 const unsigned rh = 2*margin; |
261 |
265 |
298 // create the window |
302 // create the window |
299 AscWindowSettings settings; |
303 AscWindowSettings settings; |
300 asc_window_settings_init_defaults(&settings); |
304 asc_window_settings_init_defaults(&settings); |
301 asc_window_initialize(0, &settings); |
305 asc_window_initialize(0, &settings); |
302 asc_window_set_title(0, "Snake"); |
306 asc_window_set_title(0, "Snake"); |
303 float ui_scale = asc_ui_scale_auto(); |
307 asc_window_set_size(0, asc_vec2_ftou( |
304 asc_window_set_size(0, ASC_VEC2U(700+ui_scale*200, 700)); |
308 asc_vec2f_scale(ASC_VEC2F(1024+HUD_WIDTH, 1024), asc_ui_scale_auto()))); |
|
309 asc_window_center(0); |
305 |
310 |
306 // load textures |
311 // load textures |
307 textures_init(); |
312 textures_init(); |
308 |
313 |
309 // initialize the scenes |
314 // initialize the scenes |