/// <summary> /// Creates a texture from bytes laid out in BGRA format, row major. /// </summary> /// <param name="data">The raw bytes containing the texture in BGRA format, row major.</param> /// <param name="width">Width of the texture in pixels.</param> /// <param name="height">Height of the texture in pixels.</param> /// <returns>The created texture.</returns> public static pTexture FromRawBytes(byte[] data, int width, int height) { pTexture pt = new pTexture(width, height); pt.SetData(data); return(pt); }
/// <summary> /// Creates a texture from a bitmap. /// </summary> /// <param name="bitmap">The bitmap to create the texture from.</param> /// <returns>The created texture.</returns> public static pTexture FromBitmap(Bitmap bitmap, TextureAtlas atlas = null) { if (bitmap == null) { return(null); } int usableWidth = Math.Min(OsuGlControl.MaxTextureSize, bitmap.Width); int usableHeight = Math.Min(OsuGlControl.MaxTextureSize, bitmap.Height); pTexture tex = atlas == null ? new pTexture(usableWidth, usableHeight) : atlas.Add(usableWidth, usableHeight); tex.SetData(bitmap); return(tex); }
/// <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); }
static pTexture fromDebugLocations(string filename) { if (fswUi == null) { if (fswFailed) { return(null); } try { fswUi = new FileSystemWatcher(Path.Combine("..", "..", "..", "..", "osu!ui", "Resources")) { EnableRaisingEvents = true }; fswGameplay = new FileSystemWatcher(Path.Combine("..", "..", "..", "..", "osu!gameplay", "Resources")) { EnableRaisingEvents = true }; } catch { fswFailed = true; } } pTexture tex = null; bool x2 = GameBase.UseHighResolutionSprites; string suffix = GameBase.UseHighResolutionSprites ? @"@2x.png" : @".png"; string directFile = Path.Combine("..", "..", "..", "..", "osu!ui", "Resources", filename + suffix); tex = pTexture.FromFile(directFile); if (tex != null) { fswUi.Renamed += delegate(object sender, RenamedEventArgs e) { if (Path.GetFileName(directFile) != e.Name) { return; } while (true) { try { using (Stream stream = new FileStream(directFile, FileMode.Open, FileAccess.Read, FileShare.Read)) using (Bitmap b = (Bitmap)Image.FromStream(stream, false, false)) { tex.SetData(b); tex.UploadNextFrame(); } break; } catch { } } }; tex.Filename = directFile; tex.DpiScale = x2 ? 2 : 1; return(tex); } directFile = Path.Combine("..", "..", "..", "..", "osu!gameplay", "Resources", filename + suffix); tex = pTexture.FromFile(directFile); if (tex != null) { fswGameplay.Renamed += delegate(object sender, RenamedEventArgs e) { if (Path.GetFileName(directFile) != e.Name) { return; } while (true) { try { using (Stream stream = new FileStream(directFile, FileMode.Open, FileAccess.Read, FileShare.Read)) using (Bitmap b = (Bitmap)Image.FromStream(stream, false, false)) { tex.SetData(b); tex.UploadNextFrame(); } break; } catch { } } }; tex.Filename = directFile; tex.DpiScale = x2 ? 2 : 1; return(tex); } return(tex); }