• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DemoBytom / DemoEngine / 13252710786

10 Feb 2025 11:43PM UTC coverage: 10.337% (-0.1%) from 10.456%
13252710786

push

coveralls.net

DemoBytom
Further refactoring the main loop WIP

227 of 2196 relevant lines covered (10.34%)

23027.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Demo.Engine.Core/Services/EngineService.cs
1
// Copyright © Michał Dembski and contributors.
2
// Distributed under MIT license. See LICENSE file in the root for more information.
3

4
using System.Numerics;
5
using Demo.Engine.Core.Components.Keyboard;
6
using Demo.Engine.Core.Interfaces;
7
using Demo.Engine.Core.Interfaces.Rendering;
8
using Demo.Engine.Core.Platform;
9
using Demo.Engine.Core.ValueObjects;
10
using Microsoft.Extensions.DependencyInjection;
11
using Microsoft.Extensions.Hosting;
12
using Microsoft.Extensions.Logging;
13
using Vortice.Mathematics;
14

15
namespace Demo.Engine.Core.Services;
16

17
public class EngineService : ServiceBase
18
{
19
    private bool _disposedValue;
20
    private readonly CancellationTokenSource _loopCancellationTokenSource = new();
×
21
    private readonly IServiceScopeFactory _scopeFactory;
22

23
    public EngineService(
24
        IHostApplicationLifetime applicationLifetime,
25
        ILogger<EngineService> logger,
26
        IServiceScopeFactory scopeFactory)
27
        : base("Engine",
×
28
              logger, applicationLifetime) => _scopeFactory = scopeFactory;
×
29

30
    private IServiceProvider? _sp;
31

32
    protected override async Task ExecuteAsync()
33
    {
34
        using var scope = _scopeFactory.CreateScope();
×
35
        _sp = scope.ServiceProvider;
×
36
        _drawables =
×
37
            [
×
38
                scope.ServiceProvider.GetRequiredService<ICube>(),
×
39
                //scope.ServiceProvider.GetRequiredService<ICube>()
×
40
            ];
×
41
        var mainLoop = scope.ServiceProvider.GetRequiredService<IMainLoopService>();
×
42

43
        await mainLoop.RunAsync(
×
44
            Update,
×
45
            Render,
×
46
            _loopCancellationTokenSource.Token)
×
47
            .ConfigureAwait(true);
×
48
        _sp = null;
×
49
    }
×
50

51
    private float _r, _g, _b = 0.0f;
52

53
    private float _sin = 0.0f;
54
    private bool _fullscreen = false;
55
    private bool _f11Pressed = false;
56

57
    //private bool _dontCreate = false;
58
    private ValueTask Update(
59
        IRenderingSurface renderingSurface,
60
        KeyboardHandle keyboardHandle,
61
        KeyboardCharCache keyboardCharCache)
62
    {
63
        if (keyboardHandle.GetKeyPressed(VirtualKeys.OemOpenBrackets))
×
64
        {
65
            keyboardCharCache.Clear();
×
66
        }
67
        if (keyboardHandle.GetKeyPressed(VirtualKeys.OemCloseBrackets))
×
68
        {
69
            var str = keyboardCharCache?.ReadCache();
×
70
            if (!string.IsNullOrEmpty(str))
×
71
            {
72
                _logger.LogInformation(str);
×
73
            }
74
        }
75
        if (keyboardHandle.GetKeyPressed(VirtualKeys.Escape))
×
76
        {
77
            _loopCancellationTokenSource.Cancel();
×
78
            foreach (var drawable in _drawables)
×
79
            {
80
                (drawable as IDisposable)?.Dispose();
×
81
            }
82

83
            _drawables = [];
×
84
            return ValueTask.CompletedTask;
×
85
        }
86
        if (keyboardHandle.GetKeyPressed(VirtualKeys.F11))
×
87
        {
88
            if (!_f11Pressed)
×
89
            {
90
                _fullscreen = !_fullscreen;
×
91
            }
92
            _f11Pressed = true;
×
93
        }
94
        else
95
        {
96
            _f11Pressed = false;
×
97
        }
98

99
        if (keyboardHandle.GetKeyPressed(VirtualKeys.Back)
×
100
            && _drawables.ElementAtOrDefault(0) is IDisposable d)
×
101
        {
102
            System.Diagnostics.Debug.WriteLine("Removing cube!");
103

104
            _drawables = _drawables.Length > 0
×
105
                ? _drawables[1..]
×
106
                : [];
×
107

108
            d?.Dispose();
×
109

110
            System.Diagnostics.Debug.WriteLine("Cube removed!");
111
        }
112
        if (keyboardHandle.GetKeyPressed(VirtualKeys.Enter)
×
113
            && _drawables.Length < 2
×
114
            && _sp is not null)
×
115
        {
116
            System.Diagnostics.Debug.WriteLine("Adding new Cube!");
117
            _drawables = new List<ICube>(_drawables)
×
118
                {
×
119
                    _sp.GetRequiredService<ICube>()
×
120
                }.ToArray();
×
121
            System.Diagnostics.Debug.WriteLine("Cube added!!");
122
        }
123

124
        //if (_drawables.Length == 2)
125
        //{
126
        //    foreach (var drawable in _drawables)
127
        //    {
128
        //        (drawable as IDisposable)?.Dispose();
129
        //    }
130

131
        //    _drawables = Array.Empty<ICube>();
132
        //}
133
        //else if (_drawables.Length < 2 && _sp is not null /*&& _dontCreate == false*/)
134
        //{
135
        //    _drawables = new List<ICube>(_drawables)
136
        //        {
137
        //            _sp.GetRequiredService<ICube>()
138
        //        }.ToArray();
139
        //    //_dontCreate = true;
140
        //}
141

142
        //Share the rainbow
143
        _r = MathF.Sin((_sin + 0) * MathF.PI / 180);
×
144
        _g = MathF.Sin((_sin + 120) * MathF.PI / 180);
×
145
        _b = MathF.Sin((_sin + 240) * MathF.PI / 180);
×
146

147
        //Taste the rainbow
148
        if (++_sin > 360)
×
149
        {
150
            _sin = 0;
×
151
        }
152

153
        _drawables.ElementAtOrDefault(0)
×
154
            ?.Update(renderingSurface, Vector3.Zero, _angleInRadians);
×
155
        _drawables.ElementAtOrDefault(1)
×
156
            ?.Update(renderingSurface, new Vector3(0.5f, 0.0f, -0.5f), -_angleInRadians * 1.5f);
×
157

158
        return ValueTask.CompletedTask;
×
159
    }
160

161
    /// <summary>
162
    /// https://bitbucket.org/snippets/DemoBytom/aejA59/maps-value-between-from-one-min-max-range
163
    /// </summary>
164
    public static float Map(float value, float inMin, float inMax, float outMin, float outMax)
165
        => ((value - inMin) * (outMax - outMin) / (inMax - inMin)) + outMin;
×
166

167
    private float _angleInRadians = 0.0f;
168
    private ICube[] _drawables = [];
×
169
    private const float TWO_PI = MathHelper.TwoPi;
170

171
    private ValueTask Render(
172
        IRenderingEngine renderingEngine,
173
        RenderingSurfaceId renderingSurfaceId)
174
    {
175
        _angleInRadians = (_angleInRadians + 0.01f) % TWO_PI;
×
176

177
        renderingEngine.Draw(
×
178
            color: new Color4(_r, _g, _b, 1.0f),
×
179
            renderingSurfaceId: renderingSurfaceId,
×
180
            drawables: _drawables);
×
181

182
        //if (renderingEngine.Control.IsFullscreen != _fullscreen)
183
        //{
184
        //    renderingEngine.SetFullscreen(_fullscreen);
185
        //}
186

187
        return ValueTask.CompletedTask;
×
188
    }
189

190
    #region IDisposable
191

192
    protected override void Dispose(bool disposing)
193
    {
194
        if (!_disposedValue)
×
195
        {
196
            if (disposing)
×
197
            {
198
                _loopCancellationTokenSource.Dispose();
×
199
                foreach (var drawable in _drawables)
×
200
                {
201
                    if (drawable is IDisposable disposable)
×
202
                    {
203
                        disposable.Dispose();
×
204
                    }
205
                }
206
            }
207

208
            _disposedValue = true;
×
209
        }
210
        base.Dispose(disposing);
×
211
    }
×
212

213
    #endregion IDisposable
214
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc