📄 graphics.cs
字号:
// make sure the function is passed an appropriate implementation
GdiFont gdiFont = null;
try
{
gdiFont = (GdiFont)font;
}
catch (InvalidCastException e)
{
throw new ApplicationException(
"The font given was not created by" +
"this class' CreateFont() method.", e);
}
float drawX = (float)x;
float drawY = (float)y;
if ((options & FontDrawOptions.DrawTextCenter) != 0)
{
SizeF sSize = gBack.MeasureString(text, gdiFont.Font);
drawX -= sSize.Width / 2.0F;
}
else if ((options & FontDrawOptions.DrawTextRight) != 0)
{
SizeF sSize = gBack.MeasureString(text, gdiFont.Font);
drawX -= sSize.Width;
}
Brush brush = new SolidBrush(color);
gBack.DrawString(text, gdiFont.Font, brush, drawX, drawY);
}
/// <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)
{
if (x < 0)
{
sourceRegion.Width += x;
if ((drawOptions & DrawOptions.BlitMirrorLeftRight) == 0)
{
sourceRegion.X -= x;
}
x = 0;
}
else if (x >= ScreenWidth)
{
return false;
}
if (y < 0)
{
sourceRegion.Height += y;
if ((drawOptions & DrawOptions.BlitMirrorUpDown) == 0)
{
sourceRegion.Y -= y;
}
y = 0;
}
else if (y >= ScreenHeight)
{
return false;
}
if (x + sourceRegion.Width > ScreenWidth)
{
if ((drawOptions & DrawOptions.BlitMirrorLeftRight) != 0)
sourceRegion.X += (sourceRegion.Width + x) - ScreenWidth;
sourceRegion.Width -= (sourceRegion.Width + x) - ScreenWidth;
}
if (y + sourceRegion.Height > ScreenHeight)
{
if ((drawOptions & DrawOptions.BlitMirrorUpDown) != 0)
sourceRegion.Y += (sourceRegion.Height + y) - ScreenHeight;
sourceRegion.Height -= (sourceRegion.Height + y) - ScreenHeight;
}
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 GdiBitmap(fileName, 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 GdiFont(fontName);
}
}
public class DirectXGraphics : IGraphics
{
/// <summary>
/// The rendering device for D3D
/// </summary>
Device device;
/// <summary>
/// A buffer of vertex data for colored (non-textured) drawing
/// operations
/// </summary>
VertexBuffer vbCol;
/// <summary>
/// A buffer of vertex data for textured drawing operations
/// </summary>
VertexBuffer vbTex;
/// <summary>
/// The fixed height of our rendering area
/// </summary>
private static int Width = 240;
/// <summary>
/// The fixed width of our rendering area
/// </summary>
private static int Height = 320;
public DirectXGraphics(Control ctrl)
{
Pool vertexBufferPool;
// Setup D3D parameters
PresentParameters presentParams = new PresentParameters();
#if !DESKTOP
presentParams.Windowed = false;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.BackBufferCount = 1;
presentParams.BackBufferFormat = Format.R5G6B5;
presentParams.BackBufferWidth = Width;
presentParams.BackBufferHeight = Height;
device = new Device(0, DeviceType.Default, ctrl,
CreateFlags.None, presentParams);
#else
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, ctrl,
CreateFlags.SoftwareVertexProcessing, presentParams);
#endif
device.RenderState.AlphaTestEnable = true;
device.RenderState.AlphaFunction = Compare.NotEqual;
device.RenderState.ReferenceAlpha = 0;
#if !DESKTOP
// Get the device capabilities
Caps caps;
caps = device.DeviceCaps;
if (caps.SurfaceCaps.SupportsVidVertexBuffer)
vertexBufferPool = Pool.VideoMemory;
else
vertexBufferPool = Pool.SystemMemory;
#else
vertexBufferPool = Pool.SystemMemory;
#endif
vbCol = new VertexBuffer(
typeof(CustomVertex.TransformedColored), 4, device,
0, CustomVertex.TransformedColored.Format, vertexBufferPool);
vbTex = new VertexBuffer(
typeof(CustomVertex.TransformedTextured), 4, device,
0, CustomVertex.TransformedTextured.Format,
vertexBufferPool);
// Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue,
1.0f, 0);
// Begin the scene
device.BeginScene();
}
#region IGraphics Members
/// <summary>
/// Gets the width of the screen.
/// </summary>
public int ScreenWidth
{
get
{
return Width;
}
}
/// <summary>
/// Gets the height of the screen.
/// </summary>
public int ScreenHeight
{
get
{
return Height;
}
}
/// <summary>
/// Draw the current cell of the animation to the back buffer.
/// </summary>
/// <param name="x">X destination of the draw</param>
/// <param name="y">Y destination of the draw</param>
/// <param name="animation">Animation to be drawn</param>
public void DrawAnimation(int x, int y, Animation animation)
{
DrawBitmap(x, y, animation.Region, animation.Image);
}
/// <summary>
/// Draw the bitmap to the back buffer.
/// </summary>
/// <param name="x">X destination of the draw</param>
/// <param name="y">Y destination of the draw</param>
/// <param name="sourceRegion">Source region of the draw</param>
/// <param name="bmp">Bitmap to be drawn</param>
public void DrawBitmap(int x, int y, Rectangle sourceRegion,
IBitmap bmp)
{
// make sure the function is passed an appropriate implementation
DirectXBitmap dxBitmap = null;
try
{
dxBitmap = (DirectXBitmap)bmp;
}
catch (InvalidCastException e)
{
throw new ApplicationException(
"The bitmap given was not created by" +
"this class' CreateBitmap() method.", e);
}
// Clip the regions to the screen
if (!ValidateRegions(ref x, ref y, ref sourceRegion))
return;
// determine texture coordinates so that
// the there is a 1-1 mapping between texels and screen pixels
Rectangle dest = new Rectangle(x, y, sourceRegion.Width,
sourceRegion.Height);
RectangleF textureRect = new RectangleF(
((float)sourceRegion.X + 0.5F) / dxBitmap.Width,
((float)sourceRegion.Y + 0.5F) / dxBitmap.Height,
((float)sourceRegion.Width + 0.5F) / dxBitmap.Width,
((float)sourceRegion.Height + 0.5F) / dxBitmap.Height);
// load the vertex buffer with data for the two triangles
// in the rectangle
GraphicsStream stm = vbTex.Lock(0, 0, 0);
CustomVertex.TransformedTextured[] verts =
new CustomVertex.TransformedTextured[4];
if ((drawOptions & DrawOptions.BlitMirrorLeftRight) == 0)
{
verts[0].X = dest.Right;
verts[0].Y = dest.Top;
verts[0].Z = 0.5f;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -