1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*
* Descent 3
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "lightmap.h"
#include "pstypes.h"
#include "pserror.h"
#include "bitmap.h"
#include "mono.h"
#include "mem.h"
#if !defined(POSIX)
#include "Macros.h"
#endif
#include <algorithm>
static int Num_of_lightmaps = 0;
static uint16_t Free_lightmap_list[MAX_LIGHTMAPS];
bms_lightmap GameLightmaps[MAX_LIGHTMAPS];
static int Lightmap_mem_used = 0;
// Sets all the lightmaps to unused
void lm_InitLightmaps() {
int i;
for (i = 0; i < MAX_LIGHTMAPS; i++) {
GameLightmaps[i].flags = 0;
GameLightmaps[i].used = 0;
GameLightmaps[i].data = NULL;
GameLightmaps[i].cache_slot = -1;
Free_lightmap_list[i] = i;
}
atexit(lm_ShutdownLightmaps);
}
void lm_ShutdownLightmaps(void) {
int i;
mprintf(0, "Freeing all lightmap memory.\n");
for (i = 0; i < MAX_LIGHTMAPS; i++) {
while (GameLightmaps[i].used > 0)
lm_FreeLightmap(i);
}
}
// Allocs a lightmap of w x h size
// Returns lightmap handle if successful, -1 if otherwise
int lm_AllocLightmap(int w, int h) {
int n; //,i;
if (Num_of_lightmaps == MAX_LIGHTMAPS)
Int3(); // Ran out of lightmaps!
n = Free_lightmap_list[Num_of_lightmaps++];
ASSERT(GameLightmaps[n].used == 0);
// If no go on the malloc, bail out with -1
memset(&GameLightmaps[n], 0, sizeof(bms_lightmap));
GameLightmaps[n].data = (uint16_t *)mem_malloc((w * h * 2));
if (!GameLightmaps[n].data) {
mprintf(0, "NOT ENOUGH MEMORY FOR LIGHTMAP!\n");
Int3();
return BAD_LM_INDEX;
}
GameLightmaps[n].width = w;
GameLightmaps[n].height = h;
GameLightmaps[n].used = 1;
GameLightmaps[n].cache_slot = -1;
GameLightmaps[n].flags = LF_CHANGED;
// Figure out square size
// Find power of 2 number
int res = std::max(w, h);
int lightmap_res = 2;
for (int i = 0; i <= 7; i++) {
int low_num = 1 < i;
int hi_num = 2 << i;
if (res <= hi_num && res > low_num) {
lightmap_res = hi_num;
break;
}
}
ASSERT(lightmap_res >= 2 && lightmap_res <= 128);
GameLightmaps[n].square_res = lightmap_res;
Lightmap_mem_used += (w * h * 2);
return n;
}
// Given a handle, frees the lightmap memory and flags this lightmap as unused
void lm_FreeLightmap(int handle) {
if (GameLightmaps[handle].used < 1)
return;
GameLightmaps[handle].used--;
if (GameLightmaps[handle].used == 0) {
if (GameLightmaps[handle].data != NULL)
mem_free(GameLightmaps[handle].data);
Lightmap_mem_used -= (GameLightmaps[handle].width * GameLightmaps[handle].height * 2);
GameLightmaps[handle].data = NULL;
GameLightmaps[handle].cache_slot = -1;
Free_lightmap_list[--Num_of_lightmaps] = handle;
}
}
// returns a lightmaps width else -1 if something is wrong
int lm_w(int handle) {
int w;
if (!GameLightmaps[handle].used) {
Int3();
return -1;
}
w = GameLightmaps[handle].width;
return (w);
}
// returns a lightmaps height , else -1 if something is wrong
int lm_h(int handle) {
int h;
if (!GameLightmaps[handle].used) {
Int3();
return -1;
}
h = GameLightmaps[handle].height;
return (h);
}
// returns a lightmaps data else NULL if something is wrong
uint16_t *lm_data(int handle) {
uint16_t *d;
if (!GameLightmaps[handle].used) {
Int3();
return NULL;
}
d = GameLightmaps[handle].data;
return (d);
}
|