Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Basic Texture2D Loading #20

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Ion.Examples/Ion.Examples.Breakout/Assets/Ball1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@

</ItemGroup>

<ItemGroup>
<None Update="Assets\Ball1.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\Block1.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
18 changes: 13 additions & 5 deletions Ion.Examples/Ion.Examples.Breakout/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
private readonly IInputState _input;
private readonly ISpriteBatch _spriteBatch;
private readonly IEventListener _events;
private readonly IAssetManager _assets;

private const int ROWS = 10;
private const int COLS = 10;
Expand All @@ -37,7 +38,7 @@
private readonly Color[] _blockColors = new Color[ROWS * COLS];
private readonly RectangleF[] _blockRects = new RectangleF[ROWS * COLS];

private Vector2 _blockSize = new(100, 20f);
private Vector2 _blockSize = new(100, 25f);
private readonly float _blockGap = 10f;
private readonly float _playerGap = 150f;
private readonly float _bottomGap = 20f;
Expand All @@ -53,17 +54,24 @@
private readonly Vector2 _paddleBounceMin = Vector2.Normalize(new Vector2(-1, -0.75f));
private readonly Vector2 _paddleBounceMax = Vector2.Normalize(new Vector2(+1, -0.75f));

public BreakoutSystems(IWindow window, IInputState input, ISpriteBatch spriteBatch, IEventListener events)
private Texture2D _blockTexture;
private Texture2D _ballTexture;

public BreakoutSystems(IWindow window, IInputState input, ISpriteBatch spriteBatch, IEventListener events, IAssetManager assets)

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable field '_blockTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable field '_ballTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable field '_blockTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable field '_ballTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable field '_blockTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable field '_ballTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable field '_blockTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable field '_ballTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable field '_blockTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable field '_ballTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable field '_blockTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 60 in Ion.Examples/Ion.Examples.Breakout/Program.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable field '_ballTexture' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_window = window;
_input = input;
_spriteBatch = spriteBatch;
_events = events;
_assets = assets;
}

[Init]
public void SetupBlocks(GameTime dt, GameLoopDelegate next)
{
_blockTexture = _assets.Load<Texture2D>("Block1.png");
_ballTexture = _assets.Load<Texture2D>("Ball1.png");

// Setup blocks in rows and columns across the window each with different colors:
for (int row = 0; row < ROWS; row++)
{
Expand Down Expand Up @@ -203,12 +211,12 @@
for (var col = 0; col < COLS; col++)
{
var i = (row * COLS) + col;
if (_blockStates[i]) _spriteBatch.DrawRect(_blockColors[i], _blockRects[i]);
if (_blockStates[i]) _spriteBatch.Draw(_blockTexture, _blockRects[i], color: _blockColors[i], options: (SpriteEffect)(i % 3));
}
}

_spriteBatch.DrawRect(Color.DarkBlue, _playerRect);
_spriteBatch.DrawRect(Color.OrangeRed, _ballRect);
_spriteBatch.Draw(_blockTexture, _playerRect, color: Color.DarkBlue);
_spriteBatch.Draw(_ballTexture, _ballRect, color: Color.DarkRed);

next(dt);
}
Expand Down
2 changes: 2 additions & 0 deletions Ion/Ion.Core/Builder/IonApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ internal IonApplicationBuilder(string[] args)
Services.AddSingleton<IEventEmitter, EventEmitter>();
Services.AddTransient<IEventListener, EventListener>();
Services.AddSingleton<EventSystem>();

Services.AddSingleton<IPersistentStorage, PersistentStorage>();
}

public IonApplication Build()
Expand Down
10 changes: 8 additions & 2 deletions Ion/Ion.Core/Storage/PersistentStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ internal class PersistentStorage : IPersistentStorage

public PersistentStorage(IOptions<GameConfig> config)
{
_game = new PersistentStorageProvider(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), config.Value.Title);
_assets = _game.Subpath("Assets");

#if DEBUG
_game = new PersistentStorageProvider(Environment.CurrentDirectory);
_user = new PersistentStorageProvider(Environment.CurrentDirectory);
#else
_game = new PersistentStorageProvider(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), config.Value.Title);
_user = new PersistentStorageProvider(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), config.Value.Title);
#endif

_assets = _game.Subpath("Assets");
_saves = _user.Subpath("Saves");
}

Expand Down
53 changes: 48 additions & 5 deletions Ion/Ion.Extensions.Graphics.Abstractions/Assets/AssetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ namespace Ion.Extensions.Graphics;
public interface IAssetManager
{
T Load<T>(params string[] path) where T : class, IAsset;
T LoadGlobal<T>(params string[] path) where T : class, IAsset;
T? Get<T>(int id) where T : class, IAsset;
void Unload<T>(T asset) where T : class, IAsset;
void UnloadGlobal<T>(T asset) where T : class, IAsset;
}

