Esempio n. 1
0
        public static void LoadAndProcessClickMap(string path, PathNode[,] pathNodes, Grid pathGrid, int tileSize)
        {
            var map      = new SFML.Graphics.Image(path);
            var clickMap = new bool[map.Size.X, map.Size.Y];

            float totalPixelsInTile = tileSize * tileSize;
            int   totalTrue         = 0;
            int   xMax = 0;
            int   yMax = 0;

            for (int x = 0; x < pathNodes.GetLength(0); x++)
            {
                for (int y = 0; y < pathNodes.GetLength(1); y++)
                {
                    totalTrue = 0;
                    xMax      = tileSize * x + tileSize;

                    for (int xMap = tileSize * x; xMap < xMax; xMap++)
                    {
                        yMax = tileSize * y + tileSize;
                        for (int yMap = tileSize * y; yMap < yMax; yMap++)
                        {
                            if (xMap < map.Size.X && yMap < map.Size.Y && map.GetPixel((uint)xMap, (uint)yMap).B > 40)
                            {
                                totalTrue++;
                            }
                        }
                    }
                    pathGrid.SetTile(x, y, !(pathNodes[x, y].Enabled = (totalTrue / totalPixelsInTile) >= .5f));
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Loads the image internally in the texture for image manipulation.  This is
 /// handled automatically, but it's exposed so that it can be manually controlled.
 /// </summary>
 public void CreateImage()
 {
     if (image == null)
     {
         image = texture.CopyToImage();
     }
 }
Esempio n. 3
0
 /// <summary>
 /// Loads the image internally in the texture for image manipulation.  This is
 /// handled automatically, but it's exposed so that it can be manually controlled.
 /// </summary>
 /// <param name="forceNewImage">If set to true a new image will always be created instead of only when there is no image.</param>
 public void CreateImage(bool forceNewImage = false)
 {
     if (image == null || forceNewImage)
     {
         image = texture.CopyToImage();
     }
 }
Esempio n. 4
0
        public SFML.Graphics.Image ToImage()
        {
            var res = new SFML.Graphics.Image((uint)this.Width, (uint)this.Height);

            for (uint y = 0; y < Height; y++)
            {
                for (uint x = 0; x < Width; x++)
                {
                    switch (this.Data [x, y])
                    {
                    case "#":
                        res.SetPixel(x, y, SFML.Graphics.Color.Black);
                        break;

                    case ".":
                        res.SetPixel(x, y, SFML.Graphics.Color.White);
                        break;

                    default:
                        res.SetPixel(x, y, SFML.Graphics.Color.Cyan);
                        break;
                    }
                }
            }
            res.SetPixel((uint)this.StartCell.X, (uint)this.StartCell.Y, SFML.Graphics.Color.Green);
            res.SetPixel((uint)this.EndCell.X, (uint)this.EndCell.Y, SFML.Graphics.Color.Red);
            return(res);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a pixel collider.
        /// </summary>
        /// <param name="source">The source image to create the collider from.</param>
        /// <param name="tags">The tags to register the collider with.</param>
        public PixelCollider(string source, params int[] tags) {
            texture = Textures.Load(source);
            collideImage = texture.CopyToImage();
            
            Width = texture.Size.X;
            Height = texture.Size.Y;

            AddTag(tags);
        }
Esempio n. 6
0
        public PixelCollider(Texture texture, params int[] tags) {
            this.texture = texture.SFMLTexture;
            collideImage = this.texture.CopyToImage();

            Width = this.texture.Size.X;
            Height = this.texture.Size.Y;

            AddTag(tags);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates an <see cref="SFML.Graphics.Image"/> from an array of bytes. The color is assumed to be in R8G8B8A8 format.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="source">The <see cref="Rectangle"/> to copy on the source image.</param>
        /// <returns>
        /// The <see cref="Bitmap"/> containing the <paramref name="image"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="image"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The <paramref name="source"/> specifies an area
        /// outside of the <paramref name="image"/>.</exception>
        public static Bitmap ToBitmap(this Image image, SFML.Graphics.Rectangle source)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }

            return(ToBitmap(image, new Rectangle(source.X, source.Y, source.Width, source.Height)));
        }
Esempio n. 8
0
        public PixelCollider(Texture texture, params int[] tags)
        {
            this.texture = texture.SFMLTexture;
            collideImage = this.texture.CopyToImage();

            Width  = this.texture.Size.X;
            Height = this.texture.Size.Y;

            AddTag(tags);
        }
Esempio n. 9
0
        /// <summary>
        /// Creates a pixel collider.
        /// </summary>
        /// <param name="source">The source image to create the collider from.</param>
        /// <param name="tags">The tags to register the collider with.</param>
        public PixelCollider(string source, params int[] tags)
        {
            texture      = Textures.Load(source);
            collideImage = texture.CopyToImage();

            Width  = texture.Size.X;
            Height = texture.Size.Y;

            AddTag(tags);
        }
Esempio n. 10
0
        /// <summary>
        /// Creates an <see cref="SFML.Graphics.Image"/> from an array of bytes. The color is assumed to be in R8G8B8A8 format.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="source">The <see cref="Rectangle"/> to copy on the source image.</param>
        /// <param name="destWidth">The width of the generated image.</param>
        /// <param name="destHeight">The height of the generated image.</param>
        /// <returns>
        /// The <see cref="Bitmap"/> containing the <paramref name="image"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="image"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The <paramref name="source"/> specifies an area
        /// outside of the <paramref name="image"/>.</exception>
        public static Bitmap ToBitmap(this Image image, SFML.Graphics.Rectangle source, int destWidth, int destHeight)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }

            var rect = new Rectangle(source.X, source.Y, source.Width, source.Height);

            return(ToBitmap(image, rect, destWidth, destHeight));
        }
Esempio n. 11
0
        public Color GetPixel(uint x, uint y)
        {
            EnsureNotDisposed();

            if (x >= Width || y >= Height)
            {
                throw new CoordinatesOutOfBoundsException(x, y, Width, Height,
                                                          "Tried to retrieve a pixel outside the texture area.");
            }

            return(SfmlImage.GetPixel(x, y));
        }
Esempio n. 12
0
        public void SetPixel(uint x, uint y, Color color)
        {
            EnsureNotDisposed();

            if (x >= Width || y >= Height)
            {
                throw new CoordinatesOutOfBoundsException(x, y, Width, Height,
                                                          "Tried to set a pixel outside the texture area.");
            }

            SfmlImage.SetPixel(x, y, color);
        }
Esempio n. 13
0
 public horse(string Color)
 {
     if (Color == "White")
     {
         im = new SFML.Graphics.Image(WhiteImagePath);
     }
     else
     {
         im = new SFML.Graphics.Image(BlackImagePath);
     }
     text = new SFML.Graphics.Texture(im);
     sp   = new SFML.Graphics.Sprite(text);
 }
Esempio n. 14
0
        protected virtual void Dispose(bool disposing)
        {
            if (!Disposed)
            {
                if (disposing)
                {
                    // No managed resources to dispose of.
                }

                SfmlTexture.Dispose();
                SfmlImage.Dispose();

                Disposed = true;
            }
        }
Esempio n. 15
0
        public Color GetPixel(Vector2 position)
        {
            EnsureNotDisposed();

            if ((uint)position.X >= Width || (uint)position.Y >= Height)
            {
                throw new CoordinatesOutOfBoundsException((uint)position.X, (uint)position.Y, Width, Height,
                                                          "Tried to retrieve a pixel outside the texture area.");
            }

            return(SfmlImage.GetPixel(
                       (uint)position.X,
                       (uint)position.Y
                       ));
        }
Esempio n. 16
0
        /// <summary>
        /// Creates an <see cref="SFML.Graphics.Image"/> from an array of bytes. The color is assumed to be in R8G8B8A8 format.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="source">The <see cref="Rectangle"/> to copy on the source image.</param>
        /// <returns>
        /// The <see cref="Bitmap"/> containing the <paramref name="image"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="image"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The <paramref name="source"/> specifies an area
        /// outside of the <paramref name="image"/>.</exception>
        public static Bitmap ToBitmap(this Image image, Rectangle source)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }

            if (source.X < 0 || source.Y < 0 || source.Right > image.Width || source.Bottom > image.Height)
            {
                throw new ArgumentOutOfRangeException("source");
            }

            // Create the target bitmap
            var b = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppArgb);

            // Copy the values
            CopyToBitmap(image, b, source);

            return(b);
        }
Esempio n. 17
0
        /// <summary>
        /// Creates an <see cref="SFML.Graphics.Image"/> from an array of bytes. The color is assumed to be in R8G8B8A8 format.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="source">The <see cref="Rectangle"/> to copy on the source image.</param>
        /// <param name="destWidth">The width of the generated image.</param>
        /// <param name="destHeight">The height of the generated image.</param>
        /// <returns>
        /// The <see cref="Bitmap"/> containing the <paramref name="image"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="image"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The <paramref name="source"/> specifies an area
        /// outside of the <paramref name="image"/>.</exception>
        public static Bitmap ToBitmap(this Image image, Rectangle source, int destWidth, int destHeight)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }

            if (destWidth == source.Width && destHeight == source.Height)
            {
                // The destination is the same size as the source
                return(ToBitmap(image, source));
            }
            else
            {
                // The destination is not the same size as the source, so we will have to scale it
                using (var original = ToBitmap(image, source))
                {
                    var ret = original.CreateScaled(source, destWidth, destHeight, true, null, null);
                    return(ret);
                }
            }
        }
Esempio n. 18
0
 /// <summary>
 /// Loads the image internally in the texture for image manipulation.  This is
 /// handled automatically, but it's exposed so that it can be manually controlled.
 /// </summary>
 public void CreateImage() {
     if (image == null) {
         image = texture.CopyToImage();
     }
 }
