Exemplo n.º 1
0
        public static Texture2D ToTexture(this System.Drawing.Bitmap bitmap, GraphicsDevice device)
        {
            var rect    = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
            var bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
                                          System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            byte[] buffer = new byte[bmpData.Stride * bmpData.Height];
            Marshal.Copy(bmpData.Scan0, buffer, 0, buffer.Length);
            bitmap.UnlockBits(bmpData);

            //bgra->rgba
            MonogameUtils.BgraToColor(buffer);

            var t2d = new Texture2D(device, rect.Width, rect.Height, false, SurfaceFormat.Color);

            t2d.SetData(buffer);
            return(t2d);
        }
Exemplo n.º 2
0
        public static Texture2D ToTexture(this Wz_Png png, GraphicsDevice graphicsDevice)
        {
            byte[] plainData = png.GetRawData();
            if (plainData == null)
            {
                return(null);
            }

            Texture2D t2d;

            switch (png.Form)
            {
            case 1:
                t2d = null;
                try
                {
                    t2d = MonogameUtils.CreateTexture_BGRA4444(graphicsDevice, png.Width, png.Height);
                    t2d.SetData(plainData);
                }
                catch      //monogame并不支持这个format 用gdi+转
                {
                    if (t2d != null)
                    {
                        t2d.Dispose();
                    }
                    goto default;
                }
                break;

            case 2:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Color);
                MonogameUtils.BgraToColor(plainData);
                t2d.SetData(plainData);
                break;

            case 513:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Bgr565);
                t2d.SetData(plainData);
                break;

            case 517:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Bgr565);
                byte[] texData = new byte[png.Width * png.Height * 2];
                for (int j0 = 0, j1 = png.Height / 16; j0 < j1; j0++)
                {
                    int idxTex = j0 * 16 * png.Width * 2;
                    for (int i0 = 0, i1 = png.Width / 16; i0 < i1; i0++)
                    {
                        int idx = (i0 + j0 * i1) * 2;

                        for (int k = 0; k < 16; k++)
                        {
                            texData[idxTex + i0 * 32 + k * 2]     = plainData[idx];
                            texData[idxTex + i0 * 32 + k * 2 + 1] = plainData[idx + 1];
                        }
                    }
                    for (int k = 1; k < 16; k++)
                    {
                        System.Buffer.BlockCopy(texData, idxTex, texData, idxTex + k * png.Width * 2, png.Width * 2);
                    }
                }
                t2d.SetData(texData);
                break;

            case 1026:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Dxt3);
                t2d.SetData(plainData);
                break;

            case 2050:
                t2d = new Texture2D(graphicsDevice, png.Width, png.Height, false, SurfaceFormat.Dxt5);
                t2d.SetData(plainData);
                break;

            default:     //默认从bitmap复制 二次转换
                var bitmap = png.ExtractPng();
                t2d = bitmap.ToTexture(graphicsDevice);
                bitmap.Dispose();
                break;
            }

            return(t2d);
        }
Exemplo n.º 3
0
        private Rectangle CreateCharBuffer(char c)
        {
            string text = c.ToString();
            SizeF  size;

            size = g.MeasureString(text, this.baseFont, byte.MaxValue, StringFormat.GenericTypographic);
            GDIRect originRect = new GDIRect(gdiBufferX, 0, (int)Math.Ceiling(size.Width), (int)Math.Round(size.Height));

            if (originRect.Width == 0)
            {
                originRect.Width = (int)(this.baseFont.Size / 2);
            }
            if (gdiBuffer.Height < originRect.Height)
            {
                RebuildGdiBuffer(originRect.Height);
                originRect.X = 0; //2012-10-3
            }
            if (gdiBufferX + originRect.Width > gdiBuffer.Width)
            {
                g.Clear(GDIColor.Transparent);
                gdiBufferX   = 0;
                originRect.X = 0;
            }
            g.DrawString(text, baseFont, Brushes.White, originRect.Location, StringFormat.GenericTypographic);

            //计算范围并且复制图像数据到数组
            byte[]     b    = new byte[4 * originRect.Width * originRect.Height];
            BitmapData data = gdiBuffer.LockBits(originRect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            for (int i = 0; i < originRect.Height; i++)
            {
                IntPtr source = IntPtr.Add(data.Scan0, data.Stride * i);
                System.Runtime.InteropServices.Marshal.Copy(source, b, i * 4 * originRect.Width, 4 * originRect.Width);
            }
            gdiBuffer.UnlockBits(data);
            gdiBufferX += originRect.Width;

            //调整xnaTexture的大小并粘贴图像
            textureBuffer.GraphicsDevice.Textures[0] = null;

            if (textureSpaceX + originRect.Width > textureBuffer.Width)
            {
                textureSpaceX        = 0;
                textureSpaceY       += textureCurLineHeight;
                textureCurLineHeight = 0;
            }
            textureCurLineHeight = Math.Max(textureCurLineHeight, originRect.Height);
            if (textureSpaceY + textureCurLineHeight > textureBuffer.Height)
            {
                ClearTextureBuffer();
                textureSpaceX = 0;
                textureSpaceY = 0;
                charLocation.Clear();
            }

            Rectangle rect = new Rectangle(textureSpaceX, textureSpaceY, originRect.Width, originRect.Height);

            MonogameUtils.BgraToColor(b);
            textureBuffer.SetData(0, rect, b, 0, b.Length);
            textureSpaceX += rect.Width;
            return(rect);
        }