mirror of
https://github.com/MaddyThorson/StrawberryBF.git
synced 2024-11-29 17:08:55 +08:00
Simple font loading and rendering
This commit is contained in:
parent
2a9940d7bd
commit
de0146e4e5
|
@ -1,4 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace Strawberry
|
namespace Strawberry
|
||||||
{
|
{
|
||||||
public abstract class Asset
|
public abstract class Asset
|
||||||
|
@ -9,5 +11,26 @@ namespace Strawberry
|
||||||
{
|
{
|
||||||
Path = path;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,76 @@
|
||||||
using System;
|
using System;
|
||||||
using SDL2;
|
using SDL2;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace Strawberry
|
namespace Strawberry
|
||||||
{
|
{
|
||||||
public class Font : Asset
|
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)
|
private this(String path)
|
||||||
: base(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,17 +24,12 @@ namespace Strawberry
|
||||||
Load();
|
Load();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ~this()
|
|
||||||
{
|
|
||||||
Unload();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Frame this[int index]
|
public Frame this[int index]
|
||||||
{
|
{
|
||||||
get => frames[index];
|
get => frames[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Unload()
|
override protected void Unload()
|
||||||
{
|
{
|
||||||
for (let f in frames)
|
for (let f in frames)
|
||||||
delete f;
|
delete f;
|
||||||
|
@ -53,21 +48,14 @@ namespace Strawberry
|
||||||
delete slices;
|
delete slices;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reload()
|
override protected void Load()
|
||||||
{
|
|
||||||
Unload();
|
|
||||||
Load();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Load()
|
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
Aseprite file loading based on code from Noel Berry's Foster Framework here:
|
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
|
https://github.com/NoelFB/Foster/blob/master/Framework/Graphics/Images/Aseprite.cs
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let stream = scope FileStream();
|
let stream = OpenFileStream!();
|
||||||
stream.Open(Path, .Read, .Read);
|
|
||||||
|
|
||||||
//Helpers to match ASE file format spec
|
//Helpers to match ASE file format spec
|
||||||
uint8 BYTE() => stream.Read<uint8>();
|
uint8 BYTE() => stream.Read<uint8>();
|
||||||
|
|
|
@ -9,8 +9,16 @@ namespace Strawberry
|
||||||
static public Dictionary<String, Sprite> Sprites { get; private set; }
|
static public Dictionary<String, Sprite> Sprites { get; private set; }
|
||||||
static public Dictionary<String, Font> Fonts { get; private set; }
|
static public Dictionary<String, Font> Fonts { get; private set; }
|
||||||
|
|
||||||
|
static public String ContentRoot { get; private set; }
|
||||||
|
|
||||||
static public void LoadAll()
|
static public void LoadAll()
|
||||||
{
|
{
|
||||||
|
#if DEBUG
|
||||||
|
ContentRoot = "../../../src/Content/";
|
||||||
|
#else
|
||||||
|
ContentRoot = "Content";
|
||||||
|
#endif
|
||||||
|
|
||||||
Sprites = new Dictionary<String, Sprite>();
|
Sprites = new Dictionary<String, Sprite>();
|
||||||
Load<Sprite>("Sprites", "*.ase*", Sprites);
|
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
|
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(Path.DirectorySeparatorChar);
|
||||||
root.Append(directory);
|
root.Append(directory);
|
||||||
if (Directory.Exists(root))
|
if (Directory.Exists(root))
|
||||||
LoadDir<T>(root, wildcard, putInto);
|
LoadDir<T>(root, root, wildcard, putInto);
|
||||||
else
|
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
|
//Recursive folder search
|
||||||
for (let dir in Directory.EnumerateDirectories(directory))
|
for (let dir in Directory.EnumerateDirectories(directory))
|
||||||
{
|
{
|
||||||
let path = scope String();
|
let path = scope String();
|
||||||
dir.GetFilePath(path);
|
dir.GetFilePath(path);
|
||||||
LoadDir<T>(path, wildcard, putInto);
|
LoadDir<T>(rootDir, path, wildcard, putInto);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Load files
|
//Load files
|
||||||
|
@ -51,11 +59,11 @@ namespace Strawberry
|
||||||
{
|
{
|
||||||
let path = scope String();
|
let path = scope String();
|
||||||
file.GetFilePath(path);
|
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('.'));
|
path.RemoveFromEnd(path.Length - path.IndexOf('.'));
|
||||||
putInto.Add(new String(path), sprite);
|
putInto.Add(new String(path), asset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,5 +99,18 @@ namespace Strawberry
|
||||||
SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend);
|
SDL.SetTextureBlendMode(sprite[frame].Texture, .Blend);
|
||||||
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)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user