Fix memory leak and get rid of redundant Lighting.Color_YMax_Fast function

This commit is contained in:
UnknownShadow200 2022-07-07 21:02:49 +10:00
parent 671f1a36cc
commit c6c0f525c6
3 changed files with 25 additions and 24 deletions

View File

@ -357,7 +357,7 @@ static cc_bool BuildChunk(int x1, int y1, int z1, struct ChunkInfo* info) {
info->AllAir = allAir;
if (allAir || allSolid) return false;
Lighting.LightHint(x1 - 1, z1 - 1);
Lighting.LightHint(x1 - 1, y1 - 1, z1 - 1);
Mem_Set(counts, 1, CHUNK_SIZE_3 * FACE_COUNT);
xMax = min(World.Width, x1 + CHUNK_SIZE);
@ -555,7 +555,7 @@ static PackedCol Normal_LightColor(int x, int y, int z, Face face, BlockID block
case FACE_YMIN:
return Lighting.Color_YMin_Fast(x, y - offset, z);
case FACE_YMAX:
return Lighting.Color_YMax_Fast(x, y + offset, z);
return Lighting.Color_Fast(x, y + offset, z);
}
return 0; /* should never happen */
}
@ -723,7 +723,7 @@ static void NormalBuilder_RenderBlock(int index, int x, int y, int z) {
offset = (lightFlags >> FACE_YMAX) & 1;
part = &Builder_Parts[baseOffset + Atlas1D_Index(loc)];
col = fullBright ? PACKEDCOL_WHITE : Lighting.Color_YMax_Fast(x, y + offset, z);
col = fullBright ? PACKEDCOL_WHITE : Lighting.Color_Fast(x, y + offset, z);
Drawer_YMax(count_YMax, col, loc, &part->fVertices[FACE_YMAX]);
}
}

View File

