Tue, 22 Jul 2025 21:38:02 +0200
resolve several minor TODOs
src/ascension/mesh.h | file | annotate | diff | comparison | revisions | |
src/ascension/scene.h | file | annotate | diff | comparison | revisions | |
src/mesh.c | file | annotate | diff | comparison | revisions | |
src/scene.c | file | annotate | diff | comparison | revisions | |
src/window.c | file | annotate | diff | comparison | revisions | |
test/snake/snake.c | file | annotate | diff | comparison | revisions |
--- a/src/ascension/mesh.h Tue Jul 22 20:57:13 2025 +0200 +++ b/src/ascension/mesh.h Tue Jul 22 21:38:02 2025 +0200 @@ -97,8 +97,9 @@ * * @param mesh pointer to a mesh or an array of meshes * @param count the number of meshes (maximum 32) + * @returns zero on success and non-zero on failure */ -void asc_mesh_allocate_buffers(AscMesh *mesh, unsigned count); +int asc_mesh_allocate_buffers(AscMesh *mesh, unsigned count); /** * Frees VBO and VAO for one or more meshes.
--- a/src/ascension/scene.h Tue Jul 22 20:57:13 2025 +0200 +++ b/src/ascension/scene.h Tue Jul 22 21:38:02 2025 +0200 @@ -32,10 +32,12 @@ #include "camera.h" #include <cx/list.h> +#include <cx/string.h> typedef struct asc_scene_s { AscCamera camera; AscSceneNode *root; + cxmutstr name; struct { CxList *render_groups[ASC_RENDER_GROUP_COUNT]; } internal; @@ -45,17 +47,19 @@ * Initializes a scene graph. * * @param scene the scene graph + * @param name optional name for the scene * @param camera_params initial camera parameters */ -void asc_scene_init_(AscScene *scene, struct asc_camera_init_args camera_params); +void asc_scene_init_(AscScene *scene, const char *name, struct asc_camera_init_args camera_params); /** * Initializes a scene graph. * * @param scene the scene graph + * @param name optional name for the scene * @param ... initial camera parameters */ -#define asc_scene_init(scene, ...) asc_scene_init_(scene, (struct asc_camera_init_args){__VA_ARGS__}) +#define asc_scene_init(scene, name, ...) asc_scene_init_(scene, name, (struct asc_camera_init_args){__VA_ARGS__}) /** * Destroys a scene graph.
--- a/src/mesh.c Tue Jul 22 20:57:13 2025 +0200 +++ b/src/mesh.c Tue Jul 22 21:38:02 2025 +0200 @@ -32,7 +32,7 @@ #include <GL/glew.h> -void asc_mesh_allocate_buffers(AscMesh *mesh, unsigned count) { +int asc_mesh_allocate_buffers(AscMesh *mesh, unsigned count) { asc_dprintf("Allocate mesh buffers for %u meshes.", count); GLuint buffers[count]; GLuint arrays[count]; @@ -42,7 +42,7 @@ mesh[i].vbo = buffers[i]; mesh[i].vao = arrays[i]; } - asc_error_catch_all_gl(); + return asc_error_catch_all_gl(); } void asc_mesh_free_buffers(AscMesh *mesh, unsigned count) { @@ -79,8 +79,18 @@ } void asc_mesh_init_plane_2d(AscMesh *mesh, struct asc_mesh_init_plane_2d_args args) { - if (mesh->vbo == 0) { - asc_mesh_allocate_buffers(mesh, 1); + if (mesh->vao == 0) { + if (asc_mesh_allocate_buffers(mesh, 1)) return; + // bind the buffer and configure the vertex attributes + glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); + glBindVertexArray(mesh->vao); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, pos)); + glEnableVertexAttribArray(0); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, uv)); + glEnableVertexAttribArray(1); + } else { + // only bind the buffer for updating the data + glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); } unsigned required_memory = 4 * sizeof(asc_vertex2d); @@ -121,14 +131,8 @@ // top right data[3].pos = ASC_VEC2F(args.size.x, args.size.y); data[3].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y); - glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); glBufferData(GL_ARRAY_BUFFER, mesh->vtx_data_size, mesh->vtx_data, GL_STATIC_DRAW); - // TODO: this should not be repeated for every adjustment - but it will be moved to the batch renderer anyway - glBindVertexArray(mesh->vao); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, pos)); - glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, uv)); - glEnableVertexAttribArray(1); + // TODO: replace with specific error handling for setting the buffer asc_error_catch_all_gl(); }
--- a/src/scene.c Tue Jul 22 20:57:13 2025 +0200 +++ b/src/scene.c Tue Jul 22 21:38:02 2025 +0200 @@ -30,6 +30,7 @@ #include "ascension/scene.h" #include "ascension/behavior.h" #include "ascension/shader.h" +#include "ascension/util.h" #include <cx/tree.h> #include <cx/array_list.h> @@ -38,29 +39,32 @@ #include <assert.h> -void asc_scene_init_(AscScene *scene, struct asc_camera_init_args args) { - if (scene->root != NULL) { - asc_wprintf("Scene %"PRIxPTR" is already initialized - initialization skipped.", (uintptr_t) scene); - return; - } +void asc_scene_init_(AscScene *scene, const char *name, struct asc_camera_init_args args) { + assert(scene->root == NULL); asc_camera_init_(&scene->camera, args); scene->root = asc_scene_node_empty(); for (unsigned i = 0 ; i < ASC_RENDER_GROUP_COUNT ; i++) { scene->internal.render_groups[i] = cxArrayListCreateSimple(CX_STORE_POINTERS, 32); } - - asc_dprintf("Initialized scene %"PRIxPTR, (uintptr_t) scene); + if (name == NULL) { + scene->name = asc_util_gen_name(scene); + } else { + scene->name.ptr = strdup(name); + scene->name.length = strlen(name); + } + asc_dprintf("Initialized scene %"CX_PRIstr, CX_SFMT(scene->name)); } void asc_scene_destroy(AscScene *scene) { - if (scene->root == NULL) return; + if (scene == NULL || scene->root == NULL) return; for (unsigned i = 0 ; i < ASC_RENDER_GROUP_COUNT ; i++) { cxListFree(scene->internal.render_groups[i]); scene->internal.render_groups[i] = NULL; } - // TODO: add names to scenes - asc_dprintf("Destroyed scene %"PRIxPTR, (uintptr_t) scene); asc_scene_node_free(scene->root); + scene->root = NULL; + asc_dprintf("Destroyed scene %"CX_PRIstr, CX_SFMT(scene->name)); + cx_strfree(&scene->name); } void asc_scene_execute_behaviors(AscScene *scene) {
--- a/src/window.c Tue Jul 22 20:57:13 2025 +0200 +++ b/src/window.c Tue Jul 22 21:38:02 2025 +0200 @@ -81,7 +81,9 @@ window->ui_scale = 1.0f; if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) { - asc_scene_init(&window->ui, + char ui_scene_name[16]; + snprintf(ui_scene_name, sizeof(ui_scene_name), "Window %u UI", index); + asc_scene_init(&window->ui, ui_scene_name, .type = ASC_CAMERA_ORTHO, .ortho.rect = ASC_RECT(0, 0, window->dimensions.width, window->dimensions.height), .projection_update_func = asc_camera_ortho_update_size
--- a/test/snake/snake.c Tue Jul 22 20:57:13 2025 +0200 +++ b/test/snake/snake.c Tue Jul 22 21:38:02 2025 +0200 @@ -274,11 +274,11 @@ init_textures(); // initialize the scenes - asc_scene_init(BACKDROP_SCENE, + asc_scene_init(BACKDROP_SCENE, "backdrop", .type = ASC_CAMERA_ORTHO, .projection_update_func = asc_camera_ortho_update_size ); - asc_scene_init(MAIN_SCENE, + asc_scene_init(MAIN_SCENE, "main", .type = ASC_CAMERA_ORTHO, .ortho.rect = ASC_RECT(0, 0, (game_field_size+1)*game_field_tile_size,