commit 82efe24bb04436872fd72cde3811fd46c5f961f0
parent 837098cb30ce4afcc2ad8eeb2d876001f485453f
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 22 Jun 2016 11:24:20 +0200
Implement and test the s2d_scene_<attach|detach>_shape functions
Diffstat:
2 files changed, 81 insertions(+), 0 deletions(-)
diff --git a/src/s2d_scene.c b/src/s2d_scene.c
@@ -166,6 +166,62 @@ s2d_scene_ref_put(struct s2d_scene* scn)
}
res_T
+s2d_scene_attach_shape(struct s2d_scene* scn, struct s2d_shape* shape)
+{
+ if(!scn || !shape)
+ return RES_BAD_ARG;
+ if(!is_list_empty(&shape->scene_attachment)) {
+ if(scn->dev->verbose) {
+ logger_print(scn->dev->logger, LOG_ERROR,
+ "%s: Cannot attach a shape that is already attached to a scene.\n",
+ __FUNCTION__);
+ }
+ return RES_BAD_ARG;
+ }
+ list_add_tail(&scn->shapes, &shape->scene_attachment);
+ S2D(shape_ref_get(shape));
+ return RES_OK;
+}
+
+res_T
+s2d_scene_detach_shape(struct s2d_scene* scn, struct s2d_shape* shape)
+{
+ char is_attached;
+ if(!scn || !shape) return RES_BAD_ARG;
+ if(scn->session_mask != 0) {
+ if(scn->dev->verbose) {
+ logger_print(scn->dev->logger, LOG_ERROR,
+ "%s: Cannot detach a shape while a session is active onto the scene.\n",
+ __FUNCTION__);
+ }
+ return RES_BAD_OP;
+ }
+ if(!(S2D(shape_is_attached(shape, &is_attached)), is_attached)) {
+ if(scn->dev->verbose) {
+ logger_print(scn->dev->logger, LOG_ERROR,
+ "%s: The shape to detach is not attached to any scene.\n",
+ __FUNCTION__);
+ }
+ return RES_BAD_ARG;
+ }
+#ifndef NDEBUG
+ { /* Check that the shape is attached to `scn' */
+ struct list_node* node;
+ char is_found = 0;
+ LIST_FOR_EACH(node, &scn->shapes) {
+ if(node == &shape->scene_attachment) {
+ is_found = 1;
+ break;
+ }
+ }
+ ASSERT(is_found);
+ }
+#endif
+ scene_detach_shape(scn, shape);
+ return RES_OK;
+}
+
+res_T
s2d_scene_clear(struct s2d_scene* scn)
{
struct list_node* node, *tmp;
diff --git a/src/test_s2d_scene.c b/src/test_s2d_scene.c
@@ -35,6 +35,7 @@ main(int argc, char** argv)
struct mem_allocator allocator;
struct s2d_device* dev;
struct s2d_scene* scn;
+ struct s2d_shape* shape;
(void)argc, (void)argv;
mem_init_proxy_allocator(&allocator, &mem_default_allocator);
@@ -52,6 +53,29 @@ main(int argc, char** argv)
CHECK(s2d_scene_ref_put(scn), RES_OK);
CHECK(s2d_scene_ref_put(scn), RES_OK);
+ CHECK(s2d_scene_create(dev, &scn), RES_OK);
+ CHECK(s2d_shape_create_line_segments(dev, &shape), RES_OK);
+
+ CHECK(s2d_scene_attach_shape(NULL, NULL), RES_BAD_ARG);
+ CHECK(s2d_scene_attach_shape(scn, NULL), RES_BAD_ARG);
+ CHECK(s2d_scene_attach_shape(NULL, shape), RES_BAD_ARG);
+ CHECK(s2d_scene_attach_shape(scn, shape), RES_OK);
+ CHECK(s2d_scene_attach_shape(scn, shape), RES_BAD_ARG);
+
+ CHECK(s2d_scene_detach_shape(NULL, NULL), RES_BAD_ARG);
+ CHECK(s2d_scene_detach_shape(scn, NULL), RES_BAD_ARG);
+ CHECK(s2d_scene_detach_shape(NULL, shape), RES_BAD_ARG);
+ CHECK(s2d_scene_detach_shape(scn, shape), RES_OK);
+ CHECK(s2d_scene_detach_shape(scn, shape), RES_BAD_ARG);
+
+ CHECK(s2d_scene_clear(NULL), RES_BAD_ARG);
+ CHECK(s2d_scene_clear(scn), RES_OK);
+ CHECK(s2d_scene_clear(scn), RES_OK);
+ CHECK(s2d_scene_attach_shape(scn, shape), RES_OK);
+ CHECK(s2d_scene_clear(scn), RES_OK);
+
+ CHECK(s2d_shape_ref_put(shape), RES_OK);
+ CHECK(s2d_scene_ref_put(scn), RES_OK);
CHECK(s2d_device_ref_put(dev), RES_OK);
check_memory_allocator(&allocator);
@@ -59,3 +83,4 @@ main(int argc, char** argv)
CHECK(mem_allocated_size(), 0);
return 0;
}
+