-
Notifications
You must be signed in to change notification settings - Fork 5
1. Getting Started
Version 3.0 rearchitected the structure of the engine to favor composition over inheritance. As such, it will help to understand the fundamentals of dependency injection. Don't worry, you don't need to be an expert to use the framework.
While this approach introduces more boilerplate to the bootstrapping process, it also allows for more flexibility in how the engine is implemented. In fact, you can even inject your own implementations of dependencies to change the behavior of your game.
SharpDL has helper extensions which make wiring up all of its inner dependencies easy (examples below) but requires the use of the Microsoft.Extensions.DependencyInjection framework. You can use a different dependency injection container, but you'll need to wire up the game engine dependencies on your own.
Calling the Start method on the IGameEngine interface will result in the following:
-
Initializewill initializeSDLwithSDL_INIT_EVERYTHINGflag, initializeSDL_ttf, and initializeSDL_image. -
LoadContentwill perform a no-op. - Until the
Endmethod is called on theIGameinterface, the game loop will callUpdateto update the game state andDrawto draw the game state. Note that the game loop uses a Fixed Time Step approach -
UnloadContentwill dispose theWindow,Renderer,SDL_ttf,SDL_image, andSDL.
-
First create a console application.
dotnet new console -o MyFirstSharpDL -
Create a
MainGameclass which implements theIGameinterface and takes an injected dependency onIGameEngine.public class MainGame : IGame { private readonly IGameEngine engine; public MainGame(IGameEngine engine) { this.engine = engine; } // Must be implemented as part of IGame interface. // Usually just used to start the engine. public void Run() { engine.Start(GameEngineInitializeType.Everything); } }
-
Update
Program.csto create your dependency injection container, add the game engine to it, and run yourMainGameclass. See comments below.class Program { static void Main(string[] args) { // Create dependency injection container with configured services. ServiceProvider serviceProvider = GetServiceProvider(); // Get instance of your implemented game and then run it. var game = serviceProvider.GetService<IGame>(); game.Run(); } // Creates the dependency injection container, registers services with the container, // and returns the built container. private static ServiceProvider GetServiceProvider() { var services = new ServiceCollection(); ConfigureServices(services); var serviceProvider = services.BuildServiceProvider(); return serviceProvider; } private static void ConfigureServices(ServiceCollection services) { // Add the game engine components and your game to the dependency injection container. services.AddSharpGame<MainGame>(); } }
-
Add an
Initializemethod to yourMainGameclass. Update theMainGameconstructor to set the engine'sInitializeaction to your method. This will inject your implementation into the game loop.private IWindow window; private IRenderer renderer; public MainGame(IGameEngine engine) { this.engine = engine; engine.Initialize = () => Initialize(); } // Create a window from the engine's window factory. // Create a renderer using that window from the engine's renderer factory. private void Initialize() { window = engine.WindowFactory.CreateWindow("Example 0 - Sandbox"); renderer = engine.RendererFactory.CreateRenderer(window); }
-
Build and run to see a blank window.
dotnet builddotnet run
Check the Examples folder for runnable projects.