Exemple #1
0
        private int _width = 0; // Original image width.

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Constructs a new texture resource.
        /// </summary>
        /// <param name="image">The pixel map to use for the texture.</param>
        public ResTexture(Bitmap image)
        {
            PixelMap map = new PixelMap(image);
            _pixels = (int[,])map.Pixels.Clone();

            SetTexture(map);
        }
        /// <summary>
        /// Loads a texture to the texture list
        /// </summary>
        /// <param name="image">The bitmap image used as a texture</param>
        /// <param name="id">The id of the texture.</param>
        public static void LoadTexture(PixelMap image, int id)
        {
            // If the key already exists
            if (_sprites.ContainsKey(id))
            {
                // Dispose of old texture
                _sprites[id].Dispose();

                // Create a new texture
                _sprites[id] = new ResTexture(image);
            }
            else  // Add a texture to the list
            {
                _sprites.Add(id, new ResTexture(image));
            }
        }
Exemple #3
0
        /// <summary>
        /// Pastes a pixel map at the desired position
        /// </summary>
        /// <param name="pixelMap">The pixel map to paste</param>
        /// <param name="x">The horizontal coordinate to start pasting</param>
        /// <param name="y">The vertical coordinate to start pasting</param>
        public void Paste(PixelMap pixelMap, int originX, int originY)
        {
            // Iterate through the pixels horizontally
            for (int x = 0; x < pixelMap.Width; x++)
            {
                // Iterate through the pixels vertically
                for (int y = 0; y < pixelMap.Height; y++)
                {
                    // Calculate index
                    int posX = originX + x;
                    int posY = originY + y;

                    // Set pixel if within bounds
                    if (posX >= 0 && posY >= 0 && posX < _width && posY < _height)
                    {
                        _pixels[posX, posY] = pixelMap.Pixels[x, y];
                    }
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Checks if two pixel maps are the same
        /// </summary>
        /// <param name="map2">The first pixel map to check</param>
        /// <param name="map2">The second pixel map to check</param>
        /// <returns>If the two pixel maps are the same</returns>
        public static bool Same(PixelMap map1, PixelMap map2)
        {
            // If either maps are empty, return
            if (map1 == null && map2 != null)
            {
                return(false);
            }

            // If either maps are empty, return
            if (map1 != null && map2 == null)
            {
                return(false);
            }

            // If both maps are empty, return true
            if (map1 == null && map2 == null)
            {
                return(true);
            }

            // If the pixel map sizes, color key, or use key are different, then these pixel maps are not the same
            if (map1.Width != map2.Width || map1.Height != map2.Height || map1.ColorKey != map2.ColorKey || map1.UseKey != map2.UseKey)
            {
                return(false);
            }

            // Check all the pixels for a mismatch
            for (int y = 0; y < map1.Pixels.GetLength(1); y++)
            {
                for (int x = 0; x < map1.Pixels.GetLength(0); x++)
                {
                    if (map1.Pixels[x, y] != map2.Pixels[x, y])
                    {
                        return(false);
                    }
                }
            }

            // The pixel maps are the same
            return(true);
        }
Exemple #5
0
        /// <summary>
        /// Sets the texture id.
        /// </summary>
        /// <param name="pixelMap">Pixel map to use as a texture.</param>
        private unsafe void SetTexture(PixelMap pixelMap)
        {
            // Original size.
            _width = pixelMap.Width;
            _height = pixelMap.Height;

            // Get a power of 2.
            if (_width > _height)
                _textureSize = MathMethods.Pow(_width, 2);
            else
                _textureSize = MathMethods.Pow(_height, 2);

            // Copy the pixel map and resize it.
            PixelMap copy = new PixelMap(_textureSize, _textureSize, Color.Black);
            copy.Paste(pixelMap, 0, 0);

            byte[] data = copy.ToOpenGLTexture();

            // Get texture pointer.
            fixed (uint* id = &_textureId) { OpenGL.glGenTextures(1, id); }

            // If a valid texture was created.
            if (_textureId != 0)
            {
                // Bind the new opengl texture.
                OpenGL.glBindTexture(GLTexture.Texture2D, _textureId);

                // Allocate some native memory to store data in.
                IntPtr textureData = Marshal.AllocHGlobal(data.Length);
                Marshal.Copy(data, 0, textureData, data.Length);

                // Set filters.
                OpenGL.glTexParameteri(GLTexture.Texture2D, GLTexParamPName.TextureMinFilter, GLTextureFilter.Nearest);
                OpenGL.glTexParameteri(GLTexture.Texture2D, GLTexParamPName.TextureMagFilter, GLTextureFilter.Nearest);

                // Place data into video memory.
                OpenGL.glTexImage2D(GLTexture.Texture2D, 0, GLInternalFormat.RGBA8, _textureSize, _textureSize, 0, GLPixelFormat.RGBA, GLDataType.UnsignedByte, textureData);

                // Deallocate native memory.
                Marshal.FreeHGlobal(textureData);
            }
        }
Exemple #6
0
        /// <summary>
        /// Pastes a pixel map at the desired position.
        /// </summary>
        /// <param name="pixelMap">The pixel map to paste.</param>
        /// <param name="x">The horizontal coordinate to start pasting.</param>
        /// <param name="y">The vertical coordinate to start pasting.</param>
        public void Paste(PixelMap pixelMap, int originX, int originY)
        {
            // Iterate through the pixels horizontally.
            for (int x = 0; x < pixelMap.Width; x++)
            {
                // Iterate through the pixels vertically.
                for (int y = 0; y < pixelMap.Height; y++)
                {
                    // Calculate index.
                    int posX = originX + x;
                    int posY = originY + y;

                    // Set pixel if within bounds.
                    if (posX >= 0 && posY >= 0 && posX < _width && posY < _height)
                        _pixels[posX, posY] = pixelMap.Pixels[x, y];
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Creates a shallow copy of the pixel map.
        /// </summary>
        /// <returns>A shallow copy of the pixel map.</returns>
        public PixelMap Clone()
        {
            // Clone pixel map.
            PixelMap map = new PixelMap((int[,])_pixels.Clone());
            map.ColorKey = _colorKey;
            map.UseKey = _useKey;

            // Return shallow copy.
            return map;
        }
Exemple #8
0
        /// <summary>
        /// Checks if two pixel maps are the same.
        /// </summary>
        /// <param name="map1">The first pixel map to check.</param>
        /// <param name="map2">The second pixel map to check.</param>
        /// <returns>If the two pixel maps are the same.</returns>
        public static bool Same(PixelMap map1, PixelMap map2)
        {
            // If either maps are empty, return.
            if (map1 == null || map2 == null)
                return false;

            // If the pixel map sizes are different, then these pixel maps are not the same.
            if (map1.Width != map2.Width || map1.Height != map2.Height)
                return false;

            // Check all the pixels for a mismatch.
            for (int y = 0; y < map1.Pixels.GetLength(1); y++)
                for (int x = 0; x < map1.Pixels.GetLength(0); x++)
                    if (map1.Pixels[x, y] != map2.Pixels[x, y])
                        return false;

            // The pixel maps are the same.
            return true;
        }