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

DemoBytom / DemoEngine / 13818869862

12 Mar 2025 06:29PM UTC coverage: 9.651% (-0.1%) from 9.797%
13818869862

push

coveralls.net

DemoBytom
Add a STAThreadWriter

Added a class that manages writing messages to STAThreadService
Added a ObjectPool for reusing DoEventsOkRequests

227 of 2352 relevant lines covered (9.65%)

17700.98 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.Interfaces;
6
using Demo.Engine.Core.Interfaces.Rendering;
7
using Demo.Engine.Core.Interfaces.Rendering.Shaders;
8
using Demo.Engine.Core.Requests.Keyboard;
9
using Demo.Engine.Core.ValueObjects;
10
using MediatR;
11
using Microsoft.Extensions.Logging;
12

13
namespace Demo.Engine.Core.Services;
14

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

28
    public Task ExecutingTask { get; }
×
29

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

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

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

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

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

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

72
        var doEventsOk = true;
×
73

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

84
            //process input
85
            // TODO!
86

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

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

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

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

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

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

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

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