reorganized file structure; texture and fonts now have appropriate folder resolver;

This commit is contained in:
Harrison Deng 2018-09-16 01:37:48 -05:00
parent 7735445888
commit 539a39e51f
18 changed files with 279 additions and 18 deletions

View File

@ -42,21 +42,27 @@
<ApplicationManifest>app.manifest</ApplicationManifest> <ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Zer01HD\Game\LoadingScreen.cs" /> <Compile Include="Zer01HD\Utilities\UI\LoadingScreen.cs" />
<Compile Include="Zer01HD\Game\MainScreen\MainScreen.cs" /> <Compile Include="Zer01HD\Game\MainScreen\MainScreen.cs" />
<Compile Include="Zer01HD\UI\Modular\Module.cs" /> <Compile Include="Zer01HD\Utilities\UI\Modular\Module.cs" />
<Compile Include="Zer01HD\UI\Modular\ModuleGroup.cs" /> <Compile Include="Zer01HD\Utilities\UI\Modular\ModuleGroup.cs" />
<Compile Include="RhythmBulletGame.cs" /> <Compile Include="RhythmBulletGame.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Zer01HD\UI\Book\Page.cs" /> <Compile Include="Zer01HD\Utilities\UI\Book\Page.cs" />
<Compile Include="Zer01HD\UI\Book\Book.cs" /> <Compile Include="Zer01HD\Utilities\UI\Book\Book.cs" />
<Compile Include="Zer01HD\UI\Modular\Text.cs" /> <Compile Include="Zer01HD\Utilities\UI\Modular\Text.cs" />
<Compile Include="Zer01HD\UI\Screen.cs" /> <Compile Include="Zer01HD\Utilities\UI\Screen.cs" />
<Compile Include="Zer01HD\Utilities\ContentSystem\ContentLoad.cs" /> <Compile Include="Zer01HD\Utilities\ContentSystem\ContentLoad.cs" />
<Compile Include="Zer01HD\Game\ContentResolvers\FontContentResolver.cs" />
<Compile Include="Zer01HD\Game\ContentResolvers\ResolutionContentResolver.cs" />
<Compile Include="Zer01HD\Utilities\ContentSystem\ContentSystem.cs" /> <Compile Include="Zer01HD\Utilities\ContentSystem\ContentSystem.cs" />
<Compile Include="Zer01HD\Utilities\ContentSystem\IContentResolver.cs" /> <Compile Include="Zer01HD\Utilities\ContentSystem\IContentResolver.cs" />
<Compile Include="Zer01HD\Utilities\ParticleSystem\Particle.cs" /> <Compile Include="Zer01HD\Utilities\ParticleSystem\Particle.cs" />
<Compile Include="Zer01HD\Game\Preferences\Controls.cs" />
<Compile Include="Zer01HD\Game\Preferences\General.cs" />
<Compile Include="Zer01HD\Utilities\Preferences.cs" />
<Compile Include="Zer01HD\Utilities\Resolution.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="MonoGame.Framework"> <Reference Include="MonoGame.Framework">

View File

@ -2,8 +2,10 @@
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using RhythmBullet.Zer01HD.Game; using RhythmBullet.Zer01HD.Game;
using RhythmBullet.Zer01HD.Game.ContentResolvers;
using RhythmBullet.Zer01HD.UI; using RhythmBullet.Zer01HD.UI;
using RhythmBullet.Zer01HD.Utilities.ContentSystem; using RhythmBullet.Zer01HD.Utilities.ContentSystem;
using RhythmBullet.Zer01HD.Utilities.UI;
namespace RhythmBullet namespace RhythmBullet
{ {
@ -15,6 +17,7 @@ namespace RhythmBullet
public GraphicsDeviceManager Graphics; public GraphicsDeviceManager Graphics;
SpriteBatch spriteBatch; SpriteBatch spriteBatch;
public readonly ContentSystem Assets; public readonly ContentSystem Assets;
private readonly ResolutionContentResolver resolutionContentResolver;
private Screen currentScreen; private Screen currentScreen;
Screen CurrentScreen Screen CurrentScreen
{ {
@ -41,6 +44,10 @@ namespace RhythmBullet
Window.IsBorderless = true; Window.IsBorderless = true;
Content.RootDirectory = "Content"; Content.RootDirectory = "Content";
Assets = new ContentSystem(Content); Assets = new ContentSystem(Content);
resolutionContentResolver = new ResolutionContentResolver();
FontContentResolver fcr = new FontContentResolver(resolutionContentResolver);
Assets.contentResolver.Add(typeof(Texture2D), resolutionContentResolver);
Assets.contentResolver.Add(typeof(SpriteFont), fcr);
} }
/// <summary> /// <summary>
@ -102,10 +109,15 @@ namespace RhythmBullet
base.Draw(gameTime); base.Draw(gameTime);
} }
public void Resize(int width, int height) public void Reload()
{
Assets.UnloadAll();
resolutionContentResolver.Width = Graphics.PreferredBackBufferWidth;
resolutionContentResolver.Height = Graphics.PreferredBackBufferHeight;
}
void QueueContent()
{ {
Graphics.PreferredBackBufferWidth = width;
Graphics.PreferredBackBufferHeight = height;
} }
} }

View File

