OpenBve.Renderer.DrawString C# (CSharp) Méthode

DrawString() private static méthode

Renders a string to the screen.
This function sets the OpenGL blend function to glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA).
private static DrawString ( Fonts font, string text, Point location, TextAlignment alignment, Color128 color ) : void
font Fonts The font to use.
text string The string to render.
location Point The location.
alignment TextAlignment The alignment.
color Color128 The color.
Résultat void
		private static void DrawString(Fonts.OpenGlFont font, string text, Point location, TextAlignment alignment, Color128 color)
		{
			GL.Enable(EnableCap.Blend);
			if (text == null)
			{
				return;
			}
			/*
			 * Prepare the top-left coordinates for rendering, incorporating the
			 * orientation of the string in relation to the specified location.
			 * */
			int left;
			if ((alignment & TextAlignment.Left) == 0)
			{
				int width = 0;
				for (int i = 0; i < text.Length; i++)
				{
					int texture;
					Fonts.OpenGlFontChar data;
					i += font.GetCharacterData(text, i, out texture, out data) - 1;
					width += data.TypographicSize.Width;
				}
				if ((alignment & TextAlignment.Right) != 0)
				{
					left = location.X - width;
				}
				else
				{
					left = location.X - width / 2;
				}
			}
			else
			{
				left = location.X;
			}
			int top;
			if ((alignment & TextAlignment.Top) == 0)
			{
				int height = 0;
				for (int i = 0; i < text.Length; i++)
				{
					int texture;
					Fonts.OpenGlFontChar data;
					i += font.GetCharacterData(text, i, out texture, out data) - 1;
					if (data.TypographicSize.Height > height)
					{
						height = data.TypographicSize.Height;
					}
				}
				if ((alignment & TextAlignment.Bottom) != 0)
				{
					top = location.Y - height;
				}
				else
				{
					top = location.Y - height / 2;
				}
			}
			else
			{
				top = location.Y;
			}
			/*
			 * Render the string.
			 * */
			GL.Enable(EnableCap.Texture2D);
			for (int i = 0; i < text.Length; i++)
			{
				int texture;
				Fonts.OpenGlFontChar data;
				i += font.GetCharacterData(text, i, out texture, out data) - 1;
				TextureManager.UseTexture(texture, TextureManager.UseMode.LoadImmediately);
				
				{
					GL.BindTexture(TextureTarget.Texture2D, TextureManager.Textures[texture].OpenGlTextureIndex);

					int x = left - (data.PhysicalSize.Width - data.TypographicSize.Width) / 2;
					int y = top - (data.PhysicalSize.Height - data.TypographicSize.Height) / 2;
					/*
					 * In the first pass, mask off the background with pure black.
					 * */
					GL.BlendFunc(BlendingFactorSrc.Zero, BlendingFactorDest.OneMinusSrcColor);
					GL.Begin(PrimitiveType.Polygon);
					GL.Color4(color.A, color.A, color.A, 1.0f);
					GL.TexCoord2(data.TextureCoordinates.Left, data.TextureCoordinates.Top);
					GL.Vertex2(x, y);
					GL.Color4(color.A, color.A, color.A, 1.0f);
					GL.TexCoord2(data.TextureCoordinates.Right, data.TextureCoordinates.Top);
					GL.Vertex2(x + data.PhysicalSize.Width, y);
					GL.Color4(color.A, color.A, color.A, 1.0f);
					GL.TexCoord2(data.TextureCoordinates.Right, data.TextureCoordinates.Bottom);
					GL.Vertex2(x + data.PhysicalSize.Width, y + data.PhysicalSize.Height);
					GL.Color4(color.A, color.A, color.A, 1.0f);
					GL.TexCoord2(data.TextureCoordinates.Left, data.TextureCoordinates.Bottom);
					GL.Vertex2(x, y + data.PhysicalSize.Height);
					GL.End();
					/*
					 * In the second pass, add the character onto the background.
					 * */
					GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
					GL.Begin(PrimitiveType.Polygon);
					GL.Color4(color.R, color.G, color.B, color.A);
					GL.TexCoord2(data.TextureCoordinates.Left, data.TextureCoordinates.Top);
					GL.Vertex2(x, y);
					GL.Color4(color.R, color.G, color.B, color.A);
					GL.TexCoord2(data.TextureCoordinates.Right, data.TextureCoordinates.Top);
					GL.Vertex2(x + data.PhysicalSize.Width, y);
					GL.Color4(color.R, color.G, color.B, color.A);
					GL.TexCoord2(data.TextureCoordinates.Right, data.TextureCoordinates.Bottom);
					GL.Vertex2(x + data.PhysicalSize.Width, y + data.PhysicalSize.Height);
					GL.Color4(color.R, color.G, color.B, color.A);
					GL.TexCoord2(data.TextureCoordinates.Left, data.TextureCoordinates.Bottom);
					GL.Vertex2(x, y + data.PhysicalSize.Height);
					GL.End();
				}
				left += data.TypographicSize.Width;
			}
			GL.Disable(EnableCap.Blend);
			GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // HACK //
		}

