📄 graphics.cs
字号:
verts[0].Tu = tex_rect.Right;
verts[0].Tv = tex_rect.Top;
verts[1].X = dest.Right;
verts[1].Y = dest.Bottom;
verts[1].Z = 0.5f;
verts[1].Rhw = 1;
verts[1].Tu = tex_rect.Right;
verts[1].Tv = tex_rect.Bottom;
verts[2].X = dest.Left;
verts[2].Y = dest.Top;
verts[2].Z = 0.5f;
verts[2].Rhw = 1;
verts[2].Tu = tex_rect.Left;
verts[2].Tv = tex_rect.Top;
verts[3].X = dest.Left;
verts[3].Y = dest.Bottom;
verts[3].Z = 0.5f;
verts[3].Rhw = 1;
verts[3].Tu = tex_rect.Left;
verts[3].Tv = tex_rect.Bottom;
}
else
{
verts[0].X = dest.Right;
verts[0].Y = dest.Top;
verts[0].Z = 0.5f;
verts[0].Rhw = 1;
verts[0].Tu = tex_rect.Left;
verts[0].Tv = tex_rect.Top;
verts[1].X = dest.Right;
verts[1].Y = dest.Bottom;
verts[1].Z = 0.5f;
verts[1].Rhw = 1;
verts[1].Tu = tex_rect.Left;
verts[1].Tv = tex_rect.Bottom;
verts[2].X = dest.Left;
verts[2].Y = dest.Top;
verts[2].Z = 0.5f;
verts[2].Rhw = 1;
verts[2].Tu = tex_rect.Right;
verts[2].Tv = tex_rect.Top;
verts[3].X = dest.Left;
verts[3].Y = dest.Bottom;
verts[3].Z = 0.5f;
verts[3].Rhw = 1;
verts[3].Tu = tex_rect.Right;
verts[3].Tv = tex_rect.Bottom;
}
stm.Write(verts);
vbTex.Unlock();
// bind the texture to next drawing operation
device.SetTexture(0, dx_bmp.Texture);
// bind the vertex data to the next drawing operation
device.SetStreamSource(0, vbTex, 0);
#if DESKTOP
device.VertexFormat =
CustomVertex.TransformedTextured.Format;
#endif
// perform the draw
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
}
/// <summary>
/// Draw a filled rectangle to the back buffer.
/// </summary>
/// <param name="r">Rectangle to be filled</param>
/// <param name="c">Color of rectangle</param>
public void DrawFilledRect(Rectangle r, Color c)
{
// fill the vertex buffer with the correct vertices
GraphicsStream stm = vbCol.Lock(0, 0, 0);
CustomVertex.TransformedColored[] verts =
new CustomVertex.TransformedColored[4];
verts[0].X = r.Left;
verts[0].Y = r.Bottom;
verts[0].Z = 0.5f;
verts[0].Rhw = 1;
verts[0].Color = c.ToArgb();
verts[1].X = r.Left;
verts[1].Y = r.Top;
verts[1].Z = 0.5f;
verts[1].Rhw = 1;
verts[1].Color = c.ToArgb();
verts[2].X = r.Right;
verts[2].Y = r.Top;
verts[2].Z = 0.5f;
verts[2].Rhw = 1;
verts[2].Color = c.ToArgb();
verts[3].X = r.Right;
verts[3].Y = r.Bottom;
verts[3].Z = 0.5f;
verts[3].Rhw = 1;
verts[3].Color = c.ToArgb();
stm.Write(verts);
vbCol.Unlock();
// bind the vertex data to the next drawing operation
device.SetStreamSource(0, vbCol, 0);
#if DESKTOP
device.VertexFormat = CustomVertex.TransformedColored.Format;
#endif
// draw the triangles
device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
}
/// <summary>
/// Flip the back buffer to the display.
/// </summary>
public void Flip()
{
if (device == null)
return;
// End the current rendering
device.EndScene();
device.Present();
// Start a new rendering...
// Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue,
1.0f, 0);
// Begin the scene
device.BeginScene();
}
/// <summary>
/// Draw the string to the back buffer.
/// </summary>
/// <param name="x">X destination of text</param>
/// <param name="y">Y destination of text</param>
/// <param name="text">Text to be displayed</param>
/// <param name="color">Color of text</param>
/// <param name="font">Font to be used</param>
/// <param name="options">Font draw options</param>
public void DrawText(int x, int y, string text, Color color,
IFont font, FontDrawOptions options)
{
// make sure the function is passed an appropriate implementation
DirectXFont dx_font = null;
try
{
dx_font = (DirectXFont)font;
}
catch (InvalidCastException e)
{
throw new ApplicationException(
"The font given was not created by" +
"this class' CreateFont() method.", e);
}
// determine the rectangle to draw in
Rectangle rect = new Rectangle();
#if DESKTOP
dx_font.Font.DrawText(null, text, ref rect,
DrawTextFormat.CalculateRect, color);
#else
dx_font.Font.MeasureString(null, text, ref rect, 0);
#endif
rect.Y = y;
rect.X = 0;
rect.Width = ScreenWidth;
// set options for the DrawText call
DrawTextFormat drawOptions;
if (options == FontDrawOptions.DrawTextCenter)
drawOptions = DrawTextFormat.Center;
else if (options == FontDrawOptions.DrawTextLeft)
drawOptions = DrawTextFormat.Left;
else
drawOptions = DrawTextFormat.Right;
dx_font.Font.DrawText(null, text, rect, drawOptions, color);
}
/// <summary>
/// Draw options used for drawing bitmaps.
/// </summary>
DrawOptions drawOptions = 0;
/// <summary>
/// Set the current draw mode.
/// </summary>
/// <param name="options">options to be set</param>
public void SetDrawOptions(DrawOptions options)
{
drawOptions |= options;
}
/// <summary>
/// Clear the current draw mode of the specified options.
/// </summary>
/// <param name="options">options to be cleared</param>
public void ClearDrawOptions(DrawOptions options)
{
drawOptions &= ~options;
}
/// <summary>
/// Validate draw regions be clipping them to the screen.
/// </summary>
/// <param name="x">X destination</param>
/// <param name="y">Y destination</param>
/// <param name="sourceRegion">Source region</param>
/// <returns>true if any part of the destination is drawable
/// (on-screen), false otherwise</returns>
public bool ValidateRegions(ref int x, ref int y, ref Rectangle sourceRegion)
{
Rectangle clipped_src = new Rectangle(sourceRegion.X, sourceRegion.Y,
sourceRegion.Width, sourceRegion.Height);
if (x < 0)
{
if (x + sourceRegion.Width < 0) return false;
clipped_src.Width += x;
clipped_src.X -= x;
x = 0;
}
else if (x >= ScreenWidth)
{
return false;
}
if (y < 0)
{
if (y + sourceRegion.Height < 0) return false;
clipped_src.Height += y;
clipped_src.Y -= y;
y = 0;
}
else if (y >= ScreenHeight)
{
return false;
}
if (x + sourceRegion.Width > ScreenWidth)
{
clipped_src.Width -= (x + sourceRegion.Width) - ScreenWidth;
}
if (y + sourceRegion.Height > ScreenHeight)
{
clipped_src.Height -= (y + sourceRegion.Height) - ScreenHeight;
}
if ((drawOptions & DrawOptions.BlitMirrorLeftRight) != 0)
{
sourceRegion.X = sourceRegion.X + sourceRegion.Width -
(clipped_src.X - sourceRegion.X) - clipped_src.Width;
}
else
{
sourceRegion.X = clipped_src.X;
}
sourceRegion.Width = clipped_src.Width;
sourceRegion.Y = clipped_src.Y;
sourceRegion.Height = clipped_src.Height;
return true;
}
/// <summary>
/// Creates a bitmap compatible with this graphics device
/// </summary>
/// <param name="fileName">The file to load the image from</param>
/// <param name="transparent">True if the image should be drawn
/// with alpha transparency</param>
/// <returns>A bitmap compatible with this graphics device</returns>
public IBitmap CreateBitmap(string fileName, bool transparent)
{
return new DirectXBitmap(fileName, device, transparent);
}
/// <summary>
/// Creates a font object compatible with this graphics device
/// </summary>
/// <param name="fontName"></param>
/// <returns>A font object compatible with this graphics device
/// </returns>
public IFont CreateFont(string fontName)
{
return new DirectXFont(fontName, device);
}
#endregion
#region IDisposable Members
public void Dispose()
{
device.Dispose();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -