28 #include "ascension/error.h" |
28 #include "ascension/error.h" |
29 #include "ascension/context.h" |
29 #include "ascension/context.h" |
30 #include "ascension/scene.h" |
30 #include "ascension/scene.h" |
31 #include "ascension/behavior.h" |
31 #include "ascension/behavior.h" |
32 #include "ascension/shader.h" |
32 #include "ascension/shader.h" |
|
33 #include "ascension/util.h" |
33 |
34 |
34 #include <cx/tree.h> |
35 #include <cx/tree.h> |
35 #include <cx/array_list.h> |
36 #include <cx/array_list.h> |
36 |
37 |
37 #include <GL/glew.h> |
38 #include <GL/glew.h> |
38 |
39 |
39 #include <assert.h> |
40 #include <assert.h> |
40 |
41 |
41 void asc_scene_init_(AscScene *scene, struct asc_camera_init_args args) { |
42 void asc_scene_init_(AscScene *scene, const char *name, struct asc_camera_init_args args) { |
42 if (scene->root != NULL) { |
43 assert(scene->root == NULL); |
43 asc_wprintf("Scene %"PRIxPTR" is already initialized - initialization skipped.", (uintptr_t) scene); |
|
44 return; |
|
45 } |
|
46 asc_camera_init_(&scene->camera, args); |
44 asc_camera_init_(&scene->camera, args); |
47 scene->root = asc_scene_node_empty(); |
45 scene->root = asc_scene_node_empty(); |
48 for (unsigned i = 0 ; i < ASC_RENDER_GROUP_COUNT ; i++) { |
46 for (unsigned i = 0 ; i < ASC_RENDER_GROUP_COUNT ; i++) { |
49 scene->internal.render_groups[i] = cxArrayListCreateSimple(CX_STORE_POINTERS, 32); |
47 scene->internal.render_groups[i] = cxArrayListCreateSimple(CX_STORE_POINTERS, 32); |
50 } |
48 } |
51 |
49 if (name == NULL) { |
52 asc_dprintf("Initialized scene %"PRIxPTR, (uintptr_t) scene); |
50 scene->name = asc_util_gen_name(scene); |
|
51 } else { |
|
52 scene->name.ptr = strdup(name); |
|
53 scene->name.length = strlen(name); |
|
54 } |
|
55 asc_dprintf("Initialized scene %"CX_PRIstr, CX_SFMT(scene->name)); |
53 } |
56 } |
54 |
57 |
55 void asc_scene_destroy(AscScene *scene) { |
58 void asc_scene_destroy(AscScene *scene) { |
56 if (scene->root == NULL) return; |
59 if (scene == NULL || scene->root == NULL) return; |
57 for (unsigned i = 0 ; i < ASC_RENDER_GROUP_COUNT ; i++) { |
60 for (unsigned i = 0 ; i < ASC_RENDER_GROUP_COUNT ; i++) { |
58 cxListFree(scene->internal.render_groups[i]); |
61 cxListFree(scene->internal.render_groups[i]); |
59 scene->internal.render_groups[i] = NULL; |
62 scene->internal.render_groups[i] = NULL; |
60 } |
63 } |
61 // TODO: add names to scenes |
|
62 asc_dprintf("Destroyed scene %"PRIxPTR, (uintptr_t) scene); |
|
63 asc_scene_node_free(scene->root); |
64 asc_scene_node_free(scene->root); |
|
65 scene->root = NULL; |
|
66 asc_dprintf("Destroyed scene %"CX_PRIstr, CX_SFMT(scene->name)); |
|
67 cx_strfree(&scene->name); |
64 } |
68 } |
65 |
69 |
66 void asc_scene_execute_behaviors(AscScene *scene) { |
70 void asc_scene_execute_behaviors(AscScene *scene) { |
67 CxTreeVisitor iter = cx_tree_visitor(scene->root, |
71 CxTreeVisitor iter = cx_tree_visitor(scene->root, |
68 offsetof(AscSceneNode, children), |
72 offsetof(AscSceneNode, children), |