Same methods

Renderer::DrawString ( Fonts font, string text, Point location, TextAlignment alignment, Color128 color, bool shadow ) : void

Usage Example

Exemple #1
0
        //
        // DRAW MENU
        //
        /// <summary>Draws the current menu as a screen overlay</summary>
        internal void Draw()
        {
            int i;

            if (CurrMenu < 0 || CurrMenu >= Menus.Length)
            {
                return;
            }

            SingleMenu menu = Menus[CurrMenu];

            // overlay background
            GL.Color4(overlayColor.R, overlayColor.G, overlayColor.B, overlayColor.A);
            Renderer.RenderOverlaySolid(0.0, 0.0, (double)Screen.Width, (double)Screen.Height);
            GL.Color4(1.0f, 1.0f, 1.0f, 1.0f);

            // HORIZONTAL PLACEMENT: centre the menu in the main window
            int itemLeft = (Screen.Width - menu.ItemWidth) / 2;             // item left edge
            // if menu alignment is left, left-align items, otherwise centre them in the screen
            int itemX = (menu.Align & TextAlignment.Left) != 0 ? itemLeft : Screen.Width / 2;

            int menuBottomItem = menu.TopItem + visibleItems - 1;

            // draw the menu background
            GL.Color4(backgroundColor.R, backgroundColor.G, backgroundColor.B, backgroundColor.A);
            Renderer.RenderOverlaySolid(menuXmin - MenuBorderX, menuYmin - MenuBorderY,
                                        menuXmax + MenuBorderX, menuYmax + MenuBorderY);

            // if not starting from the top of the menu, draw a dimmed ellipsis item
            if (menu.Selection == menu.TopItem - 1 && !isCustomisingControl)
            {
                GL.Color4(highlightColor.R, highlightColor.G, highlightColor.B, highlightColor.A);
                Renderer.RenderOverlaySolid(itemLeft - MenuItemBorderX, menuYmin /*-MenuItemBorderY*/,
                                            itemLeft + menu.ItemWidth + MenuItemBorderX, menuYmin + em + MenuItemBorderY * 2);
            }
            if (menu.TopItem > 0)
            {
                Renderer.DrawString(MenuFont, "...", new Point(itemX, menuYmin),
                                    menu.Align, ColourDimmed, false);
            }
            // draw the items
            int itemY = topItemY;

            for (i = menu.TopItem; i <= menuBottomItem && i < menu.Items.Length; i++)
            {
                if (menu.Items[i] == null)
                {
                    continue;
                }
                if (i == menu.Selection)
                {
                    // draw a solid highlight rectangle under the text
                    // HACK! the highlight rectangle has to be shifted a little down to match
                    // the text body. OpenGL 'feature'?
                    GL.Color4(highlightColor.R, highlightColor.G, highlightColor.B, highlightColor.A);
                    Renderer.RenderOverlaySolid(itemLeft - MenuItemBorderX, itemY /*-MenuItemBorderY*/,
                                                itemLeft + menu.ItemWidth + MenuItemBorderX, itemY + em + MenuItemBorderY * 2);
                    // draw the text
                    Renderer.DrawString(MenuFont, menu.Items[i].Text, new Point(itemX, itemY),
                                        menu.Align, ColourHighlight, false);
                }
                else if (menu.Items[i] is MenuCaption)
                {
                    Renderer.DrawString(MenuFont, menu.Items[i].Text, new Point(itemX, itemY),
                                        menu.Align, ColourCaption, false);
                }
                else
                {
                    Renderer.DrawString(MenuFont, menu.Items[i].Text, new Point(itemX, itemY),
                                        menu.Align, ColourNormal, false);
                }
                itemY += lineHeight;
            }


            if (menu.Selection == menu.TopItem + visibleItems)
            {
                GL.Color4(highlightColor.R, highlightColor.G, highlightColor.B, highlightColor.A);
                Renderer.RenderOverlaySolid(itemLeft - MenuItemBorderX, itemY /*-MenuItemBorderY*/,
                                            itemLeft + menu.ItemWidth + MenuItemBorderX, itemY + em + MenuItemBorderY * 2);
            }
            // if not at the end of the menu, draw a dimmed ellipsis item at the bottom
            if (i < menu.Items.Length - 1)
            {
                Renderer.DrawString(MenuFont, "...", new Point(itemX, itemY),
                                    menu.Align, ColourDimmed, false);
            }
        }