Skip to content

Commit

Permalink
Updated to MonoGame 3.8.2.1105
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMakesGames committed Aug 17, 2024
1 parent 35dce3c commit be2a9cb
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<Authors>Ben Hendel-Doying</Authors>
<Company>Ben Hendel-Doying</Company>
<Description>An extension for PlayPlayMini which adds methods for generating &amp; playing simple waveforms.</Description>
<Copyright>2022-2023 Ben Hendel-Doying</Copyright>
<Version>0.5.1</Version>
<Copyright>2022-2024 Ben Hendel-Doying</Copyright>
<Version>0.6.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame playplaymini sound generation square triangle saw sine wave waveform</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Company>Ben Hendel-Doying</Company>
<Description>Some GraphicsManager extensions for PlayPlayMini.</Description>
<Copyright>2023-2024 Ben Hendel-Doying</Copyright>
<Version>4.7.1</Version>
<Version>5.0.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame playplaymini graphics extensions animations text</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Company>Ben Hendel-Doying</Company>
<Description>Get seamless looping music, and cross-fade, in your MonoGame-PlayPlayMini game using NAudio.</Description>
<Copyright>2024 Ben Hendel-Doying</Copyright>
<Version>0.4.0</Version>
<Version>0.5.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame playplaymini naudio music</PackageTags>
Expand All @@ -24,7 +24,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenMakesGames.PlayPlayMini" Version="4.5.0" />
<PackageReference Include="NAudio" Version="2.2.1" />
</ItemGroup>

Expand All @@ -48,5 +47,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BenMakesGames.PlayPlayMini\BenMakesGames.PlayPlayMini.csproj" />
</ItemGroup>

</Project>
45 changes: 24 additions & 21 deletions BenMakesGames.PlayPlayMini.NAudio/Services/NAudioMusicPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public sealed class NAudioMusicPlayer<T>: INAudioMusicPlayer, IDisposable
private ILogger<NAudioMusicPlayer<T>> Logger { get; }
private ILifetimeScope IocContainer { get; }

public Dictionary<string, (WaveStream Stream, float Gain)> Songs { get; } = new();
public Dictionary<string, (WaveStream Stream, float Gain)> Songs { get; private set; } = new();

private Dictionary<string, FadeInOutSampleProvider> PlayingSongs { get; } = new();
private Dictionary<string, DateTimeOffset> FadingSongs { get; } = new();
private List<(string Name, DateTimeOffset EndTime)> FadingSongs { get; } = new();

