Example #1
0
        public unsafe Texture CreateTexture(int index)
        {
            if (!_fileIndex.FilesExist)
                return null;

            int length, extra;
            bool patched;

            Stream stream = _fileIndex.Seek(index, out length, out extra, out patched);

            if (stream == null)
                return null;

            int size = extra == 0 ? 64 : 128;

            Texture texture = new Texture(_device, size, size, 0, Usage.None, Format.A1R5G5B5, Pool.Managed);
            DataRectangle rect = texture.LockRectangle(0, LockFlags.None);
            BinaryReader bin = new BinaryReader(stream);

            ushort* line = (ushort*)rect.DataPointer;
            int delta = rect.Pitch >> 1;

            for (int y = 0; y < size; ++y, line += delta)
            {
                ushort* cur = line;
                ushort* end = cur + size;

                while (cur < end)
                    *cur++ = (ushort)(bin.ReadUInt16() ^ 0x8000);
            }

            texture.UnlockRectangle(0);

            return texture;
        }
Example #2
0
File: Art.cs Project: nydehi/openuo
        public unsafe Texture CreateTexture(int index)
        {
            index &= 0x3FFF;
            index += 0x4000;

            int length, extra;
            bool patched;

            Stream stream = _fileIndex.Seek(index, out length, out extra, out patched);

            if (stream == null)
                return null;

            BinaryReader bin = new BinaryReader(stream);

            bin.ReadInt32(); // Unknown
            int width = bin.ReadInt16();
            int height = bin.ReadInt16();

            if (width <= 0 || height <= 0)
                return null;

            int[] lookups = new int[height];

            int start = (int)bin.BaseStream.Position + (height * 2);

            for (int i = 0; i < height; ++i)
                lookups[i] = (int)(start + (bin.ReadUInt16() * 2));

            Texture texture = new Texture(_device, width, height, 0, Usage.None, Format.A1R5G5B5, Pool.Managed);
            DataRectangle rect = texture.LockRectangle(0, LockFlags.None);

            ushort* line = (ushort*)rect.DataPointer;
            int delta = rect.Pitch >> 1;

            for (int y = 0; y < height; ++y, line += delta)
            {
                bin.BaseStream.Seek(lookups[y], SeekOrigin.Begin);

                ushort* cur = line;
                ushort* end;

                int xOffset, xRun;

                while (((xOffset = bin.ReadUInt16()) + (xRun = bin.ReadUInt16())) != 0)
                {
                    cur += xOffset;
                    end = cur + xRun;

                    while (cur < end)
                        *cur++ = (ushort)(bin.ReadUInt16() ^ 0x8000);
                }
            }

            texture.UnlockRectangle(0);

            return texture;
        }
Example #3
0
        public Hues(Engine engine)
        {
            IConfigurationService configurationService = engine.Kernel.Get<IConfigurationService>();

            string ultimaOnlineDirectory = configurationService.GetValue<string>(ConfigSections.UltimaOnline, ConfigKeys.UltimaOnlineDirectory);

            if (!Directory.Exists(ultimaOnlineDirectory))
            {
                _filesExist = false;
                return;
            }

            string path = Path.Combine(ultimaOnlineDirectory, "hues.mul");

            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                BinaryReader bin = new BinaryReader(fs);

                int blockCount = (int)fs.Length / 708;

                if (blockCount > 375)
                    blockCount = 375;

                _hues = new Texture[blockCount];

                for (int i = 0; i < blockCount; ++i)
                {
                    bin.ReadInt32();

                    Texture texture = new Texture(engine.Device, 34, 1, 0, Usage.None, Format.A1R5G5B5, Pool.Managed); ;
                    IntPtr dataPtr = texture.LockRectangle(0, LockFlags.None).DataPointer;

                    unsafe
                    {
                        ushort* line = (ushort*)dataPtr;

                        for (int j = 0; j < 34; j++)
                            (*line++) = (ushort)(bin.ReadUInt16() | 0x8000);

                        texture.UnlockRectangle(0);
                        bin.ReadBytes(20); //Don't need to know the names

                        _hues[i] = texture;
                    }
                }
            }
        }
