Simple font loading and rendering

This commit is contained in:
Matt Thorson 2020-06-15 00:56:01 -07:00
parent 2a9940d7bd
commit de0146e4e5
5 changed files with 113 additions and 26 deletions

View File

@ -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();
}
}
}

View File

@ -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<uint8>();
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);
}
}
}

View File

@ -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<uint8>();

View File

@ -9,8 +9,16 @@ namespace Strawberry
static public Dictionary<String, Sprite> Sprites { get; private set; }
static public Dictionary<String, Font> 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<String, Sprite>();
Load<Sprite>("Sprites", "*.ase*", Sprites);
@ -27,23 +35,23 @@ namespace Strawberry
static private void Load<T>(String directory, String wildcard, Dictionary<String, T> 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<T>(root, wildcard, putInto);
LoadDir<T>(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<T>(String directory, String wildcard, Dictionary<String, T> putInto) where T : Asset
static private void LoadDir<T>(String rootDir, 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);
LoadDir<T>(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);
}
}
}

View File

@ -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);
}
}
}