Some minor optimisations

This commit is contained in:
UnknownShadow200 2022-06-13 21:45:07 +10:00
parent 1fa0e789c0
commit 2ee4a911d0
5 changed files with 30 additions and 27 deletions

View File

@ -484,10 +484,6 @@ static void Block_CalcCulling(BlockID block, BlockID other) {
Blocks.Hidden[(block * BLOCK_COUNT) + other] = f;
}
cc_bool Block_IsFaceHidden(BlockID block, BlockID other, Face face) {
return (Blocks.Hidden[(block * BLOCK_COUNT) + other] & (1 << face)) != 0;
}
void Block_UpdateAllCulling(void) {
int block, neighbour;
for (block = BLOCK_AIR; block < BLOCK_COUNT; block++) {

View File

@ -146,7 +146,7 @@ void Block_SetSide(TextureLoc texLoc, BlockID blockId);
/* The texture for the given face of the given block. */
#define Block_Tex(block, face) Blocks.Textures[(block) * FACE_COUNT + (face)]
cc_bool Block_IsFaceHidden(BlockID block, BlockID other, Face face);
#define Block_IsFaceHidden(block, other, face) (Blocks.Hidden[((block) * BLOCK_COUNT) + (other)] & (1 << (face)))
/* Updates culling data of all blocks. */
void Block_UpdateAllCulling(void);
/* Updates culling data just for this block. */

View File

@ -539,7 +539,7 @@ static void Builder_DrawSprite(int x, int y, int z) {
/*########################################################################################################################*
*--------------------------------------------------Normal mesh builder----------------------------------------------------*
*#########################################################################################################################*/
static PackedCol Normal_LightCol(int x, int y, int z, Face face, BlockID block) {
static PackedCol Normal_LightColor(int x, int y, int z, Face face, BlockID block) {
int offset = (Blocks.LightOffset[block] >> face) & 1;
switch (face) {
@ -551,10 +551,11 @@ static PackedCol Normal_LightCol(int x, int y, int z, Face face, BlockID block)
return z < offset ? Env.SunZSide : Lighting.Color_ZSide_Fast(x, y, z - offset);
case FACE_ZMAX:
return z > (World.MaxZ - offset) ? Env.SunZSide : Lighting.Color_ZSide_Fast(x, y, z + offset);
case FACE_YMIN:
return y <= 0 ? Env.SunYMin : Lighting.Color_YMin_Fast(x, y - offset, z);
return Lighting.Color_YMin_Fast(x, y - offset, z);
case FACE_YMAX:
return y >= World.MaxY ? Env.SunCol : Lighting.Color_YMax_Fast(x, (y + 1) - offset, z);
return Lighting.Color_YMax_Fast(x, (y + 1) - offset, z);
}
return 0; /* should never happen */
}
@ -565,7 +566,7 @@ static cc_bool Normal_CanStretch(BlockID initial, int chunkIndex, int x, int y,
if (cur != initial || Block_IsFaceHidden(cur, Builder_Chunk[chunkIndex + Builder_Offsets[face]], face)) return false;
if (Builder_FullBright) return true;
return Normal_LightCol(Builder_X, Builder_Y, Builder_Z, face, initial) == Normal_LightCol(x, y, z, face, cur);
return Normal_LightColor(Builder_X, Builder_Y, Builder_Z, face, initial) == Normal_LightColor(x, y, z, face, cur);
}
static int NormalBuilder_StretchXLiquid(int countIndex, int x, int y, int z, int chunkIndex, BlockID block) {

View File

@ -347,6 +347,7 @@ static void Heightmap_FinishCoverage(int x1, int z1, int xCount, int zCount) {
}
}
static void ClassicLighting_LightHint(int startX, int startZ) {
int x1 = max(startX, 0), x2 = min(World.Width, startX + EXTCHUNK_SIZE);
int z1 = max(startZ, 0), z2 = min(World.Length, startZ + EXTCHUNK_SIZE);
@ -359,8 +360,21 @@ static void ClassicLighting_LightHint(int startX, int startZ) {
}
}
static void ClassicLighting_OnReset(void) {
Mem_Free(classic_heightmap);
classic_heightmap = NULL;
}
static void ClassicLighting_OnNewMapLoaded(void) {
classic_heightmap = (cc_int16*)Mem_TryAlloc(World.Width * World.Length, 2);
if (classic_heightmap) {
ClassicLighting_Refresh();
} else {
World_OutOfMemory();
}
}
static void ClassicLighting_SetActive(void) {
Lighting.LightHint = ClassicLighting_LightHint;
Lighting.OnBlockChanged = ClassicLighting_OnBlockChanged;
Lighting.Refresh = ClassicLighting_Refresh;
Lighting.IsLit = ClassicLighting_IsLit;
@ -373,6 +387,10 @@ static void ClassicLighting_SetActive(void) {
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.HandleReset = ClassicLighting_OnReset;
Lighting.HandleNewMapLoaded = ClassicLighting_OnNewMapLoaded;
Lighting.LightHint = ClassicLighting_LightHint;
}
@ -380,23 +398,9 @@ static void ClassicLighting_SetActive(void) {
*---------------------------------------------------Lighting component----------------------------------------------------*
*#########################################################################################################################*/
static void OnInit(void) {
ClassicLighting_SetActive();
}
static void OnReset(void) {
Mem_Free(classic_heightmap);
classic_heightmap = NULL;
}
static void OnNewMapLoaded(void) {
classic_heightmap = (cc_int16*)Mem_TryAlloc(World.Width * World.Length, 2);
if (classic_heightmap) {
ClassicLighting_Refresh();
} else {
World_OutOfMemory();
}
}
static void OnInit(void) { ClassicLighting_SetActive(); }
static void OnReset(void) { Lighting.HandleReset(); }
static void OnNewMapLoaded(void) { Lighting.HandleNewMapLoaded(); }
struct IGameComponent Lighting_Component = {
OnInit, /* Init */

View File

@ -9,6 +9,8 @@ struct IGameComponent;
extern struct IGameComponent Lighting_Component;
CC_VAR extern struct _Lighting {
void (*HandleReset)(void);
void (*HandleNewMapLoaded)(void);
/* Equivalent to (but far more optimised form of)
* for x = startX; x < startX + 18; x++
* for z = startZ; z < startZ + 18; z++