Example #4
0
        void InitTextures(bool redraw = true)
        {
            if (CurrentRenderInfo == null)
            {
                return;
            }

            //File.AppendAllText("DEBUG.txt", DateTime.Now.ToLongTimeString() + this.GetHashCode() + " InitTextures" + Environment.NewLine);
            oldTexA = texA;
            texA    = new SharpDX.Direct3D9.Texture(
                device9.Device,
                CurrentRenderInfo.Width,
                CurrentRenderInfo.Height,
                0,
                SharpDX.Direct3D9.Usage.Dynamic,
                SharpDX.Direct3D9.Format.A8R8G8B8,
                Pool.SystemMemory);
            var data = texA.LockRectangle(0, LockFlags.None);

            //File.AppendAllText("DEBUG.txt", DateTime.Now.ToLongTimeString() + this.GetHashCode() + " " + CurrentRenderInfo.Width + "x" + CurrentRenderInfo.Height + Environment.NewLine);

            if (CurrentRenderInfo.Buffer != IntPtr.Zero && redraw)
            {
                CopyMemory(data.DataPointer, CurrentRenderInfo.Buffer, (uint)CurrentRenderInfo.NumberOfBytes);
            }
            else
            {
                CopyMemory(data.DataPointer, CurrentRenderInfo.Buffer, (uint)CurrentRenderInfo.NumberOfBytes);
            }

            texA.UnlockRectangle(0);

            oldTex = tex;
            tex    = new SharpDX.Direct3D9.Texture(
                device9.Device,
                CurrentRenderInfo.Width,
                CurrentRenderInfo.Height,
                0,
                SharpDX.Direct3D9.Usage.RenderTarget,
                SharpDX.Direct3D9.Format.A8R8G8B8,
                Pool.Default);
            texHeight = CurrentRenderInfo.Height;
            texWidth  = CurrentRenderInfo.Width;

            Debug.WriteLine("InitTextures end called");
            IsDirectXInitialized = true;
        }
Example #5
0
        //loads the data from a stream in to a texture object.
        private static void InternalDDSFromStream(Stream stream, Device device, int streamOffset, bool loadMipMap, int offsetMipMaps, out SharpDX.Direct3D9.Texture texture)
        {
            stream.Position = 0;
            if (offsetMipMaps == 0)
            {
                texture = SharpDX.Direct3D9.Texture.FromStream(device, stream, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0);
            }
            else
            {
                texture = SharpDX.Direct3D9.Texture.FromStream(device, stream, 0, 0, 0, Usage.Dynamic, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0);

                int width        = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Width);
                int height       = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Height);
                int maxLevels    = Math.Min(MaxMipMapLevels(width), MaxMipMapLevels(height));
                int actualLevels = Math.Min(maxLevels, texture.LevelCount - offsetMipMaps);

                Format  format          = texture.GetLevelDescription(0).Format;
                Texture offsetedTexture = new Texture(device, width, height, actualLevels, Usage.Dynamic, format, Pool.Default);
                for (int i = offsetMipMaps, j = 0; j < actualLevels; i++, j++)
                {
                    int levelWidth  = MipMapSize(j, width);
                    int levelHeight = MipMapSize(j, height);

                    SharpDX.DataStream ds;
                    texture.LockRectangle(i, LockFlags.ReadOnly, out ds);
                    texture.UnlockRectangle(i);

                    SharpDX.DataStream ds2;
                    offsetedTexture.LockRectangle(j, LockFlags.None, out ds2);
                    ds2.Position = 0;
                    ds2.Write(ds.DataPointer, 0, (int)MipMapSizeInBytes(levelWidth, levelHeight, format));
                    offsetedTexture.UnlockRectangle(j);
                }

                texture.Dispose();
                texture = offsetedTexture;
            }
        }
Example #6
0
        //loads the data from a stream in to a texture object.
        private static void InternalDDSFromStream(Stream stream, Device device, int streamOffset, bool loadMipMap, int offsetMipMaps, out SharpDX.Direct3D9.Texture texture)
        {
            stream.Position = 0;
            if (offsetMipMaps == 0)
            {
                texture = SharpDX.Direct3D9.Texture.FromStream(device, stream, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0);
            }
            else
            {
                texture = SharpDX.Direct3D9.Texture.FromStream(device, stream, 0, 0, 0, Usage.Dynamic, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0);

                int width = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Width);
                int height = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Height);
                int maxLevels = Math.Min(MaxMipMapLevels(width), MaxMipMapLevels(height));
                int actualLevels = Math.Min(maxLevels, texture.LevelCount - offsetMipMaps);

                Format format = texture.GetLevelDescription(0).Format;
                Texture offsetedTexture = new Texture(device, width, height, actualLevels, Usage.Dynamic, format, Pool.Default);
                for (int i = offsetMipMaps, j = 0; j < actualLevels; i++, j++)
                {
                    int levelWidth = MipMapSize(j, width);
                    int levelHeight = MipMapSize(j, height);

                    SharpDX.DataStream ds;
                    texture.LockRectangle(i, LockFlags.ReadOnly, out ds);
                    texture.UnlockRectangle(i);

                    SharpDX.DataStream ds2;
                    offsetedTexture.LockRectangle(j, LockFlags.None, out ds2);
                    ds2.Position = 0;
                    ds2.Write(ds.DataPointer, 0, (int)MipMapSizeInBytes(levelWidth, levelHeight, format));
                    offsetedTexture.UnlockRectangle(j);
                }

                texture.Dispose();
                texture = offsetedTexture;
            }
        }
        private void GeneratePermTexture2d()
        {           
            if (permTexture2d != null)
                permTexture2d.Dispose();
            permTexture2d = new Texture(MyRender.GraphicsDevice, 256, 256, 0, Usage.Dynamic, Format.A8R8G8B8, Pool.Default);
            Color[] data = new Color[256 * 256];
            for (int x = 0; x < 256; x++)
            {
                for (int y = 0; y < 256; y++)
                {
                    int A = perm2d(x) + y;
                    int AA = perm2d(A);
                    int AB = perm2d(A + 1);
                    int B = perm2d(x + 1) + y;
                    int BA = perm2d(B);
                    int BB = perm2d(B + 1);
                    //data[x + (y * 256)] = new Color((byte)(AA / 255.0), (byte)(AB / 255.0),
                    //                                (byte)(BA / 255.0), (byte)(BB / 255.0));
                    
                    //XNA Color = r g b a

                    //need a r g b
                    data[x + (y * 256)] = new Color((byte)(BA), (byte)(AB), (byte)(AA), (byte)(BB));
                }
            }

            SharpDX.DataStream ds;
            DataRectangle dr = permTexture2d.LockRectangle(0, LockFlags.None, out ds);
            ds.WriteRange(data);
            permTexture2d.UnlockRectangle(0);

        }
