Skip to content

Commit 81306e6

Browse files
Review/simon (#2)
* Ch 2 fixes * Clean usings * Ch 3 code review
1 parent df41d18 commit 81306e6

File tree

16 files changed

+246
-120
lines changed

16 files changed

+246
-120
lines changed

articles/tutorials/advanced/2d_shaders/02_hot_reload/index.md

Lines changed: 44 additions & 30 deletions
Large diffs are not rendered by default.

articles/tutorials/advanced/2d_shaders/02_hot_reload/snippets/snippet-2-17.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
using System;
2-
using System.Diagnostics;
3-
using System.IO;
4-
using Microsoft.Xna.Framework.Content;
5-
using Microsoft.Xna.Framework.Graphics;
6-
using MonoGameLibrary.Graphics;
1+
using System;
2+
using System.IO;
3+
using Microsoft.Xna.Framework.Content;
74

85
namespace MonoGameLibrary.Content;
96

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using DungeonSlime.GameObjects;
3+
using DungeonSlime.UI;
4+
using Microsoft.Xna.Framework;
5+
using Microsoft.Xna.Framework.Audio;
6+
using Microsoft.Xna.Framework.Graphics;
7+
using MonoGameGum;
8+
using MonoGameLibrary;
9+
using MonoGameLibrary.Content;
10+
using MonoGameLibrary.Graphics;
11+
using MonoGameLibrary.Scenes;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public override void Draw(GameTime gameTime)
2+
{
3+
// Clear the back buffer.
4+
Core.GraphicsDevice.Clear(Color.CornflowerBlue);
5+
6+
if (_state != GameState.Playing)
7+
{
8+
// We are in a game over state, so apply the saturation parameter.
9+
_grayscaleEffect.Asset.Parameters["Saturation"].SetValue(_saturation);
10+
11+
// And begin the sprite batch using the grayscale effect.
12+
Core.SpriteBatch.Begin(samplerState: SamplerState.PointClamp, effect: _grayscaleEffect.Asset);
13+
}
14+
else
15+
{
16+
// Otherwise, just begin the sprite batch as normal.
17+
Core.SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
18+
}
19+
20+
21+
// Rest of the Draw method...

articles/tutorials/advanced/2d_shaders/02_hot_reload/snippets/snippet-2-23.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
public static bool TryRefresh<T>(this ContentManager manager, WatchedAsset<T> watchedAsset)
22
{
3-
oldAsset = default;
4-
53
// get the same path that the ContentManager would use to load the asset
64
var path = Path.Combine(manager.RootDirectory, watchedAsset.AssetName) + ".xnb";
75

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
public static bool TryRefresh<T>(this ContentManager manager, WatchedAsset<T> watchedAsset)
22
{
3-
oldAsset = default;
4-
3+
// get the same path that the ContentManager would use to load the asset
54
var path = Path.Combine(manager.RootDirectory, watchedAsset.AssetName) + ".xnb";
5+
6+
// ask the operating system when the file was last written.
67
var lastWriteTime = File.GetLastWriteTime(path);
7-
8+
9+
// when the file's write time is less recent than the asset's latest read time,
10+
// then the asset does not need to be reloaded.
811
if (lastWriteTime <= watchedAsset.UpdatedAt)
912
{
1013
return false;
1114
}
1215

13-
if (IsFileLocked(path)) return false; // wait for the file to not be locked.
14-
16+
// wait for the file to not be locked.
17+
if (IsFileLocked(path)) return false;
18+
19+
// clear the old asset to avoid leaking
1520
manager.UnloadAsset(watchedAsset.AssetName);
21+
22+
// load the new asset and update the latest read time
1623
watchedAsset.Asset = manager.Load<T>(watchedAsset.AssetName);
1724
watchedAsset.UpdatedAt = lastWriteTime;
18-
25+
1926
return true;
2027
}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
public static bool TryRefresh<T>(this ContentManager manager, WatchedAsset<T> watchedAsset, out T oldAsset)
22
{
33
oldAsset = default;
4-
4+
5+
// get the same path that the ContentManager would use to load the asset
56
var path = Path.Combine(manager.RootDirectory, watchedAsset.AssetName) + ".xnb";
7+
8+
// ask the operating system when the file was last written.
69
var lastWriteTime = File.GetLastWriteTime(path);
7-
10+
11+
// when the file's write time is less recent than the asset's latest read time,
12+
// then the asset does not need to be reloaded.
813
if (lastWriteTime <= watchedAsset.UpdatedAt)
914
{
1015
return false;
1116
}
1217

13-
if (IsFileLocked(path)) return false; // wait for the file to not be locked.
14-
18+
// wait for the file to not be locked.
19+
if (IsFileLocked(path)) return false;
20+
21+
// clear the old asset to avoid leaking
1522
manager.UnloadAsset(watchedAsset.AssetName);
23+
24+
// return the old asset
1625
oldAsset = watchedAsset.Asset;
26+
27+
// load the new asset and update the latest read time
1728
watchedAsset.Asset = manager.Load<T>(watchedAsset.AssetName);
1829
watchedAsset.UpdatedAt = lastWriteTime;
19-
30+
2031
return true;
2132
}
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
1-
21
public static bool TryRefresh<T>(this ContentManager manager, WatchedAsset<T> watchedAsset, out T oldAsset)
32
{
43
oldAsset = default;
5-
6-
if (manager != watchedAsset.Owner)
4+
5+
// ensure the ContentManager is the same one that loaded the asset
6+
if (manager != watchedAsset.Owner)
7+
{
78
throw new ArgumentException($"Used the wrong ContentManager to refresh {watchedAsset.AssetName}");
9+
}
10+
11+
// get the same path that the ContentManager would use to load the asset
12+
var path = Path.Combine(manager.RootDirectory, watchedAsset.AssetName) + ".xnb";
813

9-
var path = Path.Combine(manager.RootDirectory, watchedAsset.AssetName) + ".xnb";
14+
// ask the operating system when the file was last written.
1015
var lastWriteTime = File.GetLastWriteTime(path);
11-
16+
17+
// when the file's write time is less recent than the asset's latest read time,
18+
// then the asset does not need to be reloaded.
1219
if (lastWriteTime <= watchedAsset.UpdatedAt)
1320
{
1421
return false;
1522
}
1623

17-
if (IsFileLocked(path)) return false; // wait for the file to not be locked.
18-
24+
// wait for the file to not be locked.
25+
if (IsFileLocked(path)) return false;
26+
27+
// clear the old asset to avoid leaking
1928
manager.UnloadAsset(watchedAsset.AssetName);
29+
30+
// return the old asset
2031
oldAsset = watchedAsset.Asset;
32+
33+
// load the new asset and update the latest read time
2134
watchedAsset.Asset = manager.Load<T>(watchedAsset.AssetName);
2235
watchedAsset.UpdatedAt = lastWriteTime;
23-
36+
2437
return true;
2538
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
public bool TryRefresh(out T oldAsset)
2-
{
3-
return Owner.TryRefresh(this, out oldAsset);
1+
/// <summary>
2+
/// Attempts to refresh the asset if it has changed on disk using the registered owner <see cref="ContentManager"/>.
3+
/// </summary>
4+
public bool TryRefresh(out T oldAsset)
5+
{
6+
return Owner.TryRefresh(this, out oldAsset);
47
}

0 commit comments

Comments
 (0)