コード例 #1
0
        /// <summary>
        /// Initializes a new TextEntity class.
        /// </summary>
        /// <param name="text">The Text.</param>
        /// <param name="font">The Font.</param>
        /// <param name="color">The Color.</param>
        /// <param name="wrapWidth">The Wordwrap Width.</param>
        public TextEntity(string text, OpenGLFont font, Color color, int wrapWidth = 0)
        {
            if (wrapWidth > 0)
            {
                text = text.WordWrap(wrapWidth);
            }
            var gdiFont = OpenGLHelper.ConvertFont(font);

            Id     = text.GetHashCode() + gdiFont.GetHashCode() + color.GetHashCode();
            _text  = text;
            _font  = gdiFont;
            _color = color;
        }
コード例 #2
0
ファイル: OpenGLRenderer.cs プロジェクト: szlatyka/Sharpex2D
        /// <summary>
        /// Measures the string.
        /// </summary>
        /// <param name="text">The String.</param>
        /// <param name="font">The Font.</param>
        /// <returns>Vector2.</returns>
        public Vector2 MeasureString(string text, IFont font)
        {
            var oglFont = font as OpenGLFont;

            if (oglFont == null)
            {
                throw new ArgumentException("Expected a OpenGLFont as resource.");
            }

            System.Drawing.Font gdiFont = OpenGLHelper.ConvertFont(oglFont);
            Size result = TextRenderer.MeasureText(text, gdiFont);

            return(new Vector2(result.Width, result.Height));
        }
コード例 #3
0
ファイル: OpenGLRenderer.cs プロジェクト: szlatyka/Sharpex2D
        /// <summary>
        /// Draws a Texture.
        /// </summary>
        /// <param name="texture">The Texture.</param>
        /// <param name="rectangle">The Rectangle.</param>
        /// <param name="opacity">The Opacity.</param>
        /// <param name="color">The Color.</param>
        public void DrawTexture(ITexture texture, Rectangle rectangle, Color color, float opacity = 1)
        {
            var tex = texture as OpenGLTexture;

            if (tex == null)
            {
                throw new ArgumentException("Expected OpenGLTexture as resource.");
            }
            OpenGLColor col = OpenGLHelper.ConvertColor(color);

            var vertices = new[]
            {
                //  Position                                         Color             Texcoords
                rectangle.X, rectangle.Y, col.R, col.G, col.B, 0.0f, 0.0f,                   // Top-left
                rectangle.X + rectangle.Width, rectangle.Y, col.R, col.G, col.B, 1.0f, 0.0f, // Top-right
                rectangle.X + rectangle.Width, rectangle.Y + tex.Height, col.R, col.G, col.B, 1.0f, 1.0f,
                // Bottom-right
                rectangle.X, rectangle.Y + rectangle.Height, col.R, col.G, col.B, 0.0f, 1.0f // Bottom-left
            };

            _sourceVbo.SetData(vertices);

            tex.Bind();

            _colorShader.SetUniform("dim", _graphicsDevice.BackBuffer.Width, _graphicsDevice.BackBuffer.Height, opacity);
            _colorShader.SetUniformMatrix("transform", _matrix4);

            uint posAttrib = _colorShader.GetAttribLocation("position");

            VertexBuffer.EnableVertexAttribArray(posAttrib);
            VertexBuffer.VertexAttribPointer(posAttrib, 2, false, 7 * sizeof(float), 0);

            uint colAttrib = _colorShader.GetAttribLocation("color");

            VertexBuffer.EnableVertexAttribArray(colAttrib);
            VertexBuffer.VertexAttribPointer(colAttrib, 3, false, 7 * sizeof(float), 2 * sizeof(float));

            uint texAttrib = _colorShader.GetAttribLocation("texcoord");

            VertexBuffer.EnableVertexAttribArray(texAttrib);
            VertexBuffer.VertexAttribPointer(texAttrib, 2, false, 7 * sizeof(float), 5 * sizeof(float));

            OpenGLInterops.DrawElements(OpenGLInterops.GL_TRIANGLES, 6, OpenGLInterops.GL_UNSIGNED_SHORT, IntPtr.Zero);

            tex.Unbind();
        }
コード例 #4
0
ファイル: OpenGLRenderer.cs プロジェクト: szlatyka/Sharpex2D
        /// <summary>
        /// Initializes the renderer.
        /// </summary>
        public void Initialize()
        {
            _renderContext.Initialize();
            _colorShader = new ShaderProgram();
            var vshader = new VertexShader();

            vshader.Compile(SimpleVertexShader.SourceCode);
            var fshader = new FragmentShader();

            fshader.Compile(SimpleFragmentShader.SourceCode);
            _colorShader.Link(vshader, fshader);
            OpenGLInterops.Enable(OpenGLInterops.GL_BLEND);
            OpenGLInterops.AlphaBlend();
            SetTransform(Matrix2x3.Identity);
            OpenGLColor clearColor = OpenGLHelper.ConvertColor(_graphicsDevice.ClearColor);

            OpenGLInterops.ClearColor(clearColor);
            _windowSize = _window.Size;
            OpenGLInterops.Viewport(0, 0, (int)_windowSize.X, (int)_windowSize.Y);
            _sourceVao = new VertexArray();
            _sourceVao.Bind();
            _colorShader.Bind();
            _sourceEbo = new IndexBuffer();

            var elements = new ushort[]
            {
                0, 1, 2,
                2, 3, 0
            };

            _sourceEbo.Bind();
            _sourceEbo.SetData(elements);

            _sourceVbo = new VertexBuffer();
            _sourceVbo.Bind();

            _graphicsDevice.ClearColorChanged += GraphicsDeviceClearColorChanged;
            _window.ScreenSizeChanged         += WindowScreenSizeChanged;
        }
コード例 #5
0
ファイル: OpenGLRenderer.cs プロジェクト: szlatyka/Sharpex2D
        /// <summary>
        /// Triggered if the clear color changed.
        /// </summary>
        private void GraphicsDeviceClearColorChanged(object sender, EventArgs e)
        {
            OpenGLColor clearColor = OpenGLHelper.ConvertColor(_graphicsDevice.ClearColor);

            OpenGLInterops.ClearColor(clearColor);
        }