@ -0,0 +1,24 @@
using RhythmBullet.Zer01HD.Utilities.ContentSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Game.ContentResolvers
{
class FontContentResolver : IContentResolver
{
readonly ResolutionContentResolver rcr;
public FontContentResolver(ResolutionContentResolver rcr)
{
this.rcr = rcr;
}
public string Load(string assetName)
{
return rcr.Load("fonts/" + assetName);
}
}
}

View File

@ -0,0 +1,109 @@
using RhythmBullet.Zer01HD.Game.Preferences;
using RhythmBullet.Zer01HD.Utilities;
using RhythmBullet.Zer01HD.Utilities.ContentSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Game.ContentResolvers
{
class ResolutionContentResolver : IContentResolver
{
int width, height;
bool updated;
Resolution bestResolution;
public int Width
{
get
{
return width;
}
set
{
updated = true;
width = value;
}
}
public int Height
{
get
{
return height;
}
set
{
updated = true;
height = value;
}
}
Resolution[] resolutions = {
new Resolution(1920, 1080),
new Resolution(2560, 1440),
new Resolution(3840, 2160)
};
Resolution ChooseRounded()
{
updated = false;
Resolution best = resolutions[0];
int leastDifference = -1;
int w = Width, h = Height;
if (w > h)
{
for (int i = 0; i < resolutions.Length; i++)
{
int currentDiff = h - resolutions[i].Height;
if (currentDiff < 0)
{
currentDiff = currentDiff * -1;
}
if ((currentDiff < leastDifference) || leastDifference == -1)
{
best = resolutions[i];
leastDifference = currentDiff;
}
}
}
else
{
for (int i = 0; i < resolutions.Length; i++)
{
int currentDiff = w - resolutions[i].Width;
if (currentDiff < 0)
{
currentDiff = currentDiff * -1;
}
if (currentDiff < leastDifference || leastDifference == -1)
{
best = resolutions[i];
leastDifference = currentDiff;
}
}
}
return best;
}
public string Load(string path)
{
if (updated)
{
bestResolution = ChooseRounded();
}
return bestResolution + "/" + path;
}
}
}

View File

@ -1,5 +1,6 @@
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using RhythmBullet.Zer01HD.UI; using RhythmBullet.Zer01HD.UI;
using RhythmBullet.Zer01HD.Utilities.UI;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Game.Preferences
{
class Controls
{
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Game.Preferences
{
class General
{
public Resolution Resolution;
public bool Fullscreen;
public float MusicVolume;
public float FXVolume;
}
}

View File

@ -16,7 +16,7 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
readonly ContentManager contentManager; readonly ContentManager contentManager;
readonly Queue<ContentLoad> queue; readonly Queue<ContentLoad> queue;
Dictionary<string, IDisposable> assets; Dictionary<string, IDisposable> assets;
readonly Dictionary<Type, IContentResolver> contentResolver; public readonly Dictionary<Type, IContentResolver> contentResolver;
public ContentSystem(ContentManager contentManager) public ContentSystem(ContentManager contentManager)
{ {
@ -69,7 +69,7 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
/// <summary> /// <summary>
/// Called whenever a batch of assets should be loaded from the queue. Safe to call once every frame. /// Called whenever a batch of assets should be loaded from the queue. Safe to call once every frame.
/// </summary> /// </summary>
void Update() public void Update()
{ {
if (queue.Count > 0) if (queue.Count > 0)
{ {
@ -81,16 +81,45 @@ namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
} }
} }
void UnloadAll() /// <summary>
/// Removes the asset from the list of assets in the system.
/// Cannot remove from queue.
/// </summary>
/// <param name="name">the string name used to load the asset</param>
public void Remove(string name)
{
if (assets.ContainsKey(name))
{
assets[name].Dispose();
assets.Remove(name);
}
}
/// <summary>
/// Clears the queue.
/// </summary>
public void ClearQueue()
{
lock(queue)
{
queue.Clear();
}
}
/// <summary>
/// Unloads everything from both queue and loaded list while properly disposing of the assets loaded.
/// </summary>
public void UnloadAll()
{ {
foreach (KeyValuePair<string, IDisposable> asset in assets) foreach (KeyValuePair<string, IDisposable> asset in assets)
{ {
asset.Value.Dispose(); asset.Value.Dispose();
assets.Remove(asset.Key); assets.Remove(asset.Key);
} }
ClearQueue();
} }
bool Done() public bool Done()
{ {
return queued; return queued;
} }

View File

@ -6,8 +6,13 @@ using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.ContentSystem namespace RhythmBullet.Zer01HD.Utilities.ContentSystem
{ {
interface IContentResolver public interface IContentResolver
{ {
string Load(string path); /// <summary>
/// Returns the complete path with the content folder as root.
/// </summary>
/// <param name="assetName">Is the asset's name</param>
/// <returns></returns>
string Load(string assetName);
} }
} }

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities
{
class Preferences
{
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Game.Preferences
{
public class Resolution : IComparable<Resolution>
{
public int Width, Height;
public Resolution(int width, int height)
{
Width = width;
Height = height;
}
public int CompareTo(Resolution other)
{
return Area() - other.Area();
}
public override string ToString()
{
return Width + "x" + Height;
}
public int Area()
{
return Width * Height;
}
}
}

View File

@ -2,6 +2,7 @@
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using RhythmBullet.Zer01HD.UI; using RhythmBullet.Zer01HD.UI;
using RhythmBullet.Zer01HD.Utilities.UI;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;

View File

@ -6,7 +6,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.UI namespace RhythmBullet.Zer01HD.Utilities.UI
{ {
public class Screen public class Screen
{ {