Пример #1
0
        /// <summary>
        /// Renders a portion of the next frame.
        /// </summary>
        public override void RenderFrame()
        {
            var sb = new GlSpriteBatch(
                ActorAtlas,
                GlProgramId
                );

            // Render immobile actors
            //
            foreach (BrickActor brick in Game.GameState.Scene.ImmobileBricks)
            {
                ActorAnimationFrame currentFrame = brick.Animation.GetCurrentFrame();
                Rectanglei          blitRect     = ActorAtlas.GetSpriteUV(currentFrame.SpriteName);
                Point pointLoc = brick.Location
                                 .Product(Game.GameState.Scene.CellSize)
                                 .Add(currentFrame.Offset).Add(Origin.Product(Game.GameState.Scene.CellSize));
                Vector2i drawLoc = new Vector2i(
                    pointLoc.X, pointLoc.Y
                    );

                sb.Draw(
                    currentFrame.SpriteName,
                    new Rectanglei(
                        drawLoc,
                        blitRect.Size
                        )
                    );
            }

            sb.Finish();
        }
Пример #2
0
 public void Blit(Rectanglei source, Vector2i destPos, int scale, Buffer2D <T> dest)
 {
     for (int x = source.Left; x < source.Right + source.Width * (scale - 1); ++x)
     {
         if (x >= 0 && x < dest.Width)
         {
             for (int y = source.Top; y < source.Bottom + source.Height * (scale - 1); ++y)
             {
                 if (y >= 0 && y < dest.Height)
                 {
                     if (typeof(T) != typeof(uint))
                     {
                         dest.Set(x - source.Left + destPos.X, y - source.Top + destPos.Y, Get(x / scale, y / scale));
                     }
                     else
                     {
                         uint color = (uint)((object)Get(x / scale, y / scale));
                         if ((color & 0xFF000000) != 0x00)
                         {
                             dest.Set(x - source.Left + destPos.X, y - source.Top + destPos.Y, (T)(object)color);
                         }
                     }
                 }
             }
         }
     }
 }
Пример #3
0
        public static Texture2D LoadTexture(bool isServer, string name, Rectanglei rect)
        {
            if (!isServer || name == "level1.gif")
            {
                try {
                    Texture2D result = new Texture2D(rect.Width, rect.Height);

                    using (Bitmap bmp = new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream(name))) {
                        for (int x = rect.Left; x < rect.Right; ++x)
                        {
                            for (int y = rect.Top; y < rect.Bottom; ++y)
                            {
                                result.Set(x - rect.Left, y - rect.Top, (uint)bmp.GetPixel(x, y).ToArgb());
                            }
                        }
                    }

                    return(result);
                } catch {
                    return(new Texture2D(1, 1));
                }
            }

            return(new Buffer2D <uint>(1, 1));
        }
Пример #4
0
        /// <summary>
        /// Issues a draw command to this batch.
        /// </summary>
        /// <param name="spriteName">The name of the sprite.</param>
        /// <param name="rect">The rectangle space to draw the sprite at.</param>
        public void Draw(string spriteName, Rectanglei rect)
        {
            Rectanglei spriteRect = Atlas.GetSpriteUV(spriteName);

            VboDrawContents.AddRange(GlUtil.MakeVboData(rect));
            VboUvContents.AddRange(GlUtil.MakeVboData(spriteRect));

            VertexCount += 12;
        }
Пример #5
0
 public SpriteInfo(uint tex, Rectanglei rect, Rectangle uv, Color color,
                   float rotation, Vector2 origin)
 {
     Texture  = tex;
     UV       = uv;
     Rect     = rect;
     Color    = color;
     Rotation = rotation;
     Origin   = origin;
 }
Пример #6
0
        public virtual bool CollidesWithVector(Vector2 playerPos)
        {
            Rectanglei rect = new Rectanglei(X, Z, 1, 1);

            if (playerPos.X > rect.Left && playerPos.Y > rect.Top && playerPos.X < rect.Right && playerPos.Y < rect.Bottom)
            {
                return(true);
            }
            return(false);
        }
Пример #7
0
        public static Rectanglei GetMouseRect()
        {
            int x, y;

            Glfw.GetMousePos(out x, out y);
#if !DEBUG
            GlfwVidMode mode;
            Glfw.GetDesktopMode(out mode);
            x -= mode.Width / 2 - MainClass.WindowWidth / 2;
            y -= mode.Height / 2 - MainClass.WindowHeight / 2;
#endif
            Rectanglei mousePixel = new Rectanglei(x / MainClass.Scale, y / MainClass.Scale, 1, 1);
            return(mousePixel);
        }
Пример #8
0
        public void Draw(Texture tex, Rectangle rect, Rectangle?clip, Color color, float rotation, Vector2 origin)
        {
            Rectanglei dstRect = rect.ToRectaglei();
            Rectanglei clipI   = clip.HasValue ? clip.Value.ToRectaglei() : new Rectanglei(0, 0, tex.Width, tex.Height);

            float sheetWidth  = tex.Width;
            float sheetHeight = tex.Height;

            float uvX = clipI.X / sheetWidth;
            float uvY = (sheetHeight - clipI.Bottom) / sheetHeight;
            float uvR = clipI.Right / sheetWidth;
            float uvB = (sheetHeight - clipI.Y) / sheetHeight;

            BatchSprite(tex, dstRect, new Rectangle(uvX, uvY, uvR, uvB), color, rotation, origin);
        }
Пример #9
0
        /// <summary>
        /// Creates data from the specified <see cref="Rectanglei"/> that can be
        /// uploaded into a vertex buffer object.
        /// </summary>
        /// <param name="rect">The rectangle.</param>
        /// <returns>
        /// A float array that contains vertex data for the <see cref="Rectanglei"/>.
        /// </returns>
        public static float[] MakeVboData(Rectanglei rect)
        {
            // Expand the rectangle to coordinates
            //
            float[] rectPoints = new float[]
            {
                rect.Left, rect.Bottom,
                rect.Left, rect.Top,
                rect.Right, rect.Top,

                rect.Left, rect.Bottom,
                rect.Right, rect.Top,
                rect.Right, rect.Bottom
            };

            return(rectPoints);
        }
Пример #10
0
        void BatchSprite(Texture tex, Rectanglei rect, Rectangle uv, Color color, float rotation, Vector2 origin)
        {
            if (currentBatch.NumSprites >= SpriteVertexBuffer.MAX_SPRITES || (currentBatch.Texture != null && currentBatch.Texture != tex))
            {
                batchedSprites.Add(currentBatch);
                currentBatch = GetBatch();
                bi           = 0;
            }

            SpriteInfo si = new SpriteInfo(tex.Id, rect, uv, color, rotation, origin);

            if (currentBatch.Texture == null)
            {
                currentBatch.Texture = tex;
            }
            currentBatch.Sprites[bi++] = si;
            currentBatch.NumSprites++;
        }
Пример #11
0
        public static void CorrectErrors(Bitmap <Color4> output, Rectanglei region, Vector2 threshold)
        {
            List <Clash> clashes = new List <Clash>();
            int          w       = output.Width;
            int          h       = output.Height;

            int xStart = Math.Min(Math.Max(0, region.Left), output.Width);
            int yStart = Math.Min(Math.Max(0, region.Top), output.Height);
            int xEnd   = Math.Min(Math.Max(0, region.Right), output.Width);
            int yEnd   = Math.Min(Math.Max(0, region.Bottom), output.Height);

            for (int y = yStart; y < yEnd; y++)
            {
                for (int x = xStart; x < xEnd; x++)
                {
                    if ((x > 0 && PixelClash(output[x, y], output[x - 1, y], threshold.X)) ||
                        (x < w - 1 && PixelClash(output[x, y], output[x + 1, y], threshold.X)) ||
                        (y > 0 && PixelClash(output[x, y], output[x, y - 1], threshold.Y)) ||
                        (y < h - 1 && PixelClash(output[x, y], output[x, y + 1], threshold.Y)))
                    {
                        clashes.Add(new Clash {
                            x = x, y = y
                        });
                    }
                }
            }

            for (int i = 0; i < clashes.Count; i++)
            {
                Color4 pixel = output[clashes[i].x, clashes[i].y];
                float  med   = Median(pixel.r, pixel.g, pixel.b);
                pixel.r = med;
                pixel.g = med;
                pixel.b = med;
                output[clashes[i].x, clashes[i].y] = pixel;
            }
        }
        public RwGlTypeFaceBufferData GetVboForString(string text)
        {
            var characterRects = new List <Rectanglei>();
            var uvRects        = new List <Rectanglei>();

            int xOffset = 0;

            for (int i = 0; i < text.Length; i++)
            {
                char c = text[i];

                // Obtain character UV
                //
                if (!InternalCharacterMap.ContainsKey(c))
                {
                    LoadGlyphToTextureCache(c);
                }

                Rectanglei uvRect = InternalCharacterMap[c];

                if (uvRect.Width > 0 && uvRect.Height > 0)
                {
                    characterRects.Add(new Rectanglei(xOffset, 0, uvRect.Width, uvRect.Height));
                    uvRects.Add(uvRect);
                }

                xOffset += 32; // Temporary until character metrics are implemented
            }

            // Expand Rectanglei objects into floats for VBOs and return them
            //
            return(new RwGlTypeFaceBufferData(
                       ExpandRectangleiList(characterRects),
                       ExpandRectangleiList(uvRects)
                       ));
        }
Пример #13
0
            public static Vector3 UnProject(Vector3 winCoords, Matrix4 modelView, Matrix4 proj, Rectanglei viewport)
            {
                Matrix4 finalMatrix = Matrix4.Invert(modelView * proj);

                Vector4 inVec = new Vector4(winCoords, 1f);

                inVec.X = (inVec.X - viewport.X) / viewport.Width;
                inVec.Y = (inVec.Y - viewport.Y) / viewport.Height;

                inVec.X = inVec.X * 2 - 1;
                inVec.Y = inVec.Y * 2 - 1;
                inVec.Z = inVec.Z * 2 - 1;

                Vector4 outVec = Vector4.Transform(inVec, finalMatrix);

                if (Math.Abs(outVec.Z) < 10f * float.Epsilon)
                {
                    throw new Exception();
                }

                outVec.X /= outVec.W;
                outVec.Y /= outVec.W;
                outVec.Z /= outVec.W;
                return(outVec.Xyz);
            }
Пример #14
0
 public void Blit(Rectanglei source, Vector2i destPos, Buffer2D <T> dest)
 {
     Blit(source, destPos, 1, dest);
 }
Пример #15
0
 public static Texture2D LoadTexture(string name, Rectanglei rect)
 {
     return(LoadTexture(false, name, rect));
 }