Step 4: Graphics service |
Let's create an instance of the DigitalRune GraphicsManager and register it in the service container. In the Game1.Draw method, we remove the GraphicsDevice.Clear call, update the graphics service and let it do the rendering.
… using DigitalRune.Graphics; // NEW … namespace MyGame { … public class Game1 : Microsoft.Xna.Framework.Game { … private GraphicsManager _graphicsManager; // NEW … protected override void Initialize() { _services = new ServiceContainer(); ServiceLocator.SetLocatorProvider(() => _services); _services.Register(typeof(IGraphicsDeviceService), null, _graphicsDeviceManager); // NEW _services.Register(typeof(GraphicsDeviceManager), null, _graphicsDeviceManager); // NEW Content = new ContentManager(_services, "Content"); // NEW _inputManager = new InputManager(false); _services.Register(typeof(IInputService), null, _inputManager); _graphicsManager = new GraphicsManager(GraphicsDevice, Window, Content); // NEW _services.Register(typeof(IGraphicsService), null, _graphicsManager); // NEW Components.Add(new MyGameComponent(this)); base.Initialize(); } … protected override void Draw(GameTime gameTime) { //GraphicsDevice.Clear(Color.CornflowerBlue); // REMOVE _graphicsManager.Update(gameTime.ElapsedGameTime); // NEW _graphicsManager.Render(false); // NEW base.Draw(gameTime); } } }
As you can see, we also add the XNA graphics device manager to the service container. Please note that IGraphicsDeviceService and GraphicsDeviceManager are XNA types. IGraphicsService and GraphicsManager are DigitalRune types.
We also create a new default content manager for the game:
Content = new ContentManager(_services, "Content");
This content manager is like the default content manager that is automatically created by XNA, except that this one uses our own service container.
Run the game and you should see this:
If you see this purple color, then you know that nothing in the XNA game is actually drawing anything into the back buffer.
Let's add a graphics screen to the graphics service.
Let's add a new item MyGraphicsScreen.cs to the game project.
using System; using DigitalRune.Graphics; using Microsoft.Xna.Framework; namespace MyGame { public class MyGraphicsScreen : GraphicsScreen { public MyGraphicsScreen(IGraphicsService graphicsService) : base(graphicsService) { } protected override void OnUpdate(TimeSpan deltaTime) { } protected override void OnRender(RenderContext context) { var graphicsDevice = GraphicsService.GraphicsDevice; graphicsDevice.Clear(Color.CornflowerBlue); } } }
In MyGameComponent.cs create an instance of this graphics screen and add it to the graphics service.
using DigitalRune.Game.Input; using DigitalRune.Graphics; // NEW using Microsoft.Practices.ServiceLocation; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; namespace MyGame { public class MyGameComponent : Microsoft.Xna.Framework.GameComponent { private IInputService _inputService; private IGraphicsService _graphicsService; // NEW private MyGraphicsScreen _myGraphicsScreen; // NEW public MyGameComponent(Game game) : base(game) { _inputService = ServiceLocator.Current.GetInstance<IInputService>(); _graphicsService = ServiceLocator.Current.GetInstance<IGraphicsService>(); // NEW _myGraphicsScreen = new MyGraphicsScreen(_graphicsService); // NEW _graphicsService.Screens.Add(_myGraphicsScreen); // NEW } public override void Update(GameTime gameTime) { if (_inputService.IsDown(Keys.Escape)) Game.Exit(); base.Update(gameTime); } } }
The rendering is now handled by the DigitalRune graphics service. The graphics screen defines the render operations. Currently it only clears the screen. That means, the game will look like this again:
Let's add some graphics. Before adding models and complex stuff, it is good to start with some simple debug graphics.