public NAudioMusicPlayer(ILogger<NAudioMusicPlayer<T>> logger, ILifetimeScope iocContainer)
{
Expand All @@ -92,6 +92,8 @@ public void LoadContent(GameStateManager gsm)

var allSongs = gsm.Assets.GetAll<NAudioSongMeta>().ToList();

Songs = allSongs.ToDictionary(meta => meta.Key, _ => ((WaveStream)null!, 0f));

foreach(var song in allSongs.Where(s => s.PreLoaded))
LoadSong(song.Key, song.Path, song.Gain);

Expand All @@ -113,12 +115,6 @@ public void UnloadContent()

private void LoadSong(string name, string filePath, float gain)
{
if(Songs.ContainsKey(name))
{
Logger.LogWarning("A song named {Name} has already been loaded", name);
return;
}

try
{
var stream = CreateWaveStream(filePath);
Expand All @@ -135,7 +131,7 @@ private void LoadSong(string name, string filePath, float gain)
return;
}

Songs.Add(name, (stream, gain));
Songs[name] = (stream, gain);
}
catch(Exception e)
{
Expand Down Expand Up @@ -254,11 +250,11 @@ public INAudioMusicPlayer StopAllSongs(int fadeOutMilliseconds = 0)

foreach(var song in PlayingSongs)
{
if(FadingSongs.ContainsKey(song.Key))
if(FadingSongs.Any(s => s.Name == song.Key))
continue;

song.Value.BeginFadeOut(fadeOutMilliseconds);
FadingSongs.Add(song.Key, DateTimeOffset.UtcNow + TimeSpan.FromMilliseconds(fadeOutMilliseconds));
FadingSongs.Add((song.Key, DateTimeOffset.UtcNow + TimeSpan.FromMilliseconds(fadeOutMilliseconds)));
}

return this;
Expand All @@ -278,11 +274,11 @@ public INAudioMusicPlayer StopAllSongsExcept(string[] songsToContinue, int fadeO
{
foreach(var song in PlayingSongs)
{
if(songsToContinue.Contains(song.Key) || FadingSongs.ContainsKey(song.Key))
if(songsToContinue.Contains(song.Key) || FadingSongs.Any(s => s.Name == song.Key))
continue;

song.Value.BeginFadeOut(fadeOutMilliseconds);
FadingSongs.Add(song.Key, DateTimeOffset.UtcNow + TimeSpan.FromMilliseconds(fadeOutMilliseconds));
FadingSongs.Add((song.Key, DateTimeOffset.UtcNow + TimeSpan.FromMilliseconds(fadeOutMilliseconds)));
}

return this;
Expand Down Expand Up @@ -315,19 +311,24 @@ public INAudioMusicPlayer StopSong(string name, int fadeOutMilliseconds = 0)
if (!PlayingSongs.TryGetValue(name, out var sample))
return this;

var fadingSongIndex = FadingSongs.FindIndex(s => s.Name == name);

if (fadeOutMilliseconds <= 0)
{
PlaybackEngine?.RemoveSample(sample);
PlayingSongs.Remove(name);
FadingSongs.Remove(name);

if(fadingSongIndex >= 0)
FadingSongs.RemoveAt(fadingSongIndex);

return this;
}

if (FadingSongs.ContainsKey(name))
if (fadingSongIndex >= 0)
return this;

sample.BeginFadeOut(fadeOutMilliseconds);
FadingSongs.Add(name, DateTimeOffset.UtcNow + TimeSpan.FromMilliseconds(fadeOutMilliseconds));
FadingSongs.Add((name, DateTimeOffset.UtcNow + TimeSpan.FromMilliseconds(fadeOutMilliseconds)));

return this;
}
Expand All @@ -348,16 +349,18 @@ public void Dispose()
public void Update(GameTime gameTime)
{
var now = DateTimeOffset.UtcNow;
var toRemove = FadingSongs.Where(kvp => now >= kvp.Value).Select(kvp => kvp.Key);

foreach(var name in toRemove)
for (var i = FadingSongs.Count - 1; i >= 0; i--)
{
var sample = PlayingSongs[name];
if (now <= FadingSongs[i].EndTime)
continue;

var sample = PlayingSongs[FadingSongs[i].Name];

PlaybackEngine?.RemoveSample(sample);

PlayingSongs.Remove(name);
FadingSongs.Remove(name);
PlayingSongs.Remove(FadingSongs[i].Name);
FadingSongs.RemoveAt(i);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Company>Ben Hendel-Doying</Company>
<Description>An extension for PlayPlayMini, adding a skinnable, object-oriented UI framework.</Description>
<Copyright>2021-2024 Ben Hendel-Doying</Copyright>
<Version>4.7.1</Version>
<Version>5.0.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame mouse ui framework oo</PackageTags>
Expand Down
4 changes: 2 additions & 2 deletions BenMakesGames.PlayPlayMini/BenMakesGames.PlayPlayMini.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Company>Ben Hendel-Doying</Company>
<Description>An opinionated framework for making smallish games with MonoGame.</Description>
<Copyright>2021-2024 Ben Hendel-Doying</Copyright>
<Version>4.7.1</Version>
<Version>5.0.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame game engine framework di state</PackageTags>
Expand All @@ -30,7 +30,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.2.1105" />
</ItemGroup>

<ItemGroup>
Expand Down
53 changes: 25 additions & 28 deletions BenMakesGames.PlayPlayMini/Services/GraphicsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ public sealed class GraphicsManager: IServiceLoadContent, IServiceInitialize
private GraphicsDeviceManager Graphics { get; set; } = null!;
public SpriteBatch SpriteBatch { get; set; } = null!;

public Dictionary<string, Texture2D> Pictures { get; } = new();
public Dictionary<string, SpriteSheet> SpriteSheets { get; } = new();
public Dictionary<string, Font> Fonts { get; } = new();
public Dictionary<string, Effect> PixelShaders { get; } = new();
public Dictionary<string, Texture2D> Pictures { get; private set; } = new();
public Texture2D WhitePixel { get; private set; } = null!;
public Dictionary<string, SpriteSheet> SpriteSheets { get; private set; } = new();
public Dictionary<string, Font> Fonts { get; private set; } = new();
public Dictionary<string, Effect> PixelShaders { get; private set; } = new();

public GraphicsManager(ILogger<GraphicsManager> logger)
{
Expand Down Expand Up @@ -79,22 +80,18 @@ public void Initialize(GameStateManager gsm)

public void LoadContent(GameStateManager gsm)
{
Pictures.Clear();
SpriteSheets.Clear();
Fonts.Clear();
WhitePixel = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
WhitePixel.SetData([ Color.White ]);

Pictures = gsm.Assets.GetAll<PictureMeta>().ToDictionary(meta => meta.Key, _ => (Texture2D)null!);
SpriteSheets = gsm.Assets.GetAll<SpriteSheetMeta>().ToDictionary(meta => meta.Key, _ => (SpriteSheet)null!);
Fonts = gsm.Assets.GetAll<FontMeta>().ToDictionary(meta => meta.Key, _ => (Font)null!);
PixelShaders = gsm.Assets.GetAll<PixelShaderMeta>().ToDictionary(meta => meta.Key, _ => (Effect)null!);

// load immediately
foreach(var meta in gsm.Assets.GetAll<PictureMeta>().Where(m => m.PreLoaded))
foreach(var meta in gsm.Assets.GetAll<PictureMeta>())
LoadPicture(meta);

if(!Pictures.ContainsKey("Pixel"))
{
var whitePixel = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
whitePixel.SetData([ Color.White ]);

Pictures.Add("Pixel", whitePixel);
}

foreach(var meta in gsm.Assets.GetAll<SpriteSheetMeta>().Where(m => m.PreLoaded))
LoadSpriteSheet(meta);

Expand Down Expand Up @@ -129,14 +126,14 @@ private void LoadFont(FontMeta font)
{
try
{
Fonts.Add(font.Key, new Font(
Fonts[font.Key] = new Font(
Content.Load<Texture2D>(font.Path),
font.Width,
font.Height,
font.HorizontalSpacing,
font.VerticalSpacing,
font.FirstCharacter
));
);
}
catch (Exception e)
{
Expand All @@ -148,7 +145,7 @@ private void LoadPicture(PictureMeta picture)
{
try
{
Pictures.Add(picture.Key, Content.Load<Texture2D>(picture.Path));
Pictures[picture.Key] = Content.Load<Texture2D>(picture.Path);
}
catch (Exception e)
{
Expand All @@ -160,7 +157,7 @@ private void LoadSpriteSheet(SpriteSheetMeta spriteSheet)
{
try
{
SpriteSheets.Add(spriteSheet.Key, new SpriteSheet(Content.Load<Texture2D>(spriteSheet.Path), spriteSheet.Width, spriteSheet.Height));
SpriteSheets[spriteSheet.Key] = new SpriteSheet(Content.Load<Texture2D>(spriteSheet.Path), spriteSheet.Width, spriteSheet.Height);
}
catch (Exception e)
{
Expand All @@ -172,7 +169,7 @@ private void LoadPixelShader(PixelShaderMeta pixelShader)
{
try
{
PixelShaders.Add(pixelShader.Key, Content.Load<Effect>(pixelShader.Path));
PixelShaders[pixelShader.Key] = Content.Load<Effect>(pixelShader.Path);
}
catch (Exception e)
{
Expand Down Expand Up @@ -298,14 +295,14 @@ public void Clear(Color c)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void DrawFilledRectangle((int x, int y) upperLeft, (int x, int y) bottomRight, Color c)
{
SpriteBatch.Draw(Pictures["Pixel"], new Rectangle(upperLeft.x, upperLeft.y, bottomRight.x - upperLeft.x + 1, bottomRight.y - upperLeft.y + 1), null, c);
SpriteBatch.Draw(WhitePixel, new Rectangle(upperLeft.x, upperLeft.y, bottomRight.x - upperLeft.x + 1, bottomRight.y - upperLeft.y + 1), null, c);
DrawCalls++;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void DrawFilledRectangle(int x, int y, int w, int h, Color c)
{
SpriteBatch.Draw(Pictures["Pixel"], new Rectangle(x, y, w, h), null, c);
SpriteBatch.Draw(WhitePixel, new Rectangle(x, y, w, h), null, c);
DrawCalls++;
}

Expand Down Expand Up @@ -339,20 +336,20 @@ public void DrawPoints(IEnumerable<(int x, int y)> points, Color c)
public void DrawRectangle(int x, int y, int w, int h, Color c)
{
// top & bottom line
SpriteBatch.Draw(Pictures["Pixel"], new Rectangle(x, y, w, 1), null, c);
SpriteBatch.Draw(Pictures["Pixel"], new Rectangle(x, y + h - 1, w, 1), null, c);
SpriteBatch.Draw(WhitePixel, new Rectangle(x, y, w, 1), null, c);
SpriteBatch.Draw(WhitePixel, new Rectangle(x, y + h - 1, w, 1), null, c);

// left & right line
SpriteBatch.Draw(Pictures["Pixel"], new Rectangle(x, y + 1, 1, h - 2), null, c);
SpriteBatch.Draw(Pictures["Pixel"], new Rectangle(x + w - 1, y + 1, 1, h - 2), null, c);
SpriteBatch.Draw(WhitePixel, new Rectangle(x, y + 1, 1, h - 2), null, c);
SpriteBatch.Draw(WhitePixel, new Rectangle(x + w - 1, y + 1, 1, h - 2), null, c);

DrawCalls += 4;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void DrawPoint(int x, int y, Color c)
{
SpriteBatch.Draw(Pictures["Pixel"], new Rectangle(x, y, 1, 1), c);
SpriteBatch.Draw(WhitePixel, new Rectangle(x, y, 1, 1), c);
DrawCalls++;
}

Expand Down
Loading

0 comments on commit be2a9cb

Please sign in to comment.