| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program 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 | * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "pstypes.h" |
| 20 | |
| 21 | #include "special_face.h" |
| 22 | #include "mono.h" |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include "mem.h" |
| 26 | |
| 27 | int Num_of_special_faces = 0; |
| 28 | special_face SpecialFaces[MAX_SPECIAL_FACES]; |
| 29 | |
| 30 | static uint16_t Free_special_face_list[MAX_SPECIAL_FACES]; |
| 31 | |
| 32 | // Sets all the special faces to unused |
| 33 | void InitSpecialFaces() { |
| 34 | int i; |
| 35 | |
| 36 | for (i = 0; i < MAX_SPECIAL_FACES; i++) { |
| 37 | SpecialFaces[i].used = 0; |
| 38 | SpecialFaces[i].vertnorms = NULL; |
| 39 | SpecialFaces[i].spec_instance = NULL; |
| 40 | Free_special_face_list[i] = i; |
| 41 | } |
| 42 | } |
| 43 | // Returns an index into the special faces array |
| 44 | int AllocSpecialFace(int type, int num, bool vertnorms, int num_vertnorms) { |
| 45 | int n; |
| 46 | |
| 47 | if (Num_of_special_faces >= MAX_SPECIAL_FACES) { |
| 48 | Int3(); // Get Jason, ran out of special faces! |
| 49 | return BAD_SPECIAL_FACE_INDEX; |
| 50 | } |
| 51 | |
| 52 | n = Free_special_face_list[Num_of_special_faces++]; |
| 53 | ASSERT(SpecialFaces[n].used == 0); |
| 54 | |
| 55 | ASSERT(n >= 0 && n < MAX_SPECIAL_FACES); |
| 56 | |
| 57 | memset(&SpecialFaces[n], 0, sizeof(special_face)); |
| 58 | |
| 59 | SpecialFaces[n].spec_instance = (specular_instance *)mem_malloc(num * sizeof(specular_instance)); |
| 60 | ASSERT(SpecialFaces[n].spec_instance); |
| 61 | |
| 62 | SpecialFaces[n].type = type; |
| 63 | SpecialFaces[n].num = num; |
| 64 | SpecialFaces[n].flags = 0; |
| 65 | SpecialFaces[n].used = 1; |
| 66 | |
| 67 | if (vertnorms) { |
| 68 | SpecialFaces[n].vertnorms = (vector *)mem_malloc(num_vertnorms * sizeof(vector)); |
| 69 | ASSERT(SpecialFaces[n].vertnorms); |
| 70 | SpecialFaces[n].flags |= SFF_SPEC_SMOOTH; |
| 71 | } |
| 72 | |
| 73 | return n; |
| 74 | } |
| 75 | |
| 76 | // Given a handle, frees the special face |
| 77 | void FreeSpecialFace(int handle) { |
| 78 | ASSERT(handle >= 0 && handle <= MAX_SPECIAL_FACES); |
| 79 | |
| 80 | if (SpecialFaces[handle].used < 1) |
| 81 | return; |
| 82 | |
| 83 | SpecialFaces[handle].used--; |
| 84 | |
| 85 | if (SpecialFaces[handle].used == 0) { |
| 86 | Free_special_face_list[--Num_of_special_faces] = handle; |
| 87 | if (SpecialFaces[handle].spec_instance) { |
| 88 | mem_free(SpecialFaces[handle].spec_instance); |
| 89 | SpecialFaces[handle].spec_instance = NULL; |
| 90 | } |
| 91 | |
| 92 | if (SpecialFaces[handle].vertnorms) { |
| 93 | mem_free(SpecialFaces[handle].vertnorms); |
| 94 | SpecialFaces[handle].vertnorms = NULL; |
| 95 | } |
| 96 | } |
| 97 | } |