test_s2d_scene.c (5428B)
1 /* Copyright (C) 2016-2021, 2023 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "s2d.h" 17 #include "test_s2d_utils.h" 18 19 #include <rsys/float2.h> 20 21 int 22 main(int argc, char** argv) 23 { 24 struct mem_allocator allocator; 25 struct s2d_vertex_data attrib; 26 struct s2d_device* dev; 27 struct s2d_device* dev2; 28 struct s2d_scene* scn; 29 struct s2d_scene_view* scnview; 30 struct s2d_shape* shape; 31 struct s2d_primitive prim; 32 size_t nprims; 33 size_t i; 34 float lower[2], upper[2]; 35 float tmp[2]; 36 float length; 37 float area; 38 (void)argc, (void)argv; 39 40 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 41 42 CHK(s2d_device_create(NULL, &allocator, 1, &dev) == RES_OK); 43 44 CHK(s2d_scene_create(NULL, NULL) == RES_BAD_ARG); 45 CHK(s2d_scene_create(dev, NULL) == RES_BAD_ARG); 46 CHK(s2d_scene_create(NULL, &scn) == RES_BAD_ARG); 47 CHK(s2d_scene_create(dev, &scn) == RES_OK); 48 49 CHK(s2d_scene_ref_get(NULL) == RES_BAD_ARG); 50 CHK(s2d_scene_ref_get(scn) == RES_OK); 51 CHK(s2d_scene_ref_put(NULL) == RES_BAD_ARG); 52 CHK(s2d_scene_ref_put(scn) == RES_OK); 53 CHK(s2d_scene_ref_put(scn) == RES_OK); 54 55 CHK(s2d_scene_create(dev, &scn) == RES_OK); 56 CHK(s2d_shape_create_line_segments(dev, &shape) == RES_OK); 57 58 CHK(s2d_scene_attach_shape(NULL, NULL) == RES_BAD_ARG); 59 CHK(s2d_scene_attach_shape(scn, NULL) == RES_BAD_ARG); 60 CHK(s2d_scene_attach_shape(NULL, shape) == RES_BAD_ARG); 61 CHK(s2d_scene_attach_shape(scn, shape) == RES_OK); 62 CHK(s2d_scene_attach_shape(scn, shape) == RES_OK); 63 64 CHK(s2d_scene_detach_shape(NULL, NULL) == RES_BAD_ARG); 65 CHK(s2d_scene_detach_shape(scn, NULL) == RES_BAD_ARG); 66 CHK(s2d_scene_detach_shape(NULL, shape) == RES_BAD_ARG); 67 CHK(s2d_scene_detach_shape(scn, shape) == RES_OK); 68 CHK(s2d_scene_detach_shape(scn, shape) == RES_BAD_ARG); 69 70 CHK(s2d_scene_clear(NULL) == RES_BAD_ARG); 71 CHK(s2d_scene_clear(scn) == RES_OK); 72 CHK(s2d_scene_clear(scn) == RES_OK); 73 CHK(s2d_scene_attach_shape(scn, shape) == RES_OK); 74 CHK(s2d_scene_clear(scn) == RES_OK); 75 76 CHK(s2d_scene_view_create(scn, S2D_TRACE, &scnview) == RES_OK); 77 CHK(s2d_scene_view_primitives_count(scnview, &nprims) == RES_OK); 78 CHK(nprims == 0); 79 CHK(s2d_scene_view_ref_put(scnview) == RES_OK); 80 81 CHK(s2d_scene_attach_shape(scn, shape) == RES_OK); 82 CHK(s2d_scene_detach_shape(scn, shape) == RES_OK); 83 84 attrib.type = S2D_FLOAT2; 85 attrib.usage = S2D_POSITION; 86 attrib.get = line_segments_get_position; 87 CHK(s2d_line_segments_setup_indexed_vertices 88 (shape, square_nsegs, line_segments_get_ids, square_nverts, &attrib, 1, 89 (void*)&square_desc) == RES_OK); 90 91 CHK(s2d_scene_attach_shape(scn, shape) == RES_OK); 92 CHK(s2d_scene_view_create(scn, S2D_GET_PRIMITIVE, &scnview) == RES_OK); 93 CHK(s2d_scene_view_primitives_count(scnview, &nprims) == RES_OK); 94 CHK(nprims == 4); 95 96 FOR_EACH(i, 0, nprims) { 97 struct s2d_attrib attr; 98 99 CHK(s2d_scene_view_get_primitive(scnview, (unsigned)i, &prim) == RES_OK); 100 CHK(s2d_primitive_get_attrib(&prim, S2D_GEOMETRY_NORMAL, 0, &attr) == RES_OK); 101 f2_normalize(attr.value, attr.value); 102 switch(i) { 103 case 0: f2_eq_eps(attr.value, f2(tmp, 0.f, 1.f), 1.e-6f); break; 104 case 1: f2_eq_eps(attr.value, f2(tmp, 1.f, 0.f), 1.e-6f); break; 105 case 2: f2_eq_eps(attr.value, f2(tmp, 0.f,-1.f), 1.e-6f); break; 106 case 3: f2_eq_eps(attr.value, f2(tmp,-1.f, 0.f), 1.e-6f); break; 107 default: FATAL("Unreachable code.\n"); 108 } 109 } 110 111 CHK(s2d_scene_view_compute_contour_length(scnview, &length) == RES_OK); 112 CHK(eq_epsf(length, 8.f, 1.e-6f)); 113 114 CHK(s2d_scene_view_compute_area(scnview, &area) == RES_OK); 115 CHK(eq_epsf(area, 4.f, 1.e-6f)); 116 117 CHK(s2d_scene_view_get_aabb(scnview, lower, upper) == RES_OK); 118 CHK(f2_eq_eps(lower, f2(tmp, 9.f, 9.f), 1.e-6f)); 119 CHK(f2_eq_eps(upper, f2(tmp, 11.f, 11.f), 1.e-6f)); 120 121 CHK(s2d_scene_view_ref_put(scnview) == RES_OK); 122 123 CHK(s2d_scene_clear(scn) == RES_OK); 124 CHK(s2d_scene_view_create(scn, S2D_SAMPLE, &scnview) == RES_OK); 125 CHK(s2d_scene_view_compute_contour_length(scnview, &length) == RES_OK); 126 CHK(length == 0.f); 127 CHK(s2d_scene_view_compute_area(scnview, &area) == RES_OK); 128 CHK(area == 0.f); 129 CHK(s2d_scene_view_get_aabb(scnview, lower, upper) == RES_OK); 130 CHK(lower[0] > upper[0]); 131 CHK(lower[1] > upper[1]); 132 133 CHK(s2d_scene_view_ref_put(scnview) == RES_OK); 134 135 CHK(s2d_scene_get_device(NULL, NULL) == RES_BAD_ARG); 136 CHK(s2d_scene_get_device(scn, NULL) == RES_BAD_ARG); 137 CHK(s2d_scene_get_device(NULL, &dev2) == RES_BAD_ARG); 138 CHK(s2d_scene_get_device(scn, &dev2) == RES_OK); 139 CHK(dev2 == dev); 140 141 CHK(s2d_shape_ref_put(shape) == RES_OK); 142 CHK(s2d_scene_ref_put(scn) == RES_OK); 143 CHK(s2d_device_ref_put(dev) == RES_OK); 144 145 check_memory_allocator(&allocator); 146 mem_shutdown_proxy_allocator(&allocator); 147 CHK(mem_allocated_size() == 0); 148 return 0; 149 } 150