From cf8812a03c0696f44b53f38f0afe927dc7fda8be Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 15 Sep 2017 22:19:44 +1000 Subject: [PATCH] Port Options.Set to C. --- ClassicalSharp/Rendering/Env/EnvRenderer.cs | 3 +- .../Rendering/Env/StandardEnvRenderer.cs | 4 +- src/Client/Camera.c | 2 +- src/Client/D3D9Api.c | 2 +- src/Client/Entity.c | 2 +- src/Client/EnvRenderer.c | 7 ++- src/Client/Game.c | 15 ++++++ src/Client/ModelCache.c | 2 +- src/Client/Options.c | 53 +++++++++++++++++-- src/Client/Options.h | 3 ++ src/Client/Picking.c | 8 +-- src/Client/String.c | 4 +- src/Client/String.h | 1 + 13 files changed, 85 insertions(+), 21 deletions(-) diff --git a/ClassicalSharp/Rendering/Env/EnvRenderer.cs b/ClassicalSharp/Rendering/Env/EnvRenderer.cs index 6102bee47..20471b988 100644 --- a/ClassicalSharp/Rendering/Env/EnvRenderer.cs +++ b/ClassicalSharp/Rendering/Env/EnvRenderer.cs @@ -50,7 +50,7 @@ namespace ClassicalSharp.Renderers { protected abstract void EnvVariableChanged(object sender, EnvVarEventArgs e); - protected BlockID BlockOn(out float fogDensity, out FastColour fogCol) { + protected void BlockOn(out float fogDensity, out FastColour fogCol) { Vector3 pos = game.CurrentCameraPos; Vector3I coords = Vector3I.Floor(pos); @@ -68,7 +68,6 @@ namespace ClassicalSharp.Renderers { float blend = (float)BlendFactor(game.ViewDistance); fogCol = FastColour.Lerp(map.Env.FogCol, map.Env.SkyCol, blend); } - return block; } double BlendFactor(float x) { diff --git a/ClassicalSharp/Rendering/Env/StandardEnvRenderer.cs b/ClassicalSharp/Rendering/Env/StandardEnvRenderer.cs index 3eb186de7..564ed8440 100644 --- a/ClassicalSharp/Rendering/Env/StandardEnvRenderer.cs +++ b/ClassicalSharp/Rendering/Env/StandardEnvRenderer.cs @@ -105,7 +105,7 @@ namespace ClassicalSharp.Renderers { if (map.blocks == null) return; FastColour fogCol = FastColour.White; float fogDensity = 0; - BlockID block = BlockOn(out fogDensity, out fogCol); + BlockOn(out fogDensity, out fogCol); gfx.ClearColour(fogCol); // TODO: rewrite this to avoid raising the event? want to avoid recreating vbos too many times often @@ -167,7 +167,7 @@ namespace ClassicalSharp.Renderers { if (map.blocks == null || minimal) return; FastColour fogCol = FastColour.White; float fogDensity = 0; - BlockID block = BlockOn(out fogDensity, out fogCol); + BlockOn(out fogDensity, out fogCol); if (fogDensity != 0) { gfx.SetFogMode(Fog.Exp); diff --git a/src/Client/Camera.c b/src/Client/Camera.c index 74f1fbe68..9203e6a05 100644 --- a/src/Client/Camera.c +++ b/src/Client/Camera.c @@ -14,7 +14,7 @@ Real32 Camera_AdjustHeadX(Real32 value) { return value * MATH_DEG2RAD; } -Vector3 PerspectiveCamera_GetDirVector() { +Vector3 PerspectiveCamera_GetDirVector(void) { Entity* p = &LocalPlayer_Instance.Base.Base; Real32 yaw = p->HeadY * MATH_DEG2RAD; Real32 pitch = Camera_AdjustHeadX(p->HeadX); diff --git a/src/Client/D3D9Api.c b/src/Client/D3D9Api.c index 500e93a71..2476f7277 100644 --- a/src/Client/D3D9Api.c +++ b/src/Client/D3D9Api.c @@ -744,7 +744,7 @@ void Gfx_BeginFrame(void) { void Gfx_EndFrame(void) { IDirect3DDevice9_EndScene(device); ReturnCode code = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL); - if (code >= 0) return; + if ((Int32)code >= 0) return; if (code != D3DERR_DEVICELOST) { ErrorHandler_FailWithCode(code, "D3D9_EndFrame"); diff --git a/src/Client/Entity.c b/src/Client/Entity.c index 45d0a7725..04fb1a543 100644 --- a/src/Client/Entity.c +++ b/src/Client/Entity.c @@ -32,7 +32,7 @@ void LocationUpdate_Empty(LocationUpdate* update) { } void LocationUpdate_MakeOri(LocationUpdate* update, Real32 rotY, Real32 headX) { - LocationUpdate_Construct(update, 0.0f, 0.0f, 0.0f, exc, rotY, exc, exc, false, false); + LocationUpdate_Construct(update, 0.0f, 0.0f, 0.0f, exc, rotY, exc, headX, false, false); } void LocationUpdate_MakePos(LocationUpdate* update, Vector3 pos, bool rel) { diff --git a/src/Client/EnvRenderer.c b/src/Client/EnvRenderer.c index fa887b255..61588685e 100644 --- a/src/Client/EnvRenderer.c +++ b/src/Client/EnvRenderer.c @@ -22,7 +22,7 @@ Real32 EnvRenderer_BlendFactor(Real32 x) { return blend; } -BlockID EnvRenderer_BlockOn(Real32* fogDensity, PackedCol* fogCol) { +void EnvRenderer_BlockOn(Real32* fogDensity, PackedCol* fogCol) { Vector3 pos = Game_CurrentCameraPos; Vector3I coords; Vector3I_Floor(&coords, &pos); /* coords = floor(pos); */ @@ -42,7 +42,6 @@ BlockID EnvRenderer_BlockOn(Real32* fogDensity, PackedCol* fogCol) { Real32 blend = EnvRenderer_BlendFactor(Game_ViewDistance); *fogCol = PackedCol_Lerp(WorldEnv_FogCol, WorldEnv_SkyCol, blend); } - return block; } @@ -51,7 +50,7 @@ void EnvRenderer_RenderMinimal(Real64 deltaTime) { PackedCol fogCol = PackedCol_White; Real32 fogDensity = 0.0f; - BlockID block = EnvRenderer_BlockOn(&fogDensity, &fogCol); + EnvRenderer_BlockOn(&fogDensity, &fogCol); Gfx_ClearColour(fogCol); /* TODO: rewrite this to avoid raising the event? want to avoid recreating vbos too many times often */ @@ -114,7 +113,7 @@ void EnvRenderer_UpdateFog(void) { if (World_Blocks == NULL || EnvRenderer_Minimal) return; PackedCol fogCol = PackedCol_White; Real32 fogDensity = 0.0f; - BlockID block = EnvRenderer_BlockOn(&fogDensity, &fogCol); + EnvRenderer_BlockOn(&fogDensity, &fogCol); if (fogDensity != 0.0f) { Gfx_SetFogMode(Fog_Exp); diff --git a/src/Client/Game.c b/src/Client/Game.c index f679d914d..73e6b754b 100644 --- a/src/Client/Game.c +++ b/src/Client/Game.c @@ -7,6 +7,7 @@ #include "GraphicsAPI.h" #include "Camera.h" #include "Options.h" +#include "Funcs.h" void Game_UpdateProjection(void) { Game_DefaultFov = Options_GetInt(OptionsKey_FieldOfView, 1, 150, 70); @@ -34,6 +35,20 @@ void Game_UpdateBlock(Int32 x, Int32 y, Int32 z, BlockID block) { MapRenderer_RefreshChunk(cx, cy, cz); } +void Game_SetViewDistance(Real32 distance, bool userDist) { + if (userDist) { + Game_UserViewDistance = distance; + Options_SetInt32(OptionsKey_ViewDist, (Int32)distance); + } + + distance = min(distance, Game_MaxViewDistance); + if (distance == Game_ViewDistance) return; + Game_ViewDistance = distance; + + Event_RaiseVoid(&GfxEvents_ViewDistanceChanged); + Game_UpdateProjection(); +} + bool Game_CanPick(BlockID block) { if (Block_Draw[block] == DrawType_Gas) return false; if (Block_Draw[block] == DrawType_Sprite) return true; diff --git a/src/Client/ModelCache.c b/src/Client/ModelCache.c index c25dd7e4c..401a3d577 100644 --- a/src/Client/ModelCache.c +++ b/src/Client/ModelCache.c @@ -1134,7 +1134,7 @@ void BlockModel_Flush(void) { } #define BlockModel_FlushIfNotSame if (BlockModel_lastTexIndex != BlockModel_texIndex) { BlockModel_Flush(); } -Int32 BlockModel_GetTex(Face face) { +TextureLoc BlockModel_GetTex(Face face) { TextureLoc texLoc = Block_GetTexLoc(BlockModel_block, face); BlockModel_texIndex = texLoc / Atlas1D_ElementsPerAtlas; BlockModel_FlushIfNotSame; diff --git a/src/Client/Options.c b/src/Client/Options.c index 15e0e5e98..989aa7412 100644 --- a/src/Client/Options.c +++ b/src/Client/Options.c @@ -1,5 +1,6 @@ #include "Options.h" #include "ExtMath.h" +#include "ErrorHandler.h" UInt8 Options_KeysBuffer[String_BufferSize(26) * OPTIONS_COUNT]; UInt8 Options_ValuesBuffer[ @@ -28,7 +29,7 @@ void Options_Init(void) { Options_InitStrs(Options_Values, OPTIONS_LARGESTRS, 512); } -Int32 Option_Find(String key) { +Int32 Options_Find(String key) { Int32 i; for (i = 0; i < OPTIONS_COUNT; i++) { if (String_CaselessEquals(&Options_Keys[i], &key)) return i; @@ -39,14 +40,14 @@ Int32 Option_Find(String key) { bool Options_TryGetValue(const UInt8* keyRaw, String* value) { String key = String_FromReadonly(keyRaw); *value = String_MakeNull(); - Int32 i = Option_Find(key); + Int32 i = Options_Find(key); if (i >= 0) { *value = Options_Values[i]; return true; } Int32 sepIndex = String_IndexOf(&key, '-', 0); if (sepIndex == -1) return false; key = String_UNSAFE_SubstringAt(&key, sepIndex + 1); - i = Option_Find(key); + i = Options_Find(key); if (i >= 0) { *value = Options_Values[i]; return true; } return false; } @@ -84,4 +85,50 @@ Real32 Options_GetFloat(const UInt8* key, Real32 min, Real32 max, Real32 defValu Math_Clamp(value, min, max); return value; +} + +void Options_Remove(Int32 i) { + String_Clear(&Options_Keys[i]); + String_Clear(&Options_Values[i]); +} + +Int32 Options_Insert(String key, String value) { + Int32 i = Options_Find(key); + /* The new value may not fit in the old slot, always insert into a new slot. */ + if (i >= 0) { + Options_Remove(i); + Options_Changed[i] = false; + } + + for (i = 0; i < OPTIONS_COUNT; i++) { + if (Options_Keys[i].length > 0) continue; + if (Options_Values[i].capacity < value.length) continue; + + String_AppendString(&Options_Keys[i], &key); + String_AppendString(&Options_Values[i], &value); + return i; + } + + ErrorHandler_Fail("No free slot left to save option"); + return -1; +} + +void Options_SetInt32(const UInt8* keyRaw, Int32 value) { + UInt8 numBuffer[String_BufferSize(STRING_INT32CHARS)]; + UInt8* ptr = numBuffer; + String numStr = String_FromRawBuffer(ptr, STRING_INT32CHARS); + String_AppendInt32(&numStr, value); + Options_Set(keyRaw, numStr); +} + +void Options_Set(const UInt8* keyRaw, String value) { + String key = String_FromReadonly(keyRaw); + Int32 i; + if (value.buffer == NULL) { + i = Options_Find(key); + if (i >= 0) Options_Remove(i); + } else { + i = Options_Insert(key, value); + } + if (i >= 0) Options_Changed[i] = true; } \ No newline at end of file diff --git a/src/Client/Options.h b/src/Client/Options.h index 1db5cb6bc..92cdd1854 100644 --- a/src/Client/Options.h +++ b/src/Client/Options.h @@ -84,4 +84,7 @@ String Options_Get(const UInt8* key); Int32 Options_GetInt(const UInt8* key, Int32 min, Int32 max, Int32 defValue); bool Options_GetBool(const UInt8* key, bool defValue); Real32 Options_GetFloat(const UInt8* key, Real32 min, Real32 max, Real32 defValue); + +void Options_SetInt32(const UInt8* keyRaw, Int32 value); +void Options_Set(const UInt8* keyRaw, String value); #endif \ No newline at end of file diff --git a/src/Client/Picking.c b/src/Client/Picking.c index 2319d0f24..1ebe6698e 100644 --- a/src/Client/Picking.c +++ b/src/Client/Picking.c @@ -90,13 +90,11 @@ void RayTracer_Step(RayTracer* t) { /* tMax.X is the lowest, an YZ cell boundary plane is nearest. */ t->X += t->step.X; t->tMax.X += t->tDelta.X; - } - else if (t->tMax.Y < t->tMax.Z) { + } else if (t->tMax.Y < t->tMax.Z) { /* tMax.Y is the lowest, an XZ cell boundary plane is nearest. */ t->Y += t->step.Y; t->tMax.Y += t->tDelta.Y; - } - else { + } else { /* tMax.Z is the lowest, an XY cell boundary plane is nearest. */ t->Z += t->step.Z; t->tMax.Z += t->tDelta.Z; @@ -165,7 +163,9 @@ bool Picking_RayTrace(Vector3 origin, Vector3 dir, Real32 reach, PickedPos* pos, if (intersect(pos)) return true; RayTracer_Step(&tracer); } + ErrorHandler_Fail("Something went wrong, did over 10,000 iterations in Picking_RayTrace()"); + return false; } bool Picking_ClipBlock(PickedPos* pos) { diff --git a/src/Client/String.c b/src/Client/String.c index fb6bab1f2..26e47693c 100644 --- a/src/Client/String.c +++ b/src/Client/String.c @@ -121,7 +121,7 @@ bool String_AppendInt32(STRING_TRANSIENT String* str, Int32 num) { if (!String_Append(str, (UInt8)'-')) return false; } - UInt8 numBuffer[20]; + UInt8 numBuffer[STRING_INT32CHARS]; Int32 numLen = String_MakeInt32(num, numBuffer); Int32 i; @@ -132,7 +132,7 @@ bool String_AppendInt32(STRING_TRANSIENT String* str, Int32 num) { } bool String_AppendPaddedInt32(STRING_TRANSIENT String* str, Int32 num, Int32 minDigits) { - UInt8 numBuffer[20]; + UInt8 numBuffer[STRING_INT32CHARS]; Int32 i; for (i = 0; i < minDigits; i++) { numBuffer[i] = '0'; diff --git a/src/Client/String.h b/src/Client/String.h index fe6bb2ffc..ced7e2ebb 100644 --- a/src/Client/String.h +++ b/src/Client/String.h @@ -9,6 +9,7 @@ */ #define String_BufferSize(n) (n + 1) +#define STRING_INT32CHARS 20 typedef struct String_ { /* Pointer to raw characters. Size is capacity + 1, as buffer is null terminated. */