/// <summary> /// Creates a texture from a file containing a bitmap. /// </summary> /// <param name="filename">The file containing the texture data.</param> /// <returns>The created texture.</returns> public static pTexture FromFile(string filename, TextureAtlas atlas = null) { if (!File.Exists(filename)) { return(null); } try { using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { pTexture t = FromStream(stream, atlas); if (t != null) { t.Filename = filename; } return(t); } } catch { return(null); } }
public pTexture(TextureGl textureGl, TextureAtlas containingAtlas = null) { Debug.Assert(textureGl != null); TextureGl = textureGl; ContainingAtlas = containingAtlas; }
public static pTexture FromNewResourceStore(string filename, TextureAtlas atlas = null) { bool highResolution = GameBase.UseHighResolutionSprites; if (highResolution) { filename += @"@2x"; } using (Bitmap b = osu_gameplay.ResourcesStore.ResourceManager.GetObject(filename) as Bitmap) if (b != null) { pTexture tex = FromBitmap(b, atlas); if (highResolution) { tex.DpiScale = 2; } return(tex); } //dll modification checks using (Bitmap b = osu_ui.ResourcesStore.ResourceManager.GetObject(filename) as Bitmap) if (b != null) { bool valid = true; switch (filename) { //case @"menu-osu": // valid &= b.GetPixel(320 / 2, 320 / 2) == System.Drawing.Color.FromArgb(255, 255, 190, 219); // valid &= b.GetPixel(220 / 2, 220 / 2) == System.Drawing.Color.FromArgb(255, 255, 199, 227); // valid &= b.GetPixel(420 / 2, 420 / 2) == System.Drawing.Color.FromArgb(255, 255, 245, 249); // break; //case @"menu-osu@2x": // valid &= b.GetPixel(320, 320) == System.Drawing.Color.FromArgb(255, 255, 190, 220); // valid &= b.GetPixel(220, 220) == System.Drawing.Color.FromArgb(255, 255, 199, 227); // valid &= b.GetPixel(420, 420) == System.Drawing.Color.FromArgb(255, 255, 245, 249); // break; //case @"menu-background": // valid &= b.GetPixel(0, 0) == System.Drawing.Color.FromArgb(243, 0, 206, 255); // valid &= b.GetPixel(400 / 2, 400 / 2) == System.Drawing.Color.FromArgb(255, 0, 165, 255); // valid &= b.GetPixel(1552 / 2, 1024 / 2) == System.Drawing.Color.FromArgb(255, 0, 144, 255); // break; //case @"menu-background@2x": // valid &= b.GetPixel(0, 0) == System.Drawing.Color.FromArgb(228, 1, 206, 255); // valid &= b.GetPixel(400, 400) == System.Drawing.Color.FromArgb(255, 0, 165, 255); // valid &= b.GetPixel(1552, 1024) == System.Drawing.Color.FromArgb(255, 0, 144, 255); // break; } if (!valid) { #if DEBUG throw new Exception(@"DLL modification checks need updating"); #else OsuMain.ExitImmediately(); #endif } pTexture tex = FromBitmap(b, atlas); if (highResolution) { tex.DpiScale = 2; } return(tex); } return(null); }
/// <summary> /// Read a pTexture from the ResourceStore (ouenresources project). /// </summary> public static pTexture FromResourceStore(string filename, TextureAtlas atlas = null) { pTexture tex = null; #if DEBUG tex = fromDebugLocations(filename); if (tex != null) { return(tex); } #endif //load using newer resources where available. if (GameBase.NewGraphicsAvailable) { tex = FromNewResourceStore(filename, atlas); if (tex != null) { tex.Filename = filename; return(tex); } } byte[] bytes = ResourcesStore.ResourceManager.GetObject(filename) as byte[]; if (bytes == null) { return(null); } using (Stream stream = new MemoryStream(bytes)) { using (HaxBinaryReader br = new HaxBinaryReader(stream)) { //XNA pipeline header crap. F**k it all. br.ReadBytes(10); int typeCount = br.Read7BitEncodedInt(); for (int i = 0; i < typeCount; i++) { br.ReadString(); br.ReadInt32(); } br.Read7BitEncodedInt(); br.Read7BitEncodedInt(); //And that's the header dealt with. br.ReadInt32(); int width = br.ReadInt32(); int height = br.ReadInt32(); tex = atlas == null ? new pTexture(width, height) : atlas.Add(width, height); tex.Filename = filename; int numberLevels = br.ReadInt32(); for (int i = 0; i < numberLevels; i++) { int count = br.ReadInt32(); byte[] data = br.ReadBytes(count); bgraToRgba(data, data.Length); tex.SetData(data, i, 0); } } } return(tex); }
internal static pTexture Load(string name, SkinSource source = SkinSource.All, TextureAtlas atlas = null) { if (name == null) { return(null); } if (SkinManager.IgnoreBeatmapSkin && source != SkinSource.Beatmap) { source &= ~SkinSource.Beatmap; } if (source == SkinSource.All) { switch (GameBase.Mode) { case OsuModes.Play: case OsuModes.Edit: break; default: //As a default, don't load from the beatmap unless we are in play or edit mode. source &= ~SkinSource.Beatmap; break; } } pTexture tex = null; Dictionary <string, pTexture> dic = null; if (!LookupCache.TryGetValue(source, out dic)) { dic = LookupCache[source] = new Dictionary <string, pTexture>(); } if (dic.TryGetValue(name, out tex)) { return(tex); } try { //Check for beatmap-specific sprite availability Beatmap current = null; if ((source & SkinSource.Beatmap) > 0 && (current = BeatmapManager.Current) != null) { if (BeatmapSpriteCache.TryGetValue(name, out tex)) { //Cached sprite is available. //Note that we must check null *inside* this if block because a cached null means this source has no available file. //If we find a null, we don't want to enter the else block and check for a file every time, so we quietly ignore this source here. if (tex != null) { return(tex); } } else { string filename = name.IndexOf('.') < 0 ? name + @".png" : name; using (Stream stream = current.GetFileStream(filename)) { if (stream != null) { tex = pTexture.FromStream(stream, atlas); if (tex != null) { tex.Filename = filename; tex.AssetName = name; tex.Source = SkinSource.Beatmap; tex.UploadNextFrame(); } BeatmapSpriteCache[name] = tex; if (atlas != null) { BeatmapTextureAtlases.Add(atlas); } return(tex); } } BeatmapSpriteCache[name] = null; } } if ((source & SkinSource.Skin) > 0 && !SkinManager.IsDefault) { if (SkinSpriteCache.TryGetValue(name, out tex)) { //Cached sprite is available. //Note that we must check null *inside* this if block because a cached null means this source has no available file. //If we find a null, we don't want to enter the else block and check for a file every time, so we quietly ignore this source here. if (tex != null) { return(tex); } } else { string filename = Path.Combine(SkinManager.Current.FullPath, name.IndexOf('.') < 0 ? name + @".png" : name); string filename2x = filename.Insert(filename.LastIndexOf('.'), @"@2x"); if (GameBase.UseHighResolutionSprites && File.Exists(filename2x)) { tex = pTexture.FromFile(filename2x, atlas); SkinSpriteCache[name] = tex; if (atlas != null) { SkinTextureAtlases.Add(atlas); } if (tex != null) { tex.DpiScale = 2; tex.Source = SkinSource.Skin; tex.AssetName = name; tex.UploadNextFrame(); } return(tex); } if (File.Exists(filename)) { tex = pTexture.FromFile(filename, atlas); SkinSpriteCache[name] = tex; if (atlas != null) { SkinTextureAtlases.Add(atlas); } if (tex != null) { tex.Source = SkinSource.Skin; tex.AssetName = name; tex.UploadNextFrame(); } return(tex); } SkinSpriteCache[name] = null; } } if ((source & SkinSource.Osu) == 0) { return(null); } try { if (DefaultSpriteCache.TryGetValue(name, out tex)) { return(tex); } #if LOAD_LOCAL if ((tex = Load(name, SkinSource.Skin)) != null) { return(tex); } #endif name = GeneralHelper.PathSanitise(name); tex = name.IndexOf(Path.DirectorySeparatorChar) > 0 ? pTexture.FromFile(name, atlas) : pTexture.FromResourceStore(GeneralHelper.StripExtension(name), atlas); DefaultSpriteCache[name] = tex; if (atlas != null) { DefaultTextureAtlases.Add(atlas); } if (tex != null) { tex.Source = SkinSource.Osu; tex.AssetName = name; tex.UploadNextFrame(); } return(tex); } catch { return(null); } } finally { dic[name] = tex; } }