Loading fix. Font usage changes

This commit is contained in:
Matt Thorson 2020-06-15 22:41:35 -07:00
parent de0146e4e5
commit f1285179cd
3 changed files with 33 additions and 54 deletions

View File

@ -2,75 +2,53 @@ using System;
using SDL2;
using System.IO;
using System.Text;
using System.Collections;
namespace Strawberry
{
public class Font : Asset
{
public SDLTTF.Font* Font { get; private set; }
public int32 Size { get; private set; }
private String sizePath ~ delete _;
private Dictionary<int32, SDLTTF.Font*> fonts;
private this(String path)
: base(path)
{
sizePath = new String(path);
sizePath.RemoveFromEnd(4);
sizePath.Append(".txt");
fonts = new Dictionary<int32, SDLTTF.Font*>();
Load();
}
private this(String path, int32 size)
: base(path)
public SDLTTF.Font* this[int32 size]
{
sizePath = null;
Size = size;
Load();
}
get
{
if (fonts.ContainsKey(size) && fonts[size] != null)
return fonts[size];
else
{
Calc.Log(Path);
let f = SDLTTF.OpenFont(Path, size);
fonts[size] = f;
return f;
}
}
}
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;
}
}
Runtime.Assert(File.Exists(Path), "Font does not exist!");
Font = SDLTTF.OpenFont(Path, Size);
for (let k in fonts.Keys)
fonts[k] = SDLTTF.OpenFont(Path, k);
}
protected override void Unload()
{
SDLTTF.CloseFont(Font);
for (let kv in fonts)
{
SDLTTF.CloseFont(kv.value);
fonts[kv.key] = null;
}
delete fonts;
}
}
}

View File

@ -57,13 +57,14 @@ namespace Strawberry
//Load files
for (let file in Directory.EnumerateFiles(directory, wildcard))
{
let path = scope String();
let path = new String();
file.GetFilePath(path);
let asset = new [Friend]T(path);
path.Remove(0, rootDir.Length + 1);
path.RemoveFromEnd(path.Length - path.IndexOf('.'));
putInto.Add(new String(path), asset);
let key = new String(path);
key.Remove(0, rootDir.Length + 1);
key.RemoveFromEnd(key.Length - key.IndexOf('.'));
putInto.Add(key, asset);
}
}
}

View File

@ -100,10 +100,10 @@ namespace Strawberry
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);
let surface = SDLTTF.RenderUTF8_Blended(font.Font, text, color);
let surface = SDLTTF.RenderUTF8_Blended(font, text, color);
let texture = SDL.CreateTextureFromSurface(Game.Renderer, surface);
SDL.Rect srcRect = .(0, 0, surface.w, surface.h);