From 2ee4a911d0ddba406eb3b7020e19fb6897ed4586 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 13 Jun 2022 21:45:07 +1000 Subject: [PATCH] Some minor optimisations --- src/Block.c | 4 ---- src/Block.h | 2 +- src/Builder.c | 9 +++++---- src/Lighting.c | 40 ++++++++++++++++++++++------------------ src/Lighting.h | 2 ++ 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/Block.c b/src/Block.c index 183ed9d9d..4221ad0db 100644 --- a/src/Block.c +++ b/src/Block.c @@ -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++) { diff --git a/src/Block.h b/src/Block.h index dc1cd350c..2b24d4179 100644 --- a/src/Block.h +++ b/src/Block.h @@ -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. */ diff --git a/src/Builder.c b/src/Builder.c index b11b8e663..2464390e4 100644 --- a/src/Builder.c +++ b/src/Builder.c @@ -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) { diff --git a/src/Lighting.c b/src/Lighting.c index 1a4eab4a9..2eedbd42b 100644 --- a/src/Lighting.c +++ b/src/Lighting.c @@ -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 */ diff --git a/src/Lighting.h b/src/Lighting.h index 4886b9f56..20219593d 100644 --- a/src/Lighting.h +++ b/src/Lighting.h @@ -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++