Skip to content

Commit 6ecbea6

Browse files
authored
chore: content/graphics code cleanup (#2439)
1 parent 784a765 commit 6ecbea6

File tree

4 files changed

+108
-77
lines changed

4 files changed

+108
-77
lines changed

Intersect (Core)/ColorF.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
namespace Intersect;
22

33

4-
public partial class ColorF
4+
public partial class ColorF : IEquatable<ColorF>
55
{
6+
private const float Tolerance = 1.0f / 256.0f;
67

78
public ColorF()
89
{
@@ -32,25 +33,25 @@ public ColorF(float r, float g, float b)
3233

3334
public float B { get; set; }
3435

35-
public static ColorF White => new ColorF(255, 255, 255, 255);
36+
public static ColorF White => new(255, 255, 255, 255);
3637

37-
public static ColorF Black => new ColorF(255, 0, 0, 0);
38+
public static ColorF Black => new(255, 0, 0, 0);
3839

39-
public static ColorF Transparent => new ColorF(0, 0, 0, 0);
40+
public static ColorF Transparent => new(0, 0, 0, 0);
4041

41-
public static ColorF Red => new ColorF(255, 255, 0, 0);
42+
public static ColorF Red => new(255, 255, 0, 0);
4243

43-
public static ColorF Green => new ColorF(255, 0, 255, 0);
44+
public static ColorF Green => new(255, 0, 255, 0);
4445

45-
public static ColorF Blue => new ColorF(255, 0, 0, 255);
46+
public static ColorF Blue => new(255, 0, 0, 255);
4647

47-
public static ColorF Yellow => new ColorF(255, 255, 255, 0);
48+
public static ColorF Yellow => new(255, 255, 255, 0);
4849

49-
public static ColorF LightCoral => new ColorF(255, 240, 128, 128);
50+
public static ColorF LightCoral => new(255, 240, 128, 128);
5051

51-
public static ColorF ForestGreen => new ColorF(255, 34, 139, 34);
52+
public static ColorF ForestGreen => new(255, 34, 139, 34);
5253

53-
public static ColorF Magenta => new ColorF(255, 255, 0, 255);
54+
public static ColorF Magenta => new(255, 255, 0, 255);
5455

5556
public byte GetHue()
5657
{
@@ -62,4 +63,8 @@ public static ColorF FromArgb(float a, float r, float g, float b)
6263
return new ColorF(a, r, g, b);
6364
}
6465

66+
public override bool Equals(object other) => other is ColorF color && Equals(color);
67+
68+
public bool Equals(ColorF other) => Math.Abs(R - other.R) < Tolerance && Math.Abs(G - other.G) < Tolerance &&
69+
Math.Abs(B - other.B) < Tolerance && Math.Abs(A - other.A) < Tolerance;
6570
}

Intersect.Client.Core/Core/Graphics.cs

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static FloatRect CurrentView
3636
Renderer?.SetView(_currentView);
3737
}
3838
}
39-
39+
4040
public static FloatRect WorldViewport => new(CurrentView.Position, CurrentView.Size / (Globals.Database?.WorldZoom ?? 1));
4141

4242
public static GameShader? DefaultShader;
@@ -123,13 +123,15 @@ public static GameFont FindFont(string font)
123123
{
124124
var size = 8;
125125

126-
if (font.IndexOf(',') > -1)
126+
if (font.IndexOf(',') < 0)
127127
{
128-
var parts = font.Split(',');
129-
font = parts[0];
130-
_ = int.TryParse(parts[1], out size);
128+
return sContentManager.GetFont(font, size);
131129
}
132130

131+
var parts = font.Split(',');
132+
font = parts[0];
133+
_ = int.TryParse(parts[1], out size);
134+
133135
return sContentManager.GetFont(font, size);
134136
}
135137

@@ -147,14 +149,16 @@ public static void InitInGame()
147149

148150
public static void DrawIntro()
149151
{
150-
var imageTex = sContentManager.GetTexture(
151-
Framework.Content.TextureType.Image, ClientConfiguration.Instance.IntroImages[Globals.IntroIndex]
152-
);
153-
154-
if (imageTex != null)
152+
if (!sContentManager.TryGetTexture(
153+
TextureType.Image,
154+
ClientConfiguration.Instance.IntroImages[Globals.IntroIndex],
155+
out var texture
156+
))
155157
{
156-
DrawFullScreenTextureFitMinimum(imageTex);
158+
return;
157159
}
160+
161+
DrawFullScreenTextureFitMinimum(texture);
158162
}
159163

160164
private static void DrawMenu()
@@ -609,8 +613,7 @@ public static void Render(TimeSpan deltaTime, TimeSpan _)
609613

