예제 #1
0
파일: Renderer.cs 프로젝트: cadahl/defense
        public Renderer()
        {
            int sw = 1024;
            int sh = 768;

            Window = new GameWindow(sw,sh,OpenTK.Graphics.GraphicsMode.Default,"Client",GameWindowFlags.Default,DisplayDevice.Default, 2, 1, OpenTK.Graphics.GraphicsContextFlags.Default);

            //Window.VSync = VSyncMode.On;
            Window.UpdateFrame += HandleWindowUpdateFrame;
            Window.RenderFrame += HandleWindowRenderFrame;

            Tilemaps = new Dictionary<string, Tilemap> ();
            Backgrounds = new Dictionary<int, Background> ();
            ShaderCache = new Dictionary<int, int>();
            Materials = new Dictionary<string, Material>();
            _drawList = new List<Drawable> ();

            _vb = new VertexBuffer();

            GL.ShadeModel (ShadingModel.Smooth);
            GL.Disable (EnableCap.CullFace);
            GL.Enable (EnableCap.Texture2D);
            GL.Disable (EnableCap.DepthTest);
            GL.DepthMask(false);
            GL.Enable (EnableCap.Blend);
            GL.BlendFunc (BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
            //GL.Enable (EnableCap.LineSmooth);
            //GL.Hint (HintTarget.LineSmoothHint, HintMode.Nicest);
            GL.PixelStore (PixelStoreParameter.UnpackAlignment, 1);
            GL.ClearColor (0.3f, 0.3f, 0.3f, 1f);
            GL.CullFace (CullFaceMode.Back);
            GL.Disable(EnableCap.AlphaTest);

            _rtt = new RenderToTexture(Width, Height);
            _compositingMaterial = GetMaterial("distort");

            ZoomLevel = 1.0f;

            Window.Resize += (sender, e) =>
            {
                GL.Viewport (0, 0, Window.ClientSize.Width, Window.ClientSize.Height);
                GL.MatrixMode (MatrixMode.Projection);
                GL.LoadIdentity ();
                GL.Ortho (0, Window.ClientSize.Width*ZoomLevel, Window.ClientSize.Height*ZoomLevel, 0, -100.0, 100.0);
            };

            Window.Unload += (sender, e) =>
            {
                var textures = (from t in Tilemaps.Values select t.Texture).ToArray();
                GL.DeleteTextures(textures.Length, textures);

                foreach(var m in Materials.Values)
                    m.Dispose();

                _rtt.Dispose();
                _compositingMaterial.Dispose();
            };
        }
        internal void Flush(DrawState state)
        {
            if (_currentVertex == 0)
                return;

            GraphicsDevice device = state.GraphicsDevice;

            using (var vertexBuffer = new VertexBuffer(device, VertexPositionTexture.VertexDeclaration, _vertices.Length, BufferUsage.None))
            {
                using (var indexBuffer = new IndexBuffer(device, IndexElementSize.SixteenBits, _quadIndices.Length, BufferUsage.None))
                {
                    vertexBuffer.SetData(_vertices, 0, _vertices.Length);
                    indexBuffer.SetData(_quadIndices, 0, _quadIndices.Length);

                    device.Indices = indexBuffer;
                    device.SetVertexBuffer(vertexBuffer);

                    for (int i = 0; i < _textures.Count; i++)
                    {
                        device.Textures[0] = _textures[i];
                        device.DrawIndexedPrimitives(PrimitiveType.TriangleList, i * 4, 0, 4, 0, 2);
                    }

                    device.SetVertexBuffer(null);
                    device.Indices = null;
                }
            }

            _currentVertex = 0;
            _textures.Clear();
        }