/// <summary> /// loads a Texture2D either from xnb or directly from a png/jpg. Note that xnb files should not contain the .xnb file /// extension or be preceded by "Content" in the path. png/jpg files should have the file extension and have an absolute /// path or a path starting with "Content". /// </summary> public Texture2D LoadTexture(string name) { // no file extension. Assumed to be an xnb so let ContentManager load it if (string.IsNullOrEmpty(Path.GetExtension(name))) { return(Load <Texture2D>(name)); } if (LoadedAssets.TryGetValue(name, out var asset)) { if (asset is Texture2D tex) { return(tex); } } var graphicsDeviceService = ServiceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService; using (var stream = TitleContainer.OpenStream(name)) { var texture = Texture2D.FromStream(graphicsDeviceService.GraphicsDevice, stream); texture.Name = name; LoadedAssets[name] = texture; DisposableAssets.Add(texture); return(texture); } }
/// <summary> /// loads a Texture2D either from xnb or directly from a png/jpg. Note that xnb files should not contain the .xnb file /// extension or be preceded by "Content" in the path. png/jpg files should have the file extension and have an absolute /// path or a path starting with "Content". /// </summary> public Texture2D LoadTexture(string name, bool premultiplyAlpha = false) { string formatted = Core.Content.RootDirectory + "/" + name; // no file extension. Assumed to be an xnb so let ContentManager load it if (string.IsNullOrEmpty(Path.GetExtension(name))) { return(Load <Texture2D>(formatted)); } if (LoadedAssets.TryGetValue(formatted, out var asset)) { if (asset is Texture2D tex) { return(tex); } } using (var stream = Path.IsPathRooted(formatted) ? File.OpenRead(formatted) : TitleContainer.OpenStream(formatted)) { var texture = premultiplyAlpha ? TextureUtils.TextureFromStreamPreMultiplied(stream) : Texture2D.FromStream(Core.GraphicsDevice, stream); texture.Name = formatted; LoadedAssets[formatted] = texture; DisposableAssets.Add(texture); return(texture); } }
/// <summary> /// Loads a BitmapFont /// </summary> public BitmapFont LoadBitmapFont(string name) { if (LoadedAssets.TryGetValue(name, out var asset)) { if (asset is BitmapFont bmFont) { return(bmFont); } } var font = BitmapFontLoader.LoadFontFromFile(name); LoadedAssets.Add(name, font); DisposableAssets.Add(font); return(font); }
/// <summary> /// Loads a SpriteAtlas created with the Sprite Atlas Packer tool /// </summary> public SpriteAtlas LoadSpriteAtlas(string name, bool premultiplyAlpha = false) { if (LoadedAssets.TryGetValue(name, out var asset)) { if (asset is SpriteAtlas spriteAtlas) { return(spriteAtlas); } } var atlas = SpriteAtlasLoader.ParseSpriteAtlas(name, premultiplyAlpha); LoadedAssets.Add(name, atlas); DisposableAssets.Add(atlas); return(atlas); }
/// <summary> /// Loads a ParticleDesigner pex file /// </summary> public Particles.ParticleEmitterConfig LoadParticleEmitterConfig(string name) { if (LoadedAssets.TryGetValue(name, out var asset)) { if (asset is Particles.ParticleEmitterConfig config) { return(config); } } var emitterConfig = ParticleEmitterConfigLoader.Load(name); LoadedAssets[name] = emitterConfig; DisposableAssets.Add(emitterConfig); return(emitterConfig); }
/// <summary> /// loads a Tiled map /// </summary> public Tiled.TmxMap LoadTiledMap(string name) { if (LoadedAssets.TryGetValue(name, out var asset)) { if (asset is Tiled.TmxMap map) { return(map); } } var tiledMap = new Tiled.TmxMap(name); LoadedAssets[name] = tiledMap; DisposableAssets.Add(tiledMap); return(tiledMap); }
/// <summary> /// Loads a SpriteAtlas created with the Sprite Atlas Packer tool /// </summary> public SpriteAtlas LoadSpriteAtlas(string name, bool premultiplyAlpha = false) { if (LoadedAssets.TryGetValue(name, out var asset)) { if (asset is SpriteAtlas spriteAtlas) { return(spriteAtlas); } } var atlasData = SpriteAtlasLoader.ParseSpriteAtlasData($"Content/{name}.atlas"); var atlas = atlasData.AsSpriteAtlas(ReadAsset <Texture2D>(name, null)); LoadedAssets.Add(name, atlas); DisposableAssets.Add(atlas); return(atlas); }
/// <summary> /// loads a Tiled map /// </summary> public TmxMap LoadTiledMap(string name) { string formatted = Core.Content.RootDirectory + "/" + name; if (LoadedAssets.TryGetValue(formatted, out var asset)) { if (asset is TmxMap map) { return(map); } } var tiledMap = new TmxMap().LoadTmxMap(formatted); LoadedAssets[formatted] = tiledMap; DisposableAssets.Add(tiledMap); return(tiledMap); }
/// <summary> /// loads a SoundEffect either from xnb or directly from a wav. Note that xnb files should not contain the .xnb file /// extension or be preceded by "Content" in the path. wav files should have the file extension and have an absolute /// path or a path starting with "Content". /// </summary> public SoundEffect LoadSoundEffect(string name) { // no file extension. Assumed to be an xnb so let ContentManager load it if (string.IsNullOrEmpty(Path.GetExtension(name))) { return(Load <SoundEffect>(name)); } if (LoadedAssets.TryGetValue(name, out var asset)) { if (asset is SoundEffect sfx) { return(sfx); } } using (var stream = Path.IsPathRooted(name) ? File.OpenRead(name) : TitleContainer.OpenStream(name)) { var sfx = SoundEffect.FromStream(stream); LoadedAssets[name] = sfx; DisposableAssets.Add(sfx); return(sfx); } }