public class AssetManager : IAssetManager
public class GlobalAssetManager : IAssetManager
{
private readonly ILogger _logger;
private readonly IPersistentStorage _storage;
private readonly ImmutableDictionary<Type, IAssetLoader> _loaders;
private readonly Dictionary<int, IAsset> _assetCache = new();
private readonly Dictionary<string, IAsset> _pathCache = new();

public AssetManager(ILogger<AssetManager> logger, IPersistentStorage storage, IEnumerable<IAssetLoader> loaders)
public GlobalAssetManager(ILogger<GlobalAssetManager> logger, IPersistentStorage storage, IEnumerable<IAssetLoader> loaders)
{
_logger = logger;
_storage = storage;
Expand All @@ -26,26 +29,66 @@ public AssetManager(ILogger<AssetManager> logger, IPersistentStorage storage, IE

public T Load<T>(params string[] path) where T : class, IAsset
{
var name = Path.Combine(path);

if (_pathCache.TryGetValue(name, out var cachedAsset)) return (T)cachedAsset;

if (!_loaders.TryGetValue(typeof(T), out IAssetLoader? loader)) throw new InvalidOperationException("No loader registered for type " + typeof(T).Name);

var name = Path.Combine(path);
using var stream = _storage.Assets.Read(path);
using var stream = _storage.Assets.Read(name);

var asset = loader.Load<T>(stream, name);

_assetCache.Add(asset.Id, asset);
_pathCache.Add(name, asset);

return asset;
}

public T? Get<T>(int id) where T : class, IAsset
public virtual T LoadGlobal<T>(params string[] path) where T : class, IAsset
{
return Load<T>(path);
}

public virtual T? Get<T>(int id) where T : class, IAsset
{
return _assetCache.TryGetValue(id, out var asset) ? (T)asset : default;
}

public void Unload<T>(T asset) where T : class, IAsset
{
_assetCache.Remove(asset.Id);
_pathCache.Remove(asset.Name);
asset.Dispose();
}

public virtual void UnloadGlobal<T>(T asset) where T : class, IAsset
{
Unload(asset);
}
}

public class ScopedAssetManager : GlobalAssetManager
{
private readonly GlobalAssetManager _globalAssetManager;

public ScopedAssetManager(ILogger<GlobalAssetManager> logger, IPersistentStorage storage, IEnumerable<IAssetLoader> loaders, GlobalAssetManager globalAssetManager) : base(logger, storage, loaders)
{
_globalAssetManager = globalAssetManager;
}

public override T LoadGlobal<T>(params string[] path)
{
return _globalAssetManager.Load<T>(path);
}

public override void UnloadGlobal<T>(T asset)
{
_globalAssetManager.Unload(asset);
}

public override T? Get<T>(int id) where T : class
{
return base.Get<T>(id) ?? _globalAssetManager.Get<T>(id);
}
}
55 changes: 0 additions & 55 deletions Ion/Ion.Extensions.Graphics.Abstractions/Assets/AssetService.cs

This file was deleted.

6 changes: 2 additions & 4 deletions Ion/Ion.Extensions.Graphics.Null/BuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ public static IServiceCollection AddNullGraphics(this IServiceCollection service
services
// Standard
.Configure<GraphicsConfig>(config)
.AddScoped<IAssetManager, AssetManager>()
.AddSingleton<GlobalAssetManager>()
.AddScoped<IAssetManager, ScopedAssetManager>()

// Implementation-specific
.AddSingleton<ISpriteBatch, SpriteBatch>()

// Loaders
//.AddSingleton<IAssetLoader, Texture2DLoader>()

// Implementation-specific Systems
.AddSingleton<IInputState, InputState>();

Expand Down
3 changes: 2 additions & 1 deletion Ion/Ion.Extensions.Graphics.Veldrid/BuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public static IServiceCollection AddVeldridGraphics(this IServiceCollection serv
services
// Standard
.Configure<GraphicsConfig>(config)
.AddScoped<IAssetManager, AssetManager>()

// Implementation-specific
.AddSingleton<IWindow, Window>()
Expand All @@ -24,6 +23,8 @@ public static IServiceCollection AddVeldridGraphics(this IServiceCollection serv

// Loaders
.AddSingleton<IAssetLoader, Texture2DLoader>()
.AddSingleton<GlobalAssetManager>()
.AddScoped<IAssetManager, ScopedAssetManager>()

// Implementation-specific Systems
.AddSingleton<WindowSystem>()
Expand Down
Loading