Foster Framework Documentation

repository·main·Indexed 21 days ago

https://github.com/fosterframework/foster

A framework for game development featuring MSDF font generation via msdf-atlas-gen and shader cross-compilation using SDL_shadercross for SPIR-V, DXIL, and MSL. It provides a core App class for managing the game lifecycle (Startup, Update, Render, Shutdown) and access to essential modules including GraphicsDevice, Input, Time, and FileSystem.

Tokens
1.6K
Snippets
5
Records
9
Agent score
25%

What's inside Foster

  1. Generate MSDF fonts using msdf-atlas-gen

    main

    The framework uses msdf-atlas-gen to generate the default MSDF fonts. To generate a font atlas, you must provide a .ttf font file and specify the origin, image output path, and JSON output path.

    Important Note on Glyph Overlaps: Many fonts contain overlapping or incorrect glyphs that can cause errors in the MSDF output. It is highly recommended to correct glyph overlaps using a tool like Font Forge before processing the font with msdf-atlas-gen to ensure a valid MSDF output.

    ./msdf-atlas-gen -font ./Roboto-Medium.ttf -yorigin top -imageout ./Compiled/Roboto.png -json ./Compiled/Roboto.json
  2. Create a game by inheriting from App

    main

    To use the Foster framework, you must create a class that inherits from the App abstract class. You are required to implement the Startup, Shutdown, Update, and Render methods to define your game's lifecycle. Once implemented, call Run() to start the main game loop.

    Note: You can only have one App instance running at a time.

    using Foster.Framework;
    
    public class MyGame : App
    {
        public MyGame(string name, int width, int height) 
            : base(name, width, height) {}
    
        protected override void Startup()
        {
            // Initialize game resources
        }
    
        protected override void Update()
        {
            // Handle game logic
        }
    
        protected override void Render()
        {
            // Draw game frames
        }
    
        protected override void Shutdown()
        {
            // Clean up resources
        }
    }
    
    // Entry point
    using var game = new MyGame("MyGame", 1280, 720);
    game.Run();
  3. Manage the Application lifecycle with App methods

    main

    The App class provides several methods to control the application state:

    • Run(): Starts the main game loop. This method blocks until the application exits.
    • Exit(): Notifies the application to exit. The application will finish the current frame before shutting down.
    • RunOnMainThread(Action action): Queues an action to be executed on the main thread. If called from the main thread, it executes immediately.
    • Dispose(): Releases all application resources. Should be called after Run() completes.
  4. Access application modules

    main

    The App instance provides access to several core modules required for game development:

    • Time: The timing module for managing frame deltas and fixed steps.
    • Input: The input module for handling keyboard, mouse, and gamepad state.
    • GraphicsDevice: The GPU rendering module.
    • FileSystem: The file system module for I/O operations.
    • Window: The primary application window.
    • Windows: A read-only collection of all open windows in the application.
  5. Configure the App with AppConfig

    main

    For more granular control over application initialization, use the AppConfig record struct when constructing your App instance. This allows you to specify window properties, graphics drivers, and initialization flags.

    var config = new AppConfig
    (
        ApplicationName: "MyGame",
        WindowTitle: "My Awesome Game",
        Width: 1920,
        Height: 1080,
        Fullscreen: false,
        Resizable: true,
        UpdateMode: UpdateMode.FixedStep(60),
        PreferredGraphicsDriver: GraphicsDriver.None,
        Flags: AppFlags.GraphicsDebugging
    );
    
    using var game = new MyGame(config);
    game.Run();
  6. Handle Application events

    main

    You can subscribe to the OnEvent action to respond to application-level lifecycle changes, such as entering or leaving the foreground/background (common on mobile platforms).

    Available AppEvents:

    • EnterBackground: Triggered when the application enters the background.
    • EnterForeground: Triggered when the application enters the foreground.
    myApp.OnEvent += (ev) => {
        if (ev == AppEvents.EnterBackground) {
            // Pause game or save state
        }
        if (ev == AppEvents.EnterForeground) {
            // Resume game
        }
    };
  7. Access the User Directory via UserPath

    main

    The UserPath property returns the platform-specific directory intended for storing application data like settings or save files (e.g., AppData/Roaming on Windows or ~/.local/share on Linux).

    Note: For non-desktop platforms, do not use System.IO directly on UserPath. Instead, use the FileSystem.OpenUserStorage(Action<ContentStorage>) API to ensure proper mounting and data access.

  8. Configure AppFlags

    main

    The AppFlags enum allows you to set specific initialization behaviors for the application:

    • None: Default behavior.
    • GraphicsDebugging: Enables graphics debugging properties and validation.
    • MultiSampledBackBuffer: Enables MultiSampling of the BackBuffer.
    • NoHeaderLog: Suppresses the logging of the Foster header (version, GPU, SDL version, etc.).
    [Flags]
    public enum AppFlags
    {
        None = 0,
        GraphicsDebugging = 1 << 0,
        MultiSampledBackBuffer = 1 << 1,
        NoHeaderLog = 1 << 2,
    }