Skip to content

Commit

Permalink
Merge pull request #12 from thiagomvas/bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
thiagomvas authored May 15, 2024
2 parents 4e286a4 + 117e7ea commit b6b3dc6
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Basalt.Core/Basalt.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
4 changes: 2 additions & 2 deletions Basalt.Raylib/Basalt.Raylib.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
20 changes: 4 additions & 16 deletions Basalt.Raylib/Graphics/RaylibGraphicsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RaylibGraphicsEngine : IGraphicsEngine
public string PostProcessingShaderCacheKey { get; set; } = string.Empty;

private readonly WindowInitParams config;
private readonly ILogger? logger;
private ILogger? logger;
internal static RaylibGraphicsEngine instance = new(new());

private EntityManager entityManager;
Expand Down Expand Up @@ -53,12 +53,13 @@ public unsafe void Initialize()
inputSystem = Engine.Instance.GetEngineComponent<IInputSystem>();
eventBus = Engine.Instance.GetEngineComponent<IEventBus>()!;
entityManager = Engine.Instance.EntityManager;
logger = Engine.Instance.Logger;



enablePostProcessing = config.PostProcessing;
instance = this;
SetTraceLogCallback(&LogCustom);
SetTraceLogCallback(&LogToLogger);
SetTraceLogLevel(TraceLogLevel.All);
if (config.Borderless)
SetConfigFlags(ConfigFlags.UndecoratedWindow);
Expand Down Expand Up @@ -191,10 +192,6 @@ public unsafe void Render()

BeginMode3D(control.camera);

#if DEBUG
DrawGrid(100, 1.0f);
#endif

eventBus?.NotifyRender();


Expand All @@ -210,15 +207,6 @@ public unsafe void Render()

}


// Draw info boxes
DrawRectangle(5, 5, 330, 100, ColorAlpha(Color.SkyBlue, 0.5f));
DrawRectangleLines(10, 10, 330, 100, Color.Blue);

DrawText($"Physics Elapsed time: {Time.PhysicsDeltaTime}s - Expected: 0.016s", 15, 30, 10, Color.White);
DrawText($"Update Elapsed time: {Time.DeltaTime}s - Expected: 0.00833s", 15, 45, 10, Color.White);
DrawFPS(15, 105);

EndDrawing();
//----------------------------------------------------------------------------------
}
Expand All @@ -228,7 +216,7 @@ public unsafe void Render()
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
private static unsafe void LogCustom(int logLevel, sbyte* text, sbyte* args)
private static unsafe void LogToLogger(int logLevel, sbyte* text, sbyte* args)
{
var message = Logging.GetLogMessage(new IntPtr(text), new IntPtr(args));
switch ((TraceLogLevel)logLevel)
Expand Down
4 changes: 2 additions & 2 deletions Basalt/Basalt.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>Basalt</Title>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
Expand Down
16 changes: 14 additions & 2 deletions Basalt/Common/Physics/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace Basalt.Common.Physics
{
public class Grid : IChunkingMechanism
{
public List<Entity> Entities = new List<Entity>();
private List<Entity> Entities = new List<Entity>();
private List<Entity> entityAddQueue = new List<Entity>();
private List<Entity> entityRemoveQueue = new List<Entity>();

Dictionary<Point, List<Entity>> chunks = new Dictionary<Point, List<Entity>>();

Expand All @@ -17,12 +19,22 @@ public Grid(int sideLength)

public void AddEntity(Entity entity)
{
Entities.Add(entity);
entityAddQueue.Add(entity);
}

public void RemoveEntity(Entity entity)
{
entityRemoveQueue.Add(entity);
}

public void Update()
{
chunks.Clear();
Entities.AddRange(entityAddQueue);
foreach(var entity in entityRemoveQueue)
{
Entities.Remove(entity);
}

foreach (var entity in Entities)
{
Expand Down
1 change: 1 addition & 0 deletions Basalt/Common/Physics/IChunkingMechanism.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Basalt.Common.Physics
public interface IChunkingMechanism
{
void AddEntity(Entity entity);
void RemoveEntity(Entity entity);
List<List<Entity>> GetEntitiesChunked();
List<Entity> GetEntitiesNearPoint(Vector3 point);
void Update();
Expand Down
13 changes: 12 additions & 1 deletion Basalt/Common/Physics/PhysicsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ public class PhysicsEngine : IPhysicsEngine

public long startTime, elapsedTime;

const float targetDeltaTime = 0.016f;
const int targetFrameTimeMs = 16;

private IChunkingMechanism chunking;
private IEventBus eventBus;
private ILogger? logger;
private bool ShouldRun = true;


/// <summary>
/// Gets or sets the gravity value for the physics engine.
/// </summary>
Expand Down Expand Up @@ -114,11 +114,14 @@ public void Simulate()
if (elapsedTime > targetFrameTimeMs)
{
logger?.LogWarning($"Physics engine is running behind. Elapsed time: {elapsedTime}ms");
Time.PhysicsDeltaTime = elapsedTime / 1000f;
continue;
}

if (elapsedTime < targetFrameTimeMs)
{
Task.Delay((int)(targetFrameTimeMs - elapsedTime)).Wait();
Time.PhysicsDeltaTime = targetDeltaTime;
}
}
}
Expand All @@ -131,5 +134,13 @@ public void AddEntityToSimulation(object entity)
}
}

public void RemoveEntityFromSimulation(object entity)
{
if (entity is Entity e)
{
chunking.RemoveEntity(e);
}
}

}
}
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Change Log

All notable changes to this project will be documented in this file. See [versionize](https://github.com/versionize/versionize) for commit guidelines.

<a name="1.0.1"></a>
## [1.0.1](https://www.github.com/thiagomvas/Basalt/releases/tag/v1.0.1) (2024-05-15)

### Bug Fixes
* No longer throws random exceptions for tightly timed entity instantiation ([e0f1ce0](https://www.github.com/thiagomvas/Basalt/commit/e0f1ce08e3b544ffd07b10e1809656eb94e1a11b))
* Physics Delta Time was not consistent ([e793eba](https://www.github.com/thiagomvas/Basalt/commit/e793eba484f1c517217dfaff2528fdf089544006))
* Raylib Graphics Engine always rendered info boxes ([d03d650](https://www.github.com/thiagomvas/Basalt/commit/d03d6502fa1896d2712af0b4c05991eebfe9e6ef))
* Raylib Graphics Engine logger was always null ([8cdd55a](https://www.github.com/thiagomvas/Basalt/commit/8cdd55acbb13ca378500a70ea55acfac2396618d))

0 comments on commit b6b3dc6

Please sign in to comment.