Skip to content

Getting Started

MrScautHD edited this page Feb 21, 2024 · 2 revisions

Basic project

First, we need to initiate a new C# project utilizing .NET 8. You have the flexibility to choose the code editor of your preference.

Following that, open the NuGet tab and search for "Sparkle" Then, select the "Install" option. Alternatively, as a second approach, you can open your terminal and execute the following command:

dotnet add package Sparkle --version [VERSION]

Creating a Game

You should create a new class that extends the "Game" class. In this example, several methods are available for you to override, and there are even more methods.

public class MyGame : Game {
    
    public MyGame(GameSettings settings) : base(settings) { }

    /// <summary>
    /// Used for Initializes objects.
    /// </summary>
    protected override void Init() {
        base.Init();
    }

    /// <summary>
    /// Used for loading resources.
    /// </summary>
    protected override void Load() {
        base.Load();
    }

    /// <summary>
    /// Is invoked during each tick and is used for updating dynamic elements and game logic.
    /// </summary>
    protected override void Update() {
        base.Update();
    }

    /// <summary>
    /// Called after the Update method on each tick to further update dynamic elements and game logic.
    /// </summary>
    protected override void AfterUpdate() {
        base.AfterUpdate();
    }

    /// <summary>
    /// Is invoked at a fixed rate of every <see cref="GameSettings.FixedTimeStep"/> frames following the <see cref="AfterUpdate"/> method.
    /// It is used for handling physics and other fixed-time operations.
    /// </summary>
    protected override void FixedUpdate() {
        base.FixedUpdate();
    }

    /// <summary>
    /// Is called every tick, used for rendering stuff.
    /// </summary>
    protected override void Draw() {
        base.Draw();
    }

...
}

Creating a Scene

Create a new class that extends the Scene class, and then you can add entities, draw objects, and much more.

public class MyScene : Scene {
    
    public MyScene(string name) : base(name) { }

    protected override void Init() {
        base.Init();
        
        Vector3 pos = new Vector3(10.0f, 10.0f, 10.0f);
        Cam3D cam3D = new Cam3D(pos, Vector3.Zero, Vector3.UnitY, 70, CameraProjection.CAMERA_PERSPECTIVE, CameraMode.CAMERA_ORBITAL);
        this.AddEntity(cam3D);
    }
    
    protected override void Draw() {
        base.Draw();
        
        // BEGIN 3D
        SceneManager.MainCam3D!.BeginMode3D();
        
        //DRAW GIRD
        ModelHelper.DrawGrid(100, 1);
        
        // END 3D
        SceneManager.MainCam3D.EndMode3D();
    }
}

Run the Game

In your Program class (the main class), you should execute this script.

GameSettings settings = new GameSettings(); // Override the options within the struct as desired.

using MyGame game = new MyGame(settings);
game.Run(new MyScene("name"));

grafik