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 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();
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); for (let k in fonts.Keys)
fonts[k] = SDLTTF.OpenFont(Path, k);
} }
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;
} }
} }
} }

View File

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

View File

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