Skip to content
Open
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
1 change: 1 addition & 0 deletions Wobble.Tests/Screens/Tests/Audio/TestAudioScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public override void Destroy()
{
Song?.Dispose();
Train?.Dispose();
HitSound?.Dispose();

base.Destroy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TestBackgroundImageScreenView : ScreenView
/// <summary>
/// </summary>
/// <param name="screen"></param>
public TestBackgroundImageScreenView(Screen screen) : base(screen) => Background = new BackgroundImage(WobbleAssets.WhiteBox, 60)
public TestBackgroundImageScreenView(Screen screen) : base(screen) => Background = new BackgroundImage(WobbleAssets.Wallpaper, 60)
{
Parent = Container
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class TestBlurContainerScreenView : ScreenView
/// </summary>
public SpriteText BlurStrengthText { get; }

private int _lastStrength;

/// <inheritdoc />
/// <summary>
/// </summary>
Expand Down Expand Up @@ -57,6 +59,8 @@ public TestBlurContainerScreenView(Screen screen) : base(screen)
Alignment = Alignment.TopCenter,
Y = 15,
};

_lastStrength = (int)Blur.Strength;
}

/// <inheritdoc />
Expand Down Expand Up @@ -85,7 +89,11 @@ public override void Update(GameTime gameTime)
if (KeyboardManager.IsUniqueKeyPress(Keys.Right))
Blur.Strength += 1;

BlurStrengthText.Text = $"Blur Strength: {Blur.Strength}";
if ((int)Blur.Strength != _lastStrength)
{
_lastStrength = (int)Blur.Strength;
BlurStrengthText.Text = $"Blur Strength: {Blur.Strength}";
}
Container?.Update(gameTime);
}

Expand All @@ -104,4 +112,4 @@ public override void Draw(GameTime gameTime)
/// </summary>
public override void Destroy() => Container?.Destroy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TestBlurredBackgroundImageScreenView : ScreenView
{
public TestBlurredBackgroundImageScreenView(Screen screen) : base(screen)
{
var blur = new GaussianBlur(1.1f);
using var blur = new GaussianBlur(1.1f);
var image = blur.PerformGaussianBlur(WobbleAssets.Wallpaper);

var background = new BackgroundImage(image)
Expand Down Expand Up @@ -42,4 +42,4 @@ public override void Draw(GameTime gameTime)
/// </summary>
public override void Destroy() => Container?.Destroy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Wobble.Tests.Screens.Tests.ScheduledUpdates
public class TestScheduledUpdatesScreenView: ScreenView
{
private SpriteTextPlus Scheduled { get; }
private CancellationTokenSource _updateTokenSource;
private Task _updateTask;

/// <summary>
/// </summary>
Expand All @@ -27,20 +29,25 @@ public TestScheduledUpdatesScreenView(Screen screen) : base(screen)
Scheduled = new SpriteTextPlus(FontManager.GetWobbleFont("exo2-semibold"),
"", 36)
{
Parent = Container,
Alignment = Alignment.TopCenter,
Y = 250
};

Task.Run(() =>
_updateTokenSource = new CancellationTokenSource();
var token = _updateTokenSource.Token;
_updateTask = Task.Run(async () =>
{
while (!Scheduled.IsDisposed)
while (!token.IsCancellationRequested && !Scheduled.IsDisposed)
{
if (IsScheduled)
Scheduled.ScheduleUpdate(() => Scheduled.Text = $"Scheduled - {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}");
else
Scheduled.Text = $"Unscheduled - {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}";

await Task.Delay(16, token);
}
});
}, token);

new SpriteText("exo2-semibold", "Press 1 to toggle between scheduled & unscheduled", 32)
{
Expand All @@ -62,10 +69,16 @@ public override void Draw(GameTime gameTime)
{
GameBase.Game.GraphicsDevice.Clear(Color.CornflowerBlue);
Container?.Draw(gameTime);

Scheduled.Draw(gameTime);
}

public override void Destroy() => Container?.Destroy();
public override void Destroy()
{
_updateTokenSource?.Cancel();
_updateTokenSource?.Dispose();
_updateTokenSource = null;
_updateTask = null;

Container?.Destroy();
}
}
}
}
81 changes: 62 additions & 19 deletions Wobble.Tests/WobbleTestsGame.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Wobble.Graphics;
using Wobble.Graphics.BitmapFonts;
using Wobble.Graphics.Sprites;
using Wobble.Graphics.Sprites.Text;
using Wobble.Graphics.UI.Debugging;
using Wobble.Input;
using Wobble.IO;
using Wobble.Logging;
using Wobble.Managers;
using Wobble.Screens;
using Wobble.Tests.Screens.Selection;
Expand All @@ -24,7 +23,11 @@ public class WobbleTestsGame : WobbleGame

private FpsCounter FpsCounter { get; set; }

