Debian Codegraph @

bitmap/lightmap.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 <stdlib.h>
20 #include <string.h>
21 #include "lightmap.h"
22 #include "pstypes.h"
23 #include "pserror.h"
24 #include "bitmap.h"
25 #include "mono.h"
26 #include "mem.h"
27 #if !defined(POSIX)
28 #include "Macros.h"
29 #endif
30
31 #include <algorithm>
32
33 static int Num_of_lightmaps = 0;
34 static uint16_t Free_lightmap_list[MAX_LIGHTMAPS];
35 bms_lightmap GameLightmaps[MAX_LIGHTMAPS];
36 static int Lightmap_mem_used = 0;
37 // Sets all the lightmaps to unused
38 void lm_InitLightmaps() {
39 int i;
40 for (i = 0; i < MAX_LIGHTMAPS; i++) {
41 GameLightmaps[i].flags = 0;
42 GameLightmaps[i].used = 0;
43 GameLightmaps[i].data = NULL;
44 GameLightmaps[i].cache_slot = -1;
45 Free_lightmap_list[i] = i;
46 }
47 atexit(lm_ShutdownLightmaps);
48 }
49 void lm_ShutdownLightmaps(void) {
50 int i;
51 mprintf(0, "Freeing all lightmap memory.\n");
52 for (i = 0; i < MAX_LIGHTMAPS; i++) {
53 while (GameLightmaps[i].used > 0)
54 lm_FreeLightmap(i);
55 }
56 }
57 // Allocs a lightmap of w x h size
58 // Returns lightmap handle if successful, -1 if otherwise
59 int lm_AllocLightmap(int w, int h) {
60 int n; //,i;
61 if (Num_of_lightmaps == MAX_LIGHTMAPS)
62 Int3(); // Ran out of lightmaps!
63
64 n = Free_lightmap_list[Num_of_lightmaps++];
65 ASSERT(GameLightmaps[n].used == 0);
66 // If no go on the malloc, bail out with -1
67
68 memset(&GameLightmaps[n], 0, sizeof(bms_lightmap));
69 GameLightmaps[n].data = (uint16_t *)mem_malloc((w * h * 2));
70 if (!GameLightmaps[n].data) {
•71 mprintf(0, "NOT ENOUGH MEMORY FOR LIGHTMAP!\n");
72 Int3();
73 return BAD_LM_INDEX;
74 }
75
76 GameLightmaps[n].width = w;
77 GameLightmaps[n].height = h;
78 GameLightmaps[n].used = 1;
79 GameLightmaps[n].cache_slot = -1;
80 GameLightmaps[n].flags = LF_CHANGED;
81 // Figure out square size
82 // Find power of 2 number
83 int res = std::max(w, h);
84 int lightmap_res = 2;
85 for (int i = 0; i <= 7; i++) {
86 int low_num = 1 < i;
87 int hi_num = 2 << i;
88 if (res <= hi_num && res > low_num) {
89 lightmap_res = hi_num;
90 break;
91 }
92 }
93 ASSERT(lightmap_res >= 2 && lightmap_res <= 128);
94 GameLightmaps[n].square_res = lightmap_res;
95 Lightmap_mem_used += (w * h * 2);
96
97 return n;
98 }
99 // Given a handle, frees the lightmap memory and flags this lightmap as unused
100 void lm_FreeLightmap(int handle) {
101 if (GameLightmaps[handle].used < 1)
102 return;
103 GameLightmaps[handle].used--;
104 if (GameLightmaps[handle].used == 0) {
105 if (GameLightmaps[handle].data != NULL)
106 mem_free(GameLightmaps[handle].data);
107 Lightmap_mem_used -= (GameLightmaps[handle].width * GameLightmaps[handle].height * 2);
108
109 GameLightmaps[handle].data = NULL;
110 GameLightmaps[handle].cache_slot = -1;
111
112 Free_lightmap_list[--Num_of_lightmaps] = handle;
113 }
114 }
115 // returns a lightmaps width else -1 if something is wrong
116 int lm_w(int handle) {
117 int w;
118 if (!GameLightmaps[handle].used) {
119 Int3();
120 return -1;
121 }
122 w = GameLightmaps[handle].width;
123
124 return (w);
125 }
126 // returns a lightmaps height , else -1 if something is wrong
127 int lm_h(int handle) {
128 int h;
129 if (!GameLightmaps[handle].used) {
130 Int3();
131 return -1;
132 }
133 h = GameLightmaps[handle].height;
134
135 return (h);
136 }
137 // returns a lightmaps data else NULL if something is wrong
138 uint16_t *lm_data(int handle) {
139 uint16_t *d;
140 if (!GameLightmaps[handle].used) {
141 Int3();
142 return NULL;
143 }
144
145 d = GameLightmaps[handle].data;
146 return (d);
147 }