protected override void Initialize() { base.Initialize(); // Get the standard XNA window. _gameForm = Control.FromHandle(Window.Handle) as Form; #if MONOGAME // Create a "virtual file system" for reading game assets. var titleStorage = new TitleStorage("Content"); var assetsStorage = new ZipStorage(titleStorage, "Content.zip"); var drStorage = new ZipStorage(titleStorage, "DigitalRune.zip"); var vfsStorage = new VfsStorage(); vfsStorage.MountInfos.Add(new VfsMountInfo(titleStorage, null)); vfsStorage.MountInfos.Add(new VfsMountInfo(assetsStorage, null)); vfsStorage.MountInfos.Add(new VfsMountInfo(drStorage, null)); Content = new StorageContentManager(Services, vfsStorage); #else Content.RootDirectory = "Content"; #endif // Create the DigitalRune GraphicsManager. _graphicsManager = new GraphicsManager(GraphicsDevice, Window, Content); // Add graphics service to service provider Services.AddService(typeof(IGraphicsService), _graphicsManager); // Add a few GraphicsScreens that draw stuff. _graphicsManager.Screens.Add(new BackgroundGraphicsScreen(_graphicsManager)); _graphicsManager.Screens.Add(new TriangleGraphicsScreen(_graphicsManager)); _graphicsManager.Screens.Add(new TextGraphicsScreen(_graphicsManager, Content)); }
protected override void Initialize() { // create all application services and add to main services container. _services = new ServiceContainer(); ServiceLocator.SetLocatorProvider(() => _services); var vfsStorage = new VfsStorage(); var pathStorage = new FileSystemStorage("data"); var uiAssetsStorage = new ZipStorage(pathStorage, "UI_Assets.zip"); vfsStorage.MountInfos.Add(new VfsMountInfo(uiAssetsStorage, null)); // Register the virtual file system as a service. _services.Register(typeof(IStorage), null, vfsStorage); // The GraphicsDeviceManager needs to be registered in the service container. // (This is required by the XNA content managers.) _services.Register(typeof(IGraphicsDeviceService), null, _graphicsDeviceManager); _services.Register(typeof(GraphicsDeviceManager), null, _graphicsDeviceManager); var uiContentManager = new StorageContentManager(_services, uiAssetsStorage); _services.Register(typeof(ContentManager), "UIContent", uiContentManager); // ----- Initialize Services // Register the game class. _services.Register(typeof(Microsoft.Xna.Framework.Game), null, this); _services.Register(typeof(kbPCB), null, this); // Input _inputManager = new InputManager(false); _services.Register(typeof(IInputService), null, _inputManager); // Graphics _graphicsManager = new GraphicsManager(GraphicsDevice, Window, uiContentManager); _services.Register(typeof(IGraphicsService), null, _graphicsManager); // GUI _uiManager = new UIManager(this, _inputManager); _services.Register(typeof(IUIService), null, _uiManager); // Animation _animationManager = new AnimationManager(); _services.Register(typeof(IAnimationService), null, _animationManager); // Game logic _gameObjectManager = new GameObjectManager(); _services.Register(typeof(IGameObjectService), null, _gameObjectManager); // Profiler _profiler = new HierarchicalProfiler("Main"); _services.Register(typeof(HierarchicalProfiler), "Main", _profiler); // add more stuff here var editor2D = new Editor2D(this); Components.Add(editor2D); base.Initialize(); }
public MonoGameContent(string rootDirectoryName, string fileName, VfsStorage storage, StorageContentManager contentManager, object asset) { Debug.Assert(!string.IsNullOrEmpty(rootDirectoryName)); Debug.Assert(!string.IsNullOrEmpty(fileName)); Debug.Assert(storage != null); Debug.Assert(contentManager != null); Debug.Assert(asset != null); RootDirectoryName = rootDirectoryName; FileName = fileName; Storage = storage; ContentManager = contentManager; Asset = asset; }
public MonoGameContent LoadXnb(string rootDirectoryName, string fileName, bool cacheResult) { Debug.Assert(rootDirectoryName != null); Debug.Assert(rootDirectoryName.Length > 0); Debug.Assert(fileName != null); Debug.Assert(fileName.Length > 0); // Check whether content has already been loaded and is still cached. if (_cachedContent != null && !_cachedContent.IsDisposed && _cachedContent.RootDirectoryName == rootDirectoryName && _cachedContent.FileName == fileName) { var result = _cachedContent; if (!cacheResult) _cachedContent = null; return result; } // Clear cache. _cachedContent?.Dispose(); _cachedContent = null; // External references in MonoGame are relative to the content root directory, not the // model. Loading the model fails, if a external reference cannot be resolved. // --> Try different content root directories. var rootDirectory = new DirectoryInfo(rootDirectoryName); while (rootDirectory != null) { VfsStorage storage = null; StorageContentManager content = null; try { // Create a virtual file system which contains the DigitalRune effects and the content folder. storage = new VfsStorage(); storage.MountInfos.Add(new VfsMountInfo(new ZipStorage(new TitleStorage("Content"), "DigitalRune.zip"), null)); storage.MountInfos.Add(new VfsMountInfo(new FileSystemStorage(rootDirectory.FullName), null)); content = new StorageContentManager(_services, storage); string assetName = DRPath.GetRelativePath(rootDirectory.FullName, fileName); var asset = content.Load<object>(assetName); var result = new MonoGameContent(rootDirectoryName, fileName, storage, content, asset); if (cacheResult) _cachedContent = result; return result; } catch (Exception exception) { storage?.Dispose(); content?.Dispose(); if (exception is ContentLoadException && exception.InnerException is ContentLoadException && rootDirectory.Parent != null) { // ExternalReference could probably not be resolved. // --> Retry with parent folder as content root. } else { // Asset could not be loaded. throw; } } rootDirectory = rootDirectory.Parent; } // Execution should never reach this point. throw new EditorException("Unexpected error."); }
/// <summary> /// Called when graphics resources need to be loaded. Override this method to load any /// game-specific graphics resources. /// </summary> protected override void LoadContent() { try { // Configure ContentManager to load the SpriteFont. var titleStorage = new TitleStorage("Content"); var vfsStorage = new VfsStorage(); vfsStorage.MountInfos.Add(new VfsMountInfo(titleStorage, null)); #if MONOGAME var assetsStorage = new ZipStorage(titleStorage, "Content.zip"); vfsStorage.MountInfos.Add(new VfsMountInfo(assetsStorage, null)); var drStorage = new ZipStorage(titleStorage, "DigitalRune.zip"); vfsStorage.MountInfos.Add(new VfsMountInfo(drStorage, null)); #endif Content = new StorageContentManager(Services, vfsStorage); // Load SpriteFont. _spriteFont = Content.Load<SpriteFont>("SpriteFont1"); _spriteBatch = new SpriteBatch(GraphicsDevice); } catch { // Failed to load sprite font. } }
// Initializes services and adds game components. protected override void Initialize() { #if WINDOWS || WINDOWS_UWP || XBOX if (GraphicsDevice.GraphicsProfile == GraphicsProfile.Reach) { throw new NotSupportedException( "The DigitalRune Samples and Content for Windows, Universal Windows Apps and Xbox 360 are " + "designed for the HiDef graphics profile. A graphics cards supporting DirectX 10.0 or better " + "is required."); } #endif // ----- Service Container // The DigitalRune ServiceContainer is an "inversion of control" container. // All game services (such as input, graphics, physics, etc.) are registered // in this container. Other game components can access these services via lookup // in the service container. // The DigitalRune ServiceContainer replaces the XNA GameServiceContainer (see // property Game.Services). // Note: The DigitalRune libraries do not require the use of the ServiceContainer // or any other IoC container. The ServiceContainer is only used in the sample // for convenience - but it is not mandatory. _services = new ServiceContainer(); // The service container is either passed directly to the game components // or accessed through the global variable ServiceLocator.Current. // The following call makes the service container publicly available in // ServiceLocator.Current. ServiceLocator.SetLocatorProvider(() => _services); // ----- Storage // For XNA the assets are stored in the following folders: // // <gameLocation>/ // Content/ // DigitalRune/ // ... DigitalRune assets ... // ... other assets ... // // For MonoGame the assets (*.xnb files) are stored in ZIP packages. The // sample assets are stored in "Content/Content.zip" and the DigitalRune // assets are stored in "Content/DigitalRune.zip". // // <gameLocation>/ // Content/ // Content.zip // DigitalRune.zip // // DigitalRune introduces the concept of "storages". Storages can be used // to access files on disk or files stored in packages (e.g. ZIP archives). // These storages can be mapped into a "virtual file system", which makes // it easier to write portable code. (The game logic can read the files // from the virtual file system and does not need to know the specifics // about the platform.) // // The virtual files system should look like this: // // / <-- root of virtual file system // DigitalRune/ // ... DigitalRune assets ... // ... other assets ... // The VfsStorage creates a virtual file system. var vfsStorage = new VfsStorage(); // The TitleStorage reads files from the game's default storage location. // --> Create a TitleStorage that reads files from "<gameLocation>/Content". var titleStorage = new TitleStorage("Content"); #if MONOGAME // A ZipStorage can be used to access files inside a ZIP archive. // --> Mount the sample assets to the root of the virtual file system. var assetsStorage = new ZipStorage(titleStorage, "Content.zip"); vfsStorage.MountInfos.Add(new VfsMountInfo(assetsStorage, null)); #if !ANDROID && !IOS && !LINUX && !MACOS // --> Mount the DigitalRune assets to the root of the virtual file system. var drStorage = new ZipStorage(titleStorage, "DigitalRune.zip"); vfsStorage.MountInfos.Add(new VfsMountInfo(drStorage, null)); #endif #endif // Finally, map the TitleStorage to the root of the virtual file system. // (The TitleStorage is added as the last mount point. The ZIP archives // have priority.) vfsStorage.MountInfos.Add(new VfsMountInfo(titleStorage, null)); // Register the virtual file system as a service. _services.Register(typeof(IStorage), null, vfsStorage); // ----- Content Managers // The GraphicsDeviceManager needs to be registered in the service container. // (This is required by the XNA content managers.) _services.Register(typeof(IGraphicsDeviceService), null, _graphicsDeviceManager); _services.Register(typeof(GraphicsDeviceManager), null, _graphicsDeviceManager); // Register a default, shared content manager. // The new StorageContentManager can be used to read assets from the virtual // file system. (Replaces the content manager stored in Game.Content.) Content = new StorageContentManager(_services, vfsStorage); _services.Register(typeof(ContentManager), null, Content); // Create and register content manager that will be used to load the GUI. #if !MONOGAME var uiContentManager = new ContentManager(_services, "Content"); #else var uiContentManager = new StorageContentManager(_services, assetsStorage); #endif _services.Register(typeof(ContentManager), "UIContent", uiContentManager); // Create content manager that will be used exclusively by the graphics service // to load the pre-built effects and resources of DigitalRune.Graphics. (We // could use Game.Content, but it is recommended to separate the content. This // allows to unload the content of the samples without unloading the other // content.) var graphicsContentManager = new StorageContentManager(_services, vfsStorage); // ----- Initialize Services // Register the game class. _services.Register(typeof(Microsoft.Xna.Framework.Game), null, this); _services.Register(typeof(SampleGame), null, this); #if XBOX // On Xbox, we use the XNA gamer services (e.g. for text input). Components.Add(new GamerServicesComponent(this)); #endif // Input #if XBOX const bool useGamerServices = true; #else const bool useGamerServices = false; #endif _inputManager = new InputManager(useGamerServices); _services.Register(typeof(IInputService), null, _inputManager); // Graphics _graphicsManager = new GraphicsManager(GraphicsDevice, Window, graphicsContentManager); _services.Register(typeof(IGraphicsService), null, _graphicsManager); // GUI _uiManager = new UIManager(this, _inputManager); _services.Register(typeof(IUIService), null, _uiManager); // Animation _animationManager = new AnimationManager(); _services.Register(typeof(IAnimationService), null, _animationManager); // Particle simulation _particleSystemManager = new ParticleSystemManager(); _services.Register(typeof(IParticleSystemService), null, _particleSystemManager); // Physics simulation ResetPhysicsSimulation(); // Game logic _gameObjectManager = new GameObjectManager(); _services.Register(typeof(IGameObjectService), null, _gameObjectManager); // Profiler _profiler = new HierarchicalProfiler("Main"); _services.Register(typeof(HierarchicalProfiler), "Main", _profiler); // Initialize delegates for running tasks in parallel. // (Creating delegates allocates memory, therefore we do this only once and // cache the delegates.) _updateAnimation = () => _animationManager.Update(_deltaTime); _updatePhysics = () => _simulation.Update(_deltaTime); _updateParticles = () => _particleSystemManager.Update(_deltaTime); // SampleFramework // The SampleFramework automatically discovers all samples using reflection, provides // controls for switching samples and starts the initial sample. #if KINECT var initialSample = typeof(Kinect.KinectSkeletonMappingSample); #elif WINDOWS || WINDOWS_UWP var initialSample = typeof(Graphics.DeferredLightingSample); #else var initialSample = typeof(Graphics.BasicEffectSample); #endif _sampleFramework = new SampleFramework(this, initialSample); _services.Register(typeof(SampleFramework), null, _sampleFramework); base.Initialize(); }