Skip to content

Commit

Permalink
Simplified frame creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ben_singer committed Nov 29, 2024
1 parent 4e072e9 commit d43d8db
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
10 changes: 10 additions & 0 deletions NetAF.Imaging.Tests/VisualHelper_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ namespace NetAF.Imaging.Tests
[TestClass]
public class VisualHelper_Tests
{
[TestMethod]
public void GivenWhiteImage_WhenCreateFrame_ThenFrameIsNotNull()
{
var path = "Resources/FullWhite.bmp";

var result = VisualHelper.CreateFrame(path, new(5, 5), CellAspectRatio.Square, new BrightnessTexturizer());

Assert.IsNotNull(result);
}

[TestMethod]
public void GivenWhiteImage_WhenFromImage_ThenImageIsBrightWhite()
{
Expand Down
2 changes: 1 addition & 1 deletion NetAF.Imaging/NetAF.Imaging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NetAF" Version="2.9.0" />
<PackageReference Include="NetAF" Version="2.9.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.6" />
</ItemGroup>

Expand Down
25 changes: 25 additions & 0 deletions NetAF.Imaging/VisualHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ public static GridVisualBuilder FromImage(string path, Assets.Size targetSize, C
return builder;
}

/// <summary>
/// Create a new GridVisualFrame from an image.
/// </summary>
/// <param name="path">The path to the image.</param>
/// <param name="targetSize">The target size of the GridVisualFrame.</param>
/// <param name="cellAspectRatio">The aspect ratio of the cells used to display the image. For square cells use CellAspectRatio.Square.</param>
/// <returns>An approximation of the image as a GridVisualFrame.</returns>
public static GridVisualFrame CreateFrame(string path, Assets.Size targetSize, CellAspectRatio cellAspectRatio)
{
return CreateFrame(path, targetSize, cellAspectRatio, new NoTexturizer());
}

/// <summary>
/// Create a new GridVisualFrame from an image.
/// </summary>
/// <param name="path">The path to the image.</param>
/// <param name="targetSize">The target size of the GridVisualFrame.</param>
/// <param name="cellAspectRatio">The aspect ratio of the cells used to display the image. For square cells use CellAspectRatio.Square.</param>
/// <param name="texturizer">The texturizer to use for providing texture.</param>
/// <returns>An approximation of the image as a GridVisualFrame.</returns>
public static GridVisualFrame CreateFrame(string path, Assets.Size targetSize, CellAspectRatio cellAspectRatio, ITexturizer texturizer)
{
return new GridVisualFrame(FromImage(path, targetSize, cellAspectRatio, texturizer));
}

#endregion;
}
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Generating visuals is made easy with the *VisualHelper* class. The following exa
var displaySize = new Size(80, 50);
var adapter = new SystemConsoleAdapter();

var frame = new GridVisualFrame(VisualHelper.FromImage(@"C:\TestImage.jpg", displaySize, CellAspectRatio.Console));
var frame = VisualHelper.CreateFrame(@"C:\TestImage.jpg", displaySize, CellAspectRatio.Console);
adapter.RenderFrame(frame);
```

Expand All @@ -51,7 +51,7 @@ The generated visual:
This can be used in a game:

```csharp
var frame = new GridVisualFrame(VisualHelper.FromImage(@"C:\TestImage.jpg", displaySize, CellAspectRatio.Console));
var frame = VisualHelper.CreateFrame(@"C:\TestImage.jpg", displaySize, CellAspectRatio.Console);
game.ChangeMode(new VisualMode(frame));
```

Expand All @@ -61,7 +61,7 @@ return new Room("Hillside", "A wild hillside with a lone tree", commands:
[
new CustomCommand(new CommandHelp("Look at view", "Look at the current view."), true, true, (game, args) =>
{
var frame = new GridVisualFrame(VisualHelper.FromImage(@"C:\TestImage.jpg", game.Configuration.DisplaySize, CellAspectRatio.Console));
var frame = VisualHelper.CreateFrame(@"C:\TestImage.jpg", game.Configuration.DisplaySize, CellAspectRatio.Console);
game.ChangeMode(new VisualMode(frame));
return new(ReactionResult.GameModeChanged, string.Empty);
})
Expand All @@ -72,7 +72,7 @@ return new Room("Hillside", "A wild hillside with a lone tree", commands:
A texturizer can be applied to add extra depth to the image:

```csharp
var frame = new GridVisualFrame(VisualHelper.FromImage(@"C:\TestImage.jpg", displaySize, CellAspectRatio.Console, new BrightnessTexturizer()));
var frame = VisualHelper.CreateFrame(@"C:\TestImage.jpg", displaySize, CellAspectRatio.Console, new BrightnessTexturizer());
```

The generated visual:
Expand Down

0 comments on commit d43d8db

Please sign in to comment.