Example #8
0
        public unsafe Texture CreateTexture(int index)
        {
            if (!_fileIndex.FilesExist)
                return null;

            int length, extra;
            bool patched;

            Stream stream = _fileIndex.Seek(index, out length, out extra, out patched);

            if (stream == null)
                return null;

            int width = (extra >> 16) & 0xFFFF;
            int height = extra & 0xFFFF;

            if (width == 0 || height == 0)
                return null;

            Texture texture = new Texture(_device, width, height, 0, Usage.None, Format.A1R5G5B5, Pool.Managed);
            DataRectangle rect = texture.LockRectangle(0, LockFlags.None);
            BinaryReader bin = new BinaryReader(stream);

            int[] lookups = new int[height];
            int start = (int)bin.BaseStream.Position;

            for (int i = 0; i < height; ++i)
                lookups[i] = start + (bin.ReadInt32() * 4);

            ushort* line = (ushort*)rect.DataPointer;
            int delta = rect.Pitch >> 1;

            for (int y = 0; y < height; ++y, line += delta)
            {
                bin.BaseStream.Seek(lookups[y], SeekOrigin.Begin);

                ushort* cur = line;
                ushort* end = line + width;

                while (cur < end)
                {
                    ushort color = bin.ReadUInt16();
                    ushort* next = cur + bin.ReadUInt16();

                    if (color == 0)
                    {
                        cur = next;
                    }
                    else
                    {
                        color ^= 0x8000;

                        while (cur < next)
                            *cur++ = color;
                    }
                }
            }

            texture.UnlockRectangle(0);

            return texture;
        }
Example #9
0
        private static void InternalDDSFromStream(Stream stream, Device device, int streamOffset, bool loadMipMap, int offsetMipMaps, out SharpDX.Direct3D9.Texture texture)
        {
            var fileStream = stream as FileStream;
            byte[] data = fileStream != null ? null : SharpDX.Utilities.ReadStream(stream);

            int maxTextureDimension = MyRender.GraphicsDevice.Capabilities.MaxTextureWidth;
            if (IntPtr.Size == 4 || MyRender.GraphicsDevice.AvailableTextureMemory < 1024 * 1024 * 600)
            {
                maxTextureDimension = 2048;
            }

            ImageInformation imageInfo;
            if (fileStream != null)
            {
                //imageInfo = GetImageInfoFromFileW(fileStream.Name);
				imageInfo = (ImageInformation)GetImageInfoFromFileWMethodInfo.Invoke(null, new object[] { fileStream.Name });
            }
            else
            {
                imageInfo = ImageInformation.FromMemory(data);
            }
            var textureWidth = imageInfo.Width;
            var textureHeight = imageInfo.Height;

            textureWidth = Math.Max(1, textureWidth >> offsetMipMaps);
            textureHeight = Math.Max(1, textureHeight >> offsetMipMaps);
            int loadMipmapOffset = offsetMipMaps;
            while (textureWidth > maxTextureDimension || textureHeight > maxTextureDimension)
            {
                loadMipmapOffset++;
                textureWidth = Math.Max(1, textureWidth >> 1);
                textureHeight = Math.Max(1, textureHeight >> 1);
            }

            if (offsetMipMaps == 0 && loadMipmapOffset == 0)
            {
                if (fileStream != null)
                {
                    texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.Linear, 0);
                }
                else
                {
                    texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.Linear, 0);
                }
            }
            else if (loadMipmapOffset > 0)
            {
                var skipFilter = (Filter)D3DXSkipDDSMipLevels((uint)loadMipmapOffset);

                if (fileStream != null)
                {
                    texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, skipFilter, 0);
                }
                else
                {
                    texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, skipFilter, 0);
                }
            }
            else
            {
                if (fileStream != null)
                {
                    texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0);
                }
                else
                {
                    texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0);
                }

                int width = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Width);
                int height = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Height);
                int maxLevels = Math.Min(MaxMipMapLevels(width), MaxMipMapLevels(height));
                int actualLevels = Math.Min(maxLevels, texture.LevelCount - offsetMipMaps);

                Format format = texture.GetLevelDescription(0).Format;
                Texture offsetedTexture = new Texture(device, width, height, actualLevels, Usage.Dynamic, format, Pool.Default);
                for (int i = offsetMipMaps, j = 0; j < actualLevels; i++, j++)
                {
                    int levelWidth = MipMapSize(j, width);
                    int levelHeight = MipMapSize(j, height);

                    SharpDX.DataStream ds;
                    texture.LockRectangle(i, LockFlags.ReadOnly, out ds);
                    texture.UnlockRectangle(i);

                    SharpDX.DataStream ds2;
                    offsetedTexture.LockRectangle(j, LockFlags.None, out ds2);
                    ds2.Position = 0;
                    ds2.Write(ds.DataPointer, 0, (int)MipMapSizeInBytes(levelWidth, levelHeight, format));
                    offsetedTexture.UnlockRectangle(j);
                }

                texture.Dispose();
                texture = offsetedTexture;
            }
        }
Example #10
0
        private void RenderData(RenderInfo renderInfo, bool forceChangeSource)
        {
            //File.AppendAllText("DEBUG.txt", DateTime.Now.ToLongTimeString() + this.GetHashCode() + " RenderData" + Environment.NewLine);

            var sec = DateTime.Now.Second;

            if (Framerate_LastSecond != sec)
            {
                Framerate_FramerateValue    = Framerate_FrameCountByDelta;
                Framerate_LastSecond        = sec;
                Framerate_FrameCountByDelta = 1;
            }
            else
            {
                Framerate_FrameCountByDelta++;
            }

            var data = texA.LockRectangle(0, LockFlags.None);

            if (!renderInfo.IsPopup)
            {
                //File.AppendAllText("DEBUG.txt", DateTime.Now.ToLongTimeString() + this.GetHashCode() + " !popup" + Environment.NewLine);
                //Case of popup
                if (popupMemoryMappedFile != null && PopupVisibility == true)
                {
                    CopyMemoryGentle(memoryMappedViewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), data.DataPointer, CurrentRenderInfo.NumberOfBytes);
                    CopyMemoryGentle(popupMemoryMappedViewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), data.DataPointer, CurrentPopupRenderInfo, renderInfo);
                    Debug.WriteLine("Popup case over redeaw");
                }
                ///After closing popup
                else if (popupMemoryMappedFile != null && PopupVisibility == false)
                {
                    CopyMemoryGentle(renderInfo.Buffer, data.DataPointer, renderInfo.NumberOfBytes);
                    ReleaseMemoryMappedView(ref popupMemoryMappedFile, ref popupMemoryMappedViewAccessor);
                    Debug.WriteLine("After closing popup " + renderInfo.NumberOfBytes);
                }
                else
                {
                    //File.AppendAllText("DEBUG.txt", DateTime.Now.ToLongTimeString() + this.GetHashCode() + "Global case posX:" + renderInfo.DirtyRect.X + " posY:" + renderInfo.DirtyRect.Y + " w:" + renderInfo.DirtyRect.Width + " h:" + renderInfo.DirtyRect.Height + Environment.NewLine);
                    CopyMemoryGentle(renderInfo.Buffer, data.DataPointer, renderInfo.DirtyRect, renderInfo);
                    Debug.WriteLine("Global case posX:" + renderInfo.DirtyRect.X + " posY:" + renderInfo.DirtyRect.Y + " w:" + renderInfo.DirtyRect.Width + " h:" + renderInfo.DirtyRect.Height);
                }
            }
            else
            {
                //File.AppendAllText("DEBUG.txt", DateTime.Now.ToLongTimeString() + this.GetHashCode() + " popup" + Environment.NewLine);
                CopyMemoryGentle(popupMemoryMappedViewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), data.DataPointer, CurrentPopupRenderInfo, CurrentRenderInfo);
                Debug.WriteLine("Popup case");
            }

            texA.UnlockRectangle(0);


            device9.Device.UpdateTexture(texA, tex);

            CurrentRenderInfo.Image.Dispatcher.BeginInvoke((Action)(() =>
            {
                //File.AppendAllText("DEBUG.txt", DateTime.Now.ToLongTimeString() + this.GetHashCode() + " InvalidateImageSource" + Environment.NewLine);

                InvalidateImageSource(forceChangeSource);
            }),
                                                           DispatcherPriority.Render);
        }
        static Texture CreateRandomTexture()
        {
            Random r = new Random();
            int size = 256;
            float[] rnd = new float[size];
            for (int i = 0; i < size; i++)
            {
                rnd[i] = MyUtils.GetRandomFloat(-1, 1);
            }

            var result = new Texture(GraphicsDevice, size, 1, 0, Usage.None, Format.R32F, Pool.Managed);
            DataStream ds;
            result.LockRectangle(0, LockFlags.None, out ds);
            ds.WriteRange(rnd);
            result.UnlockRectangle(0);

            return result;
        }
        private void GenerateGradTexture4d()
        {
            if (gradTexture4d != null)
                gradTexture4d.Dispose();

            //NormalizedByte4
            gradTexture4d = new Texture(MyRender.GraphicsDevice, 32, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default);
            NormalizedByte4[] data = new NormalizedByte4[32 * 1];
            for (int x = 0; x < 32; x++)
            {
                for (int y = 0; y < 1; y++)
                {
                    data[x + (y * 32)] = new NormalizedByte4(g4[x, 0], g4[x, 1], g4[x, 2], g4[x, 3]);
                    //data[31 - x + (y * 32)] = new NormalizedByte4(0, 0, 0, 0);
                }
            }

            SharpDX.DataStream ds;
            DataRectangle dr = gradTexture4d.LockRectangle(0, LockFlags.None, out ds);
            ds.WriteRange(data);
            gradTexture4d.UnlockRectangle(0);
                       
        }
        private void GeneratePermGrad4dTexture()
        {                                                                                     //NormalizedByte4
            permGrad4dTexture = new Texture(MyRender.GraphicsDevice, 256, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default);
            NormalizedByte4[] data = new NormalizedByte4[256 * 1];
            for (int x = 0; x < 256; x++)
            {
                for (int y = 0; y < 1; y++)
                {
                    data[x + (y * 256)] = new NormalizedByte4(g4[perm[x] % 32, 0], g4[perm[x] % 32, 1], g4[perm[x] % 32, 2], g4[perm[x] % 32, 3]);
                }
            }

            SharpDX.DataStream ds;
            DataRectangle dr = permGrad4dTexture.LockRectangle(0, LockFlags.None, out ds);
            ds.WriteRange(data);
            permGrad4dTexture.UnlockRectangle(0);

            
        }
 // permuted gradient texture for optimized version
 private void GeneratePermGradTexture()
 {
     if (permGradTexture != null)
         permGradTexture.Dispose();
     //NormalizedByte4
     permGradTexture = new Texture(MyRender.GraphicsDevice, 256, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default);
     
          
     NormalizedByte4[] data = new NormalizedByte4[256 * 1];
     for (int x = 0; x < 256; x++)
     {
         for (int y = 0; y < 1; y++)
         {
             //data[x + (y * 256)] = new NormalizedByte4(g3[perm[x] % 16, 0], g3[perm[x] % 16, 1], g3[perm[x] % 16, 2], 1);
             data[x + (y * 256)] = new NormalizedByte4(g3[perm[x] % 16, 0], g3[perm[x] % 16, 1], g3[perm[x] % 16, 2], 1);
         }
     }      
          
     /*
     byte[] data = new byte[256 * 1 * 4];
     for (int x = 0; x < 256; x++)
     {
         for (int y = 0; y < 1; y++)
         {
             data[(x + (y * 256)) * 4 + 0] = (byte)(255.0f * ((g3[perm[x] % 16, 0] + 1) / 2.0f));
             data[(x + (y * 256)) * 4 + 1] = (byte)(255.0f * ((g3[perm[x] % 16, 1] + 1) / 2.0f));
             data[(x + (y * 256)) * 4 + 2] = (byte)(255.0f * ((g3[perm[x] % 16, 2] + 1) / 2.0f));
             data[(x + (y * 256)) * 4 + 3] = 1;
         }
     } */   
    
     SharpDX.DataStream ds;
     DataRectangle dr = permGradTexture.LockRectangle(0, LockFlags.None, out ds);
     ds.WriteRange(data);
     permGradTexture.UnlockRectangle(0);
 }