Esempio n. 19
0
        /// <summary>
        /// Copies the pixels from a <see cref="SFML.Graphics.Image"/> to a <see cref="Bitmap"/>.
        /// </summary>
        /// <param name="image">The source <see cref="SFML.Graphics.Image"/>.</param>
        /// <param name="b">The destination <see cref="Bitmap"/>.</param>
        /// <param name="source">The source area to copy.</param>
        static unsafe void CopyToBitmap(Image image, Bitmap b, Rectangle source)
        {
            const int bytesPerColor = 4;

            var pixels = image.Pixels;

            // Lock the whole bitmap for write only
            var rect = new Rectangle(0, 0, source.Width, source.Height);
            var data = b.LockBits(rect, ImageLockMode.WriteOnly, b.PixelFormat);

            try
            {
                var srcStride = rect.Width * bytesPerColor;
                var dataStride = data.Stride / bytesPerColor;
                var srcXtimesBPP = source.X * bytesPerColor;
                var srcY = source.Y;

                // Grab the pointer to the pixels array
                fixed (byte* p = pixels)
                {
                    var row = (int*)data.Scan0;

                    // Copy the pixel values byte-by-byte, making sure to copy the RGBA source to the ARGB destination
                    for (var y = 0; y < data.Height; y++)
                    {
                        var srcOff = ((y + srcY) * srcStride) + srcXtimesBPP;

                        for (var x = 0; x < data.Width; x++)
                        {
                            // Masks for getting the 4 bytes in the int
                            const int b0 = 255;
                            const int b1 = b0 << 8;
                            const int b2 = b0 << 16;
                            const int b3 = b0 << 24;

                            // Get the raw value at the source (pixels[]) as an int (instead of grabbing each byte at a time)
                            var raw = *((int*)(p + srcOff));

                            // Convert to the correct format by moving doing the following to the source bytes:
                            //      src 0 -> dst 2 (move left 2 bytes)
                            //      src 1 -> dst 1 (no moving)
                            //      src 2 -> dst 0 (move right 2 bytes)
                            //      src 3 -> dst 3 (no moving)
                            var converted = (raw & (b3 | b1)) | ((raw & b0) << 16) | ((raw & b2) >> 16);

                            // Store the converted result
                            row[x] = converted;

                            // Move the source over 4 bytes
                            srcOff += bytesPerColor;
                        }

                        row += dataStride;
                    }
                }
            }
            finally
            {
                b.UnlockBits(data);
            }
        }
