Пример #1
0
        /// <see cref="UISpriteManagerBase.LoadSprite"/>
        public override UISprite LoadSprite(byte[] imageData, RCIntVector pixelSize)
        {
            if (this.ObjectDisposed)
            {
                throw new ObjectDisposedException("XnaSpriteManager");
            }
            if (null == imageData)
            {
                throw new ArgumentNullException("imageData");
            }
            if (pixelSize == RCIntVector.Undefined)
            {
                throw new ArgumentNullException("pixelSize");
            }
            if (pixelSize.X <= 0 || pixelSize.Y <= 0)
            {
                throw new ArgumentOutOfRangeException("pixelSize");
            }

            /// Load the bitmap from the given byte array.
            Stream byteStream   = new MemoryStream(imageData);
            Bitmap loadedBitmap = (Bitmap)Image.FromStream(byteStream);

            byteStream.Close();
            if (loadedBitmap.PixelFormat != PixelFormat.Format24bppRgb)
            {
                throw new ArgumentException("Pixel format of the given Bitmap must be PixelFormat.Format24bppRgb",
                                            "originalBmp");
            }

            /// Create a new bitmap and copy the original to this new with the given pixel size.
            Bitmap scaledBitmap = new Bitmap(loadedBitmap.Width * pixelSize.X,
                                             loadedBitmap.Height * pixelSize.Y,
                                             PixelFormat.Format24bppRgb);

            XnaBitmapUtils.CopyBitmapScaled(loadedBitmap, scaledBitmap, new RCIntVector(1, 1), pixelSize);
            loadedBitmap.Dispose();

            lock (this.lockObj)
            {
                /// Create the XnaSprite object and register it to this sprite manager.
                XnaSprite newSprite = new XnaSprite(scaledBitmap, pixelSize, this.platform);
                this.sprites.Add(newSprite);

                TraceManager.WriteAllTrace("XnaSpriteManager.LoadSprite: Sprite created", XnaTraceFilters.INFO);
                return(newSprite);
            }
        }
Пример #2
0
        /// <see cref="UISpriteManagerBase.CloseRenderContext"/>
        public override void CloseRenderContext(UISprite sprite)
        {
            if (this.ObjectDisposed)
            {
                throw new ObjectDisposedException("XnaSpriteManager");
            }

            lock (this.lockObj)
            {
                XnaSprite target = (XnaSprite)sprite;
                if (!this.renderContexts.ContainsKey(target))
                {
                    throw new UIException("The given sprite doesn't have active render context!");
                }
                XnaSpriteRenderContext targetContext = this.renderContexts[target];
                this.renderContexts.Remove(target);
                targetContext.Close();
                TraceManager.WriteAllTrace("XnaSpriteManager.CloseRenderContext: Render context for sprite closed", XnaTraceFilters.INFO);
            }
        }
Пример #3
0
        /// <see cref="UISpriteManagerBase.CreateRenderContext_i"/>
        protected override IUIRenderContext CreateRenderContext_i(UISprite sprite)
        {
            if (this.ObjectDisposed)
            {
                throw new ObjectDisposedException("XnaSpriteManager");
            }

            lock (this.lockObj)
            {
                XnaSprite target = (XnaSprite)sprite;
                if (this.renderContexts.ContainsKey(target))
                {
                    throw new UIException("The given sprite has already an active render context!");
                }
                XnaSpriteRenderContext targetContext = new XnaSpriteRenderContext(target, this);
                this.renderContexts.Add(target, targetContext);
                TraceManager.WriteAllTrace("XnaSpriteManager.CreateRenderContext: Render context for sprite created", XnaTraceFilters.INFO);
                return(targetContext);
            }
        }
        /// <see cref="IUIRenderContext.RenderSprite"/>
        public void RenderSprite(UISprite sprite, RCIntVector position, RCIntRectangle section)
        {
            if (section == RCIntRectangle.Undefined)
            {
                this.RenderSprite(sprite, position);
                return;
            }

            if (this.isClosed)
            {
                throw new UIException("Render context unavailable!");
            }
            if (sprite == null)
            {
                throw new ArgumentNullException("sprite");
            }
            if (position == RCIntVector.Undefined)
            {
                throw new ArgumentNullException("position");
            }

            XnaSprite srcSprite  = (XnaSprite)sprite;
            Bitmap    sectionBmp = new Bitmap(section.Width * this.targetSprite.PixelSize.X,
                                              section.Height * this.targetSprite.PixelSize.Y,
                                              PixelFormat.Format24bppRgb);

            XnaBitmapUtils.CopyBitmapScaled(srcSprite.RawBitmap, sectionBmp,
                                            sprite.PixelSize, this.targetSprite.PixelSize,
                                            section, new RCIntVector(0, 0));
            if (srcSprite.TransparentColor != RCColor.Undefined)
            {
                sectionBmp.MakeTransparent(Color.FromArgb(srcSprite.TransparentColor.R,
                                                          srcSprite.TransparentColor.G,
                                                          srcSprite.TransparentColor.B));
            }
            this.targetGC.DrawImageUnscaled(sectionBmp,
                                            position.X * this.targetSprite.PixelSize.X,
                                            position.Y * this.targetSprite.PixelSize.Y);
            sectionBmp.Dispose();
        }
Пример #5
0
        /// <summary>
        /// Internal method to render a sprite in order to clip it with the clip rectangle.
        /// </summary>
        /// <param name="sprite">The sprite to render.</param>
        /// <param name="position">The position where to render in screen coordinates.</param>
        /// <param name="absSection">
        /// The section of the sprite to render in the coordinate-system of the XNA-texture.
        /// </param>
        private void RenderSpriteWithClip(XnaSprite sprite, RCIntVector position, RCIntRectangle absSection)
        {
            /// Compute the clipped section in the coordinate-system of the XNA-texture.
            RCIntRectangle clippedSection = new RCIntRectangle(this.Clip.Location - position + absSection.Location,
                                                               this.Clip.Size);

            clippedSection.Intersect(absSection);

            if (clippedSection != RCIntRectangle.Undefined)
            {
                Microsoft.Xna.Framework.Rectangle srcRect =
                    new Microsoft.Xna.Framework.Rectangle(clippedSection.X,
                                                          clippedSection.Y,
                                                          clippedSection.Width,
                                                          clippedSection.Height);
                this.implementation.SpriteBatch.Draw(sprite.XnaTexture,
                                                     new Vector2((float)position.X + (float)clippedSection.X - (float)absSection.X,
                                                                 (float)position.Y + (float)clippedSection.Y - (float)absSection.Y),
                                                     srcRect,
                                                     Microsoft.Xna.Framework.Color.White);
            }
        }
Пример #6
0
        /// <see cref="IUISpriteManager.CreateSprite"/>
        public override UISprite CreateSprite(RCColor color, RCIntVector spriteSize, RCIntVector pixelSize)
        {
            if (this.ObjectDisposed)
            {
                throw new ObjectDisposedException("XnaSpriteManager");
            }
            if (color == RCColor.Undefined)
            {
                throw new ArgumentNullException("color");
            }
            if (spriteSize == RCIntVector.Undefined)
            {
                throw new ArgumentNullException("spriteSize");
            }
            if (pixelSize == RCIntVector.Undefined)
            {
                throw new ArgumentNullException("pixelSize");
            }

            /// Create the empty bitmap and fill with the given color
            Bitmap   emptyBitmap = new Bitmap(spriteSize.X * pixelSize.X, spriteSize.Y * pixelSize.Y, PixelFormat.Format24bppRgb);
            Graphics gc          = Graphics.FromImage(emptyBitmap);

            gc.Clear(Color.FromArgb(color.R, color.G, color.B));
            gc.Dispose();

            lock (this.lockObj)
            {
                /// Create the XnaSprite object and register it to this sprite manager.
                XnaSprite newSprite = new XnaSprite(emptyBitmap, pixelSize, this.platform);
                this.sprites.Add(newSprite);

                TraceManager.WriteAllTrace("XnaSpriteManager.CreateSprite: Sprite created", XnaTraceFilters.INFO);
                return(newSprite);
            }
        }
Пример #7
0
        /// <see cref="UISpriteManagerBase.DestroySprite"/>
        public override void DestroySprite(UISprite sprite)
        {
            if (this.ObjectDisposed)
            {
                throw new ObjectDisposedException("XnaSpriteManager");
            }

            lock (this.lockObj)
            {
                XnaSprite spriteToDestroy = (XnaSprite)sprite;
                if (this.renderContexts.ContainsKey(spriteToDestroy))
                {
                    throw new UIException("The given sprite still has active render context!");
                }

                /// Remove the sprite from the list and destroy it.
                if (!this.sprites.Remove(spriteToDestroy))
                {
                    throw new UIException("The given sprite has already been disposed or has not been created by this sprite manager!");
                }
                spriteToDestroy.Dispose();
                TraceManager.WriteAllTrace("XnaSpriteManager.DestroySprite: Sprite destroyed", XnaTraceFilters.INFO);
            }
        }
Пример #8
0
        /// <see cref="UISpriteManagerBase.ShrinkSprite"/>
        public override UISprite ShrinkSprite(UISprite sprite, RCIntVector spriteSize, RCIntVector pixelSize)
        {
            if (this.ObjectDisposed)
            {
                throw new ObjectDisposedException("XnaSpriteManager");
            }
            if (spriteSize == RCIntVector.Undefined)
            {
                throw new ArgumentNullException("spriteSize");
            }
            if (pixelSize == RCIntVector.Undefined)
            {
                throw new ArgumentNullException("pixelSize");
            }

            lock (this.lockObj)
            {
                /// Search the sprite in the list.
                XnaSprite spriteToShrink = (XnaSprite)sprite;

                /// Create a copy of the original bitmap with pixelsize (1, 1) if necessary.
                Bitmap bitmapToShrink;
                if (spriteToShrink.PixelSize != new RCIntVector(1, 1))
                {
                    bitmapToShrink = new Bitmap(spriteToShrink.Size.X, spriteToShrink.Size.Y, PixelFormat.Format24bppRgb);
                    XnaBitmapUtils.CopyBitmapScaled(spriteToShrink.RawBitmap, bitmapToShrink, spriteToShrink.PixelSize, new RCIntVector(1, 1));
                }
                else
                {
                    bitmapToShrink = spriteToShrink.RawBitmap;
                }

                /// Create the shrinked bitmap with pixelsize (1, 1).
                Bitmap   shrinkedBitmap = new Bitmap(spriteSize.X, spriteSize.Y, PixelFormat.Format24bppRgb);
                Graphics gc             = Graphics.FromImage(shrinkedBitmap);
                gc.InterpolationMode = InterpolationMode.NearestNeighbor;
                gc.DrawImage(bitmapToShrink, new Rectangle(0, 0, spriteSize.X, spriteSize.Y), new Rectangle(0, 0, bitmapToShrink.Width, bitmapToShrink.Height), GraphicsUnit.Pixel);
                gc.Dispose();

                /// Scale the shrinked bitmap to the target pixel size if necessary.
                Bitmap scaledShrinkedBitmap;
                if (pixelSize != new RCIntVector(1, 1))
                {
                    scaledShrinkedBitmap = new Bitmap(shrinkedBitmap.Width * pixelSize.X,
                                                      shrinkedBitmap.Height * pixelSize.Y,
                                                      PixelFormat.Format24bppRgb);
                    XnaBitmapUtils.CopyBitmapScaled(shrinkedBitmap, scaledShrinkedBitmap, new RCIntVector(1, 1), pixelSize);
                }
                else
                {
                    scaledShrinkedBitmap = shrinkedBitmap;
                }

                /// Create the XnaSprite object and register it to this sprite manager.
                XnaSprite shrinkedSprite = new XnaSprite(scaledShrinkedBitmap, pixelSize, this.platform);
                shrinkedSprite.TransparentColor = sprite.TransparentColor;
                this.sprites.Add(shrinkedSprite);

                /// Cleanup if necessary.
                if (bitmapToShrink != spriteToShrink.RawBitmap)
                {
                    bitmapToShrink.Dispose();
                }
                if (shrinkedBitmap != scaledShrinkedBitmap)
                {
                    shrinkedBitmap.Dispose();
                }

                TraceManager.WriteAllTrace("XnaSpriteManager.ShrinkedSprite: Sprite shrinked", XnaTraceFilters.INFO);
                return(shrinkedSprite);
            }
        }