private SpriteText WaylandState { get; set; }
private SpriteTextPlus WaylandState { get; set; }

private bool _logGc;
private double _gcLogTimer;
private readonly int[] _lastGcCounts = new int[3];

public WobbleTestsGame() : base(true)
{
Expand Down Expand Up @@ -72,36 +75,30 @@ protected override void LoadContent()

Resources.AddStore(new DllResourceStore("Wobble.Tests.Resources.dll"));

if (!BitmapFontFactory.CustomFonts.ContainsKey("exo2-bold"))
BitmapFontFactory.AddFont("exo2-bold", GameBase.Game.Resources.Get("Wobble.Tests.Resources/Fonts/exo2-bold.ttf"));

if (!BitmapFontFactory.CustomFonts.ContainsKey("exo2-regular"))
BitmapFontFactory.AddFont("exo2-regular", GameBase.Game.Resources.Get("Wobble.Tests.Resources/Fonts/exo2-regular.ttf"));

if (!BitmapFontFactory.CustomFonts.ContainsKey("exo2-semibold"))
BitmapFontFactory.AddFont("exo2-semibold", GameBase.Game.Resources.Get("Wobble.Tests.Resources/Fonts/exo2-semibold.ttf"));

if (!BitmapFontFactory.CustomFonts.ContainsKey("exo2-medium"))
BitmapFontFactory.AddFont("exo2-medium", GameBase.Game.Resources.Get("Wobble.Tests.Resources/Fonts/exo2-medium.ttf"));
var fonts = new List<string> { "exo2-bold", "exo2-regular", "exo2-semibold", "exo2-medium" };
foreach (var fontName in fonts)
{
FontManager.CacheWobbleFont(fontName, new WobbleFontStore(20, GameBase.Game.Resources.Get($"Wobble.Tests.Resources/Fonts/{fontName}.ttf")));
}

var font = new WobbleFontStore(20, GameBase.Game.Resources.Get("Wobble.Tests.Resources/Fonts/exo2-semibold.ttf"), new Dictionary<string, byte[]>()
var japaneseFont = new WobbleFontStore(20, GameBase.Game.Resources.Get("Wobble.Tests.Resources/Fonts/exo2-semibold.ttf"), new Dictionary<string, byte[]>()
{
{"Emoji", GameBase.Game.Resources.Get("Wobble.Tests.Resources/Fonts/symbola-emoji.ttf")},
{"Japanese", GameBase.Game.Resources.Get("Wobble.Tests.Resources/Fonts/droid-sans-japanese.ttf")}
});

FontManager.CacheWobbleFont("exo2-semibold", font);
FontManager.CacheWobbleFont("exo2-semibold-japanese", japaneseFont);

IsReadyToUpdate = true;

FpsCounter = new FpsCounter(FontManager.LoadBitmapFont("Content/gotham"), 18)
FpsCounter = new FpsCounter(FontManager.GetWobbleFont("exo2-semibold"), 18)
{
Parent = GlobalUserInterface,
Alignment = Alignment.BotRight,
Size = new ScalableVector2(70, 30),
};

WaylandState = new SpriteText("exo2-semibold", $"Wayland: {WaylandVsync}", 18)
WaylandState = new SpriteTextPlus(FontManager.GetWobbleFont("exo2-semibold"), $"Wayland: {WaylandVsync}", 18)
{
Parent = GlobalUserInterface,
Alignment = Alignment.BotRight,
Expand Down Expand Up @@ -137,7 +134,33 @@ protected override void Update(GameTime gameTime)
if (KeyboardManager.IsUniqueKeyPress(Keys.W) && OperatingSystem.IsLinux())
{
WaylandVsync = !WaylandVsync;
WaylandState.ScheduleUpdate(() => WaylandState.Text = $"Wayland: {WaylandVsync}");
WaylandState.Text = $"Wayland: {WaylandVsync}";
}

if (KeyboardManager.IsUniqueKeyPress(Keys.F10))
{
_logGc = !_logGc;
_gcLogTimer = 0;
Logger.Debug($"GC logging {(_logGc ? "enabled" : "disabled")}.", LogType.Runtime);
LogGc("GC toggle");
}

if (KeyboardManager.IsUniqueKeyPress(Keys.F9))
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
LogGc("GC forced");
}

if (_logGc)
{
_gcLogTimer += gameTime.ElapsedGameTime.TotalMilliseconds;
if (_gcLogTimer >= 1000)
{
_gcLogTimer = 0;
LogGc("GC tick");
}
}
}

Expand All @@ -150,5 +173,25 @@ protected override void Draw(GameTime gameTime)
GlobalUserInterface?.Draw(gameTime);
GameBase.Game.TryEndBatch();
}