Esempio n. 20
0
 /// <summary>
 /// Loads the image internally in the texture for image manipulation.  This is
 /// handled automatically, but it's exposed so that it can be manually controlled.
 /// </summary>
 /// <param name="forceNewImage">If set to true a new image will always be created instead of only when there is no image.</param>
 public void CreateImage(bool forceNewImage = false) {
     if (image == null || forceNewImage) {
         image = texture.CopyToImage();
     }
 }
Esempio n. 21
0
 public Texture(Stream stream)
 {
     SfmlImage = new SfmlImage(stream);
 }
Esempio n. 22
0
 public Texture(byte[] data)
 {
     SfmlImage = new SfmlImage(data);
 }
Esempio n. 23
0
        public void SaveToFile(string filePath)
        {
            EnsureNotDisposed();

            SfmlImage.SaveToFile(filePath);
        }
Esempio n. 24
0
 public Texture(uint width, uint height)
 {
     SfmlImage   = new SfmlImage(width, height);
     SfmlTexture = new SfmlTexture(SfmlImage);
 }
Esempio n. 25
0
        public void FlipVertical()
        {
            EnsureNotDisposed();

            SfmlImage.FlipVertically();
        }
Esempio n. 26
0
        public void FlipHorizontal()
        {
            EnsureNotDisposed();

            SfmlImage.FlipHorizontally();
        }
Esempio n. 27
0
        /// <summary>
        /// Copies the pixels from a <see cref="SFML.Graphics.Image"/> to a <see cref="Bitmap"/>.
        /// </summary>
        /// <param name="image">The source <see cref="SFML.Graphics.Image"/>.</param>
        /// <param name="b">The destination <see cref="Bitmap"/>.</param>
        /// <param name="source">The source area to copy.</param>
        static unsafe void CopyToBitmap(Image image, Bitmap b, Rectangle source)
        {
            const int bytesPerColor = 4;

            var pixels = image.Pixels;

            // Lock the whole bitmap for write only
            var rect = new Rectangle(0, 0, source.Width, source.Height);
            var data = b.LockBits(rect, ImageLockMode.WriteOnly, b.PixelFormat);

            try
            {
                var srcStride    = rect.Width * bytesPerColor;
                var dataStride   = data.Stride / bytesPerColor;
                var srcXtimesBPP = source.X * bytesPerColor;
                var srcY         = source.Y;

                // Grab the pointer to the pixels array
                fixed(byte *p = pixels)
                {
                    var row = (int *)data.Scan0;

                    // Copy the pixel values byte-by-byte, making sure to copy the RGBA source to the ARGB destination
                    for (var y = 0; y < data.Height; y++)
                    {
                        var srcOff = ((y + srcY) * srcStride) + srcXtimesBPP;

                        for (var x = 0; x < data.Width; x++)
                        {
                            // Masks for getting the 4 bytes in the int
                            const int b0 = 255;
                            const int b1 = b0 << 8;
                            const int b2 = b0 << 16;
                            const int b3 = b0 << 24;

                            // Get the raw value at the source (pixels[]) as an int (instead of grabbing each byte at a time)
                            var raw = *((int *)(p + srcOff));

                            // Convert to the correct format by moving doing the following to the source bytes:
                            //      src 0 -> dst 2 (move left 2 bytes)
                            //      src 1 -> dst 1 (no moving)
                            //      src 2 -> dst 0 (move right 2 bytes)
                            //      src 3 -> dst 3 (no moving)
                            var converted = (raw & (b3 | b1)) | ((raw & b0) << 16) | ((raw & b2) >> 16);

                            // Store the converted result
                            row[x] = converted;

                            // Move the source over 4 bytes
                            srcOff += bytesPerColor;
                        }

                        row += dataStride;
                    }
                }
            }
            finally
            {
                b.UnlockBits(data);
            }
        }
