s3d_instance.c (2337B)
1 /* Copyright (C) 2015-2023, 2026 |Méso|Star> (contact@meso-star.com) 2 * 3 * This file is part of Star-3D. 4 * 5 * Star-3D is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * Star-3D is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with Star-3D. If not, see <http://www.gnu.org/licenses/>. */ 17 18 #include "s3d.h" 19 #include "s3d_backend.h" 20 #include "s3d_device_c.h" 21 #include "s3d_instance.h" 22 #include "s3d_shape_c.h" 23 #include "s3d_scene_c.h" 24 25 #include <rsys/float33.h> 26 27 /******************************************************************************* 28 * Helper functions 29 ******************************************************************************/ 30 static void 31 instance_release(ref_T* ref) 32 { 33 struct instance* inst; 34 struct s3d_scene* scn; 35 ASSERT(ref); 36 inst = CONTAINER_OF(ref, struct instance, ref); 37 scn = inst->scene; 38 MEM_RM(scn->dev->allocator, inst); 39 S3D(scene_ref_put(scn)); 40 } 41 42 /******************************************************************************* 43 * Local functions 44 ******************************************************************************/ 45 res_T 46 instance_create 47 (struct s3d_scene* scn, 48 struct instance** out_inst) 49 { 50 struct instance* inst = NULL; 51 res_T res = RES_OK; 52 ASSERT(scn && out_inst); 53 54 inst = (struct instance*) 55 MEM_CALLOC(scn->dev->allocator, 1, sizeof(struct instance)); 56 if(!inst) { 57 res = RES_MEM_ERR; 58 goto error; 59 } 60 f33_set_identity(inst->transform); /* rotation */ 61 f3_splat(inst->transform + 9, 0.f); /* Translation */ 62 ref_init(&inst->ref); 63 S3D(scene_ref_get(scn)); 64 inst->scene = scn; 65 exit: 66 *out_inst = inst; 67 return res; 68 error: 69 if(inst) { 70 instance_ref_put(inst); 71 inst = NULL; 72 } 73 goto exit; 74 } 75 76 void 77 instance_ref_get(struct instance* inst) 78 { 79 ASSERT(inst); 80 ref_get(&inst->ref); 81 } 82 83 void 84 instance_ref_put(struct instance* inst) 85 { 86 ASSERT(inst); 87 ref_put(&inst->ref, instance_release); 88 } 89