s3d_instance.c (2310B)
1 /* Copyright (C) 2015-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 "s3d.h" 17 #include "s3d_backend.h" 18 #include "s3d_device_c.h" 19 #include "s3d_instance.h" 20 #include "s3d_shape_c.h" 21 #include "s3d_scene_c.h" 22 23 #include <rsys/float33.h> 24 25 /******************************************************************************* 26 * Helper functions 27 ******************************************************************************/ 28 static void 29 instance_release(ref_T* ref) 30 { 31 struct instance* inst; 32 struct s3d_scene* scn; 33 ASSERT(ref); 34 inst = CONTAINER_OF(ref, struct instance, ref); 35 scn = inst->scene; 36 MEM_RM(scn->dev->allocator, inst); 37 S3D(scene_ref_put(scn)); 38 } 39 40 /******************************************************************************* 41 * Local functions 42 ******************************************************************************/ 43 res_T 44 instance_create 45 (struct s3d_scene* scn, 46 struct instance** out_inst) 47 { 48 struct instance* inst = NULL; 49 res_T res = RES_OK; 50 ASSERT(scn && out_inst); 51 52 inst = (struct instance*) 53 MEM_CALLOC(scn->dev->allocator, 1, sizeof(struct instance)); 54 if(!inst) { 55 res = RES_MEM_ERR; 56 goto error; 57 } 58 f33_set_identity(inst->transform); /* rotation */ 59 f3_splat(inst->transform + 9, 0.f); /* Translation */ 60 ref_init(&inst->ref); 61 S3D(scene_ref_get(scn)); 62 inst->scene = scn; 63 exit: 64 *out_inst = inst; 65 return res; 66 error: 67 if(inst) { 68 instance_ref_put(inst); 69 inst = NULL; 70 } 71 goto exit; 72 } 73 74 void 75 instance_ref_get(struct instance* inst) 76 { 77 ASSERT(inst); 78 ref_get(&inst->ref); 79 } 80 81 void 82 instance_ref_put(struct instance* inst) 83 { 84 ASSERT(inst); 85 ref_put(&inst->ref, instance_release); 86 } 87