mirror of
				https://github.com/MaddyThorson/StrawberryBF.git
				synced 2025-11-04 01:41:33 +08:00 
			
		
		
		
	Loading fix. Font usage changes
This commit is contained in:
		@ -2,75 +2,53 @@ using System;
 | 
				
			|||||||
using SDL2;
 | 
					using SDL2;
 | 
				
			||||||
using System.IO;
 | 
					using System.IO;
 | 
				
			||||||
using System.Text;
 | 
					using System.Text;
 | 
				
			||||||
 | 
					using System.Collections;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Strawberry
 | 
					namespace Strawberry
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	public class Font : Asset
 | 
						public class Font : Asset
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		public SDLTTF.Font* Font { get; private set; }
 | 
							private Dictionary<int32, SDLTTF.Font*> fonts;
 | 
				
			||||||
		public int32 Size { get; private set; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		private String sizePath ~ delete _;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		private this(String path)
 | 
							private this(String path)
 | 
				
			||||||
			: base(path)
 | 
								: base(path)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			sizePath = new String(path);
 | 
								fonts = new Dictionary<int32, SDLTTF.Font*>();
 | 
				
			||||||
			sizePath.RemoveFromEnd(4);
 | 
					 | 
				
			||||||
			sizePath.Append(".txt");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			Load();
 | 
								Load();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		private this(String path, int32 size)
 | 
							public SDLTTF.Font* this[int32 size]
 | 
				
			||||||
			: base(path)
 | 
					 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			sizePath = null;
 | 
								get
 | 
				
			||||||
			Size = size;
 | 
								{
 | 
				
			||||||
 | 
									if (fonts.ContainsKey(size) && fonts[size] != null)
 | 
				
			||||||
			Load();
 | 
										return fonts[size];
 | 
				
			||||||
 | 
									else
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										Calc.Log(Path);
 | 
				
			||||||
 | 
										let f = SDLTTF.OpenFont(Path, size);
 | 
				
			||||||
 | 
										fonts[size] = f;
 | 
				
			||||||
 | 
										return f;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}	
 | 
							}	
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		protected override void Load()
 | 
							protected override void Load()
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			if (sizePath != null)
 | 
								Runtime.Assert(File.Exists(Path), "Font does not exist!");
 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				if (File.Exists(sizePath))
 | 
					 | 
				
			||||||
				{
 | 
					 | 
				
			||||||
					//Load size
 | 
					 | 
				
			||||||
					let stream = scope FileStream();
 | 
					 | 
				
			||||||
					stream.Open(sizePath, .Read);
 | 
					 | 
				
			||||||
					let arr = scope uint8[stream.Length];
 | 
					 | 
				
			||||||
					for (let i < arr.Count)
 | 
					 | 
				
			||||||
						arr[i] = stream.Read<uint8>();
 | 
					 | 
				
			||||||
					stream.Close();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
					let str = scope String();
 | 
								for (let k in fonts.Keys)
 | 
				
			||||||
					Encoding.UTF8.DecodeToUTF8(arr, str);
 | 
									fonts[k] = SDLTTF.OpenFont(Path, k);
 | 
				
			||||||
	
 | 
					 | 
				
			||||||
					Size = int32.Parse(str);
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				else
 | 
					 | 
				
			||||||
				{
 | 
					 | 
				
			||||||
					//Create size file
 | 
					 | 
				
			||||||
					let stream = scope FileStream();
 | 
					 | 
				
			||||||
					stream.Create(sizePath, .Write);
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
					stream.Write("12");
 | 
					 | 
				
			||||||
					stream.Close();
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
					Calc.Log("Warning: Edit '{0}' to define load size of font '{1}'", sizePath, Path);
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
					Size = 12;
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			Font = SDLTTF.OpenFont(Path, Size);
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		protected override void Unload()
 | 
							protected override void Unload()
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			SDLTTF.CloseFont(Font);
 | 
								for (let kv in fonts)
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									SDLTTF.CloseFont(kv.value);
 | 
				
			||||||
 | 
									fonts[kv.key] = null;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								delete fonts;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -57,13 +57,14 @@ namespace Strawberry
 | 
				
			|||||||
			//Load files
 | 
								//Load files
 | 
				
			||||||
			for (let file in Directory.EnumerateFiles(directory, wildcard))
 | 
								for (let file in Directory.EnumerateFiles(directory, wildcard))
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				let path = scope String();
 | 
									let path = new String();
 | 
				
			||||||
				file.GetFilePath(path);
 | 
									file.GetFilePath(path);
 | 
				
			||||||
				let asset = new [Friend]T(path);
 | 
									let asset = new [Friend]T(path);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				path.Remove(0, rootDir.Length + 1);
 | 
									let key = new String(path);
 | 
				
			||||||
				path.RemoveFromEnd(path.Length - path.IndexOf('.'));
 | 
									key.Remove(0, rootDir.Length + 1);
 | 
				
			||||||
				putInto.Add(new String(path), asset);
 | 
									key.RemoveFromEnd(key.Length - key.IndexOf('.'));
 | 
				
			||||||
 | 
									putInto.Add(key, asset);
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -100,10 +100,10 @@ namespace Strawberry
 | 
				
			|||||||
			SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, rotation, &cnt, .None);
 | 
								SDL.RenderCopyEx(Game.Renderer, sprite[frame].Texture, &src, &dst, rotation, &cnt, .None);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		static public void Text(Strawberry.Font font, String text, Point position, Color color)
 | 
							static public void Text(SDL2.SDLTTF.Font* font, String text, Point position, Color color)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A);
 | 
								SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A);
 | 
				
			||||||
			let surface = SDLTTF.RenderUTF8_Blended(font.Font, text, color);
 | 
								let surface = SDLTTF.RenderUTF8_Blended(font, text, color);
 | 
				
			||||||
			let texture = SDL.CreateTextureFromSurface(Game.Renderer, surface);
 | 
								let texture = SDL.CreateTextureFromSurface(Game.Renderer, surface);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			SDL.Rect srcRect = .(0, 0, surface.w, surface.h);
 | 
								SDL.Rect srcRect = .(0, 0, surface.w, surface.h);
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user