Esempio n. 28
0
        /// <summary>
        /// Fills this image object with pixel data
        /// </summary>
        /// <param name="fileName">File to load the image from</param>
        public void LoadFromFile(string fileName)
        {
            SFML.Graphics.Image img = new SFML.Graphics.Image(fileName);
            SFML.System.Vector2u dimensions = img.Size;

            rows = (int)dimensions.Y;
            cols = (int)dimensions.X;

            pixels = new byte[3 * rows * cols];
            for(int r = 0;r < rows; r++)
            {
                for(int c = 0; c < cols; c++)
                {
                    SFML.Graphics.Color color = img.GetPixel((uint)(c), (uint)(rows-r-1));
                    pixels[(cols * r * 3) + (3*c)] = color.R;
                    pixels[(cols * r * 3) + (3*c) + 1] = color.G;
                    pixels[(cols * r * 3) + (3*c) + 2] = color.B;
                }
            }
        }
Esempio n. 29
0
        /// <summary>
        /// Creates an <see cref="SFML.Graphics.Image"/> from an array of bytes. The color is assumed to be in R8G8B8A8 format.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <returns>
        /// The <see cref="Bitmap"/> containing the <paramref name="image"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="image"/> is null.</exception>
        public static Bitmap ToBitmap(this Image image)
        {
            var source = new Rectangle(0, 0, (int)image.Width, (int)image.Height);

            return(ToBitmap(image, source));
        }
Esempio n. 30
0
 public Texture(string filePath)
 {
     SfmlImage = new SfmlImage(filePath);
 }