Example #15
0
        public unsafe Texture GetTexture(int fontId, string text)
        {
            UnicodeFont font = _fonts[fontId];

            if (font == null)
                return null;

            int width = font.GetWidth(text) + 2;
            int height = font.GetHeight(text) + 2;

            Texture result = new Texture(_device, font.GetWidth(text) + 2, font.GetHeight(text) + 2, 0, Usage.None, Format.A1R5G5B5, Pool.Managed);
            DataRectangle rect = result.LockRectangle(0, LockFlags.None);

            ushort* line = (ushort*)rect.DataPointer;
            int delta = rect.Pitch >> 1;

            int dx = 2;
            int dy = 2;

            int index = 0;

            for (int i = 0; i < text.Length; ++i)
            {
                int c = (int)text[i] % 0x10000;
                UnicodeChar ch = font.Chars[c];

                int charWidth = ch.Width;
                int charHeight = ch.Height;

                byte[] data = ch.Bytes;

                if (c == 32)
                {
                    dx += 5;
                    continue;
                }
                else
                {
                    dx += ch.XOffset;
                }

                ushort linesSkipped = (ushort)(height - (charHeight + ch.YOffset));

                for (int y = 0; y < charHeight; ++y)
                {
                    ushort* cur = line + ((y + ch.YOffset) * delta) + dx;

                    for (int x = 0; x < charWidth; ++x)
                    {
                        int offset = x / 8 + y * ((charWidth + 7) / 8);

                        if (offset > data.Length)
                            continue;

                        if ((data[offset] & (1 << (7 - (x % 8)))) != 0)
                            *cur++ = 0x8000;
                        else
                            *cur++ = 0x0000;
                    }
                }

                dx += ch.Width;
            }

            return result;
        }
