Esempio n. 1
0
        protected void DrawBackground(Cairo.Context cr)
        {
            var totalRect = GetTotalBounds();

            cr.SetSourceColor(BackgroundColor);
            cr.Rectangle(totalRect.X, totalRect.Y, totalRect.Width, totalRect.Height);
            cr.Fill();

            cr.Save();
            cr.Translate(XOffset, YOffset);
            cr.Scale(Scale, Scale);

            if (Surface != null)
            {
                cr.SetSource(Surface, 0, 0);

                using (SurfacePattern pattern = (SurfacePattern)cr.GetSource()) {
                    pattern.Filter = Filter.Nearest;
                }

                cr.Paint();
            }
            else if (Image != null)
            {
                using (Surface source = new BitmapSurface(Image)) {
                    cr.SetSource(source, 0, 0);

                    using (SurfacePattern pattern = (SurfacePattern)cr.GetSource()) {
                        pattern.Filter = Filter.Nearest;
                    }

                    cr.Paint();
                }
            }
            else
            {
                Cairo.Rectangle extents = cr.ClipExtents();
                for (int i = 0; i < Width * Height; i++)
                {
                    int             tileX = i % Width;
                    int             tileY = i / Width;
                    Cairo.Rectangle rect  = GetTileRectWithPadding(tileX, tileY, scale: false, offset: false);

                    if (!CairoHelper.RectsOverlap(extents, rect))
                    {
                        continue;
                    }

                    cr.Save();
                    cr.Translate(rect.X + TilePaddingX, rect.Y + TilePaddingY);
                    cr.Rectangle(0, 0, TileWidth, TileHeight);
                    cr.Clip();
                    TileDrawer(i, cr);
                    cr.Restore();
                }
            }

            cr.Restore(); // Undo scale, offset
        }
Esempio n. 2
0
 static BitmapSurface GetBitmapSurface(Bitmap bitmap)
 {
     if (surfaceDict.ContainsKey(bitmap))
     {
         throw new Exception("Tried to lock an already locked bitmap!");
     }
     surfaceDict[bitmap] = new BitmapSurface(bitmap);
     return(surfaceDict[bitmap]);
 }
Esempio n. 3
0
 public override void Draw(Bitmap bitmap, int x, int y)
 {
     using (Cairo.Surface s = new BitmapSurface(bitmap)) {
         cr.SetSourceSurface(s, x + xOffset, y + yOffset);
         using (Cairo.SurfacePattern pattern = (Cairo.SurfacePattern)cr.GetSource()) {
             pattern.Filter = Cairo.Filter.Nearest;
         }
         cr.Paint();
     }
 }
Esempio n. 4
0
 /// <summary>
 ///  Makes a copy of a bitmap as a Cairo.ImageSurface. No need to unlock the bitmap later.
 ///  I don't know why, but this seems to have caused segfaults, so don't use it I guess...
 /// </summary>
 public static Cairo.Surface CopyBitmap(System.Drawing.Bitmap bitmap)
 {
     throw new Exception("This function caused segfaults, don't use it...");
     byte[] data;
     using (Cairo.ImageSurface surface = new BitmapSurface(bitmap)) {
         data = new byte[surface.Height * surface.Stride];
         System.Runtime.InteropServices.Marshal.Copy(surface.DataPtr, data, 0, data.Length);
         return(new Cairo.ImageSurface(data, surface.Format, surface.Width, surface.Height, surface.Stride));
     }
 }
Esempio n. 5
0
        // Overridable function which generates the image for a tile on the map.
        // Child classes could choose override "TileDrawer" instead. The advantage of overriding
        // this function is that the image will be cached.
        protected virtual Cairo.Surface GenerateTileImage(int x, int y)
        {
            RoomLayout layout      = GetRoomLayout(x, y);
            var        tileSurface = this.Window.CreateSimilarSurface(Cairo.Content.Color,
                                                                      (int)(layout.Width * 16 * _scale), (int)(layout.Height * 16 * _scale));

            // The below line works fine, but doesn't look as good on hidpi monitors
            //_surface = new Cairo.ImageSurface(Cairo.Format.Rgb24,
            //        (int)(layout.Width * 16 * _scale), (int)(layout.Height * 16 * _scale));

            Bitmap img = layout.GetImage();

            using (var cr = new Cairo.Context(tileSurface))
                using (var source = new BitmapSurface(img)) {
                    cr.Scale(_scale, _scale);
                    cr.SetSource(source, 0, 0);
                    cr.Paint();
                }

            return(tileSurface);
        }