StrawberryBF/src/Assets/Font.bf

77 lines
1.3 KiB
Brainfuck
Raw Normal View History

2020-06-15 11:48:01 +08:00
using System;
using SDL2;
2020-06-15 15:56:01 +08:00
using System.IO;
using System.Text;
2020-06-15 11:48:01 +08:00
namespace Strawberry
{
public class Font : Asset
{
2020-06-15 15:56:01 +08:00
public SDLTTF.Font* Font { get; private set; }
public int32 Size { get; private set; }
private String sizePath ~ delete _;
2020-06-15 11:48:01 +08:00
private this(String path)
: base(path)
{
2020-06-15 15:56:01 +08:00
sizePath = new String(path);
sizePath.RemoveFromEnd(4);
sizePath.Append(".txt");
Load();
}
private this(String path, int32 size)
: base(path)
{
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;
}
}
2020-06-15 11:48:01 +08:00
2020-06-15 15:56:01 +08:00
Font = SDLTTF.OpenFont(Path, Size);
2020-06-15 11:48:01 +08:00
}
2020-06-15 15:56:01 +08:00
protected override void Unload()
2020-06-15 11:48:01 +08:00
{
2020-06-15 15:56:01 +08:00
SDLTTF.CloseFont(Font);
2020-06-15 11:48:01 +08:00
}
}
}