test/snake/snake.c

changeset 101
febf3dc10011
parent 100
5231da78831e
equal deleted inserted replaced
100:5231da78831e 101:febf3dc10011
30 30
31 #include <cx/printf.h> 31 #include <cx/printf.h>
32 32
33 enum Textures2d { 33 enum Textures2d {
34 TEX_SHIP = 0, 34 TEX_SHIP = 0,
35 TEX_BACKDROP,
35 TEX2D_COUNT 36 TEX2D_COUNT
36 }; 37 };
37 static AscTexture tex2d[TEX2D_COUNT]; 38 static AscTexture tex2d[TEX2D_COUNT];
38 #define TEXTURE_SHIP &tex2d[TEX_SHIP] 39 #define TEXTURE_SHIP &tex2d[TEX_SHIP]
39 40 #define TEXTURE_BACKDROP &tex2d[TEX_BACKDROP]
40 #define MAIN_SCENE asc_window_scene(0) 41
42 #define BACKDROP_SCENE asc_window_scene(0)
43 #define MAIN_SCENE asc_window_scene(1)
41 44
42 static void destroy_textures(void *dummy) { 45 static void destroy_textures(void *dummy) {
43 asc_texture_destroy(tex2d, TEX2D_COUNT); 46 asc_texture_destroy(tex2d, TEX2D_COUNT);
44 } 47 }
45 48
46 static void init_textures(void) { 49 static void init_textures(void) {
47 asc_texture_init_2d(tex2d, TEX2D_COUNT); 50 asc_texture_init_2d(tex2d, TEX2D_COUNT);
48 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");
49 cxMempoolRegister(asc_active_glctx_mpool, tex2d, destroy_textures); 53 cxMempoolRegister(asc_active_glctx_mpool, tex2d, destroy_textures);
50 } 54 }
51 55
52 static void update_fps_counter(AscSceneNode *node) { 56 static void update_fps_counter(AscSceneNode *node) {
53 static uint64_t last_fps = 0; 57 static uint64_t last_fps = 0;
74 bottom_right.y - scale.height - 10 78 bottom_right.y - scale.height - 10
75 ); 79 );
76 } 80 }
77 } 81 }
78 82
83 static void scale_backdrop(AscSceneNode *node) {
84 // scale the backdrop to the size of the window
85 if (asc_active_window->resized) {
86 asc_vec2i window_size = asc_active_window->dimensions;
87 asc_set_scale2d(node, window_size.width, window_size.height);
88 // TODO: implement texture repetition
89 }
90 }
91
92 static void create_backdrop(void) {
93 AscSceneNode *node = asc_sprite(.texture = TEXTURE_BACKDROP);
94 asc_scene_add_behavior(node, scale_backdrop);
95 asc_scene_add_node(BACKDROP_SCENE, node);
96 }
97
79 static void create_fps_counter(void) { 98 static void create_fps_counter(void) {
80 asc_font(ASC_FONT_REGULAR, 12); 99 asc_font(ASC_FONT_REGULAR, 12);
81 asc_ink_rgba(128, 128, 128, 196); 100 asc_ink_rgba(224, 224, 224, 196);
82 AscSceneNode *node = asc_text(); 101 AscSceneNode *node = asc_text();
83 asc_scene_add_behavior(node, update_fps_counter); 102 asc_scene_add_behavior(node, update_fps_counter);
84 asc_add_ui_node(node); 103 asc_add_ui_node(node);
85 } 104 }
86 105
100 .x = 250, 119 .x = 250,
101 .y = 300, 120 .y = 300,
102 .width = 64, 121 .width = 64,
103 .height = 64 122 .height = 64
104 ); 123 );
105
106 asc_scene_add_node(MAIN_SCENE, sprite); 124 asc_scene_add_node(MAIN_SCENE, sprite);
107 125
108 // TODO: return something 126 // TODO: return something
109 } 127 }
110 128
147 AscWindowSettings settings; 165 AscWindowSettings settings;
148 asc_window_settings_init_defaults(&settings); 166 asc_window_settings_init_defaults(&settings);
149 settings.title = "Snake"; 167 settings.title = "Snake";
150 asc_window_initialize(0, &settings); 168 asc_window_initialize(0, &settings);
151 169
152 // initialize the main scene (a 500x500 game field) 170 // load textures
171 init_textures();
172
173 // initialize the scenes
174 const int game_field_size = 500;
175 asc_scene_init(BACKDROP_SCENE, (AscCameraParams) {
176 .type = ASC_CAMERA_ORTHO,
177 .projection_update_func = asc_camera_ortho_update_size
178 });
153 asc_scene_init(MAIN_SCENE, (AscCameraParams) { 179 asc_scene_init(MAIN_SCENE, (AscCameraParams) {
154 .type = ASC_CAMERA_ORTHO, 180 .type = ASC_CAMERA_ORTHO,
155 .ortho.rect = (asc_recti){0, 0, 500, 500}, 181 .ortho.rect = (asc_recti){0, 0, game_field_size, game_field_size},
156 .viewport_update_func = update_viewport_for_window_resize 182 .viewport_update_func = update_viewport_for_window_resize
157 }); 183 });
158 184
159 // load textures 185 // backdrop for letterbox/pillarbox
160 init_textures(); 186 create_backdrop();
161 187
162 // create UI elements 188 // create UI elements
163 create_fps_counter(); 189 create_fps_counter();
164 create_score_counter(); 190 create_score_counter();
165 191

mercurial