Skip to content

Commit

Permalink
shader-loading support (finally >_>)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMakesGames committed May 24, 2024
1 parent 90a2129 commit 7ee4533
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 12 deletions.
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 which adds methods for generating &amp; playing simple waveforms.</Description>
<Copyright>2022-2023 Ben Hendel-Doying</Copyright>
<Version>0.4.0</Version>
<Version>0.5.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.5.0</Version>
<Version>4.7.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.2.0</Version>
<Version>0.3.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame playplaymini naudio music</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>An extension for PlayPlayMini, adding a skinnable, object-oriented UI framework.</Description>
<Copyright>2021-2024 Ben Hendel-Doying</Copyright>
<Version>4.5.0</Version>
<Version>4.7.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame mouse ui framework oo</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>An opinionated framework for making smallish games with MonoGame.</Description>
<Copyright>2021-2024 Ben Hendel-Doying</Copyright>
<Version>4.6.2</Version>
<Version>4.7.0</Version>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>monogame game engine framework di state</PackageTags>
Expand Down
3 changes: 3 additions & 0 deletions BenMakesGames.PlayPlayMini/Model/PixelShaderMeta.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace BenMakesGames.PlayPlayMini.Model;

public sealed record PixelShaderMeta(string Key, string Path, bool PreLoaded = false): IAsset;
38 changes: 31 additions & 7 deletions BenMakesGames.PlayPlayMini/Services/GraphicsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public sealed class GraphicsManager: IServiceLoadContent, IServiceInitialize
public bool FullyLoaded { get; private set; }

public Matrix? TransformMatrix { get; private set; }
public Effect? PostProcessingShader { get; private set; }
public int Zoom { get; private set; } = 2;
public bool FullScreen { get; private set; }
public int Width { get; private set; } = 1920 / 3;
Expand All @@ -39,6 +40,7 @@ public sealed class GraphicsManager: IServiceLoadContent, IServiceInitialize
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 GraphicsManager(ILogger<GraphicsManager> logger)
{
Expand Down Expand Up @@ -99,6 +101,9 @@ public void LoadContent(GameStateManager gsm)
foreach(var meta in gsm.Assets.GetAll<FontMeta>().Where(m => m.PreLoaded))
LoadFont(meta);

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

// deferred
Task.Run(() => LoadDeferredContent(gsm.Assets));
}
Expand All @@ -114,6 +119,9 @@ private void LoadDeferredContent(AssetCollection assets)
foreach(var meta in assets.GetAll<FontMeta>().Where(m => !m.PreLoaded))
LoadFont(meta);

foreach(var meta in assets.GetAll<PixelShaderMeta>().Where(m => !m.PreLoaded))
LoadPixelShader(meta);

FullyLoaded = true;
}

Expand All @@ -132,7 +140,7 @@ private void LoadFont(FontMeta font)
}
catch (Exception e)
{
Logger.LogError("Failed to load {Path}: {Message}", font.Path, e.Message);
Logger.LogError("Failed to load Font (Texture2D) {Path}: {Message}", font.Path, e.Message);
}
}

Expand All @@ -144,7 +152,7 @@ private void LoadPicture(PictureMeta picture)
}
catch (Exception e)
{
Logger.LogError("Failed to load {Path}: {Message}", picture.Path, e.Message);
Logger.LogError("Failed to load Picture (Texture2D) {Path}: {Message}", picture.Path, e.Message);
}
}

Expand All @@ -156,7 +164,19 @@ private void LoadSpriteSheet(SpriteSheetMeta spriteSheet)
}
catch (Exception e)
{
Logger.LogError("Failed to load {Path}: {Message}", spriteSheet.Path, e.Message);
Logger.LogError("Failed to load SpriteSheet (Texture2D) {Path}: {Message}", spriteSheet.Path, e.Message);
}
}

private void LoadPixelShader(PixelShaderMeta pixelShader)
{
try
{
PixelShaders.Add(pixelShader.Key, Content.Load<Effect>(pixelShader.Path));
}
catch (Exception e)
{
Logger.LogError("Failed to load PixelShader (Effect) {Path}: {Message}", pixelShader.Path, e.Message);
}
}

Expand All @@ -166,9 +186,13 @@ public void UnloadContent()
}

public void SetTransformMatrix(Matrix? matrix)
{
TransformMatrix = matrix;
}
=> TransformMatrix = matrix;

public void SetPostProcessingPixelShader(Effect? effect)
=> PostProcessingShader = effect;

public void SetPostProcessingPixelShader(string pixelShaderName)
=> SetPostProcessingPixelShader(PixelShaders[pixelShaderName]);

/// <summary>
/// "Zoom" controls how large each "pixel" is.
Expand Down Expand Up @@ -821,7 +845,7 @@ public void EndDraw()

GraphicsDevice.SetRenderTarget(null);

SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.PointClamp);
SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.PointClamp, effect: PostProcessingShader);
SpriteBatch.Draw(RenderTarget, new Rectangle(0, 0, Width * Zoom, Height * Zoom), Color.White);
SpriteBatch.End();
}
Expand Down

0 comments on commit 7ee4533

Please sign in to comment.