Esempio n. 31
0
        public static void GameLoop()
        {
            Point    dest      = new Point(frmMapEditor.Default.PointToScreen(frmMapEditor.Default.picScreen.Location).X, frmMapEditor.Default.PointToScreen(frmMapEditor.Default.picScreen.Location).Y);
            Graphics g         = frmMapEditor.Default.picScreen.CreateGraphics();
            int      starttime = 0;
            int      Tick      = 0;
            int      fogtmr    = 0;
            int      FrameTime;
            int      tmr500      = 0;
            int      tmpfps      = 0;
            int      rendercount = 0;

            starttime = ClientDataBase.GetTickCount();

            do
            {
                if (E_Globals.GameDestroyed == true)
                {
                    ProjectData.EndApp();
                }

                UpdateUI();

                if (E_Globals.GameStarted == true)
                {
                    Tick = ClientDataBase.GetTickCount();

                    // update animation editor
                    if (E_Globals.Editor == E_Globals.EDITOR_ANIMATION)
                    {
                        E_Graphics.EditorAnim_DrawAnim();
                    }

                    if (E_Globals.Editor == E_Projectiles.EDITOR_PROJECTILE)
                    {
                        E_Graphics.Draw_ProjectilePreview();
                    }

                    FrameTime = Tick;
                    if (E_Globals.InMapEditor && !E_Globals.GettingMap)
                    {
                        //Calculate FPS
                        if (starttime < Tick)
                        {
                            E_Globals.FPS = tmpfps;

                            frmMapEditor.Default.tsCurFps.Text = "Current FPS: " + E_Globals.FPS;
                            tmpfps    = 0;
                            starttime = (ClientDataBase.GetTickCount() + 1000);
                        }
                        tmpfps++;

                        lock (E_Types.MapLock)
                        {
                            // fog scrolling
                            if (fogtmr < Tick)
                            {
                                if (E_Globals.CurrentFogSpeed > 0)
                                {
                                    //move
                                    E_Globals.fogOffsetX--;
                                    E_Globals.fogOffsetY--;
                                    FileSystem.Reset();
                                    if (E_Globals.fogOffsetX < -256)
                                    {
                                        E_Globals.fogOffsetX = 0;
                                    }
                                    if (E_Globals.fogOffsetY < -256)
                                    {
                                        E_Globals.fogOffsetY = 0;
                                    }
                                    fogtmr = Tick + 255 - E_Globals.CurrentFogSpeed;
                                }
                            }

                            if (tmr500 < Tick)
                            {
                                // animate waterfalls
                                switch (E_AutoTiles.waterfallFrame)
                                {
                                case 0:
                                    E_AutoTiles.waterfallFrame = 1;
                                    break;

                                case 1:
                                    E_AutoTiles.waterfallFrame = 2;
                                    break;

                                case 2:
                                    E_AutoTiles.waterfallFrame = 0;
                                    break;
                                }
                                // animate autotiles
                                switch (E_AutoTiles.autoTileFrame)
                                {
                                case 0:
                                    E_AutoTiles.autoTileFrame = 1;
                                    break;

                                case 1:
                                    E_AutoTiles.autoTileFrame = 2;
                                    break;

                                case 2:
                                    E_AutoTiles.autoTileFrame = 0;
                                    break;
                                }

                                tmr500 = Tick + 500;
                            }

                            E_Weather.ProcessWeather();

                            if (E_Sound.FadeInSwitch == true)
                            {
                                E_Sound.FadeIn();
                            }

                            if (E_Sound.FadeOutSwitch == true)
                            {
                                E_Sound.FadeOut();
                            }

                            if (rendercount < Tick)
                            {
                                //Auctual Game Loop Stuff :/
                                E_Graphics.Render_Graphics();
                                rendercount = Tick + 32;
                            }

                            //Do we need this?
                            //Application.DoEvents();

                            E_Graphics.EditorMap_DrawTileset();

                            if (E_Globals.TakeScreenShot)
                            {
                                if (E_Globals.ScreenShotTimer < Tick)
                                {
                                    SFML.Graphics.Image screenshot = E_Graphics.GameWindow.Capture();

                                    if (!System.IO.Directory.Exists(Application.StartupPath + "\\Data\\Screenshots"))
                                    {
                                        System.IO.Directory.CreateDirectory(Application.StartupPath + "\\Data\\Screenshots");
                                    }
                                    screenshot.SaveToFile(Application.StartupPath + "\\Data\\Screenshots\\Map" + System.Convert.ToString(E_Types.Map.mapNum) + ".png");

                                    E_Globals.HideCursor     = false;
                                    E_Globals.TakeScreenShot = false;
                                }
                            }

                            if (E_Globals.MakeCache)
                            {
                                if (E_Globals.ScreenShotTimer < Tick)
                                {
                                    SFML.Graphics.Image screenshot = E_Graphics.GameWindow.Capture();

                                    if (!System.IO.Directory.Exists(Application.StartupPath + "\\Data\\Cache"))
                                    {
                                        System.IO.Directory.CreateDirectory(Application.StartupPath + "\\Data\\Cache");
                                    }
                                    screenshot.SaveToFile(Application.StartupPath + "\\Data\\Cache\\Map" + System.Convert.ToString(E_Types.Map.mapNum) + ".png");

                                    E_Globals.HideCursor = false;
                                    E_Globals.MakeCache  = false;
                                    E_Editors.MapEditorSend();
                                }
                            }
                        }
                    }
                }

                // This should be the only one we need?
                Application.DoEvents();
                //Do we need to pause the thread? without it we gain like 200+ fps
                //Thread.Sleep(1);
                // Lets Yield Instead
                Thread.Yield();
            } while (true);
        }
Esempio n. 32
0
 public Texture(string filePath)
 {
     SfmlImage   = new SfmlImage(filePath);
     SfmlTexture = new SfmlTexture(SfmlImage); // Not in there for some reason?
 }