Example #16
0
        private static void InternalDDSFromStream(Stream stream, Device device, int streamOffset, bool loadMipMap, int offsetMipMaps, out SharpDX.Direct3D9.Texture texture)
        {
            var fileStream = stream as FileStream;

            byte[] data = fileStream != null ? null : SharpDX.Utilities.ReadStream(stream);

            int maxTextureDimension = MyRender.GraphicsDevice.Capabilities.MaxTextureWidth;

            if (IntPtr.Size == 4 || MyRender.GraphicsDevice.AvailableTextureMemory < 1024 * 1024 * 600)
            {
                maxTextureDimension = 2048;
            }

            ImageInformation imageInfo;

            if (fileStream != null)
            {
                imageInfo = GetImageInfoFromFileW(fileStream.Name);
            }
            else
            {
                imageInfo = ImageInformation.FromMemory(data);
            }
            var textureWidth  = imageInfo.Width;
            var textureHeight = imageInfo.Height;

            textureWidth  = Math.Max(1, textureWidth >> offsetMipMaps);
            textureHeight = Math.Max(1, textureHeight >> offsetMipMaps);
            int loadMipmapOffset = offsetMipMaps;

            while (textureWidth > maxTextureDimension || textureHeight > maxTextureDimension)
            {
                loadMipmapOffset++;
                textureWidth  = Math.Max(1, textureWidth >> 1);
                textureHeight = Math.Max(1, textureHeight >> 1);
            }

            if (offsetMipMaps == 0 && loadMipmapOffset == 0)
            {
                if (fileStream != null)
                {
                    texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0);
                }
                else
                {
                    texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0);
                }
            }
            else if (loadMipmapOffset > 0)
            {
                var skipFilter = (Filter)D3DXSkipDDSMipLevels((uint)loadMipmapOffset);

                if (fileStream != null)
                {
                    texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, skipFilter, 0);
                }
                else
                {
                    texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, skipFilter, 0);
                }
            }
            else
            {
                if (fileStream != null)
                {
                    texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0);
                }
                else
                {
                    texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0);
                }

                int width        = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Width);
                int height       = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Height);
                int maxLevels    = Math.Min(MaxMipMapLevels(width), MaxMipMapLevels(height));
                int actualLevels = Math.Min(maxLevels, texture.LevelCount - offsetMipMaps);

                Format  format          = texture.GetLevelDescription(0).Format;
                Texture offsetedTexture = new Texture(device, width, height, actualLevels, Usage.Dynamic, format, Pool.Default);
                for (int i = offsetMipMaps, j = 0; j < actualLevels; i++, j++)
                {
                    int levelWidth  = MipMapSize(j, width);
                    int levelHeight = MipMapSize(j, height);

                    SharpDX.DataStream ds;
                    texture.LockRectangle(i, LockFlags.ReadOnly, out ds);
                    texture.UnlockRectangle(i);

                    SharpDX.DataStream ds2;
                    offsetedTexture.LockRectangle(j, LockFlags.None, out ds2);
                    ds2.Position = 0;
                    ds2.Write(ds.DataPointer, 0, (int)MipMapSizeInBytes(levelWidth, levelHeight, format));
                    offsetedTexture.UnlockRectangle(j);
                }

                texture.Dispose();
                texture = offsetedTexture;
            }
        }
        private void GeneratePermTexture()
        {
            if (permTexture != null)
                permTexture.Dispose();

            permTexture = new Texture(MyRender.GraphicsDevice, 256, 1, 0, Usage.Dynamic, Format.L8, Pool.Default);
            byte[] data = new byte[256 * 1];
            for (int x = 0; x < 256; x++)
            {
                for (int y = 0; y < 1; y++)
                {
                    data[x + (y * 256)] = (byte)(perm[x]);// / 255.0);
                }
            }
            
            SharpDX.DataStream ds;
            DataRectangle dr = permTexture.LockRectangle(0, LockFlags.None, out ds);
            ds.WriteRange(data);
            permTexture.UnlockRectangle(0);

        }                     
