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

DemoBytom / DemoEngine / 13822325961

12 Mar 2025 09:58PM UTC coverage: 9.454% (-0.2%) from 9.651%
13822325961

push

coveralls.net

DemoBytom
Moving all StaThread functionality to a separate vertical slice

227 of 2401 relevant lines covered (9.45%)

15799.73 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 Demo.Engine.Core.Features.StaThread;
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 IStaThreadWriter _staThreadWriter;
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
        IStaThreadWriter staThreadWriter,
×
34
        IMediator mediator,
×
35
        IShaderAsyncCompiler shaderCompiler,
×
36
        IFpsTimer fpsTimer,
×
37
        IRenderingEngine renderingEngine,
×
38
        IMainLoopLifetime mainLoopLifetime,
×
39
        ILoopJob loopJob)
×
40
    {
41
        _logger = logger;
×
42
        _staThreadWriter = staThreadWriter;
×
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 _staThreadWriter.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 doEventsOk = true;
×
74

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

85
            //process input
86
            // TODO!
87

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

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

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

121
                using var scope = _fpsTimer.StartRenderingTimerScope(
×
122
                    renderingSurfaceId);
×
123

124
                _loopJob.Render(
×
125
                    renderingEngine,
×
126
                    renderingSurfaceId);
×
127
            }
128
        }
129
        _mainLoopLifetime.Cancel();
×
130
    }
×
131

132
    private void Dispose(bool disposing)
133
    {
134
        if (!_disposedValue)
×
135
        {
136
            if (disposing)
×
137
            {
138
            }
139

140
            _disposedValue = true;
×
141
        }
142
    }
×
143

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