610614
private static void DrawMap(Guid mapId, int layer = 0)
611615
{
612-
var map = MapInstance.Get(mapId);
613-
if (map == null)
616+
if (!MapInstance.TryGet(mapId, out var map))
614617
{
615618
return;
616619
}
@@ -631,18 +634,24 @@ private static void DrawMap(Guid mapId, int layer = 0)
631634

632635
private static void DrawMapPanorama(Guid mapId)
633636
{
634-
var map = MapInstance.Get(mapId);
635-
if (map != null)
637+
if (!MapInstance.TryGet(mapId, out var map))
636638
{
637-
if (!new FloatRect(
638-
map.GetX(), map.GetY(), Options.TileWidth * Options.MapWidth, Options.TileHeight * Options.MapHeight
639-
).IntersectsWith(WorldViewport))
640-
{
641-
return;
642-
}
639+
return;
640+
}
641+
642+
var mapBounds = new FloatRect(
643+
map.GetX(),
644+
map.GetY(),
645+
Options.TileWidth * Options.MapWidth,
646+
Options.TileHeight * Options.MapHeight
647+
);
643648

644-
map.DrawPanorama();
649+
if (!mapBounds.IntersectsWith(WorldViewport))
650+
{
651+
return;
645652
}
653+
654+
map.DrawPanorama();
646655
}
647656

648657
public static void DrawOverlay()
@@ -652,8 +661,7 @@ public static void DrawOverlay()
652661
return;
653662
}
654663

655-
var map = MapInstance.Get(Globals.Me?.MapId ?? Guid.Empty);
656-
if (map != null)
664+
if (MapInstance.TryGet(Globals.Me?.MapId ?? default, out var map))
657665
{
658666
float ecTime = Timing.Global.MillisecondsUtc - sOverlayUpdate;
659667

@@ -1027,14 +1035,28 @@ private static void ClearDarknessTexture()
10271035

10281036
private static void GenerateLightMap()
10291037
{
1030-
// If we're not allowed to draw lighting, exit out.
1031-
if (!Globals.Database.EnableLighting || Renderer == default || Globals.Me == default)
1038+
if (Renderer == default)
1039+
{
1040+
return;
1041+
}
1042+
1043+
if (sDarknessTexture == default)
1044+
{
1045+
return;
1046+
}
1047+
1048+
var mapId = Globals.Me?.MapId ?? default;
1049+
if (mapId == default)
1050+
{
1051+
return;
1052+
}
1053+
1054+
if (!Globals.Database.EnableLighting)
10321055
{
10331056
return;
10341057
}
10351058

1036-
var map = MapInstance.Get(Globals.Me.MapId);
1037-
if (map == null || sDarknessTexture == null)
1059+
if (!MapInstance.TryGet(mapId, out var map))
10381060
{
10391061
return;
10401062
}
@@ -1159,8 +1181,7 @@ public static void UpdatePlayerLight()
11591181
}
11601182

11611183
//Draw Light Around Player
1162-
var map = MapInstance.Get(Globals.Me.MapId);
1163-
if (map != null)
1184+
if (MapInstance.TryGet(Globals.Me.MapId, out var map))
11641185
{
11651186
float ecTime = Timing.Global.MillisecondsUtc - sLightUpdate;
11661187
var valChange = 255 * ecTime / 2000f;

Intersect.Client.Core/MonoGame/File Management/MonoContentManager.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public partial class MonoContentManager : GameContentManager
1818

1919
public MonoContentManager()
2020
{
21-
Init(this);
22-
2321
var rootPath = Path.GetFullPath(ClientConfiguration.ResourcesDirectory);
2422

2523
if (!Directory.Exists(rootPath))
@@ -288,7 +286,7 @@ public override void LoadSounds()
288286
}
289287
}
290288
}
291-
289+
292290
}
293291

294292
public override void LoadMusic()

Intersect.Client.Framework/File Management/GameContentManager.cs

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using Intersect.Async;
23
using Intersect.Client.Framework.Audio;
34
using Intersect.Client.Framework.Content;
@@ -26,37 +27,43 @@ public enum UI
2627
Debug,
2728
}
2829

29-
public static GameContentManager Current;
30+
public static GameContentManager Current { get; private set; }
3031

31-
protected Dictionary<string, IAsset> mAnimationDict = new Dictionary<string, IAsset>();
32+
protected readonly Dictionary<string, IAsset> mAnimationDict = [];
3233

33-
protected Dictionary<string, IAsset> mEntityDict = new Dictionary<string, IAsset>();
34+
protected readonly Dictionary<string, IAsset> mEntityDict = [];
3435

35-
protected Dictionary<string, IAsset> mFaceDict = new Dictionary<string, IAsset>();
36+
protected readonly Dictionary<string, IAsset> mFaceDict = [];
3637

37-
protected Dictionary<string, IAsset> mFogDict = new Dictionary<string, IAsset>();
38+
protected readonly Dictionary<string, IAsset> mFogDict = [];
3839

39-
protected List<GameFont> mFontDict = new List<GameFont>();
40+
protected readonly List<GameFont> mFontDict = [];
4041