Example #18
0
        public static void SaveScreenshot(Texture texture2D, string file)
        {      
            MyMwcLog.WriteLine("MyScreenshot.SaveTexture2D() - START");
            MyMwcLog.IncreaseIndent();

            Texture systemTex =  new Texture(MinerWars.AppCode.App.MyMinerGame.Static.GraphicsDevice, texture2D.GetLevelDescription(0).Width, texture2D.GetLevelDescription(0).Height, 0, Usage.None, Format.A8R8G8B8, Pool.SystemMemory);


            Surface sourceSurface = texture2D.GetSurfaceLevel(0);
            Surface destSurface = systemTex.GetSurfaceLevel(0);
            MinerWars.AppCode.App.MyMinerGame.Static.GraphicsDevice.GetRenderTargetData(sourceSurface, destSurface);
            sourceSurface.Dispose();
            destSurface.Dispose();

            texture2D = systemTex;

            try
            {
                MyMwcLog.WriteLine("File: " + file);

                MyFileSystemUtils.CreateFolderForFile(file);

                Stack<SharpDX.Rectangle> tiles = new Stack<SharpDX.Rectangle>();

                int tileWidth = texture2D.GetLevelDescription(0).Width;
                int tileHeight = texture2D.GetLevelDescription(0).Height;

                while (tileWidth > 3200)
                {
                    tileWidth /= 2;
                    tileHeight /= 2;
                }

                int widthOffset = 0;
                int heightOffset = 0;

                while (widthOffset < texture2D.GetLevelDescription(0).Width)
                {
                    while (heightOffset < texture2D.GetLevelDescription(0).Height)
                    {
                        tiles.Push(new SharpDX.Rectangle(widthOffset, heightOffset, tileWidth, tileHeight));
                        heightOffset += tileHeight;
                    }

                    heightOffset = 0;
                    widthOffset += tileWidth;
                }

                int sc = 0;
                while (tiles.Count > 0)
                {
                    SharpDX.Rectangle rect = tiles.Pop();

                    byte[] data = new byte[rect.Width * rect.Height * 4];
                    SharpDX.Rectangle rect2 = new SharpDX.Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
                    //texture2D.GetData<byte>(0, rect2, data, 0, data.Length);
                    DataStream ds;
                    texture2D.LockRectangle(0, rect2, LockFlags.None, out ds); 

                    ds.Read(data, 0, data.Length);
                            /*
                    for (int i = 0; i < data.Length; i += 4)
                    {
                        //Swap ARGB <-> RGBA
                        byte b = data[i + 0];
                        byte g = data[i + 1];
                        byte r = data[i + 2];
                        byte a = data[i + 3];
                        data[i + 0] = r;  //Blue
                        data[i + 1] = g; //Green
                        data[i + 2] = b; //Red
                        data[i + 3] = a; //Alpha
                    }         */

                    ds.Seek(0, SeekOrigin.Begin);
                    ds.WriteRange(data);

                    texture2D.UnlockRectangle(0);

                    string filename = file.Replace(".png", "_" + sc.ToString("##00") + ".png");
                    using (Stream stream = File.Create(filename))
                    {        
                        System.Drawing.Bitmap image = new System.Drawing.Bitmap(rect.Width, rect.Height);

                        System.Drawing.Imaging.BitmapData imageData = image.LockBits(new System.Drawing.Rectangle(0,0,rect.Width, rect.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        System.Runtime.InteropServices.Marshal.Copy(data, 0, imageData.Scan0, data.Length);

                        image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

                        image.UnlockBits(imageData);
                        image.Dispose();
                               
                        //texture2D.SaveAsPng(stream, texture2D.Width, texture2D.Height);
                        //BaseTexture.ToStream(texture2D, ImageFileFormat.Png);
                    }

                    sc++;
                    GC.Collect();
                }
            }
            catch (Exception exc)
            {
                //  Write exception to log, but continue as if nothing wrong happened
                MyMwcLog.WriteLine(exc);
            }

            texture2D.Dispose();

            //BaseTexture.ToFile(texture2D, "c:\\test.png", ImageFileFormat.Png);

            MyMwcLog.DecreaseIndent();
            MyMwcLog.WriteLine("MyScreenshot.SaveTexture2D() - END");   
        }
Example #19
0
        public static string SaveScreenshot(Texture tex, string file)
        {
            MyRender.Log.WriteLine("MyScreenshot.SaveTexture2D() - START");
            MyRender.Log.IncreaseIndent();

            string filename = null;

            using (Texture systemTex = new Texture(MyRender.GraphicsDevice, tex.GetLevelDescription(0).Width, tex.GetLevelDescription(0).Height, 1, Usage.None, Format.A8R8G8B8, Pool.SystemMemory))
            {
                string extension = Path.GetExtension(file);

                using (Surface sourceSurface = tex.GetSurfaceLevel(0))
                using (Surface destSurface = systemTex.GetSurfaceLevel(0))
                {
                    MyRender.GraphicsDevice.GetRenderTargetData(sourceSurface, destSurface);
                }
                
                try
                {
                    MyRender.Log.WriteLine("File: " + file);

                    Stack<SharpDX.Rectangle> tiles = new Stack<SharpDX.Rectangle>();

                    int tileWidth = systemTex.GetLevelDescription(0).Width;
                    int tileHeight = systemTex.GetLevelDescription(0).Height;

                    while (tileWidth > 3200)
                    {
                        tileWidth /= 2;
                        tileHeight /= 2;
                    }

                    int widthOffset = 0;
                    int heightOffset = 0;

                    while (widthOffset < systemTex.GetLevelDescription(0).Width)
                    {
                        while (heightOffset < systemTex.GetLevelDescription(0).Height)
                        {
                            tiles.Push(new SharpDX.Rectangle(widthOffset, heightOffset, widthOffset + tileWidth, heightOffset + tileHeight));
                            heightOffset += tileHeight;
                        }

                        heightOffset = 0;
                        widthOffset += tileWidth;
                    }

                    bool multipleTiles = tiles.Count > 1;

                    int sc = 0;
                    byte[] data = new byte[tileWidth * tileHeight * 4];

                    int sysTexWidth = systemTex.GetLevelDescription(0).Width;
                    int sysTexHeight = systemTex.GetLevelDescription(0).Height;
                    while (tiles.Count > 0)
                    {
                        SharpDX.Rectangle rect = tiles.Pop();
                        //texture2D.GetData<byte>(0, rect2, data, 0, data.Length);
                        DataStream ds;
                        //DataRectangle dr = texture2D.LockRectangle(0, rect2, LockFlags.ReadOnly, out ds);
                        DataRectangle dr = systemTex.LockRectangle(0, LockFlags.ReadOnly, out ds);

                        //we have to go line by line..
                        ds.Seek(rect.Y * sysTexWidth * 4, SeekOrigin.Begin);
                        int targetOffset = 0;

                        int linesCount = tileHeight;

                        int pixelsBefore = rect.X;
                        int pixelsAfter = sysTexWidth - tileWidth - rect.X;

                        while (linesCount-- > 0)
                        {
                            if (pixelsBefore > 0)
                                ds.Seek(pixelsBefore * 4, SeekOrigin.Current);
                            
                            ds.Read(data, targetOffset, tileWidth * 4);
                            targetOffset += tileWidth * 4;
                            
                            if (pixelsAfter > 0 && linesCount > 0)
                                ds.Seek(pixelsAfter * 4, SeekOrigin.Current);
                        }

                        systemTex.UnlockRectangle(0);
                        filename = file;

                        if (multipleTiles)
                        {
                            filename = file.Replace(extension, "_" + sc.ToString("##00") + extension);
                        }

                        using (var stream = MyFileSystem.OpenWrite(MyFileSystem.UserDataPath, filename))
                        {
                            using (System.Drawing.Bitmap image = new System.Drawing.Bitmap(tileWidth, tileHeight))
                            {
                                System.Drawing.Imaging.BitmapData imageData = image.LockBits(new System.Drawing.Rectangle(0, 0, tileWidth, tileHeight), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                                System.Runtime.InteropServices.Marshal.Copy(data, 0, imageData.Scan0, data.Length);

                                if (extension == ".png")
                                    image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                                else if (extension == ".jpg" || extension == ".jpeg")
                                    image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                                else if (extension == ".bmp")
                                    image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
                                else
                                    throw new InvalidOperationException("Invalid file extension: " + extension + ", please use png, jpg or bmp");

                                image.UnlockBits(imageData);
                            }

                            //texture2D.SaveAsPng(stream, texture2D.Width, texture2D.Height);
                            //BaseTexture.ToStream(texture2D, ImageFileFormat.Png);
                        }

                        sc++;
                        GC.Collect();
                    }
                }
                catch (Exception exc)
                {
                    //  Write exception to log, but continue as if nothing wrong happened
                    MyRender.Log.WriteLine(exc);
                    filename = null;
                }
            }
            //BaseTexture.ToFile(texture2D, "c:\\test.png", ImageFileFormat.Png);

            MyRender.Log.DecreaseIndent();
            MyRender.Log.WriteLine("MyScreenshot.SaveTexture2D() - END");

            return filename;
        }
Example #20
0
    public override void Allocate()
    {
      lock (_syncObj)
      {
        if (!IsAllocated)
        {
          byte[] buffer = new byte[TEXTURE_SIZE * TEXTURE_SIZE * 4];
          int offset = 0;
          while (offset < TEXTURE_SIZE * TEXTURE_SIZE * 4)
          {
            buffer[offset++] = _color.R;
            buffer[offset++] = _color.G;
            buffer[offset++] = _color.B;
            buffer[offset++] = _color.A;
          }

          _texture = new Texture(GraphicsDevice.Device, TEXTURE_SIZE, TEXTURE_SIZE, 1, Usage.Dynamic, Format.A8R8G8B8, Pool.Default);

          DataStream dataStream;
          _texture.LockRectangle(0, LockFlags.None, out dataStream);
          using (dataStream)
          {
            dataStream.Write(buffer, 0, buffer.Length);
            _texture.UnlockRectangle(0);
          }

          _width = TEXTURE_SIZE;
          _height = TEXTURE_SIZE;
          _allocationSize = TEXTURE_SIZE * TEXTURE_SIZE * 4;
        }
      }
      // Don't hold a lock while calling external code
      FireAllocationChanged(_allocationSize);
    }
        private void GenerateGradTexture()
        {                                                                                       //NormalizedByte4
            gradTexture = new Texture(MyRender.GraphicsDevice, 16, 1, 0, Usage.Dynamic, Format.Q8W8V8U8, Pool.Default);
            NormalizedByte4[] data = new NormalizedByte4[16 * 1];
            for (int x = 0; x < 16; x++)
            {
                for (int y = 0; y < 1; y++)
                {
                    data[x + (y * 16)] = new NormalizedByte4(g3[x, 0], g3[x, 1], g3[x, 2], 1);
                }
            }

            SharpDX.DataStream ds;
            DataRectangle dr = gradTexture.LockRectangle(0, LockFlags.None, out ds);
            ds.WriteRange(data);
            gradTexture.UnlockRectangle(0);
        }