⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gxgraphics.cs

📁 功能:基于windows mobile 的地图查看器。使用vs2005开发
💻 CS
📖 第 1 页 / 共 2 页
字号:
			if (y < 0)
			{
				rSrc.Height += y;
				if ((m_drawFlags & DrawFlags.GDBLT_MIRRORUPDOWN) == 0)
				{
					rSrc.Y -= y;
				}

				y = 0;
			}
			else if (y >= ScreenHeight)
			{
				return false;
			}

			if (x + rSrc.Width > ScreenWidth)
			{
				if ((m_drawFlags & DrawFlags.GDBLT_MIRRORLEFTRIGHT) != 0)
					rSrc.X += (rSrc.Width + x) - ScreenWidth;

				rSrc.Width -= (rSrc.Width + x) - ScreenWidth;
			}

			if (y + rSrc.Height > ScreenHeight)
			{
				if ((m_drawFlags & DrawFlags.GDBLT_MIRRORUPDOWN) != 0)
					rSrc.Y += (rSrc.Height + y) - ScreenHeight;

				rSrc.Height -= (rSrc.Height + y) - ScreenHeight;
			}

			return true;
		}

		
		public IGXBitmap CreateBitmap(string file_name, bool transparent)
		{
			return new GDIGXBitmap(file_name, transparent);
		}

		public IGXFont CreateFont(string font_name)
		{
			return new GDIGXFont(font_name);
		}
	}

	public class DirectXGXGraphics : IGXGraphics
	{

		Device device;
		VertexBuffer vb_col;
		VertexBuffer vb_tex;

		public DirectXGXGraphics(Control ctrl)
		{
			// Now let's setup our D3D stuff
			PresentParameters presentParams = new PresentParameters();
#if !DESKTOP
			presentParams.Windowed = false;
			presentParams.SwapEffect = SwapEffect.Discard;
			presentParams.BackBufferCount = 1;
			presentParams.BackBufferFormat = Format.R5G6B5;
			presentParams.BackBufferWidth = 240;
			presentParams.BackBufferHeight = 320;
		    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;

            vb_col = new VertexBuffer(typeof(CustomVertex.TransformedColored), 5, device,
									  0, CustomVertex.TransformedColored.Format, Pool.SystemMemory);
			vb_tex = new VertexBuffer(typeof(CustomVertex.TransformedTextured), 4, device,
				0, CustomVertex.TransformedTextured.Format, Pool.SystemMemory);

			//Clear the backbuffer to a blue color 
			device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
			//Begin the scene
			device.BeginScene();
		}


		#region IGXGraphics Members

		public int ScreenWidth
		{
			get
			{
				return 240;
			}
		}

		public int ScreenHeight
		{
			get
			{
				return 320;
			}
		}

		public void DrawAnimation(int x, int y, GXAnimation anim)
		{
			DrawBitmap(x, y, anim.Region, anim.Image);
		}

		public void DrawBitmap(int x, int y, Rectangle srcRegion, IGXBitmap bmp)
		{
			try
			{
				DirectXGXBitmap dx_bmp = (DirectXGXBitmap)bmp;

				// Clip the regions to the screen
				if (!ValidateRegions(ref x, ref y, ref srcRegion))
					return;

				Rectangle dest = new Rectangle(x, y, srcRegion.Width, srcRegion.Height);
				RectangleF tex_rect = new RectangleF(  ((float)srcRegion.X+0.5F)/dx_bmp.Width,
					((float)srcRegion.Y+0.5F)/dx_bmp.Height,
					((float)srcRegion.Width+0.5F)/dx_bmp.Width,
					((float)srcRegion.Height+0.5F)/dx_bmp.Height);


				GraphicsStream stm = vb_tex.Lock(0, 0, 0);
				CustomVertex.TransformedTextured[] verts = new CustomVertex.TransformedTextured[4];

				if ((m_drawFlags & DrawFlags.GDBLT_MIRRORLEFTRIGHT) == 0)
				{
					verts[0].X=dest.Right; verts[0].Y=dest.Top; verts[0].Z=0.5f;
					verts[0].Rhw=1; 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);
				vb_tex.Unlock();
				device.SetTexture(0, dx_bmp.Texture);
				device.SetStreamSource( 0, vb_tex, 0);
#if DESKTOP
				device.VertexFormat = CustomVertex.TransformedTextured.Format;
#endif
				device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
			}
			finally {}
		}

		public void DrawFilledRect(Rectangle r, Color c)
		{
            GraphicsStream stm = vb_col.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);
			vb_col.Unlock();
            device.SetStreamSource( 0, vb_col, 0);
#if DESKTOP
			device.VertexFormat = CustomVertex.TransformedColored.Format;
#endif
			device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
		}

		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();
		}

		public void DrawText(int x, int y, string text, Color color, IGXFont f, FontDrawFlags flags)
		{
			DirectXGXFont font = (DirectXGXFont) f;

			Rectangle rect = new Rectangle();
			//TODO
			//font.MyFont.DrawText(null, text, ref rect, DrawTextFormat.CalculateRect, color);
			font.MyFont.DrawText(null, text, rect, DrawTextFormat.Center, color);
			rect.Y = y;
			rect.X = 0;
			rect.Width = ScreenWidth;

			DrawTextFormat dflags;
			if(flags == FontDrawFlags.GDDRAWTEXT_CENTER)
				dflags = DrawTextFormat.Center;
			else if(flags == FontDrawFlags.GDDRAWTEXT_LEFT)
				dflags = DrawTextFormat.Left;
			else
				dflags = DrawTextFormat.Right;

			font.MyFont.DrawText(null, text, rect, dflags, color);
		}

		/// <summary>
		/// Draw flags used for drawing bitmaps.  These flags
		/// map to GapiDraw flags so they can be used directly.
		/// </summary>
		protected DrawFlags m_drawFlags = 0;
		/// <summary>
		/// Set the current draw mode.
		/// </summary>
		/// <param name="flags">Flags to be set</param>
		public void SetDrawFlags(DrawFlags flags)
		{
			m_drawFlags |= flags;
		}

		/// <summary>
		/// Clear the current draw mode of the specified flags.
		/// </summary>
		/// <param name="flags">Flags to be cleared</param>
		public void ClearDrawFlags(DrawFlags flags)
		{
			m_drawFlags &= ~flags;
		}

		/// <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="rSrc">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 rSrc)
		{
			Rectangle clipped_src = new Rectangle(rSrc.X, rSrc.Y, rSrc.Width, rSrc.Height);
			if (x < 0)
			{
				if(x + rSrc.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 + rSrc.Height < 0) return false;
				clipped_src.Height += y;
				clipped_src.Y -= y;
				y = 0;
			}
			else if (y >= ScreenHeight)
			{
				return false;
			}

			if (x + rSrc.Width > ScreenWidth)
			{
				clipped_src.Width -= (x + rSrc.Width) - ScreenWidth;
			}

			if (y + rSrc.Height > ScreenHeight)
			{
				clipped_src.Height -= (y + rSrc.Height) - ScreenHeight;
			}

			if ((m_drawFlags & DrawFlags.GDBLT_MIRRORLEFTRIGHT) != 0)
			{
				rSrc.X = rSrc.X + rSrc.Width - (clipped_src.X - rSrc.X) - clipped_src.Width;
			}
			else
			{
				rSrc.X = clipped_src.X;
			}

			rSrc.Width = clipped_src.Width;
			rSrc.Y = clipped_src.Y;
			rSrc.Height = clipped_src.Height;

			return true;
		}


		public IGXBitmap CreateBitmap(string file_name, bool transparent)
		{
			return new DirectXGXBitmap(file_name, device, transparent);
		}

		public IGXFont CreateFont(string font_name)
		{
			return new DirectXGXFont(font_name, device);
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			//device.Dispose();
		}

		#endregion

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -