Example #1
0
 public void SetRenderTarget(Texture target)
 {
     if (target == null)
     {
         GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
     }
     else
     {
         GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);
         GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, target.ID, 0);
         GL.Clear(ClearBufferMask.ColorBufferBit);
     }
 }
Example #2
0
        public void Load(Texture atlas, string declaration_file)
        {
            XmlTextReader r = new XmlTextReader(declaration_file);
            r.WhitespaceHandling = WhitespaceHandling.None;
            string[] tmp;
            Rectangle rectangle = Rectangle.Empty;
            this.tex = atlas;

            while (r.Read())
            {
                if (r.NodeType == XmlNodeType.Element && r.Name == "part")
                {
                    string name = r.GetAttribute("name");
                    string tl = r.GetAttribute("tl");
                    string br = r.GetAttribute("br");
                    string rect = r.GetAttribute("rect");
                    string border = r.GetAttribute("border");

                    if (tl != null)
                    {
                        tmp = tl.Split(' ');
                        rectangle.X = int.Parse(tmp[0]);
                        rectangle.Y = int.Parse(tmp[1]);
                    }
                    if (br != null)
                    {
                        tmp = br.Split(' ');
                        rectangle.Width = int.Parse(tmp[0]) - rectangle.X;
                        rectangle.Height = int.Parse(tmp[1]) - rectangle.Y;
                    }
                    if (rect != null)
                    {
                        tmp = rect.Split(' ');
                        rectangle.X = int.Parse(tmp[0]);
                        rectangle.Y = int.Parse(tmp[1]);
                        rectangle.Width = int.Parse(tmp[2]);
                        rectangle.Height = int.Parse(tmp[3]);
                    }
                    if (border != null)
                    {
                        int b = int.Parse(border);
                        rectangle.Inflate(-b, -b);
                    }

                    content.Add(name, rectangle);
                }
            }
        }
Example #3
0
 public void DrawTexture(Texture texture, RectangleF destinationRectangle, Color4 color)
 {
     DrawTexture(texture, destinationRectangle.X, destinationRectangle.Y, destinationRectangle.Width, destinationRectangle.Height, RectangleF.Empty, color);
 }
Example #4
0
 public void DrawTexture(Texture texture, Vector2 position, Color4 color)
 {
     DrawTexture(texture, position.X, position.Y, RectangleF.Empty, color);
 }
Example #5
0
 public void DrawTexture(Texture texture, Vector2 position, RectangleF sourceRectangle, Color4 color)
 {
     DrawTexture(texture, position.X, position.Y, sourceRectangle, color);
 }
Example #6
0
 public void DrawTexture(Texture texture, RectangleF destinationRectangle, RectangleF sourceRectangle, Color4 color,
                         float rotation, Vector2 origin,
                         bool flipHorizontally = false, bool flipVertically = false)
 {
     DrawTexture(texture, destinationRectangle.X, destinationRectangle.Y, destinationRectangle.Width, destinationRectangle.Height, sourceRectangle, color, rotation, origin.X, origin.Y, flipHorizontally, flipVertically);
 }
Example #7
0
 public void DrawTexture(Texture texture, Vector2 position, RectangleF sourceRectangle, Color4 color,
                         float rotation, Vector2 origin, Vector2 scale,
                         bool flipHorizontally = false, bool flipVertically = false)
 {
     DrawTexture(texture, position.X, position.Y, sourceRectangle, color, rotation, origin.X, origin.Y, scale.X, scale.Y, flipHorizontally, flipVertically);
 }
Example #8
0
 public void DrawTexture(Texture texture, float x, float y, RectangleF sourceRectangle, Color4 color,
                         float rotation = 0.0f, float xOrigin = 0.5f, float yOrigin = 0.5f, float xScale = 1, float yScale = 1,
                         bool flipHorizontally = false, bool flipVertically = false)
 {
     DrawTexture(texture, x, y,
         sourceRectangle != RectangleF.Empty ? sourceRectangle.Width * xScale : texture.Size.Width * xScale,
         sourceRectangle != RectangleF.Empty ? sourceRectangle.Height * yScale : texture.Size.Height * yScale,
         sourceRectangle, color, rotation, xOrigin, yOrigin, flipHorizontally, flipVertically);
 }
Example #9
0
 public void BlendWith(Texture tex)
 {
     back = tex;
 }
Example #10
0
 public void InitTextures()
 {
     _wallTile = CM.I.tex("wall_tile");
     _stairTile = CM.I.tex("stair_tile");
 }
Example #11
0
        public SpriteBatch()
        {
            dipQueue = new Queue<SpriteBatch.BatchItem>(25);

            vbuffer = new VertexBuffer(1024);
            Diagnostics.DiagnosticsCenter.Instance.Add(vbuffer);

            Shader defaultShader = new Shader();
            defaultMaterial = new Material (defaultShader);

            if (Shader.Version < 3.3f)
            {
                defaultShader.LoadVertexSource(@"#version 120
                uniform mat4 WPV;
                attribute vec2 vposition;
                attribute vec4 vcolor;
                attribute vec2 vtexcoord;
                varying vec4 fcolor;
                varying vec2 ftexcoord;
                void main(void)
                {
                    fcolor = vcolor;
                    ftexcoord = vtexcoord;
                    gl_Position = WPV * vec4(vposition, 0, 1);
                }");

                defaultShader.LoadFragmentSource(@"#version 120
                                          uniform sampler2D colorTexture;
                                          varying vec4 fcolor; varying vec2 ftexcoord;

                                          void main(void) { gl_FragColor = texture2D(colorTexture, ftexcoord) * fcolor; }");

            }
            else
            {
                defaultShader.LoadVertexSource(@"#version 330 core
                                    uniform mat4 WPV;
                                    in vec2 vposition; in vec4 vcolor; in vec2 vtexcoord;
                                    out vec4 fcolor; out vec2 ftexcoord;
                                    void main(void) {
                                    fcolor = vcolor;
                                    ftexcoord = vtexcoord;
                                    gl_Position = WPV * vec4(vposition, 0, 1);
                                    }");
                defaultShader.LoadFragmentSource(@"#version 330 core
                                          uniform sampler2D colorTexture;
                                          in vec4 fcolor; in vec2 ftexcoord;
                                          out vec4 color;
                                          void main(void) { color = texture(colorTexture, ftexcoord) * fcolor; }");
            }
            defaultShader.Link();
            BindShader(defaultShader, "vposition", "vcolor", "vtexcoord");

            int[] p = new int[4];
            GL.GetInteger(GetPName.Viewport, p);

            proj = Matrix4.CreateOrthographicOffCenter(p[0], p[2], p[3], p[1], 1, -1);

            pixelTex = new Texture();

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 0);

            float[] pix = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, 1, 1, 0,
               PixelFormat.Bgra, PixelType.Float, pix);
        }
Example #12
0
 public void DrawTexture(Texture texture, Material material, RectangleF destinationRectangle, RectangleF sourceRectangle, Color4 color)
 {
     DrawTexture(texture, material, destinationRectangle.X, destinationRectangle.Y, destinationRectangle.Width, destinationRectangle.Height, sourceRectangle, color);
 }
Example #13
0
        public unsafe void DrawTexture(Texture texture, Material material, float x, float y, float width, float height, RectangleF sourceRectangle, Color4 color,
            float rotation = 0.0f, float xOrigin = 0.5f, float yOrigin = 0.5f,
            bool flipHorizontally = false, bool flipVertically = false)
        {
            if (texture == null)
                throw new ArgumentException("texture");
            TryPush(texture, material, BeginMode.Triangles);

            if (sourceRectangle.IsEmpty)
            {
                tempRect.X = 0;
                tempRect.Y = 0;
                tempRect.Width = texture.Size.Width;
                tempRect.Height = texture.Size.Height;
            }
            else
                tempRect = sourceRectangle;

            texCoordTL = texture.GetTextureCoordinate(new Vector2(tempRect.X, tempRect.Y));
            texCoordBR = texture.GetTextureCoordinate(new Vector2(tempRect.Right, tempRect.Bottom));

            if (flipVertically)
            {
                float temp = texCoordBR.Y;
                texCoordBR.Y = texCoordTL.Y;
                texCoordTL.Y = temp;
            }
            if (flipHorizontally)
            {
                float temp = texCoordBR.X;
                texCoordBR.X = texCoordTL.X;
                texCoordTL.X = temp;
            }

            #region Add vertices

            float dx = -xOrigin * width;
            float dy = -yOrigin * height;

            float sin = 0;
            float cos = 1;

            if (rotation != 0)
            {
                sin = (float)Math.Sin(rotation);
                cos = (float)Math.Cos(rotation);
            }

            int offset = vbuffer.VertexOffset / vbuffer.Stride;
            int* ind = vbuffer.GetIndexPointerToFill(6);
            float* vert = vbuffer.GetVertexPointerToFill(4);

            *(ind++) = offset; *(ind++) = offset + 1; *(ind++) = offset + 2;
            *(ind++) = offset; *(ind++) = offset + 2; *(ind++) = offset + 3;

            *(vert++) = x + dx * cos - dy * sin;
            *(vert++) = y + dx * sin + dy * cos;
            *(vert++) = color.R; *(vert++) = color.G; *(vert++) = color.B; *(vert++) = color.A;
            *(vert++) = texCoordTL.X; *(vert++) = texCoordTL.Y;

            *(vert++) = x + (dx + width) * cos - dy * sin;
            *(vert++) = y + (dx + width) * sin + dy * cos;
            *(vert++) = color.R; *(vert++) = color.G; *(vert++) = color.B; *(vert++) = color.A;
            *(vert++) = texCoordBR.X; *(vert++) = texCoordTL.Y;

            *(vert++) = x + (dx + width) * cos - (dy + height) * sin;
            *(vert++) = y + (dx + width) * sin + (dy + height) * cos;
            *(vert++) = color.R; *(vert++) = color.G; *(vert++) = color.B; *(vert++) = color.A;
            *(vert++) = texCoordBR.X; *(vert++) = texCoordBR.Y;

            *(vert++) = x + dx * cos - (dy + height) * sin;
            *(vert++) = y + dx * sin + (dy + height) * cos;
            *(vert++) = color.R; *(vert++) = color.G; *(vert++) = color.B; *(vert++) = color.A;
            *(vert++) = texCoordTL.X; *(vert++) = texCoordBR.Y;

            #endregion Add vertices
        }
Example #14
0
 private bool TryPush(Texture tex, Material material, BeginMode mode)
 {
     if (last.texture != tex || last.mode != mode || last.material != material)
     {
         last = new BatchItem() { texture = tex, mode = mode, startIndex = vbuffer.IndexOffset, material = material };
         dipQueue.Enqueue(last);
         return true;
     }
     return false;
 }
Example #15
0
 public void End(Texture target, bool clear, bool use_back_buffer = false)
 {
     if (use_back_buffer)
     {
         if (clear)
             GL.Clear(ClearBufferMask.ColorBufferBit);
         End();
         GL.BindTexture(TextureTarget.Texture2D, target.ID);
         GL.CopyTexImage2D(TextureTarget.Texture2D, 0,PixelInternalFormat.Rgba, 0, 0, target.Size.Width, target.Size.Height, 0);
         return;
     }
     if (framebuffer == -1)
     {
         GL.GenFramebuffers(1, out framebuffer);
     }
     GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);
     GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, target.ID, 0);
     if (clear)
         GL.Clear(ClearBufferMask.ColorBufferBit);
     End();
     GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
 }
Example #16
0
        public void DrawTexture(Texture texture, float x, float y, float width, float height, RectangleF sourceRectangle, Color4 color,
                                float rotation = 0.0f, float xOrigin = 0.5f, float yOrigin = 0.5f,
                                bool flipHorizontally = false, bool flipVertically = false)
        {
            if (texture == null)
            {
                throw new ArgumentException("texture");
            }

            BatchItem item = GetFreeBatchItem();

            item.texture = (int)texture.ID;
            item.mode = BeginMode.Triangles;
            item.startIndex = vbuffer.IndexOffset;
            item.count = 6;

            if (sourceRectangle.IsEmpty)
            {
                tempRect.X = 0;
                tempRect.Y = 0;
                tempRect.Width = texture.Size.Width;
                tempRect.Height = texture.Size.Height;
            }
            else
                tempRect = sourceRectangle;

            texCoordTL = texture.GetTextureCoordinate(new Vector2(tempRect.X, tempRect.Y));
            texCoordBR = texture.GetTextureCoordinate(new Vector2(tempRect.Right, tempRect.Bottom));

            if (flipVertically)
            {
                float temp = texCoordBR.Y;
                texCoordBR.Y = texCoordTL.Y;
                texCoordTL.Y = temp;
            }
            if (flipHorizontally)
            {
                float temp = texCoordBR.X;
                texCoordBR.X = texCoordTL.X;
                texCoordTL.X = temp;
            }

            #region Add vertices

            float dx = -xOrigin * width;
            float dy = -yOrigin * height;
            float sin = (float)Math.Sin(rotation);
            float cos = (float)Math.Cos(rotation);

            int offset = vbuffer.VertexOffset / vbuffer.Stride;
            vbuffer.AddIndices(offset, offset + 1, offset + 2, offset, offset + 2, offset + 3);

            //              first
            vbuffer.AddVertices(4, x + dx * cos - dy * sin, y + dx * sin + dy * cos,
                                color.R, color.G, color.B, color.A, texCoordTL.X, texCoordTL.Y,
            //              second
                                x + (dx + width) * cos - dy * sin, y + (dx + width) * sin + dy * cos,
                                color.R, color.G, color.B, color.A, texCoordBR.X, texCoordTL.Y,
            //              third
                                x + (dx + width) * cos - (dy + height) * sin, y + (dx + width) * sin + (dy + height) * cos,
                                color.R, color.G, color.B, color.A, texCoordBR.X, texCoordBR.Y,
            //              fourth
                                x + dx * cos - (dy + height) * sin, y + dx * sin + (dy + height) * cos,
                                color.R, color.G, color.B, color.A, texCoordTL.X, texCoordBR.Y);

            #endregion Add vertices
        }
Example #17
0
        private static void LoadTextures(XmlNode main)
        {
            XmlNode texture = main.SelectSingleNode("Textures");

            string texturesBasePath = texture.Attributes["basePath"].Value;

            XmlNodeList textures = texture.SelectNodes("Texture");

            foreach (XmlNode tex in textures)
            {
                string texGameName = tex.Attributes["gameName"].Value;
                string texFileName = tex.Attributes["fileName"].Value;

                Texture t = new Texture(texturesBasePath + "//" + texFileName);
                _textures[texGameName] = t;
            }
        }
Example #18
0
 public void SetRenderTarget(Texture target)
 {
     if (target == null)
     {
         if (Capabilities.Framebuffers == GLExtensionSupport.Core)
             GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
         else
             GL.Ext.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
     }
     else
     {
         if (Capabilities.Framebuffers == GLExtensionSupport.Core)
         {
             GL.BindFramebuffer(FramebufferTarget.Framebuffer, _framebuffer);
             GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, target.ID, 0);
         }
         else
         {
             GL.Ext.BindFramebuffer(FramebufferTarget.Framebuffer, _framebuffer);
             GL.Ext.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, target.ID, 0);
         }
         GL.Clear(ClearBufferMask.ColorBufferBit);
     }
 }