Graphics
Description
- Derived from
-
Disposable abstract
IBeginEnd
ILineThickness
The Graphics class provides methods for performing pixel-perfect 2D drawing.
Pixel coordinates always refer to the center of a pixel, e.g. the pixel coordinates (0|0)
refer to the center of the top-left pixel in the viewport. The center of the lower-right pixel is at (ScreenWidth-1|ScreenHeight-1)
. Names of method parameters that take pixel coordinates have the 'pixel'
prefix (e.g. 'pixelX'
).
Screen coordinates refer to the whole viewport: the screen coordinates (0|0)
refer to the top-left corner of the top-left pixel in the viewport. The lower-right corner of the lower-right pixel is at (ScreenWidth|ScreenHeight)
. Names of method parameters that take screen coordinates have the 'screen'
prefix (e.g. 'screenX'
).
The Flush method may be called to make sure that all buffered work gets submitted. When this method has returned, all used resources may be disposed, without any impact on 2D rendering.
The Draw*
methods allow passing of null
resources (e.g. Bitmap, ITexture2D, PixelFont). In this case, these methods draw nothing and return silently.
The members of this class may be grouped by their usage:
-
Render cycle control, resource management and retrieval of general information:
Bounds / Fullscreen
IBeginEnd.Begin / Flush / Reset / IBeginEnd.End
Context, ILineFeatureLevels.FeatureLevel, Cache, Loader
ScreenBounds / ScreenHeight / ScreenSize / ScreenWidth -
Static helpers for rendering:
ComputeAnchorX1
ComputeAnchorY1
FindAnchor
FlipAnchor -
Render state control:
Clear / Clip / PushClip1 / PopClip / TestClip
ColorChannels / ILineThickness.LineThickness / Mask / Offset
PushDepth / PopDepth
Translate1 / Translation -
Rendering of bitmaps:
CreateBitmap / LoadBitmapFrom1
DrawBitmap1 / DrawBitmapOutline
DrawBitmapPart1
DrawBitmapStretch / DrawBitmapTransform -
Rendering of text:
DefaultFont / LoadFontFrom
DrawChar
DrawString
DrawStringOutline -
Rendering of geometric shapes:
DrawCircle
DrawLine / DrawLineHorizontal / DrawLineVertical
DrawRectangle1 / DrawRoundRectangle1
DrawPolygon1 / DrawPolyline / DrawShape1 -
Rendering of custom primitives:
CreatePrimitiveBuffer / Write
DrawPrimitivePrepare / DrawPrimitive
DrawPrimitiveBegin / DrawPrimitiveVertex1 / DrawPrimitiveEnd
Public / Constructors
Graphics
Creates a new instance of Graphics.
The recommended way to obtain an instance of this class is by querying the shared graphics object of the graphics context (see IGraphicsContext.Graphics).
- RenderException
-
If a graphics subsystem error has occurred.
Public / Methods
ColorChannels
Specifies the color channel sources for 2D rendering.
Color channel values are sourced from the texture samples. When rendering without textures, an opaque white texture sample is used instead. Modulation colors that have been passed to render methods will be applied after the color sources are applied.
ComputeAnchorX
2 overloads
Computes the position for the given anchor.
Computes the position for the given anchor.
ComputeAnchorY
2 overloads
Computes the position for the given anchor.
Computes the position for the given anchor.
CreateBitmap
Creates a new image resource.
- RenderException
-
If a graphics subsystem error has occurred.
CreatePrimitiveBuffer
Creates a vertex buffer that can be used to draw batched 2D primitives.
The returned buffer has the following layout:
-
Byte offset #0: Screen X-coordinate (32-bit floating point).
-
Byte offset #4: Screen Y-coordinate (32-bit floating point).
-
Byte offset #8: Depth coordinate (32-bit floating point).
-
Byte offset #12: Color value (32-bit integer, A8R8G8B8).
-
Byte offset #16: Texture U-coordinate (32-bit floating point).
-
Byte offset #20: Texture V-coordinate (32-bit floating point).
-
Byte offset #24: Unused padding bytes (should be 0).
-
Byte offset #32: End of vertex data (exclusive).
Different layouts may be used, as long as the following vertex elements are present:
-
VertexElementUsage.Position as VertexElementType.Float with a VertexElement.Size of 3 (screen and depth coordinates).
-
VertexElementUsage.Color with a VertexElement.Size of 4 and VertexElementFlags.Srgb (color value).
-
VertexElementUsage.TextureCoords as VertexElementType.Float with a VertexElement.Size of 2 (texture coordinates).
- RenderException
-
If a graphics subsystem error has occurred.
DrawBitmap
2 overloads
Draws a bitmap image onto the screen.
- See also
Draws a bitmap image onto the screen.
- See also
DrawBitmapPart
2 overloads
Draws a bitmap image onto the screen.
- See also
Draws a bitmap image onto the screen.
- See also
DrawBitmapTransform
Draws a bitmap image onto the screen.
First the untransformed bitmap bounds are computed from the given image anchor. Then the given transformation matrix is applied to the corner vertices of the bitmap bounds. Finally, a textured quad is rendered using the transformed corner vertices.
DrawPrimitive
Draws a batched 2D primitive.
If an index buffer has been specified to DrawPrimitivePrepare, the given first in and count in parameters refer to elements in the index buffer. Otherwise, they refer to vertices in the 2D vertex buffer.
DrawPrimitiveBegin
Begins to render a 2D primitive which vertices are specified via the Vertex(…)
method.
DrawPrimitivePrepare
Prepares to render batched 2D primitives.
After all batched primitives have been submitted, this method should be called to set the geometry buffer to null
. Performing any other subsequent non-batched rendering will have the same effect.
DrawPrimitiveVertex
4 overloads
Specifies the next vertex for the current 2D primitive.
Specifies the next vertex for the current 2D primitive.
Specifies the next vertex for the current 2D primitive.
Specifies the next vertex for the current 2D primitive.
DrawRoundRectangle
2 overloads
Draws a rectangle with round corners.
- See also
Draws a rectangle with round corners.
DrawShape
2 overloads
Draws the given vector shape (see IShape.EdgeAt).
Shape triangles (see IShape.TriangleAt) will be drawn with the given colorFill opt. Shape edges (see IShape.EdgeAt will be drawn with the given colorBorder in. Aggregated shapes (see IShape.ShapeAt) will be drawn by calling this method recursively. Shape vertex coordinates are interpreted as screen coordinates.
Draws the given vector shape.
Shape triangles (see IShape.TriangleAt) will be drawn with the given colorFill opt. Shape edges (see IShape.EdgeAt will be drawn with the given colorBorder in. Aggregated shapes (see IShape.ShapeAt) will be drawn by calling this method recursively.
Flush
Flushes buffered render commands to the GPU.
Usually it is not necessary to call this method, because a flush automatically happens when IBeginEnd.End is called.
Fullscreen
Sets the Bounds of this Graphics object to full-screen (i.e. the size of the current render target, see IGraphicsContext.Size).
LoadBitmapFrom
2 overloads
Loads an image resource from the given file.
This method will always perform sRGB decompression on file in.
- IOException
-
If an I/O error has occurred.
- ValidatingException
-
If the image reader has reported an error, for example because of an unsupported file format feature.
- RenderException
-
If a graphics subsystem error has occurred.
Loads an image resource from the given image descriptor.
- IOException
-
If an I/O error has occurred.
- ValidatingException
-
If the image reader has reported an error, for example because of an unsupported file format feature.
- RenderException
-
If a graphics subsystem error has occurred.
LoadFontFrom
Loads a pixel font resource from the given image file.
- IOException
-
If an I/O error has occurred.
- ValidatingException
-
If the given image is not a valid pixel font, see PixelFontBuilder.
- RenderException
-
If a graphics subsystem error has occurred.
- See also
Mask
Specifies an opacity mask to use for subsequent rendering.
The opacity mask is enabled by passing a non-null texture opt and non-empty screen opt bounds. While enabled, the mask will affect rendering in the following way:
-
For each rendered pixel, the screen coordinates of the pixel center are transformed with a matrix which maps the screen coordinates given by Box2F.TopLeft and Box2F.BottomRight of screen opt to the texture coordinates (0,0) and (1,1).
-
The given texture opt is sampled at the computed texture coordinates, using SamplerStatePreset.LinearClamp. The red channel of the texture sample is used as the mask opacity, the other channels are ignored.
-
Applying the color channels (see ColorChannels) yields the pixel color. The alpha channel of the pixel color is modulated with the mask opacity.
PushClip
2 overloads
Pushes the current clip rectangle onto the stack, intersects it with the given rectangle and sets the clip rectangle to the result.
- See also
Pushes the current clip rectangle onto the clip stack and intersects it with the given rectangle.
The current Translation is applied to pixelX in and pixelY in.
- See also
Reset
Resets the render state to default values.
This method is called automatically when the outermost call to IBeginEnd.Begin is made and when Bounds is set between calls to IBeginEnd.Begin and IBeginEnd.End. The following render state is reset:
-
The viewport of the graphics context is set to Bounds, see IGraphicsContext.SetViewport.
-
The clip rectangle is set to the bounds and the clip stack is cleared, see PushClip1.
-
The depth value is reset to
0
and the depth value stack is cleared, see PushDepth. -
The translation is reset to zero, see Translation.
-
The subpixel offset is reset to zero, see Offset.
-
The line thickness is reset to one, see ILineThickness.LineThickness.
-
The color channel swizzle is reset to default, see ColorChannels.
Public / Attributes
Cache
The shared resource cache of this Graphics instance.
Resources that have been obtained via IResourceCache.GetNull will be disposed when this Graphics object is disposed.
Offset
Specifies the sub-pixel offset for subsequent rendering of pixel-aligned visuals.
Only the fractional parts of the components of the given vector are used, the integral parts are set to zero.
Draw methods that take integer coordinates make use of the sub-pixel offset. Namely, these are:
Please note that draw methods that use pixel fonts do not apply the sub-pixels offset.