Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Getting Started

Noel Berry edited this page Feb 27, 2020 · 2 revisions

This guide will show you how to quickly set up a simple Application using Foster

Installation

  1. Clone the Foster repo
  2. Install dotnet core 3.1
  3. Create a new dotnet core console application in an adjacent folder to the Foster repo
  4. Add Foster Framework (Foster/Framework/Foster.Framework.csproj) as a dependency, along with the Platform Modules you intend to use.

By default, a good System and Graphics Module are GLFW and OpenGL (as of writing, these are the only 2 full implementations)

  • Foster/Platforms/GLFW/Foster.GLFW.csrpoj
  • Foster/Platforms/OpenGL/Foster.OpenGL.csproj

Running an Application

Once you've created a new dotnet core project, you can create a simple Application. Foster requires you to first register the Platform Modules you intend to use before launching. A simple Main method looks like this:

static void Main(string[] args)
{
    // our System Module (this is Mandatory)
    App.Modules.Register<GLFW_System>();

    // our Graphics Module (not Mandatory but required for drawing anything)
    App.Modules.Register<GL_Graphics>(); 

    // start the Application with a single 1280x720 Window
    App.Start("My Application", 1280, 720);
}

At this point if you run your project you should see a blank Window.

Adding a custom Module

To run your own code, you need to register a custom Module. A Module gets callbacks for all the main system events, like Startup, Update, Render, and Shutdown.

A simple Module that draws a Rectangle to the screen with the Batch2D class looks like this:

public class CustomModule : Module
{
    public readonly Batch2D Batcher = new Batch2D();
    public float Offset = 0f;

    // This is called when the Application has Started
    protected override void Startup()
    {
        // Add a Callback to the primary window's Render loop
        App.Window.OnRender += Render;
    }

    // This is called when the Application is shutting down, or when the Module is removed
    protected override void Shutdown()
    {
        // Remove our Callback
        App.Window.OnRender -= Render;
    }

    // This is called every frame of the Application
    protected override void Update()
    {
        Offset += 32 * Time.DeltaTime;
    }

    private void Render(Window window)
    {
        // clear the batcher from the previous frame
        Batcher.Clear();

        // draw a rectangle
        Batcher.Rect(Offset, 0, 32, 32, Color.Red);
        
        // clear the Window
        App.Graphics.Clear(window, Color.Black);
        
        // draw the batcher to the Window
        Batcher.Render(window);
    }
}

You then need to add your Module before the Application is started. We can do this by modifying our Main method from earlier:

static void Main(string[] args)
{
    // our System Module (this is Mandatory)
    App.Modules.Register<GLFW_System>();

    // our Graphics Module (not Mandatory but required for drawing anything)
    App.Modules.Register<GL_Graphics>(); 

    // our custom module
    App.Modules.Register<CustomModule>();

    // start the Application with a single 1280x720 Window
    App.Start("My Application", 1280, 720);
}

At this point if you run the Application you should now see a red Rectangle that slowly moves across the screen!

Clone this wiki locally