| 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"cxx . . $ `<file>/lib/lightmap.h`/ |
| 22 |
#include "pstypes.h"cxx . . $ `<file>/lib/pstypes.h`/ |
| 23 |
#include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 24 |
#include "bitmap.h"cxx . . $ `<file>/lib/bitmap.h`/ |
| 25 |
#include "mono.h"cxx . . $ `<file>/ddebug/mono.h`/ |
| 26 |
#include "mem.h"cxx . . $ `<file>/mem/mem.h`/ |
| 27 |
#if !defined(POSIXcxx . . $ `<built-in>:478:9`!) |
| 28 |
#include "Macros.h" |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <algorithm> |
| 32 |
|
| 33 |
static int Num_of_lightmapscxx . . $ Num_of_lightmaps. = 0; |
| 34 |
static uint16_tcxx . . $ uint16_t# Free_lightmap_listcxx . . $ Free_lightmap_list.[MAX_LIGHTMAPScxx . . $ `lib/lightmap.h:24:9`!]; |
| 35 |
bms_lightmapcxx . . $ bms_lightmap# GameLightmapscxx . . $ GameLightmaps.[MAX_LIGHTMAPScxx . . $ `lib/lightmap.h:24:9`!]; |
| 36 |
static int Lightmap_mem_usedcxx . . $ Lightmap_mem_used. = 0; |
| 37 |
// Sets all the lightmaps to unused |
| 38 |
void lm_InitLightmapscxx . . $ lm_InitLightmaps(49f6e7a06ebc5aa8).() { |
| 39 |
int i; |
| 40 |
for (i = 0; i < MAX_LIGHTMAPScxx . . $ `lib/lightmap.h:24:9`!; i++) { |
| 41 |
GameLightmapscxx . . $ GameLightmaps.[i].flagscxx . . $ bms_lightmap#flags. = 0; |
| 42 |
GameLightmapscxx . . $ GameLightmaps.[i].usedcxx . . $ bms_lightmap#used. = 0; |
| 43 |
GameLightmapscxx . . $ GameLightmaps.[i].datacxx . . $ bms_lightmap#data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 44 |
GameLightmapscxx . . $ GameLightmaps.[i].cache_slotcxx . . $ bms_lightmap#cache_slot. = -1; |
| 45 |
Free_lightmap_listcxx . . $ Free_lightmap_list.[i] = i; |
| 46 |
} |
| 47 |
atexitcxx . . $ atexit(cc8c495369588262).(lm_ShutdownLightmapscxx . . $ lm_ShutdownLightmaps(49f6e7a06ebc5aa8).); |
| 48 |
} |
| 49 |
void lm_ShutdownLightmapscxx . . $ lm_ShutdownLightmaps(49f6e7a06ebc5aa8).(void) { |
| 50 |
int i; |
| 51 |
mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Freeing all lightmap memory.\n"); |
| 52 |
for (i = 0; i < MAX_LIGHTMAPScxx . . $ `lib/lightmap.h:24:9`!; i++) { |
| 53 |
while (GameLightmapscxx . . $ GameLightmaps.[i].usedcxx . . $ bms_lightmap#used. > 0) |
| 54 |
lm_FreeLightmapcxx . . $ lm_FreeLightmap(d4f767463ce0a6b3).(i); |
| 55 |
} |
| 56 |
} |
| 57 |
// Allocs a lightmap of w x h size |
| 58 |
// Returns lightmap handle if successful, -1 if otherwise |
| 59 |
int lm_AllocLightmapcxx . . $ lm_AllocLightmap(9b79fb6aee4c0440).(int w, int h) { |
| 60 |
int n; //,i; |
| 61 |
if (Num_of_lightmapscxx . . $ Num_of_lightmaps. == MAX_LIGHTMAPScxx . . $ `lib/lightmap.h:24:9`!) |
| 62 |
Int3cxx . . $ `misc/pserror.h:247:9`!(); // Ran out of lightmaps! |
| 63 |
|
| 64 |
n = Free_lightmap_listcxx . . $ Free_lightmap_list.[Num_of_lightmapscxx . . $ Num_of_lightmaps.++]; |
| 65 |
ASSERTcxx . . $ `misc/pserror.h:246:9`!(GameLightmaps[n].used == 0); |
| 66 |
// If no go on the malloc, bail out with -1 |
| 67 |
|
| 68 |
memset(&GameLightmapscxx . . $ GameLightmaps.[n], 0, sizeof(bms_lightmapcxx . . $ bms_lightmap#)); |
| 69 |
GameLightmapscxx . . $ GameLightmaps.[n].datacxx . . $ bms_lightmap#data. = (uint16_tcxx . . $ uint16_t# *)mem_malloccxx . . $ `mem/mem.h:79:9`!((w * h * 2)); |
| 70 |
if (!GameLightmapscxx . . $ GameLightmaps.[n].datacxx . . $ bms_lightmap#data.) { |
| •71 |
mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "NOT ENOUGH MEMORY FOR LIGHTMAP!\n"); |
| 72 |
Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 73 |
return BAD_LM_INDEXcxx . . $ `lib/lightmap.h:25:9`!; |
| 74 |
} |
| 75 |
|
| 76 |
GameLightmapscxx . . $ GameLightmaps.[n].widthcxx . . $ bms_lightmap#width. = w; |
| 77 |
GameLightmapscxx . . $ GameLightmaps.[n].heightcxx . . $ bms_lightmap#height. = h; |
| 78 |
GameLightmapscxx . . $ GameLightmaps.[n].usedcxx . . $ bms_lightmap#used. = 1; |
| 79 |
GameLightmapscxx . . $ GameLightmaps.[n].cache_slotcxx . . $ bms_lightmap#cache_slot. = -1; |
| 80 |
GameLightmapscxx . . $ GameLightmaps.[n].flagscxx . . $ bms_lightmap#flags. = LF_CHANGEDcxx . . $ `lib/lightmap.h:28:9`!; |
| 81 |
// Figure out square size |
| 82 |
// Find power of 2 number |
| 83 |
int res = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).(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 |
ASSERTcxx . . $ `misc/pserror.h:246:9`!(lightmap_res >= 2 && lightmap_res <= 128); |
| 94 |
GameLightmapscxx . . $ GameLightmaps.[n].square_rescxx . . $ bms_lightmap#square_res. = lightmap_res; |
| 95 |
Lightmap_mem_usedcxx . . $ 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_FreeLightmapcxx . . $ lm_FreeLightmap(d4f767463ce0a6b3).(int handle) { |
| 101 |
if (GameLightmapscxx . . $ GameLightmaps.[handle].usedcxx . . $ bms_lightmap#used. < 1) |
| 102 |
return; |
| 103 |
GameLightmapscxx . . $ GameLightmaps.[handle].usedcxx . . $ bms_lightmap#used.--; |
| 104 |
if (GameLightmapscxx . . $ GameLightmaps.[handle].usedcxx . . $ bms_lightmap#used. == 0) { |
| 105 |
if (GameLightmapscxx . . $ GameLightmaps.[handle].datacxx . . $ bms_lightmap#data. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 106 |
mem_freecxx . . $ `mem/mem.h:80:9`!(GameLightmaps[handle].data); |
| 107 |
Lightmap_mem_usedcxx . . $ Lightmap_mem_used. -= (GameLightmapscxx . . $ GameLightmaps.[handle].widthcxx . . $ bms_lightmap#width. * GameLightmapscxx . . $ GameLightmaps.[handle].heightcxx . . $ bms_lightmap#height. * 2); |
| 108 |
|
| 109 |
GameLightmapscxx . . $ GameLightmaps.[handle].datacxx . . $ bms_lightmap#data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 110 |
GameLightmapscxx . . $ GameLightmaps.[handle].cache_slotcxx . . $ bms_lightmap#cache_slot. = -1; |
| 111 |
|
| 112 |
Free_lightmap_listcxx . . $ Free_lightmap_list.[--Num_of_lightmapscxx . . $ Num_of_lightmaps.] = handle; |
| 113 |
} |
| 114 |
} |
| 115 |
// returns a lightmaps width else -1 if something is wrong |
| 116 |
int lm_wcxx . . $ lm_w(7864480464b09eea).(int handle) { |
| 117 |
int w; |
| 118 |
if (!GameLightmapscxx . . $ GameLightmaps.[handle].usedcxx . . $ bms_lightmap#used.) { |
| 119 |
Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
w = GameLightmapscxx . . $ GameLightmaps.[handle].widthcxx . . $ bms_lightmap#width.; |
| 123 |
|
| 124 |
return (w); |
| 125 |
} |
| 126 |
// returns a lightmaps height , else -1 if something is wrong |
| 127 |
int lm_hcxx . . $ lm_h(7864480464b09eea).(int handle) { |
| 128 |
int h; |
| 129 |
if (!GameLightmapscxx . . $ GameLightmaps.[handle].usedcxx . . $ bms_lightmap#used.) { |
| 130 |
Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 131 |
return -1; |
| 132 |
} |
| 133 |
h = GameLightmapscxx . . $ GameLightmaps.[handle].heightcxx . . $ bms_lightmap#height.; |
| 134 |
|
| 135 |
return (h); |
| 136 |
} |
| 137 |
// returns a lightmaps data else NULL if something is wrong |
| 138 |
uint16_tcxx . . $ uint16_t# *lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(int handle) { |
| 139 |
uint16_tcxx . . $ uint16_t# *d; |
| 140 |
if (!GameLightmapscxx . . $ GameLightmaps.[handle].usedcxx . . $ bms_lightmap#used.) { |
| 141 |
Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 142 |
return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 143 |
} |
| 144 |
|
| 145 |
d = GameLightmapscxx . . $ GameLightmaps.[handle].datacxx . . $ bms_lightmap#data.; |
| 146 |
return (d); |
| 147 |
} |