private void LogGc(string tag)
{
var totalBytes = GC.GetTotalMemory(false);
var gen0 = GC.CollectionCount(0);
var gen1 = GC.CollectionCount(1);
var gen2 = GC.CollectionCount(2);

var delta0 = gen0 - _lastGcCounts[0];
var delta1 = gen1 - _lastGcCounts[1];
var delta2 = gen2 - _lastGcCounts[2];

_lastGcCounts[0] = gen0;
_lastGcCounts[1] = gen1;
_lastGcCounts[2] = gen2;

Logger.Debug(
$"{tag}: managed={totalBytes / (1024 * 1024)}MB gen0={gen0}(+{delta0}) gen1={gen1}(+{delta1}) gen2={gen2}(+{delta2})",
LogType.Runtime);
}
}
}
8 changes: 6 additions & 2 deletions Wobble/Assets/WobbleAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ internal static void Load()
/// <summary>
/// Disposes of all the assets that Wobble has included
/// </summary>
internal static void Dispose() => WhiteBox.Dispose();
internal static void Dispose()
{
WhiteBox?.Dispose();
Wallpaper?.Dispose();
}
}
}
}
16 changes: 15 additions & 1 deletion Wobble/Audio/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,21 @@ public static void Initialize(int? devicePeriod, int? deviceBufferLength, int? d
/// <summary>
/// Disposes of any resources used by BASS.
/// </summary>
internal static void Dispose() => Bass.Free();
internal static void Dispose()
{
if (Tracks != null)
{
lock (Tracks)
{
for (var i = 0; i < Tracks.Count; i++)
Tracks[i]?.Dispose();

Tracks.Clear();
}
}

Bass.Free();
}

/// <summary>
/// Updates the AudioManager and keeps things up-to-date.
Expand Down
5 changes: 4 additions & 1 deletion Wobble/Graphics/BitmapFonts/BitmapFontFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ internal static Texture2D Create(string fontName, string text, float fontSize, C
textSize.Width = Math.Max(1, textSize.Width);
textSize.Height = Math.Max(1, textSize.Height);

// Add a pad based on font size to avoid descender clipping when rendering into the bitmap.
textSize.Height += Math.Max(2f, fontSize * 0.25f);

// Create the actual bitmap using the size of the text.
using (var bmp = new Bitmap((int)(textSize.Width + 0.5), (int)(textSize.Height + 0.5), PixelFormat.Format32bppArgb))
using (var g = System.Drawing.Graphics.FromImage(bmp))
Expand Down Expand Up @@ -220,4 +223,4 @@ internal static void Dispose()
font.Value.Family.Dispose();
}
}
}
}
6 changes: 6 additions & 0 deletions Wobble/Graphics/Drawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@ public virtual void Draw(GameTime gameTime)
for (var i = 0; i < Children.Count; i++)
{
var drawable = Children[i];
if (drawable == null)
{
Children.RemoveAt(i);
i--;
continue;
}
drawable.Draw(gameTime);

TotalDrawn++;
Expand Down
12 changes: 11 additions & 1 deletion Wobble/Graphics/ImGUI/ImGuiRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ public IntPtr BindTexture(Texture2D texture)
/// <summary>
/// Removes a previously created texture pointer, releasing its reference and allowing it to be deallocated
/// </summary>
public void UnbindTexture(IntPtr textureId) => LoadedTextures.Remove(textureId);
public void UnbindTexture(IntPtr textureId)
{
if (LoadedTextures.TryGetValue(textureId, out var texture))
texture.Dispose();

LoadedTextures.Remove(textureId);
}

/// <summary>
/// Sets up ImGui for a new frame, should be called at frame start
Expand Down Expand Up @@ -546,6 +552,10 @@ public void Dispose()
if (DestroyContext)
ImGui.DestroyContext(Context);

foreach (var texture in LoadedTextures.Values)
texture.Dispose();

LoadedTextures.Clear();
Effect?.Dispose();
RasterizerState?.Dispose();
VertexBuffer?.Dispose();
Expand Down
13 changes: 11 additions & 2 deletions Wobble/Graphics/Shaders/GaussianBlur.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace Wobble.Graphics.Shaders
/// offsetsHoriz and offsetsVert fields.
/// </para>
/// </summary>
public class GaussianBlur
public class GaussianBlur : IDisposable
{
private Game game => GameBase.Game;
private Effect effect;
Expand Down Expand Up @@ -142,6 +142,15 @@ public GaussianBlur(float strength)
ComputeKernel(7, strength);
}

/// <inheritdoc />
/// <summary>
/// </summary>
public void Dispose()
{
effect?.Dispose();
effect = null;
}

/// <summary>
/// Calculates the Gaussian blur filter kernel. This implementation is
/// ported from the original Java code appearing in chapter 16 of
Expand Down Expand Up @@ -280,4 +289,4 @@ public Texture2D PerformGaussianBlur(Texture2D srcTexture)
return outputTexture;
}
}
}
}
Loading