@ -195,7 +195,6 @@ static void Heightmap_Allocate(void) {
/*########################################################################################################################*
*----------------------------------------------------Classic lighting-----------------------------------------------------*
*#########################################################################################################################*/
/* Outside color is same as sunlight color, so we reuse when possible */
static cc_bool ClassicLighting_IsLit(int x, int y, int z) {
return y > Heightmap_GetLightHeight(x, z);
@ -219,10 +218,6 @@ static PackedCol ClassicLighting_Color_Fast(int x, int y, int z) {
return y > heightmap[Heightmap_Pack(x, z)] ? Env.SunCol : Env.ShadowCol;
}
static PackedCol ClassicLighting_Color_YMax_Fast(int x, int y, int z) {
return y > heightmap[Heightmap_Pack(x, z)] ? Env.SunCol : Env.ShadowCol;
}
static PackedCol ClassicLighting_Color_YMin_Fast(int x, int y, int z) {
return y > heightmap[Heightmap_Pack(x, z)] ? Env.SunYMin : Env.ShadowYMin;
}
@ -235,6 +230,11 @@ static PackedCol ClassicLighting_Color_ZSide_Fast(int x, int y, int z) {
return y > heightmap[Heightmap_Pack(x, z)] ? Env.SunZSide : Env.ShadowZSide;
}
static void ClassicLighting_LightHint(int startX, int startY, int startZ) {
Heightmap_PreCalcColumns(startX, startZ);
}
static void ClassicLighting_UpdateLighting(int x, int y, int z, BlockID oldBlock, BlockID newBlock, int index, int lightH) {
cc_bool didBlock = Blocks.BlocksLight[oldBlock];
cc_bool nowBlocks = Blocks.BlocksLight[newBlock];
@ -385,14 +385,13 @@ static void ClassicLighting_SetActive(void) {
Lighting.IsLit_Fast = ClassicLighting_IsLit_Fast;
Lighting.Color_Fast = ClassicLighting_Color_Fast;
Lighting.Color_YMax_Fast = ClassicLighting_Color_YMax_Fast;
Lighting.Color_YMin_Fast = ClassicLighting_Color_YMin_Fast;
Lighting.Color_XSide_Fast = ClassicLighting_Color_XSide_Fast;
Lighting.Color_ZSide_Fast = ClassicLighting_Color_ZSide_Fast;
Lighting.FreeState = Heightmap_Free;
Lighting.AllocState = Heightmap_Allocate;
Lighting.LightHint = Heightmap_PreCalcColumns;
Lighting.LightHint = ClassicLighting_LightHint;
}
@ -554,12 +553,14 @@ static void ModernLighting_InitPalettes(void) {
ModernLighting_InitPalette(modernLighting_paletteY, PACKEDCOL_SHADE_YMIN);
}
static int chunksCount;
static void ModernLighting_AllocState(void) {
Heightmap_Allocate();
ModernLighting_InitPalettes();
chunksCount = World.ChunksCount;
chunkLightingDataFlags = (cc_uint8*)Mem_TryAllocCleared(World.ChunksCount, sizeof(cc_uint8));
chunkLightingData = (LightingChunk*)Mem_TryAllocCleared(World.ChunksCount, sizeof(LightingChunk));
chunkLightingDataFlags = (cc_uint8*)Mem_TryAllocCleared(chunksCount, sizeof(cc_uint8));
chunkLightingData = (LightingChunk*)Mem_TryAllocCleared(chunksCount, sizeof(LightingChunk));
LightQueue_Init(&lightQueue);
}
@ -568,16 +569,17 @@ static void ModernLighting_FreeState(void) {
Heightmap_Free();
int i;
/* This function can be called multiple times without calling ModernLighting_AllocState, so... */
if (chunkLightingDataFlags == NULL) { return; }
if (!chunkLightingDataFlags) return;
for (i = 0; i < World.ChunksCount; i++) {
if (chunkLightingDataFlags[i] > CHUNK_SELF_CALCULATED || chunkLightingDataFlags[i] == CHUNK_UNCALCULATED) { continue; }
for (i = 0; i < chunksCount; i++) {
if (chunkLightingDataFlags[i] == CHUNK_UNCALCULATED) continue;
Mem_Free(chunkLightingData[i]);
}
Mem_Free(chunkLightingDataFlags);
Mem_Free(chunkLightingData);
chunkLightingDataFlags = NULL;
chunkLightingData = NULL;
chunkLightingData = NULL;
LightQueue_Clear(&lightQueue);
}
@ -849,6 +851,7 @@ static void CalculateChunkLightingSelf(int chunkIndex, int cx, int cy, int cz) {
}
chunkLightingDataFlags[chunkIndex] = CHUNK_SELF_CALCULATED;
}
static void CalculateChunkLightingAll(int chunkIndex, int cx, int cy, int cz) {
int x, y, z;
int chunkStartX, chunkStartY, chunkStartZ; //chunk coords
@ -943,8 +946,10 @@ static PackedCol ModernLighting_Color_YMinSide(int x, int y, int z) {
return ModernLighting_Color_Core(x, y, z, modernLighting_paletteY, Env.SunYMin);
}
static void ModernLighting_LightHint(int startX, int startZ) {
static void ModernLighting_LightHint(int startX, int startY, int startZ) {
int cx, cy, cz, x, y, z;
Heightmap_PreCalcColumns(startX, startZ);
}
static void ModernLighting_SetActive(void) {
@ -956,7 +961,6 @@ static void ModernLighting_SetActive(void) {
Lighting.IsLit_Fast = ModernLighting_IsLit_Fast;
Lighting.Color_Fast = ModernLighting_Color;
Lighting.Color_YMax_Fast = ModernLighting_Color;
Lighting.Color_YMin_Fast = ModernLighting_Color_YMinSide;
Lighting.Color_XSide_Fast = ModernLighting_Color_XSide;
Lighting.Color_ZSide_Fast = ModernLighting_Color_ZSide;

View File

@ -24,11 +24,9 @@ CC_VAR extern struct _Lighting {
/* Allocates the per-level lighting state */
/* (called after map has been fully loaded) */
void (*AllocState)(void);
/* Equivalent to (but far more optimised form of)
* for x = startX; x < startX + 18; x++
* for z = startZ; z < startZ + 18; z++
* CalcLight(x, maxY, z) */
void (*LightHint)(int startX, int startZ);
/* Quickly calculates lighting between
/* (startX, startY, startZ) to (startX + 18, startY + 18, startZ + 18) */
void (*LightHint)(int startX, int startY, int startZ);
/* Called when a block is changed to update internal lighting state. */
/* NOTE: Implementations ***MUST*** mark all chunks affected by this lighting change as needing to be refreshed. */
@ -51,7 +49,6 @@ CC_VAR extern struct _Lighting {
cc_bool (*IsLit_Fast)(int x, int y, int z);
PackedCol (*Color_Fast)(int x, int y, int z);
PackedCol (*Color_YMax_Fast)(int x, int y, int z);
PackedCol (*Color_YMin_Fast)(int x, int y, int z);
PackedCol (*Color_XSide_Fast)(int x, int y, int z);
PackedCol (*Color_ZSide_Fast)(int x, int y, int z);