41-
protected Dictionary<string, IAsset> mGuiDict = new Dictionary<string, IAsset>();
42+
protected readonly Dictionary<string, IAsset> mGuiDict = [];
4243

43-
protected Dictionary<string, IAsset> mImageDict = new Dictionary<string, IAsset>();
44+
protected readonly Dictionary<string, IAsset> mImageDict = [];
4445

45-
protected Dictionary<string, IAsset> mItemDict = new Dictionary<string, IAsset>();
46+
protected readonly Dictionary<string, IAsset> mItemDict = [];
4647

47-
protected Dictionary<string, IAsset> mMiscDict = new Dictionary<string, IAsset>();
48+
protected readonly Dictionary<string, IAsset> mMiscDict = [];
4849

49-
protected Dictionary<string, IAsset> mMusicDict = new Dictionary<string, IAsset>();
50+
protected readonly Dictionary<string, IAsset> mMusicDict = [];
5051

51-
protected Dictionary<string, IAsset> mPaperdollDict = new Dictionary<string, IAsset>();
52+
protected readonly Dictionary<string, IAsset> mPaperdollDict = [];
5253

53-
protected Dictionary<string, IAsset> mResourceDict = new Dictionary<string, IAsset>();
54+
protected readonly Dictionary<string, IAsset> mResourceDict = [];
5455

55-
protected Dictionary<string, GameShader> mShaderDict = new Dictionary<string, GameShader>();
56+
protected readonly Dictionary<string, GameShader> mShaderDict = [];
5657

57-
protected Dictionary<string, IAsset> mSoundDict = new Dictionary<string, IAsset>();
58+
protected readonly Dictionary<string, IAsset> mSoundDict = [];
5859

59-
protected Dictionary<KeyValuePair<UI, string>, string> mUiDict = new Dictionary<KeyValuePair<UI, string>, string>();
60+
protected readonly Dictionary<KeyValuePair<UI, string>, string> mUiDict = [];
61+
62+
protected readonly Dictionary<string, IAsset> mSpellDict = [];
63+
64+
protected readonly Dictionary<string, IAsset> mTexturePackDict = [];
65+
66+
protected readonly Dictionary<string, IAsset> mTilesetDict = [];
6067

6168
/// <summary>
6269
/// Contains all indexed files and their caches from sound pack files.
@@ -68,20 +75,13 @@ public enum UI
6875
/// </summary>
6976
public AssetPacker MusicPacks { get; set; }
7077

71-
protected Dictionary<string, IAsset> mSpellDict = new Dictionary<string, IAsset>();
72-
73-
protected Dictionary<string, IAsset> mTexturePackDict = new Dictionary<string, IAsset>();
74-
75-
//Game Content
76-
protected Dictionary<string, IAsset> mTilesetDict = new Dictionary<string, IAsset>();
77-
7878
public bool TilesetsLoaded = false;
7979

8080
public ContentWatcher ContentWatcher { get; protected set; }
8181

82-
public void Init(GameContentManager manager)
82+
protected GameContentManager()
8383
{
84-
Current = manager;
84+
Current = this;
8585
}
8686

8787
//Content Loading
@@ -199,8 +199,14 @@ public string[] GetTextureNames(TextureType type)
199199
return null;
200200
}
201201

202+
public bool TryGetTexture(TextureType textureType, string textureName, [NotNullWhen(true)] out GameTexture? texture)
203+
{
204+
texture = GetTexture(textureType, textureName);
205+
return texture != default;
206+
}
207+
202208
//Content Getters
203-
public virtual GameTexture GetTexture(TextureType type, string name)
209+
public virtual GameTexture? GetTexture(TextureType type, string name)
204210
{
205211
if (string.IsNullOrEmpty(name))
206212
{
@@ -282,19 +288,20 @@ public virtual GameTexture GetTexture(TextureType type, string name)
282288
return textureDict.TryGetValue(name.ToLower(), out var asset) ? asset as GameTexture : default;
283289
}
284290

285-
public virtual GameShader GetShader(string name)
291+
public bool TryGetShader(string shaderName, [NotNullWhen(true)] out GameShader? shader)
286292
{
287-
if (string.IsNullOrEmpty(name))
288-
{
289-
return null;
290-
}
293+
shader = GetShader(shaderName);
294+
return shader != default;
295+
}
291296

292-
if (mShaderDict == null)
297+
public virtual GameShader? GetShader(string name)
298+
{
299+
if (string.IsNullOrEmpty(name))
293300
{
294301
return null;
295302
}
296303

297-
return mShaderDict.TryGetValue(name.ToLower(), out var effect) ? effect : null;
304+
return mShaderDict?.GetValueOrDefault(name.ToLower());
298305
}
299306

300307
public virtual GameFont GetFont(string name, int size)

0 commit comments

Comments
 (0)