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

DemoBytom / DemoEngine / 13474989684

22 Feb 2025 05:15PM UTC coverage: 9.797% (-0.07%) from 9.865%
13474989684

push

coveralls.net

DemoBytom
More work on `MainLoopService`

Separating the loop render and update jobs into a separate class - `LoopJob` for now.

227 of 2317 relevant lines covered (9.8%)

16017.04 hits per line

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

0.0
/src/Demo.Engine.Core/Services/MainLoopServiceNew.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.Diagnostics;
5
using System.Threading.Channels;
6
using Demo.Engine.Core.Interfaces;
7
using Demo.Engine.Core.Interfaces.Rendering;
8
using Demo.Engine.Core.Interfaces.Rendering.Shaders;
9
using Demo.Engine.Core.Requests.Keyboard;
10
using Demo.Engine.Core.ValueObjects;
11
using MediatR;
12
using Microsoft.Extensions.Logging;
13

14
namespace Demo.Engine.Core.Services;
15

16
internal sealed class MainLoopServiceNew
17
    : IMainLoopServiceNew,
18
      IDisposable
19
{
20
    private readonly ILogger<MainLoopServiceNew> _logger;
21
    private readonly ChannelWriter<StaThreadRequests> _channelWriter;
22
    private readonly IMediator _mediator;
23
    private readonly IShaderAsyncCompiler _shaderCompiler;
24
    private readonly IFpsTimer _fpsTimer;
25
    private readonly IMainLoopLifetime _mainLoopLifetime;
26
    private readonly ILoopJob _loopJob;
27
    private bool _disposedValue;
28

29
    public Task ExecutingTask { get; }
×
30

31
    public MainLoopServiceNew(
×
32
        ILogger<MainLoopServiceNew> logger,
×
33
        ChannelWriter<StaThreadRequests> channelWriter,
×
34
        IMediator mediator,
×
35
        IShaderAsyncCompiler shaderCompiler,
×
36
        IFpsTimer fpsTimer,
×
37
        IRenderingEngine renderingEngine,
×
38
        IMainLoopLifetime mainLoopLifetime,
×
39
        ILoopJob loopJob)
×
40
    {
41
        _logger = logger;
×
42
        _channelWriter = channelWriter;
×
43
        _mediator = mediator;
×
44
        _shaderCompiler = shaderCompiler;
×
45
        _fpsTimer = fpsTimer;
×
46
        _mainLoopLifetime = mainLoopLifetime;
×
47
        _loopJob = loopJob;
×
48
        ExecutingTask = DoAsync(
×
49
            renderingEngine);
×
50
    }
×
51

52
    private async Task DoAsync(
53
        IRenderingEngine renderingEngine)
54
    {
55
        _ = await _shaderCompiler.CompileShaders(_mainLoopLifetime.Token);
×
56

57
        var keyboardHandle = await _mediator.Send(new KeyboardHandleRequest(), CancellationToken.None);
×
58
        var keyboardCharCache = await _mediator.Send(new KeyboardCharCacheRequest(), CancellationToken.None);
×
59

60
        var surfaces = new RenderingSurfaceId[]
×
61
        {
×
62
            await _channelWriter.CreateSurface(
×
63
                _mainLoopLifetime.Token),
×
64
            await _channelWriter.CreateSurface(
×
65
                _mainLoopLifetime.Token),
×
66
        };
×
67

68
        var previous = Stopwatch.GetTimestamp();
×
69
        var lag = TimeSpan.Zero;
×
70

71
        var msPerUpdate = TimeSpan.FromSeconds(1) / 60;
×
72

73
        var doEventsFunc = StaThreadRequests.DoEventsOk(RenderingSurfaceId.Empty);
×
74
        var doEventsOk = true;
×
75

76
        while (
×
77
            doEventsOk
×
78
            //&& IsRunning
×
79
            && !_mainLoopLifetime.Token.IsCancellationRequested)
×
80
        {
81
            var current = Stopwatch.GetTimestamp();
×
82
            var elapsed = Stopwatch.GetElapsedTime(previous, current);
×
83
            previous = current;
×
84
            lag += elapsed;
×
85

86
            //process input
87
            // TODO!
88

89
            while (lag >= msPerUpdate)
×
90
            {
91
                //Update
92
                // TODO - fix the UPS timer.. somehow :D
93
                _fpsTimer.StopUpdateTimer();
×
94
                foreach (var renderingSurfaceId in surfaces)
×
95
                {
96
                    if (!renderingEngine.TryGetRenderingSurface(
×
97
                        renderingSurfaceId,
×
98
                        out var renderingSurface))
×
99
                    {
100
                        _logger.LogCritical(
×
101
                            "Rendering surface {id} not found!",
×
102
                            renderingSurfaceId);
×
103
                        break;
×
104
                    }
105

106
                    await _loopJob.Update(
×
107
                          renderingSurface,
×
108
                          keyboardHandle,
×
109
                          keyboardCharCache);
×
110
                }
111
                lag -= msPerUpdate;
×
112
                _fpsTimer.StartUpdateTimer();
×
113
            }
114

115
            //Render
116
            foreach (var renderingSurfaceId in surfaces)
×
117
            {
118
                doEventsOk &= await _channelWriter.DoEventsOk(
×
119
                    renderingSurfaceId,
×
120
                    doEventsFunc,
×
121
                    _mainLoopLifetime.Token);
×
122

123
                using var scope = _fpsTimer.StartRenderingTimerScope(
×
124
                    renderingSurfaceId);
×
125

126
                _loopJob.Render(
×
127
                    renderingEngine,
×
128
                    renderingSurfaceId);
×
129
            }
×
130
        }
131
        _channelWriter.Complete();
×
132
        _mainLoopLifetime.Cancel();
×
133
    }
×
134

135
    private void Dispose(bool disposing)
136
    {
137
        if (!_disposedValue)
×
138
        {
139
            if (disposing)
×
140
            {
141
            }
142

143
            _disposedValue = true;
×
144
        }
145
    }
×
146

147
    public void Dispose()
148
    {
149
        // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
150
        Dispose(disposing: true);
×
151
        GC.SuppressFinalize(this);
×
152
    }
×
153
}
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