/// <summary> /// A8B8G8R8: Uses the R + G + B values of a texture to form a heightmap /// R32F: Reads the value straight off /// </summary> public static R32F[,] ToHeightmap(SlimDX.Direct3D9.Texture texture, float heightMultiplier) { SlimDX.Direct3D9.SurfaceDescription d = texture.GetLevelDescription(0); R32F[,] heightmap = new R32F[d.Height, d.Width]; DataRectangle r = texture.LockRectangle(0, SlimDX.Direct3D9.LockFlags.ReadOnly); for (int y = 0; y < d.Height; y++) { for (int x = 0; x < d.Width; x++) { if (d.Format == SlimDX.Direct3D9.Format.A8R8G8B8) { var col = r.Data.Read <Software.Texel.A8R8G8B8>(); heightmap[y, x] = new R32F((col.R + col.G + col.B + col.A) * heightMultiplier / 256f); } else if (d.Format == SlimDX.Direct3D9.Format.X8R8G8B8) { var col = r.Data.Read <Software.Texel.A8R8G8B8>(); heightmap[y, x] = new R32F((col.R + col.G + col.B + col.A) * heightMultiplier / 256f); } else if (d.Format == SlimDX.Direct3D9.Format.R32F) { heightmap[y, x] = new R32F(r.Data.Read <float>()); } else { throw new ArgumentException("Unsupported texture format: " + d.Format); } } } texture.UnlockRectangle(0); return(heightmap); }
// common internal void ReleaseAll() { // TODO: critical section foreach (Sprite sprite in sprites.Values) { for (int i = 0; i < sprite.internalFrames.Length; i++) { if (sprite.internalFrames[i].texture == null || sprite.internalFrames[i].texture.Disposed) { continue; } try { SlimDX.Direct3D9.SurfaceDescription desc = sprite.internalFrames[i].texture.GetLevelDescription(0); MemoryUsage -= desc.Width * desc.Height * 4; } catch (Exception) { // TODO make cleaner } } sprite.Unload(); } MemoryUsage = 0; foreach (Font font in fonts.Values) { if (font.sprite.internalFrames[0].texture == null) { continue; } try { SlimDX.Direct3D9.SurfaceDescription desc = font.sprite.internalFrames[0].texture.GetLevelDescription(0); MemoryUsage -= desc.Width * desc.Height * 4; } catch { } font.sprite.Unload(); } }
private TextureHandle LoadBlpTexture(SlimDX.Direct3D9.Device Render, System.IO.BinaryReader reader) { reader.BaseStream.Position += 4; byte compression = reader.ReadByte(); byte alphaDepth = reader.ReadByte(); byte alphaEncoding = reader.ReadByte(); byte hasMipMap = reader.ReadByte(); int width = reader.ReadInt32(); int height = reader.ReadInt32(); int[] Offsets = new int[16]; int[] Sizes = new int[16]; byte[] ofsTmp = reader.ReadBytes(16 * 4); byte[] sizTmp = reader.ReadBytes(16 * 4); int levelCount = 0; int blockSize = 0; for (int i = 0; i < 16; ++i) { Offsets[i] = BitConverter.ToInt32(ofsTmp, 4 * i); Sizes[i] = BitConverter.ToInt32(sizTmp, 4 * i); if (Offsets[i] != 0 && Sizes[i] != 0) { ++levelCount; } } SlimDX.Direct3D9.Format texFmt = SlimDX.Direct3D9.Format.Unknown; if (compression == 2) { switch (alphaEncoding) { case 0: texFmt = SlimDX.Direct3D9.Format.Dxt1; blockSize = 2; break; case 1: texFmt = SlimDX.Direct3D9.Format.Dxt3; blockSize = 4; break; case 7: texFmt = SlimDX.Direct3D9.Format.Dxt5; blockSize = 4; break; } } if (compression == 3) { texFmt = SlimDX.Direct3D9.Format.A8R8G8B8; blockSize = 4; } if (texFmt == SlimDX.Direct3D9.Format.Unknown) { throw new FormatException("This format is not yet supported, sorry!"); } var texture = new SlimDX.Direct3D9.Texture(Render, width, height, levelCount, SlimDX.Direct3D9.Usage.None, texFmt, SlimDX.Direct3D9.Pool.Managed); int curLevel = 0; for (int i = 0; i < 16; ++i) { if (Sizes[i] != 0 && Offsets[i] != 0) { reader.BaseStream.Position = Offsets[i]; byte[] layerData = reader.ReadBytes(Sizes[i]); SlimDX.Direct3D9.Surface surf = texture.GetSurfaceLevel(curLevel); SlimDX.Direct3D9.SurfaceDescription desc = texture.GetLevelDescription(curLevel); System.Drawing.Rectangle rec = System.Drawing.Rectangle.FromLTRB(0, 0, desc.Width, desc.Height); SlimDX.Direct3D9.Surface.FromMemory(surf, layerData, SlimDX.Direct3D9.Filter.Triangle, 0, texFmt, blockSize * rec.Width, rec); ++curLevel; } } return(new TextureHandle(texture)); }
internal void Reload(Sprite Sprite, ResourceLoadType LoadType) { if (Sprite.internalFrames != null) { Sprite.internalFrames[0].loading = true; } if (LoadType == ResourceLoadType.Delayed) { delayLoader.Enqueue(Sprite); return; } for (int i = 0; i < Sprite.internalFrames.Length; i++) { if (Sprite.internalFrames[i].HasSystemCopy) { SlimDX.Direct3D9.Texture tex = engine.Device.CreateTexture(MakePowerOfTwo(Sprite.internalFrames[i].size.x), MakePowerOfTwo(Sprite.internalFrames[i].size.y)); SlimDX.Direct3D9.SurfaceDescription desc = tex.GetLevelDescription(0); MemoryUsage += desc.Width * desc.Height * 4; Sprite.internalFrames[i].texture = tex; Sprite.internalFrames[i].RestoreFromSystemCopy(); Sprite.internalFrames[i].loading = false; Sprite.internalFrames[i].loaded = true; } } ResourceID id = Sprite.ID; if (id.File == "" || !spriteProcessors.ContainsKey(id.Format)) { return; } ResourceID realid = (string)id; // check replacements if (replacement != null) { ResourceID newid = GetReplacementID(id); if (newid != null) { if (CheckReplacementID(newid)) { id = newid; } } } engine.IncreaseLoadingCount(); ISpriteProcessor loader = GetSpriteProcessor(id, true); if (loader is ISpriteAnimationProcessor) { ISpriteAnimationProcessor loaderAni = (ISpriteAnimationProcessor)loader; loader.Process(id); Log.Debug("reload \"" + id + "\""); for (int i = 0; i < loaderAni.FrameCount; i++) { if (!loaderAni.SetFrame(i)) { SpriteFrame[] frames = new SpriteFrame[i]; for (int j = 0; j < i; j++) { frames[j] = Sprite.Frames[j]; } Sprite.internalFrames = frames; Sprite.ani.frameCount = i; break; } SlimDX.Direct3D9.Texture tex = engine.Device.CreateTexture(MakePowerOfTwo(loaderAni.FrameSize.x), MakePowerOfTwo(loaderAni.FrameSize.y)); SlimDX.Direct3D9.SurfaceDescription desc = tex.GetLevelDescription(0); MemoryUsage += desc.Width * desc.Height * 4; SlimDX.DataRectangle data = tex.LockRectangle(0, SlimDX.Direct3D9.LockFlags.Discard); loader.Render(data.Data, data.Pitch); tex.UnlockRectangle(0); Sprite.Frames[i].Texture = tex; Sprite.Frames[i].Width = loaderAni.FrameSize.x; Sprite.Frames[i].Height = loaderAni.FrameSize.y; Sprite.Frames[i].TimeStamp = System.Diagnostics.Stopwatch.GetTimestamp(); } } else { loader.Process(id); Log.Debug("reload \"" + id + "\""); SlimDX.Direct3D9.Texture tex = engine.Device.CreateTexture(MakePowerOfTwo(loader.Size.x), MakePowerOfTwo(loader.Size.y)); SlimDX.Direct3D9.SurfaceDescription desc = tex.GetLevelDescription(0); MemoryUsage += desc.Width * desc.Height * 4; SlimDX.DataRectangle data = tex.LockRectangle(0, SlimDX.Direct3D9.LockFlags.Discard); loader.Render(data.Data, data.Pitch); tex.UnlockRectangle(0); Sprite.Frame.Texture = tex; Sprite.Frame.Width = loader.Size.x; Sprite.Frame.Height = loader.Size.y; Sprite.Frame.TimeStamp = System.Diagnostics.Stopwatch.GetTimestamp(); } if (Sprite.internalFrames != null) { Sprite.internalFrames[0].loading = false; Sprite.internalFrames[0].loaded = true; } engine.DecreaseLoadingCount(); }
public Font GetFont(String File, PixelColor ForeColor, PixelColor BackColor) { ResourceInfoFont info; info.Name = File; if (BackColor != PixelColor.Black) { info.Fore = ForeColor; info.Back = BackColor; } else { info.Fore = PixelColor.White; info.Back = PixelColor.Black; } Font font = new Font(engine); font.Info = new FontInfo(); font.Info.Font = File.ToLower(); font.Info.ForeColor = ForeColor; font.Info.BackColor = BackColor; font.Info.UseBackColor = !(BackColor.a == 255 && BackColor.r == 0 && BackColor.g == 0 && BackColor.b == 0); if (fonts.ContainsKey(info)) { font.charInfo = fonts[info].charInfo; font.sprite = fonts[info].sprite; font.offset = fonts[info].offset; font.height = fonts[info].height; return(font); } FilePath path = File; if (!fontProcessors.ContainsKey(path.Extension)) { return(null); } engine.IncreaseLoadingCount(); IFontProcessor processor = (IFontProcessor)Activator.CreateInstance(fontProcessors[path.Extension].GetType()); processor.Process((string)path); Log.Debug("load \"" + path.FullPath + "\""); SlimDX.Direct3D9.Texture tex = engine.Device.CreateTexture(MakePowerOfTwo(processor.Size.x), MakePowerOfTwo(processor.Size.y)); SlimDX.Direct3D9.SurfaceDescription desc = tex.GetLevelDescription(0); MemoryUsage += desc.Width * desc.Height * 4; SlimDX.DataRectangle data = tex.LockRectangle(0, SlimDX.Direct3D9.LockFlags.Discard); System.IO.MemoryStream systemCopy = new System.IO.MemoryStream(); processor.Render(systemCopy, data.Pitch, ForeColor, BackColor); processor.Render(data.Data, data.Pitch, ForeColor, BackColor); tex.UnlockRectangle(0); //// debug output //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(processor.Size.x, processor.Size.y, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //System.Drawing.Imaging.BitmapData bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, processor.Size.x, processor.Size.y), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //byte[] bytes = new byte[processor.Size.y * bmpdata.Stride]; //systemCopy.Seek(0, System.IO.SeekOrigin.Begin); //for (int y = 0; y < processor.Size.y; y++) //{ // systemCopy.Read(bytes, y * bmpdata.Stride, processor.Size.x * 4); //} //System.Runtime.InteropServices.Marshal.Copy(bytes, 0, bmpdata.Scan0, bytes.Length); //bmp.UnlockBits(bmpdata); //bmp.Save("c:\\font.png"); //bmp.Dispose(); SpriteFrame frame = new SpriteFrame(this, tex, processor.Size, systemCopy.ToArray()); font.sprite = new Sprite(this, "", frame); font.sprite.Resolution = processor.Factor; font.charInfo = processor.CharInfo; font.offset = processor.Offset; font.height = processor.Size.y; engine.DecreaseLoadingCount(); fonts.Add(info, font); return(font); }