SpriteFile CreateSpriteFile(String filepath) { if (filepath == null) { throw new ArgumentNullException("filepath"); } IO.File file = GetSubSystem <IO.FileSystem>().OpenFile(filepath); IO.FileHeaders.SpriteFileHeader header = new IO.FileHeaders.SpriteFileHeader(file); List <SpriteFileData> datalist = new List <SpriteFileData>(header.NumberOfImages); Int32 subheader_offset = header.SubheaderOffset; for (file.SeekFromBeginning(subheader_offset); file.ReadPosition != file.FileLength; file.SeekFromBeginning(subheader_offset)) { IO.FileHeaders.SpriteFileSubHeader subheader = new IO.FileHeaders.SpriteFileSubHeader(file); SpriteFileData data = new SpriteFileData((Int32)file.ReadPosition + 13, subheader.ImageSize, subheader.Axis, subheader.Id, subheader.SharedIndex, subheader.CopyLastPalette); datalist.Add(data); subheader_offset = subheader.NextOffset; } return(new SpriteFile(this, file, header.Version, datalist, header.SharedPalette)); }
/// <summary> /// Loads sounds contained in a SND file. /// </summary> /// <param name="filepath">The filepath of the SND file to load sounds from.</param> /// <returns>A cached ReadOnlyDictionary mapping SoundIds to their respective SecondaryBuffers.</returns> private ReadOnlyDictionary <SoundId, byte[]> GetSounds(string filepath) { if (filepath == null) { throw new ArgumentNullException(nameof(filepath)); } var sounds = new Dictionary <SoundId, byte[]>(); using (IO.File file = GetSubSystem <IO.FileSystem>().OpenFile(filepath)) { var header = new IO.FileHeaders.SoundFileHeader(file); file.SeekFromBeginning(header.SubheaderOffset); for (var i = 0; i != header.NumberOfSounds; ++i) { var subheader = new IO.FileHeaders.SoundFileSubHeader(file); if (sounds.ContainsKey(subheader.Id) == true) { Log.Write(LogLevel.Warning, LogSystem.SoundSystem, "Duplicate sound with ID {0} discarded.", subheader.Id); } else { try { sounds.Add(subheader.Id, CreateSoundBuffer(file, subheader.SoundLength)); } catch { Log.Write(LogLevel.Warning, LogSystem.SoundSystem, "Error reading sound with ID {0}.", subheader.Id); } } file.SeekFromBeginning(subheader.NextOffset); } } ReadOnlyDictionary <SoundId, byte[]> savedsounds = new ReadOnlyDictionary <SoundId, byte[]>(sounds); m_soundcache.Add(filepath, savedsounds); return(savedsounds); }
public Font LoadFont(String filepath) { if (filepath == null) { throw new ArgumentNullException("filepath"); } if (m_fontcache.Contains(filepath) == true) { return(m_fontcache[filepath]); } Point size = new Point(0, 0); Texture2D pixels = null; Texture2D palette = null; IO.TextFile textfile = null; using (IO.File file = GetSubSystem <IO.FileSystem>().OpenFile(filepath)) { IO.FileHeaders.FontFileHeader header = new IO.FileHeaders.FontFileHeader(file); file.SeekFromBeginning(header.ImageOffset); LoadImage(file, header.ImageSize, out size, out pixels, out palette); file.SeekFromBeginning(header.ImageOffset + header.ImageSize); textfile = GetSubSystem <IO.FileSystem>().BuildTextFile(file); } Sprite sprite = new Sprite(size, new Point(0, 0), true, pixels, true, palette, false); IO.TextSection data = textfile.GetSection("Def"); IO.TextSection textmap = textfile.GetSection("Map"); Int32 colors = data.GetAttribute <Int32>("colors"); Point defaultcharsize = data.GetAttribute <Point>("size"); Int32 numchars = 0; Dictionary <Char, Rectangle> sizemap = new Dictionary <Char, Rectangle>(); foreach (String line in textmap.Lines) { Match m = m_fontlinemapregex.Match(line); if (m.Success == false) { continue; } Char c = GetChar(m.Groups[1].Value); Point offset = (m.Groups[2].Value == "") ? new Point(defaultcharsize.X * numchars, 0) : new Point(Int32.Parse(m.Groups[2].Value), 0); Point charsize = (m.Groups[3].Value == "") ? defaultcharsize : new Point(Int32.Parse(m.Groups[3].Value), sprite.Size.Y); if (sizemap.ContainsKey(c) == false) { Rectangle r = new Rectangle(offset.X, offset.Y, charsize.X, charsize.Y); sizemap.Add(c, r); } ++numchars; } Font font = new Font(this, filepath, sprite, new ReadOnlyDictionary <Char, Rectangle>(sizemap), defaultcharsize, colors); m_fontcache.Add(font); return(font); }