-
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:
-
Initialize
will initializeSDL
withSDL_INIT_EVERYTHING
flag, initializeSDL_ttf
, and initializeSDL_image
. -
LoadContent
will perform a no-op. - Until the
End
method is called on theIGame
interface, the game loop will callUpdate
to update the game state andDraw
to draw the game state. Note that the game loop uses a Fixed Time Step approach -
UnloadContent
will dispose theWindow
,Renderer
,SDL_ttf
,SDL_image
, andSDL
.
-
First create a console application.
dotnet new console -o MyFirstSharpDL
-
Create a
MainGame
class which implements theIGame
interface 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.cs
to create your dependency injection container, add the game engine to it, and run yourMainGame
class. 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
Initialize
method to yourMainGame
class. Update theMainGame
constructor to set the engine'sInitialize
action 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 build
dotnet run
Check the Examples folder for runnable projects.