Skip to content

Commit 20b81f3

Browse files
committed
Fix monogame bench
1 parent 8c44f07 commit 20b81f3

7 files changed

Lines changed: 34 additions & 12 deletions

File tree

src/Hexecs.Benchmarks.MonoGame/BenchmarkGame.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ public BenchmarkGame()
4848
};
4949

5050
// Включаем поддержку сглаживания для устройства
51-
_graphics.PreparingDeviceSettings += (sender, e) =>
51+
_graphics.PreparingDeviceSettings += (_, e) =>
5252
{
5353
e.GraphicsDeviceInformation.PresentationParameters.MultiSampleCount = 8; // 8x MSAA
5454
};
5555

56+
_graphics.ApplyChanges();
57+
5658
IsFixedTimeStep = false;
5759
}
5860

@@ -87,15 +89,18 @@ protected override void Initialize()
8789
}
8890

8991

90-
private void SpawnEntity()
92+
private void SpawnEntity(CircleColor? color = null)
9193
{
9294
var actor = _context.CreateActor();
93-
actor.Add(new Position(new Vector2(_random.Next(_graphics.PreferredBackBufferWidth),
94-
_random.Next(_graphics.PreferredBackBufferHeight))));
95-
actor.Add(new Velocity(new Vector2((float)(_random.NextDouble() * 200 - 100),
96-
(float)(_random.NextDouble() * 200 - 100))));
97-
actor.Add(new CircleColor(new Color((byte)_random.Next(256), (byte)_random.Next(256), (byte)_random.Next(256),
98-
(byte)255)));
95+
actor.Add(Position.Create(
96+
x: _graphics.PreferredBackBufferWidth / 2,
97+
y: _graphics.PreferredBackBufferHeight / 2));
98+
99+
actor.Add(Velocity.Create(
100+
x: (float)(_random.NextDouble() * 200 - 100),
101+
y: (float)(_random.NextDouble() * 200 - 100)));
102+
103+
actor.Add(color ?? CircleColor.CreateRgba(_random));
99104
}
100105

101106
protected override void Update(GameTime gameTime)
@@ -105,14 +110,15 @@ protected override void Update(GameTime gameTime)
105110
var keyboard = Keyboard.GetState();
106111
if (keyboard.IsKeyDown(Keys.Space))
107112
{
113+
var color = CircleColor.CreateRgba(_random);
108114
for (var i = 0; i < 50; i++)
109115
{
110116
if (count >= MaxEntityCount)
111117
{
112118
break;
113119
}
114120

115-
SpawnEntity();
121+
SpawnEntity(color);
116122
}
117123
}
118124

@@ -160,7 +166,7 @@ protected override void Update(GameTime gameTime)
160166

161167
protected override void Draw(GameTime gameTime)
162168
{
163-
GraphicsDevice.Clear(Color.Black);
169+
GraphicsDevice.Clear(Color.White);
164170

165171
_world.Draw(gameTime.ElapsedGameTime, gameTime.TotalGameTime);
166172

src/Hexecs.Benchmarks.MonoGame/Components/CircleColor.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ namespace Hexecs.Benchmarks.MonoGame.Components;
55

66
public readonly struct CircleColor(Color value) : IActorComponent
77
{
8+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9+
public static CircleColor CreateRgba(Random random, byte? alpha = null)
10+
{
11+
var r = (byte)random.Next(256);
12+
var g = (byte)random.Next(256);
13+
var b = (byte)random.Next(256);
14+
15+
return new CircleColor(new Color(r, g, b, alpha ?? 255));
16+
}
17+
818
public readonly Color Value = value;
919

1020
public static implicit operator CircleColor(Color value) => new(value);

src/Hexecs.Benchmarks.MonoGame/Components/Position.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ namespace Hexecs.Benchmarks.MonoGame.Components;
55

66
public struct Position(Vector2 value) : IActorComponent
77
{
8+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9+
public static Position Create(int x, int y) => new(new Vector2(x, y));
10+
811
public Vector2 Value = value;
912

1013
public static implicit operator Position(Vector2 value) => new(value);

src/Hexecs.Benchmarks.MonoGame/Components/Velocity.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Hexecs.Benchmarks.MonoGame.Components;
55

66
public struct Velocity(Vector2 value) : IActorComponent
77
{
8+
public static Velocity Create(float x, float y) => new(new Vector2(x, y));
9+
810
public Vector2 Value = value;
911

1012
public static implicit operator Velocity(Vector2 value) => new(value);

src/Hexecs.Benchmarks.MonoGame/Systems/MovementSystem.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Runtime.CompilerServices;
21
using Hexecs.Actors;
32
using Hexecs.Actors.Systems;
43
using Hexecs.Benchmarks.MonoGame.Components;

src/Hexecs.Benchmarks.MonoGame/Systems/RenderSystem.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public override void Draw(in WorldTime time)
7070
if (i >= _hostBuffer.Length) break;
7171

7272
ref var data = ref _hostBuffer[i];
73-
// Позиция + масштаб (3.0f)
7473
data.PositionScale = new Vector4(actor.Component1.Value.X, actor.Component1.Value.Y, 4.0f, 0f);
7574
data.Color = actor.Component2.Value;
7675
i++;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Global using directives
2+
3+
global using System.Runtime.CompilerServices;

0 commit comments

Comments
 (0)