--- a/test/snake/snake.c Mon Apr 28 21:13:01 2025 +0200 +++ b/test/snake/snake.c Tue Apr 29 21:51:29 2025 +0200 @@ -32,12 +32,15 @@ enum Textures2d { TEX_SHIP = 0, + TEX_BACKDROP, TEX2D_COUNT }; static AscTexture tex2d[TEX2D_COUNT]; #define TEXTURE_SHIP &tex2d[TEX_SHIP] +#define TEXTURE_BACKDROP &tex2d[TEX_BACKDROP] -#define MAIN_SCENE asc_window_scene(0) +#define BACKDROP_SCENE asc_window_scene(0) +#define MAIN_SCENE asc_window_scene(1) static void destroy_textures(void *dummy) { asc_texture_destroy(tex2d, TEX2D_COUNT); @@ -46,6 +49,7 @@ static void init_textures(void) { asc_texture_init_2d(tex2d, TEX2D_COUNT); asc_texture_from_file(TEXTURE_SHIP, "ship.png"); + asc_texture_from_file(TEXTURE_BACKDROP, "backdrop.png"); cxMempoolRegister(asc_active_glctx_mpool, tex2d, destroy_textures); } @@ -76,9 +80,24 @@ } } +static void scale_backdrop(AscSceneNode *node) { + // scale the backdrop to the size of the window + if (asc_active_window->resized) { + asc_vec2i window_size = asc_active_window->dimensions; + asc_set_scale2d(node, window_size.width, window_size.height); + // TODO: implement texture repetition + } +} + +static void create_backdrop(void) { + AscSceneNode *node = asc_sprite(.texture = TEXTURE_BACKDROP); + asc_scene_add_behavior(node, scale_backdrop); + asc_scene_add_node(BACKDROP_SCENE, node); +} + static void create_fps_counter(void) { asc_font(ASC_FONT_REGULAR, 12); - asc_ink_rgba(128, 128, 128, 196); + asc_ink_rgba(224, 224, 224, 196); AscSceneNode *node = asc_text(); asc_scene_add_behavior(node, update_fps_counter); asc_add_ui_node(node); @@ -102,7 +121,6 @@ .width = 64, .height = 64 ); - asc_scene_add_node(MAIN_SCENE, sprite); // TODO: return something @@ -149,15 +167,23 @@ settings.title = "Snake"; asc_window_initialize(0, &settings); - // initialize the main scene (a 500x500 game field) + // load textures + init_textures(); + + // initialize the scenes + const int game_field_size = 500; + asc_scene_init(BACKDROP_SCENE, (AscCameraParams) { + .type = ASC_CAMERA_ORTHO, + .projection_update_func = asc_camera_ortho_update_size + }); asc_scene_init(MAIN_SCENE, (AscCameraParams) { .type = ASC_CAMERA_ORTHO, - .ortho.rect = (asc_recti){0, 0, 500, 500}, + .ortho.rect = (asc_recti){0, 0, game_field_size, game_field_size}, .viewport_update_func = update_viewport_for_window_resize }); - // load textures - init_textures(); + // backdrop for letterbox/pillarbox + create_backdrop(); // create UI elements create_fps_counter();