From dde7c6258ec5474e89bdcb59fe5737f7b013da16 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 10 May 2017 17:56:25 +1000 Subject: [PATCH] Implement WorldEnv in C --- src/Client/EventHandler.h | 10 +- src/Client/FastColour.c | 4 + src/Client/FastColour.h | 3 + src/Client/WorldEnv.c | 105 +++++++++++++++++++-- src/Client/WorldEnv.h | 194 ++++++++++++++------------------------ src/Client/WorldEvents.h | 30 +++--- 6 files changed, 194 insertions(+), 152 deletions(-) diff --git a/src/Client/EventHandler.h b/src/Client/EventHandler.h index 76550b98b..f22536051 100644 --- a/src/Client/EventHandler.h +++ b/src/Client/EventHandler.h @@ -27,11 +27,11 @@ void EventHandler_Unregister(void** handlers, Int32* count, void* handler); /* Calls handlers for an event that has no arguments.*/ -void EventHandler_Call_Void(Event_Void* handlers, Int32 handlersCount); +void EventHandler_Raise_Void(Event_Void* handlers, Int32 handlersCount); -/* Calls handlers for an event that has no arguments.*/ -void EventHandler_Call_Int32(Event_Int32* handlers, Int32 handlersCount); +/* Calls handlers for an event that takes a 32-bit signed interger argument.*/ +void EventHandler_Raise_Int32(Event_Int32* handlers, Int32 handlersCount, Int32 arg); -/* Calls handlers for an event that has no arguments.*/ -void EventHandler_Call_Float32(Event_Float32* handlers, Int32 handlersCount); +/* Calls handlers for an event that takes a 32-bit floating point argument.*/ +void EventHandler_Raise_Float32(Event_Float32* handlers, Int32 handlersCount, Real32 arg); #endif \ No newline at end of file diff --git a/src/Client/FastColour.c b/src/Client/FastColour.c index 622ce2173..99b3d6ba7 100644 --- a/src/Client/FastColour.c +++ b/src/Client/FastColour.c @@ -12,6 +12,10 @@ FastColour FastColour_Create3(UInt8 r, UInt8 g, UInt8 b) { return col; } +bool FastColour_Equals(FastColour a, FastColour b) { + return a.Packed == b.Packed; +} + FastColour FastColour_Scale(FastColour value, Real32 t) { value.R = (UInt8)(value.R * t); value.G = (UInt8)(value.G * t); diff --git a/src/Client/FastColour.h b/src/Client/FastColour.h index 222123ab6..77093be3f 100644 --- a/src/Client/FastColour.h +++ b/src/Client/FastColour.h @@ -24,6 +24,9 @@ FastColour FastColour_Create4(UInt8 r, UInt8 g, UInt8 b, UInt8 a); /* Constructs a new ARGB colour. */ FastColour FastColour_Create3(UInt8 r, UInt8 g, UInt8 b); +/* Returns whether two packed colours are equal. */ +bool FastColour_Equals(FastColour a, FastColour b); + /* Multiplies the RGB components by t, where t is in [0, 1] */ FastColour FastColour_Scale(FastColour value, Real32 t); diff --git a/src/Client/WorldEnv.c b/src/Client/WorldEnv.c index 84102d0b3..ac1879efb 100644 --- a/src/Client/WorldEnv.c +++ b/src/Client/WorldEnv.c @@ -1,4 +1,20 @@ #include "WorldEnv.h" +#include "EventHandler.h" +#include "WorldEvents.h" + + +/* Sets a value and potentially raises event. */ +#define WorldEnv_Set(value, dst, envVar)\ +if (value == dst) return;\ +dst = value;\ +WorldEvents_RaiseEnvVariableChanged(envVar); + +/* Sets a colour and potentially raises event. */ +#define WorldEnv_SetCol(value, dst, envVar)\ +if (FastColour_Equals(value, dst)) return;\ +dst = value;\ +WorldEvents_RaiseEnvVariableChanged(envVar); + void WorldEnv_Reset() { WorldEnv_DefaultSkyCol = FastColour_Create3(0x99, 0xCC, 0xFF); @@ -7,7 +23,7 @@ void WorldEnv_Reset() { WorldEnv_EdgeHeight = -1; WorldEnv_SidesOffset = -2; - WorldEnv_CloudHeight = -1; + WorldEnv_CloudsHeight = -1; WorldEnv_EdgeBlock = BlockID_StillWater; WorldEnv_SidesBlock = BlockID_Bedrock; @@ -25,14 +41,89 @@ void WorldEnv_Reset() { } void WorldEnv_ResetLight() { - WorldEnv_DefaultShadow = FastColour_Create3(0x9B, 0x9B, 0x9B); - WorldEnv_DefaultSun = FastColour_Create3(0xFF, 0xFF, 0xFF); + WorldEnv_DefaultShadowCol = FastColour_Create3(0x9B, 0x9B, 0x9B); + WorldEnv_DefaultSunCol = FastColour_Create3(0xFF, 0xFF, 0xFF); - WorldEnv_Shadow = WorldEnv_DefaultShadow; - FastColour_GetShaded(WorldEnv_Shadow, &WorldEnv_ShadowXSide, + WorldEnv_ShadowCol = WorldEnv_DefaultShadowCol; + FastColour_GetShaded(WorldEnv_ShadowCol, &WorldEnv_ShadowXSide, &WorldEnv_ShadowZSide, &WorldEnv_ShadowYBottom); - WorldEnv_Sun = WorldEnv_DefaultSun; - FastColour_GetShaded(WorldEnv_Sun, &WorldEnv_SunXSide, + WorldEnv_SunCol = WorldEnv_DefaultSunCol; + FastColour_GetShaded(WorldEnv_SunCol, &WorldEnv_SunXSide, &WorldEnv_SunZSide, &WorldEnv_SunYBottom); +} + + +void WorldEnv_SetEdgeBlock(BlockID block) { + if (block == BlockID_Invalid) return; + WorldEnv_Set(block, WorldEnv_EdgeBlock, EnvVar_EdgeBlock); +} + +void WorldEnv_SetSidesBlock(BlockID block) { + if (block == BlockID_Invalid) return; + WorldEnv_Set(block, WorldEnv_SidesBlock, EnvVar_SidesBlock); +} + +void WorldEnv_SetEdgeHeight(Int32 height) { + WorldEnv_Set(height, WorldEnv_EdgeHeight, EnvVar_EdgeHeight); +} + +void WorldEnv_SetSidesOffset(Int32 offset) { + WorldEnv_Set(offset, WorldEnv_SidesOffset, EnvVar_SidesOffset); +} + +void WorldEnv_SetCloudsHeight(Int32 height) { + WorldEnv_Set(height, WorldEnv_CloudsHeight, EnvVar_CloudsHeight); +} + +void WorldEnv_SetCloudsSpeed(Real32 speed) { + WorldEnv_Set(speed, WorldEnv_CloudsSpeed, EnvVar_CloudsSpeed); +} + + +void WorldEnv_SetWeatherSpeed(Real32 speed) { + WorldEnv_Set(speed, WorldEnv_WeatherSpeed, EnvVar_WeatherSpeed); +} + +void WorldEnv_SetWeatherFade(Real32 rate) { + WorldEnv_Set(rate, WorldEnv_WeatherFade, EnvVar_WeatherFade); +} + +void WorldEnv_SetWeather(Int32 weather) { + WorldEnv_Set(weather, WorldEnv_Weather, EnvVar_Weather); +} + +void WorldEnv_SetExpFog(bool expFog) { + WorldEnv_Set(expFog, WorldEnv_ExpFog, EnvVar_ExpFog); +} + + +void WorldEnv_SetSkyCol(FastColour col) { + WorldEnv_SetCol(col, WorldEnv_SkyCol, EnvVar_SkyCol); +} + +void WorldEnv_SetFogCol(FastColour col) { + WorldEnv_SetCol(col, WorldEnv_FogCol, EnvVar_FogCol); +} + +void WorldEnv_SetCloudsCol(FastColour col) { + WorldEnv_SetCol(col, WorldEnv_CloudsCol, EnvVar_CloudsCol); +} + +void WorldEnv_SetSunCol(FastColour col) { + if (FastColour_Equals(col, WorldEnv_SunCol)) return; + + WorldEnv_SunCol = col; + FastColour_GetShaded(WorldEnv_SunCol, &WorldEnv_SunXSide, + &WorldEnv_SunZSide, &WorldEnv_SunYBottom); + WorldEvents_RaiseEnvVariableChanged(EnvVar_SunCol); +} + +void WorldEnv_SetShadowCol(FastColour col) { + if (FastColour_Equals(col, WorldEnv_ShadowCol)) return; + + WorldEnv_ShadowCol = col; + FastColour_GetShaded(WorldEnv_ShadowCol, &WorldEnv_ShadowXSide, + &WorldEnv_ShadowZSide, &WorldEnv_ShadowYBottom); + WorldEvents_RaiseEnvVariableChanged(EnvVar_ShadowCol); } \ No newline at end of file diff --git a/src/Client/WorldEnv.h b/src/Client/WorldEnv.h index ee191e535..d116997a0 100644 --- a/src/Client/WorldEnv.h +++ b/src/Client/WorldEnv.h @@ -13,6 +13,39 @@ #define Weather_Snowy 2 +/* Block that surrounds map the map horizontally (default water) */ +BlockID WorldEnv_EdgeBlock = BlockID_StillWater; + +/* Block that surrounds the map that fills the bottom of the map horizontally, +fills part of the vertical sides of the map, and also surrounds map the map horizontally. (default bedrock) */ +BlockID WorldEnv_SidesBlock = BlockID_Bedrock; + +/* Height of the map edge. */ +Int32 WorldEnv_EdgeHeight; + +/* Offset of height of map sides from height of map edge. */ +Int32 WorldEnv_SidesOffset = -2; + +/* Height of the clouds. */ +Int32 WorldEnv_CloudsHeight; + +/* Modifier of how fast clouds travel across the world, defaults to 1. */ +Real32 WorldEnv_CloudsSpeed = 1.0f; + + +/* Modifier of how fast rain/snow falls, defaults to 1. */ +Real32 WorldEnv_WeatherSpeed = 1.0f; + +/* Modifier of how fast rain/snow fades, defaults to 1. */ +Real32 WorldEnv_WeatherFade = 1.0f; + +/* Current weather for this particular map. */ +Int32 WorldEnv_Weather = Weather_Sunny; + +/* Whether exponential fog mode is used by default. */ +bool WorldEnv_ExpFog = false; + + /* Colour of the sky located behind / above clouds. */ FastColour WorldEnv_SkyCol; FastColour WorldEnv_DefaultSkyCol; @@ -26,46 +59,15 @@ FastColour WorldEnv_DefaultFogCol; FastColour WorldEnv_CloudsCol; FastColour WorldEnv_DefaultCloudsCol; -/* Height of the clouds. */ -Int32 WorldEnv_CloudHeight; - -/* Modifier of how fast clouds travel across the world, defaults to 1. */ -Real32 WorldEnv_CloudsSpeed = 1.0f; - -/* Modifier of how fast rain/snow falls, defaults to 1. */ -Real32 WorldEnv_WeatherSpeed = 1.0f; - -/* Modifier of how fast rain/snow fades, defaults to 1. */ -Real32 WorldEnv_WeatherFade = 1.0f; - /* Colour applied to blocks located in direct sunlight. */ -FastColour WorldEnv_Sun; +FastColour WorldEnv_SunCol; FastColour WorldEnv_SunXSide, WorldEnv_SunZSide, WorldEnv_SunYBottom; -FastColour WorldEnv_DefaultSun; +FastColour WorldEnv_DefaultSunCol; /* Colour applied to blocks located in shadow / hidden from direct sunlight. */ -FastColour WorldEnv_Shadow; +FastColour WorldEnv_ShadowCol; FastColour WorldEnv_ShadowXSide, WorldEnv_ShadowZSide, WorldEnv_ShadowYBottom; -FastColour WorldEnv_DefaultShadow; - -/* Current weather for this particular map. */ -Int32 WorldEnv_Weather = Weather_Sunny; - -/* Block that surrounds map the map horizontally (default water) */ -BlockID WorldEnv_EdgeBlock = BlockID_StillWater; - -/* Height of the map edge. */ -Int32 WorldEnv_EdgeHeight; - -/* Block that surrounds the map that fills the bottom of the map horizontally, -fills part of the vertical sides of the map, and also surrounds map the map horizontally. (default bedrock) */ -BlockID WorldEnv_SidesBlock = BlockID_Bedrock; - -/* Offset of height of map sides from height of map edge. */ -Int32 WorldEnv_SidesOffset = -2; - -/* Whether exponential fog mode is used by default. */ -bool WorldEnv_ExpFog = false; +FastColour WorldEnv_DefaultShadowCol; /* Resets all of the environment properties to their defaults. */ @@ -74,109 +76,51 @@ void WorldEnv_Reset(); /*Resets sun and shadow environment properties to their defaults. */ void WorldEnv_ResetLight(); -// TODO: all the missing events !!! - /// Sets sides block to the given block, and raises - /// EnvVariableChanged event with variable 'SidesBlock'. - public void SetSidesBlock(BlockID block) { - if (block == SidesBlock) return; - if (block == Block.MaxDefinedBlock) { - Utils.LogDebug("Tried to set sides block to an invalid block: " + block); - block = Block.Bedrock; - } - SidesBlock = block; - game.WorldEvents.RaiseEnvVariableChanged(EnvVar.SidesBlock); - } +/* Sets edge block to the given block, and raises event with variable 'EdgeBlock'. */ +void WorldEnv_SetEdgeBlock(BlockID block); - /// Sets edge block to the given block, and raises - /// EnvVariableChanged event with variable 'EdgeBlock'. - public void SetEdgeBlock(BlockID block) { - if (block == EdgeBlock) return; - if (block == Block.MaxDefinedBlock) { - Utils.LogDebug("Tried to set edge block to an invalid block: " + block); - block = Block.StillWater; - } - EdgeBlock = block; - game.WorldEvents.RaiseEnvVariableChanged(EnvVar.EdgeBlock); - } +/* Sets sides block to the given block, and raises event with variable 'SidesBlock'. */ +void WorldEnv_SetSidesBlock(BlockID block); - /// Sets clouds height in world space, and raises - /// EnvVariableChanged event with variable 'CloudsLevel'. - public void SetCloudsLevel(int level) { Set(level, ref CloudHeight, EnvVar.CloudsLevel); } +/* Sets height of the map edges, raises event with variable 'EdgeHeight'. */ +void WorldEnv_SetEdgeHeight(Int32 height); - /// Sets clouds speed, and raises EnvVariableChanged - /// event with variable 'CloudsSpeed'. - public void SetCloudsSpeed(float speed) { Set(speed, ref CloudsSpeed, EnvVar.CloudsSpeed); } +/* Sets offset of the height of the map sides from map edges, raises event with variable 'SidesLevel'. */ +void WorldEnv_SetSidesOffset(Int32 offset); - /// Sets weather speed, and raises EnvVariableChanged - /// event with variable 'WeatherSpeed'. - public void SetWeatherSpeed(float speed) { Set(speed, ref WeatherSpeed, EnvVar.WeatherSpeed); } +/* Sets clouds height in world space, raises event with variable 'CloudsHeight'. */ +void WorldEnv_SetCloudsHeight(Int32 height); - /// Sets weather fade rate, and raises EnvVariableChanged - /// event with variable 'WeatherFade'. - public void SetWeatherFade(float rate) { Set(rate, ref WeatherFade, EnvVar.WeatherFade); } - - /// Sets height of the map edges in world space, and raises - /// EnvVariableChanged event with variable 'EdgeLevel'. - public void SetEdgeLevel(int level) { Set(level, ref EdgeHeight, EnvVar.EdgeLevel); } - - /// Sets offset of the height of the map sides from map edges in world space, and raises - /// EnvVariableChanged event with variable 'SidesLevel'. - public void SetSidesOffset(int level) { Set(level, ref SidesOffset, EnvVar.SidesOffset); } - - /// Sets whether exponential fog is used, and raises - /// EnvVariableChanged event with variable 'ExpFog'. - public void SetExpFog(bool expFog) { Set(expFog, ref ExpFog, EnvVar.ExpFog); } - - /// Sets weather, and raises - /// EnvVariableChanged event with variable 'Weather'. - public void SetWeather(Weather weather) { - if (weather == Weather) return; - Weather = weather; - game.WorldEvents.RaiseEnvVariableChanged(EnvVar.Weather); - } +/* Sets clouds speed, raises event with variable 'CloudsSpeed'. */ +void WorldEnv_SetCloudsSpeed(Real32 speed); - /// Sets tsky colour, and raises - /// EnvVariableChanged event with variable 'SkyColour'. - public void SetSkyColour(FastColour col) { Set(col, ref SkyCol, EnvVar.SkyColour); } +/* Sets weather speed, raises event with variable 'WeatherSpeed'. */ +void WorldEnv_SetWeatherSpeed(Real32 speed); - /// Sets fog colour, and raises - /// EnvVariableChanged event with variable 'FogColour'. - public void SetFogColour(FastColour col) { Set(col, ref FogCol, EnvVar.FogColour); } +/* Sets weather fade rate, raises event with variable 'WeatherFade'. */ +void WorldEnv_SetWeatherFade(Real32 rate); - /// Sets clouds colour, and raises - /// EnvVariableChanged event with variable 'CloudsColour'. - public void SetCloudsColour(FastColour col) { Set(col, ref CloudsCol, EnvVar.CloudsColour); } +/* Sets weather, raises event with variable 'Weather'. */ +void WorldEnv_SetWeather(Int32 weather); - /// Sets sunlight colour, and raises - /// EnvVariableChanged event with variable 'SunlightColour'. - public void SetSunlight(FastColour col) { - if (!Set(col, ref Sunlight, EnvVar.SunlightColour)) return; +/* Sets whether exponential fog is used, raises event with variable 'ExpFog'. */ +void WorldEnv_SetExpFog(bool expFog); - FastColour.GetShaded(Sunlight, out SunXSide, - out SunZSide, out SunYBottom); - Sun = Sunlight.Pack(); - game.WorldEvents.RaiseEnvVariableChanged(EnvVar.SunlightColour); - } - /// Sets current shadowlight colour, and raises - /// EnvVariableChanged event with variable 'ShadowlightColour'. - public void SetShadowlight(FastColour col) { - if (!Set(col, ref Shadowlight, EnvVar.ShadowlightColour)) return; +/* Sets sky colour, raises event with variable 'SkyCol'. */ +void WorldEnv_SetSkyCol(FastColour col); - FastColour.GetShaded(Shadowlight, out ShadowXSide, - out ShadowZSide, out ShadowYBottom); - Shadow = Shadowlight.Pack(); - game.WorldEvents.RaiseEnvVariableChanged(EnvVar.ShadowlightColour); - } +/* Sets fog colour, raises event with variable 'FogCol'. */ +void WorldEnv_SetFogCol(FastColour col); - bool Set(T value, ref T target, EnvVar var) where T : IEquatable{ - if (value.Equals(target)) return false; - target = value; - game.WorldEvents.RaiseEnvVariableChanged(var); - return true; - } -} -} +/* Sets clouds colour, and raises event with variable 'CloudsCol'. */ +void WorldEnv_SetCloudsCol(FastColour col); + +/* Sets sun colour, and raises event with variable 'SunCol'. */ +void WorldEnv_SetSunCol(FastColour col); + +/* Sets shadow colour, and raises event with variable 'ShadowCol'. */ +void WorldEnv_SetShadowCol(FastColour col); #endif \ No newline at end of file diff --git a/src/Client/WorldEvents.h b/src/Client/WorldEvents.h index 8e43b99e9..638236e30 100644 --- a/src/Client/WorldEvents.h +++ b/src/Client/WorldEvents.h @@ -41,21 +41,21 @@ void WorldEvents_RaiseEnvVariableChanged(Int32 envVar); /* Environment variable identifiers*/ -#define EnvVar_SidesBlock 0 -#define EnvVar_EdgeBlock 1 -#define EnvVar_EdgeLevel 2 -#define EnvVar_CloudsLevel 3 -#define EnvVar_CloudsSpeed 4 -#define EnvVar_Weather 5 +#define EnvVar_EdgeBlock 0 +#define EnvVar_SidesBlock 1 +#define EnvVar_EdgeHeight 2 +#define EnvVar_SidesOffset 3 +#define EnvVar_CloudsHeight 4 +#define EnvVar_CloudsSpeed 5 -#define EnvVar_SkyCol 6 -#define EnvVar_CloudsCol 7 -#define EnvVar_FogCol 8 -#define EnvVar_SunCol 9 -#define EnvVar_ShadowCol 10 +#define EnvVar_WeatherSpeed 6 +#define EnvVar_WeatherFade 7 +#define EnvVar_Weather 8 +#define EnvVar_ExpFog 9 -#define EnvVar_WeatherSpeed 11 -#define EnvVar_WeatherFade 12 -#define EnvVar_ExpFog 13 -#define EnvVar_SidesOffset 14 +#define EnvVar_SkyCol 10 +#define EnvVar_CloudsCol 11 +#define EnvVar_FogCol 12 +#define EnvVar_SunCol 13 +#define EnvVar_ShadowCol 14 #endif \ No newline at end of file