Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Add some profiler stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Jan 22, 2024
1 parent 978cc53 commit 86a2612
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 49 deletions.
9 changes: 7 additions & 2 deletions Client/Keybinding/Keybinds.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Veldrid;
using Voxel.Common.Util.Profiling;
using Voxel.Core.Input.Gamepad;
using VMouseButton = Veldrid.MouseButton;

Expand Down Expand Up @@ -112,8 +113,12 @@ public static void WriteToConfig() {
ClientConfig.keybindings = keybinds;
}

private static readonly Profiler.ProfilerKey BindKey = Profiler.GetProfilerKey("Poll Keybinds");

public static void Poll() {
foreach (var bind in Keybindings.Values)
bind.Poll();
using (BindKey.Push()) {
foreach (var bind in Keybindings.Values)
bind.Poll();
}
}
}
7 changes: 4 additions & 3 deletions Client/Rendering/CameraStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ public CameraStateManager(RenderSystem system) {
));
}

public void SetToCamera(Camera c, double timeSinceLastTick) {
public void SetToCamera(Camera c) {
c.UpdateFrustum();
currentCameraPosition = c.position;

var data = new CameraData();
data.VPMatrix = ((quat)c.rotationVec.RotationVecToQuat()).ToMat4 * mat4.Perspective(-c.fovy, c.aspect, c.nearClip, c.farClip).Transposed;
var data = new CameraData {
VPMatrix = ((quat)c.rotationVec.RotationVecToQuat()).ToMat4 * mat4.Perspective(-c.fovy, c.aspect, c.nearClip, c.farClip).Transposed
};
CameraBuffer.value = data;
}

Expand Down
2 changes: 1 addition & 1 deletion Client/Rendering/GameRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public override void Render(double delta) {

MainCamera.position = Client.PlayerEntity?.SmoothPosition(Client.smoothFactor) + Client.PlayerEntity?.eyeOffset ?? dvec3.Zero;
MainCamera.rotationVec = Client.PlayerEntity?.rotation ?? dvec2.Zero;
CameraStateManager.SetToCamera(MainCamera, Client.timeSinceLastTick);
CameraStateManager.SetToCamera(MainCamera);

WorldRenderer.Render(delta);
GuiRenderer.Render(delta);
Expand Down
88 changes: 46 additions & 42 deletions Client/World/Entity/ControlledClientPlayerEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
using Voxel.Common.Network.Packets.C2S.Gameplay.Actions;
using Voxel.Common.Network.Packets.Utils;
using Voxel.Common.Util;
using Voxel.Common.Util.Profiling;
using Voxel.Core.Util;

namespace Voxel.Client.World.Entity;

public class ControlledClientPlayerEntity : ClientPlayerEntity {
private static readonly Profiler.ProfilerKey PlayerKey = Profiler.GetProfilerKey("Update Player Input");

private dvec3 vel;

Expand All @@ -23,63 +25,65 @@ public ControlledClientPlayerEntity() {
}

public void Update(double delta) {
var movement = Keybinds.Move.axis;
var looking = -Keybinds.Look.axis;
using (PlayerKey.Push()) {
var movement = Keybinds.Move.axis;
var looking = -Keybinds.Look.axis;

movement += new dvec2(0, -1) * Keybinds.Forward.strength;
movement += new dvec2(0, 1) * Keybinds.Backward.strength;
movement += new dvec2(-1, 0) * Keybinds.StrafeLeft.strength;
movement += new dvec2(1, 0) * Keybinds.StrafeRight.strength;
movement += new dvec2(0, -1) * Keybinds.Forward.strength;
movement += new dvec2(0, 1) * Keybinds.Backward.strength;
movement += new dvec2(-1, 0) * Keybinds.StrafeLeft.strength;
movement += new dvec2(1, 0) * Keybinds.StrafeRight.strength;

if (Keybinds.LookLeft.isPressed || Keybinds.LookRight.isPressed)
cameraPanTimers.x += (float)delta * MathF.Abs((float)(Keybinds.LookLeft.strength - Keybinds.LookRight.strength));
else
cameraPanTimers.x = 0;
if (Keybinds.LookUp.isPressed || Keybinds.LookDown.isPressed)
cameraPanTimers.y += (float)delta * MathF.Abs((float)(Keybinds.LookUp.strength - Keybinds.LookDown.strength));
else
cameraPanTimers.y = 0;
if (Keybinds.LookLeft.isPressed || Keybinds.LookRight.isPressed)
cameraPanTimers.x += (float)delta * MathF.Abs((float)(Keybinds.LookLeft.strength - Keybinds.LookRight.strength));
else
cameraPanTimers.x = 0;
if (Keybinds.LookUp.isPressed || Keybinds.LookDown.isPressed)
cameraPanTimers.y += (float)delta * MathF.Abs((float)(Keybinds.LookUp.strength - Keybinds.LookDown.strength));
else
cameraPanTimers.y = 0;

looking += new dvec2(Keybinds.LookLeft.strength - Keybinds.LookRight.strength, Keybinds.LookUp.strength - Keybinds.LookDown.strength) * new dvec2(cameraPanEase.F(cameraPanTimers.x), cameraPanEase.F(cameraPanTimers.y));
looking += new dvec2(Keybinds.LookLeft.strength - Keybinds.LookRight.strength, Keybinds.LookUp.strength - Keybinds.LookDown.strength) * new dvec2(cameraPanEase.F(cameraPanTimers.x), cameraPanEase.F(cameraPanTimers.y));

if (movement.LengthSqr > 1)
movement = movement.Normalized;
if (movement.LengthSqr > 1)
movement = movement.Normalized;

var movement3d = new dvec3(movement.x, 0, movement.y);
var movement3d = new dvec3(movement.x, 0, movement.y);

// movement3d += new dvec3(0, 1, 0) * Keybinds.Jump.strength;
// movement3d += new dvec3(0, -1, 0) * Keybinds.Crouch.strength;
// movement3d += new dvec3(0, 1, 0) * Keybinds.Jump.strength;
// movement3d += new dvec3(0, -1, 0) * Keybinds.Crouch.strength;

rotation += new dvec2((float)(looking.y * delta) * 1, (float)(looking.x * delta) * 1);
if (VoxelClient.isMouseCapruted)
rotation += VoxelClient.Instance.InputManager.MouseDelta.swizzle.yx * -1 / 192;
rotation += new dvec2((float)(looking.y * delta) * 1, (float)(looking.x * delta) * 1);
if (VoxelClient.isMouseCapruted)
rotation += VoxelClient.Instance.InputManager.MouseDelta.swizzle.yx * -1 / 192;

if (rotation.x < -MathF.PI/2)
rotation.x = -MathF.PI/2;
if (rotation.x > MathF.PI/2)
rotation.x = MathF.PI/2;
if (rotation.x < -MathF.PI/2)
rotation.x = -MathF.PI/2;
if (rotation.x > MathF.PI/2)
rotation.x = MathF.PI/2;

movement3d = new dvec2(0, rotation.y).RotationVecToQuat() * movement3d * 4;
var localVel = dvec2.Lerp(velocity.xz, movement3d.xz, 0.9);
velocity = velocity.WithXZ(localVel);
movement3d = new dvec2(0, rotation.y).RotationVecToQuat() * movement3d * 4;
var localVel = dvec2.Lerp(velocity.xz, movement3d.xz, 0.9);
velocity = velocity.WithXZ(localVel);

if (Keybinds.Jump.isPressed)
Jump();
if (Keybinds.Jump.isPressed)
Jump();

if (Keybinds.Crouch.justPressed)
position -= new dvec3(0, 1, 0);
if (Keybinds.Crouch.justPressed)
position -= new dvec3(0, 1, 0);

var transformUpdate = PacketPool.GetPacket<PlayerUpdated>();
transformUpdate.Position = position;
transformUpdate.Rotation = rotation;
VoxelClient.Instance.connection!.SendPacket(transformUpdate);
var transformUpdate = PacketPool.GetPacket<PlayerUpdated>();
transformUpdate.Position = position;
transformUpdate.Rotation = rotation;
VoxelClient.Instance.connection!.SendPacket(transformUpdate);


if (Keybinds.Attack.justPressed)
BreakBlock();
if (Keybinds.Attack.justPressed)
BreakBlock();

if (Keybinds.Use.justPressed)
PlaceBlock();
if (Keybinds.Use.justPressed)
PlaceBlock();
}
}

private void BreakBlock() {
Expand Down
2 changes: 1 addition & 1 deletion Core/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void Run(int tps = 20, string windowTitle = "Game") {
var gdo = new GraphicsDeviceOptions {
PreferDepthRangeZeroToOne = true,
PreferStandardClipSpaceYDirection = true,
SyncToVerticalBlank = true,
SyncToVerticalBlank = false,
};

VeldridStartup.CreateWindowAndGraphicsDevice(wci, gdo, GraphicsBackend.Vulkan, out var nw, out var gd);
Expand Down

0 comments on commit 86a2612

Please sign in to comment.