diff --git a/src/Assets/Asset.bf b/src/Assets/Asset.bf index 98cabdd..51156fb 100644 --- a/src/Assets/Asset.bf +++ b/src/Assets/Asset.bf @@ -1,4 +1,6 @@ using System; +using System.IO; + namespace Strawberry { public abstract class Asset @@ -9,5 +11,26 @@ namespace Strawberry { Path = path; } + + public ~this() + { + Unload(); + } + + protected mixin OpenFileStream() + { + let stream = scope:: FileStream(); + stream.Open(Path, .Read, .Read); + stream + } + + protected abstract void Load(); + protected abstract void Unload(); + + public void Reload() + { + Unload(); + Load(); + } } } diff --git a/src/Assets/Font.bf b/src/Assets/Font.bf index 492dcca..4f9422a 100644 --- a/src/Assets/Font.bf +++ b/src/Assets/Font.bf @@ -1,21 +1,76 @@ using System; using SDL2; +using System.IO; +using System.Text; namespace Strawberry { public class Font : Asset { - private SDLTTF.Font* font; + public SDLTTF.Font* Font { get; private set; } + public int32 Size { get; private set; } + + private String sizePath ~ delete _; private this(String path) : base(path) { + sizePath = new String(path); + sizePath.RemoveFromEnd(4); + sizePath.Append(".txt"); + Load(); } - public ~this() + private this(String path, int32 size) + : base(path) { - SDLTTF.CloseFont(font); + sizePath = null; + Size = size; + + Load(); + } + + protected override void Load() + { + if (sizePath != null) + { + 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(); + stream.Close(); + + let str = scope String(); + Encoding.UTF8.DecodeToUTF8(arr, str); + + 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() + { + SDLTTF.CloseFont(Font); } } } diff --git a/src/Assets/Sprite.bf b/src/Assets/Sprite.bf index d0fb685..720d243 100644 --- a/src/Assets/Sprite.bf +++ b/src/Assets/Sprite.bf @@ -24,17 +24,12 @@ namespace Strawberry Load(); } - public ~this() - { - Unload(); - } - public Frame this[int index] { get => frames[index]; } - private void Unload() + override protected void Unload() { for (let f in frames) delete f; @@ -53,21 +48,14 @@ namespace Strawberry delete slices; } - public void Reload() - { - Unload(); - Load(); - } - - private void Load() + override protected void Load() { /* Aseprite file loading based on code from Noel Berry's Foster Framework here: https://github.com/NoelFB/Foster/blob/master/Framework/Graphics/Images/Aseprite.cs */ - let stream = scope FileStream(); - stream.Open(Path, .Read, .Read); + let stream = OpenFileStream!(); //Helpers to match ASE file format spec uint8 BYTE() => stream.Read(); diff --git a/src/Static/Assets.bf b/src/Static/Assets.bf index 2c23921..085b032 100644 --- a/src/Static/Assets.bf +++ b/src/Static/Assets.bf @@ -9,8 +9,16 @@ namespace Strawberry static public Dictionary Sprites { get; private set; } static public Dictionary Fonts { get; private set; } + static public String ContentRoot { get; private set; } + static public void LoadAll() { + #if DEBUG + ContentRoot = "../../../src/Content/"; + #else + ContentRoot = "Content"; + #endif + Sprites = new Dictionary(); Load("Sprites", "*.ase*", Sprites); @@ -27,23 +35,23 @@ namespace Strawberry static private void Load(String directory, String wildcard, Dictionary putInto) where T : Asset { - let root = scope String(Game.ContentRoot); + let root = scope String(ContentRoot); root.Append(Path.DirectorySeparatorChar); root.Append(directory); if (Directory.Exists(root)) - LoadDir(root, wildcard, putInto); + LoadDir(root, root, wildcard, putInto); else - Calc.Log("Content/{0} folder does not exist!", directory); + Calc.Log("Create a Content/{0} folder to load {0}", directory); } - static private void LoadDir(String directory, String wildcard, Dictionary putInto) where T : Asset + static private void LoadDir(String rootDir, String directory, String wildcard, Dictionary putInto) where T : Asset { //Recursive folder search for (let dir in Directory.EnumerateDirectories(directory)) { let path = scope String(); dir.GetFilePath(path); - LoadDir(path, wildcard, putInto); + LoadDir(rootDir, path, wildcard, putInto); } //Load files @@ -51,11 +59,11 @@ namespace Strawberry { let path = scope String(); file.GetFilePath(path); - let sprite = new [Friend]T(path); + let asset = new [Friend]T(path); - path.Remove(0, Game.ContentRoot.Length + 9); + path.Remove(0, rootDir.Length + 1); path.RemoveFromEnd(path.Length - path.IndexOf('.')); - putInto.Add(new String(path), sprite); + putInto.Add(new String(path), asset); } } } diff --git a/src/Static/Draw.bf b/src/Static/Draw.bf index 33e2176..4829bf4 100644 --- a/src/Static/Draw.bf +++ b/src/Static/Draw.bf @@ -99,5 +99,18 @@ namespace Strawberry SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend); 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) + { + SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); + let surface = SDLTTF.RenderUTF8_Blended(font.Font, text, color); + let texture = SDL.CreateTextureFromSurface(Game.Renderer, surface); + + SDL.Rect srcRect = .(0, 0, surface.w, surface.h); + SDL.Rect destRect = .((int32)position.X, (int32)position.Y, surface.w, surface.h); + SDL.RenderCopy(Game.Renderer, texture, &srcRect, &destRect); + SDL.FreeSurface(surface); + SDL.DestroyTexture(texture); + } } }