Implement WorldEnv in C

This commit is contained in:
UnknownShadow200 2017-05-10 17:56:25 +10:00
parent 63942793f0
commit dde7c6258e
6 changed files with 194 additions and 152 deletions

View File

@ -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

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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 !!!
/// <summary> Sets sides block to the given block, and raises
/// EnvVariableChanged event with variable 'SidesBlock'. </summary>
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);
/// <summary> Sets edge block to the given block, and raises
/// EnvVariableChanged event with variable 'EdgeBlock'. </summary>
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);
/// <summary> Sets clouds height in world space, and raises
/// EnvVariableChanged event with variable 'CloudsLevel'. </summary>
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);
/// <summary> Sets clouds speed, and raises EnvVariableChanged
/// event with variable 'CloudsSpeed'. </summary>
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);
/// <summary> Sets weather speed, and raises EnvVariableChanged
/// event with variable 'WeatherSpeed'. </summary>
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);
/// <summary> Sets weather fade rate, and raises EnvVariableChanged
/// event with variable 'WeatherFade'. </summary>
public void SetWeatherFade(float rate) { Set(rate, ref WeatherFade, EnvVar.WeatherFade); }
/// <summary> Sets height of the map edges in world space, and raises
/// EnvVariableChanged event with variable 'EdgeLevel'. </summary>
public void SetEdgeLevel(int level) { Set(level, ref EdgeHeight, EnvVar.EdgeLevel); }
/// <summary> Sets offset of the height of the map sides from map edges in world space, and raises
/// EnvVariableChanged event with variable 'SidesLevel'. </summary>
public void SetSidesOffset(int level) { Set(level, ref SidesOffset, EnvVar.SidesOffset); }
/// <summary> Sets whether exponential fog is used, and raises
/// EnvVariableChanged event with variable 'ExpFog'. </summary>
public void SetExpFog(bool expFog) { Set(expFog, ref ExpFog, EnvVar.ExpFog); }
/// <summary> Sets weather, and raises
/// EnvVariableChanged event with variable 'Weather'. </summary>
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);
/// <summary> Sets tsky colour, and raises
/// EnvVariableChanged event with variable 'SkyColour'. </summary>
public void SetSkyColour(FastColour col) { Set(col, ref SkyCol, EnvVar.SkyColour); }
/* Sets weather speed, raises event with variable 'WeatherSpeed'. */
void WorldEnv_SetWeatherSpeed(Real32 speed);
/// <summary> Sets fog colour, and raises
/// EnvVariableChanged event with variable 'FogColour'. </summary>
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);
/// <summary> Sets clouds colour, and raises
/// EnvVariableChanged event with variable 'CloudsColour'. </summary>
public void SetCloudsColour(FastColour col) { Set(col, ref CloudsCol, EnvVar.CloudsColour); }
/* Sets weather, raises event with variable 'Weather'. */
void WorldEnv_SetWeather(Int32 weather);
/// <summary> Sets sunlight colour, and raises
/// EnvVariableChanged event with variable 'SunlightColour'. </summary>
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);
}
/// <summary> Sets current shadowlight colour, and raises
/// EnvVariableChanged event with variable 'ShadowlightColour'. </summary>
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>(T value, ref T target, EnvVar var) where T : IEquatable<T>{
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

View File

@ -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