mirror of
				https://github.com/MaddyThorson/StrawberryBF.git
				synced 2025-11-04 01:41:33 +08:00 
			
		
		
		
	Assets split off. Better Calc.Log
This commit is contained in:
		
							
								
								
									
										13
									
								
								src/Assets/Asset.bf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/Assets/Asset.bf
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
using System;
 | 
			
		||||
namespace Strawberry
 | 
			
		||||
{
 | 
			
		||||
	public abstract class Asset
 | 
			
		||||
	{
 | 
			
		||||
		public readonly String Path;
 | 
			
		||||
 | 
			
		||||
		protected this(String path)
 | 
			
		||||
		{
 | 
			
		||||
			Path = path;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								src/Assets/Font.bf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/Assets/Font.bf
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
using System;
 | 
			
		||||
using SDL2;
 | 
			
		||||
 | 
			
		||||
namespace Strawberry
 | 
			
		||||
{
 | 
			
		||||
	public class Font : Asset
 | 
			
		||||
	{
 | 
			
		||||
		private SDLTTF.Font* font;
 | 
			
		||||
 | 
			
		||||
		private this(String path)
 | 
			
		||||
			: base(path)
 | 
			
		||||
		{
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public ~this()
 | 
			
		||||
		{
 | 
			
		||||
			SDLTTF.CloseFont(font);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -7,32 +7,8 @@ using MiniZ;
 | 
			
		||||
 | 
			
		||||
namespace Strawberry
 | 
			
		||||
{
 | 
			
		||||
	public class Sprite
 | 
			
		||||
	public class Sprite : Asset
 | 
			
		||||
	{
 | 
			
		||||
		private enum Modes
 | 
			
		||||
		{
 | 
			
		||||
		    Indexed = 1,
 | 
			
		||||
		    Grayscale = 2,
 | 
			
		||||
		    RGBA = 4,
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		private enum Chunks
 | 
			
		||||
		{
 | 
			
		||||
		    OldPaletteA = 0x0004,
 | 
			
		||||
		    OldPaletteB = 0x0011,
 | 
			
		||||
		    Layer = 0x2004,
 | 
			
		||||
		    Cel = 0x2005,
 | 
			
		||||
		    CelExtra = 0x2006,
 | 
			
		||||
		    Mask = 0x2016,
 | 
			
		||||
		    Path = 0x2017,
 | 
			
		||||
		    FrameTags = 0x2018,
 | 
			
		||||
		    Palette = 0x2019,
 | 
			
		||||
		    UserData = 0x2020,
 | 
			
		||||
		    Slice = 0x2022
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public readonly String Path;
 | 
			
		||||
 | 
			
		||||
		public int Width { get; private set; }
 | 
			
		||||
		public int Height { get; private set; }
 | 
			
		||||
 | 
			
		||||
@ -43,14 +19,13 @@ namespace Strawberry
 | 
			
		||||
		private Modes mode;
 | 
			
		||||
 | 
			
		||||
		private this(String path)
 | 
			
		||||
			: base(path)
 | 
			
		||||
		{
 | 
			
		||||
			Path = new String(path);
 | 
			
		||||
			Load();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public ~this()
 | 
			
		||||
		{
 | 
			
		||||
			delete Path;
 | 
			
		||||
			Unload();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@ -438,6 +413,28 @@ namespace Strawberry
 | 
			
		||||
 | 
			
		||||
		// Data
 | 
			
		||||
 | 
			
		||||
		private enum Modes
 | 
			
		||||
		{
 | 
			
		||||
		    Indexed = 1,
 | 
			
		||||
		    Grayscale = 2,
 | 
			
		||||
		    RGBA = 4,
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		private enum Chunks
 | 
			
		||||
		{
 | 
			
		||||
		    OldPaletteA = 0x0004,
 | 
			
		||||
		    OldPaletteB = 0x0011,
 | 
			
		||||
		    Layer = 0x2004,
 | 
			
		||||
		    Cel = 0x2005,
 | 
			
		||||
		    CelExtra = 0x2006,
 | 
			
		||||
		    Mask = 0x2016,
 | 
			
		||||
		    Path = 0x2017,
 | 
			
		||||
		    FrameTags = 0x2018,
 | 
			
		||||
		    Palette = 0x2019,
 | 
			
		||||
		    UserData = 0x2020,
 | 
			
		||||
		    Slice = 0x2022
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public class Frame
 | 
			
		||||
		{
 | 
			
		||||
			public SDL.Texture* Texture;
 | 
			
		||||
@ -15,7 +15,6 @@ namespace Strawberry
 | 
			
		||||
 | 
			
		||||
	public class Game
 | 
			
		||||
	{
 | 
			
		||||
		public readonly Dictionary<String, Sprite> Sprites;
 | 
			
		||||
		public readonly List<VirtualInput> VirtualInputs;
 | 
			
		||||
		public readonly String Title;
 | 
			
		||||
		public readonly int Width;
 | 
			
		||||
@ -84,10 +83,8 @@ namespace Strawberry
 | 
			
		||||
			VirtualInputs = new List<VirtualInput>();
 | 
			
		||||
			Input.[Friend]Init(gamepadLimit);
 | 
			
		||||
 | 
			
		||||
			Sprites = new Dictionary<String, Sprite>();
 | 
			
		||||
			LoadSprites();
 | 
			
		||||
 | 
			
		||||
			BuildTypeLists();
 | 
			
		||||
			Assets.LoadAll();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public ~this()
 | 
			
		||||
@ -107,10 +104,9 @@ namespace Strawberry
 | 
			
		||||
				delete VirtualInputs;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			Assets.DisposeAll();
 | 
			
		||||
			DisposeTypeLists();
 | 
			
		||||
			Input.[Friend]Dispose();
 | 
			
		||||
			DisposeSprites();
 | 
			
		||||
			Sprite.[Friend]Dispose();
 | 
			
		||||
			Game = null;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@ -230,54 +226,6 @@ namespace Strawberry
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Load Images
 | 
			
		||||
 | 
			
		||||
		private void LoadSprites()
 | 
			
		||||
		{
 | 
			
		||||
			let root = scope String(ContentRoot);
 | 
			
		||||
			root.Append(Path.DirectorySeparatorChar);
 | 
			
		||||
			root.Append("Sprites");
 | 
			
		||||
			if (Directory.Exists(root))
 | 
			
		||||
				LoadSpritesDir(root);
 | 
			
		||||
			else
 | 
			
		||||
				Calc.Log("Content/Sprites folder does not exist!");
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		private void LoadSpritesDir(String directory)
 | 
			
		||||
		{
 | 
			
		||||
			for (let dir in Directory.EnumerateDirectories(directory))
 | 
			
		||||
			{
 | 
			
		||||
				let path = scope String();
 | 
			
		||||
				dir.GetFilePath(path);
 | 
			
		||||
				LoadSpritesDir(path);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			for (let file in Directory.EnumerateFiles(directory, "*.ase*"))
 | 
			
		||||
			{
 | 
			
		||||
				let path = scope String();
 | 
			
		||||
				file.GetFilePath(path);
 | 
			
		||||
				if (path.EndsWith(".ase") || path.EndsWith(".aseprite"))
 | 
			
		||||
				{
 | 
			
		||||
					let sprite = new [Friend]Sprite(path);
 | 
			
		||||
	
 | 
			
		||||
					path.Remove(0, ContentRoot.Length + 9);
 | 
			
		||||
					path.RemoveFromEnd(path.Length - path.IndexOf('.'));
 | 
			
		||||
					Sprites.Add(new String(path), sprite);
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		private void DisposeSprites()
 | 
			
		||||
		{
 | 
			
		||||
			for (let kv in Sprites)
 | 
			
		||||
			{
 | 
			
		||||
				delete kv.key;
 | 
			
		||||
				delete kv.value;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			delete Sprites;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Type assignable caching
 | 
			
		||||
 | 
			
		||||
		private void BuildTypeLists()
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										62
									
								
								src/Static/Assets.bf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								src/Static/Assets.bf
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,62 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections;
 | 
			
		||||
using System.IO;
 | 
			
		||||
 | 
			
		||||
namespace Strawberry
 | 
			
		||||
{
 | 
			
		||||
	static public class Assets
 | 
			
		||||
	{
 | 
			
		||||
		static public Dictionary<String, Sprite> Sprites { get; private set; }
 | 
			
		||||
		static public Dictionary<String, Font> Fonts { get; private set; }
 | 
			
		||||
 | 
			
		||||
		static public void LoadAll()
 | 
			
		||||
		{
 | 
			
		||||
			Sprites = new Dictionary<String, Sprite>();
 | 
			
		||||
			Load<Sprite>("Sprites", "*.ase*", Sprites);
 | 
			
		||||
 | 
			
		||||
			Fonts = new Dictionary<String, Font>();
 | 
			
		||||
			Load<Font>("Fonts", "*.ttf", Fonts);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		static public void DisposeAll()
 | 
			
		||||
		{
 | 
			
		||||
			DeleteDictionaryAndKeysAndItems!(Sprites);
 | 
			
		||||
			DeleteDictionaryAndKeysAndItems!(Fonts);
 | 
			
		||||
			Sprite.[Friend]Dispose();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		static private void Load<T>(String directory, String wildcard, Dictionary<String, T> putInto) where T : Asset
 | 
			
		||||
		{
 | 
			
		||||
			let root = scope String(Game.ContentRoot);
 | 
			
		||||
			root.Append(Path.DirectorySeparatorChar);
 | 
			
		||||
			root.Append(directory);
 | 
			
		||||
			if (Directory.Exists(root))
 | 
			
		||||
				LoadDir<T>(root, wildcard, putInto);
 | 
			
		||||
			else
 | 
			
		||||
				Calc.Log("Content/{0} folder does not exist!", directory);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		static private void LoadDir<T>(String directory, String wildcard, Dictionary<String, T> putInto) where T : Asset
 | 
			
		||||
		{
 | 
			
		||||
			//Recursive folder search
 | 
			
		||||
			for (let dir in Directory.EnumerateDirectories(directory))
 | 
			
		||||
			{
 | 
			
		||||
				let path = scope String();
 | 
			
		||||
				dir.GetFilePath(path);
 | 
			
		||||
				LoadDir<T>(path, wildcard, putInto);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			//Load files
 | 
			
		||||
			for (let file in Directory.EnumerateFiles(directory, wildcard))
 | 
			
		||||
			{
 | 
			
		||||
				let path = scope String();
 | 
			
		||||
				file.GetFilePath(path);
 | 
			
		||||
				let sprite = new [Friend]T(path);
 | 
			
		||||
 | 
			
		||||
				path.Remove(0, Game.ContentRoot.Length + 9);
 | 
			
		||||
				path.RemoveFromEnd(path.Length - path.IndexOf('.'));
 | 
			
		||||
				putInto.Add(new String(path), sprite);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -75,5 +75,24 @@ namespace Strawberry
 | 
			
		||||
			del(string);
 | 
			
		||||
			Debug.WriteLine(string);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		static public void Log(StringView str, params Object[] args)
 | 
			
		||||
		{
 | 
			
		||||
			String string = scope String(str);
 | 
			
		||||
 | 
			
		||||
			for (let i < args.Count)
 | 
			
		||||
			{
 | 
			
		||||
				String arg = scope String();
 | 
			
		||||
				String num = scope String("{x}");
 | 
			
		||||
				i.ToString(arg);
 | 
			
		||||
				num.Replace("x", arg);
 | 
			
		||||
 | 
			
		||||
				arg.Clear();
 | 
			
		||||
				args[i].ToString(arg);
 | 
			
		||||
				string.Replace(num, arg);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			Debug.WriteLine(string);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user