I’m trying to load a font into an object that is a member of a static class in the C# binding of Raylib. My objective is to use a single static class accessible from across my entire project using a single namespace. I use Newtonsoft’s JSON library to de-serialize my objects. Since I’ve had trouble de-serializing into a static class in a neat and compact way, I’ve resorted into a solution like this:
using Raylib_cs;
using Newtonsoft.Json;
using System.Runtime.Serialization;
namespace Game.Configs
{
public static class Configuration
{
/*Debug config*/
private static DebugConfig? _debug;
public static DebugConfig Debug => _debug!;
/*Graphics config*/
private static GraphicsConfig? _graphics;
public static GraphicsConfig Graphics => _graphics!;
public static void Load(string Path)
{
try
{
_debug = JsonConvert.DeserializeObject<DebugConfig>(File.ReadAllText($"{Path}dbg.json"))!;
_graphics = JsonConvert.DeserializeObject<GraphicsConfig>(File.ReadAllText($"{Path}gfx.json"))!;
}
catch (Exception e)
{
// Log the exception
Console.WriteLine($"Exception during deserialization: {e}");
}
}
}
//these classes are in separate source files, but within the same namespace so I added them here
public class DebugConfig
{
public string? FontPath {get; set;}
[JsonIgnore]
public Font DebugFont {get;set;}
[OnDeserialized]
internal void OnDeserializedMethod(StreamingContext context)
{
if(!string.IsNullOrEmpty(FontPath))
DebugFont = Raylib.LoadFont(FontPath);
}
}
public class GraphicsConfig
{
public string? Title {get; set;}
public V2Di Resolution {get;set;}
public bool Windowed {get;set;}
public byte Framerate {get;set;}
}
}
And I have a “Game” class which is implemented like so:
partial class Game
{
public Game()
{
Configuration.Load("res/cfg/");
}
public void Initialize()
{
Raylib.InitWindow(Configuration.Graphics.Resolution.X,Configuration.Graphics.Resolution.Y,Configuration.Graphics.Title);
if(!Configuration.Graphics.Windowed && !Raylib.IsWindowFullscreen())
Raylib.ToggleFullscreen();
Raylib.SetTargetFPS(Configuration.Graphics.Framerate);
Start();
}
}
The Game constructor is called first thing in my program entry point. Running in debug, the program immediately terminates itself after raylib reporting a successful load of Configuration.Debug.DebugFont
from said object’s de-serialization callback method.
INFO: FILEIO: [res/fnt/_stmr.ttf] File loaded successfully
The program '[24301] rayNet' has exited with code 0 (0x0).
Loading the font into it’s own static variable within the “Game” class works just fine: public static Font ttfTest = Raylib.LoadFont("res/fnt/_stmr.ttf");
And the loading and use of Configuration.Graphics
works as expected. Commenting out the line:
_debug = JsonConvert.DeserializeObject<DebugConfig>(File.ReadAllText($"{Path}dbg.json"))!;
in the Configuration
class prevents the application from terminating on load, and the de-serialized data for Configuration.Graphics
works just fine.
There are no exceptions thrown on program termination, or any warnings when compiling. What causes Configuration.Graphics
to work, but not
Configuration.Debug
?