本文整理汇总了C#中Microsoft.Xna.Framework.Graphics.RenderTarget2D.GetData方法的典型用法代码示例。如果您正苦于以下问题:C# RenderTarget2D.GetData方法的具体用法?C# RenderTarget2D.GetData怎么用?C# RenderTarget2D.GetData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Xna.Framework.Graphics.RenderTarget2D
的用法示例。
在下文中一共展示了RenderTarget2D.GetData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddNewLevelBlock
public static bool AddNewLevelBlock(Level level, Vector3 coordinate, Texture2D heightMap, Texture2D alphaMap, string[] detailTextureNames, Vector2[] uvOffsets, Vector2[] uvScales)
{
if (level.Contains(coordinate))
{
return false;
}
Color[] heightColors = new Color[HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_VERTICES];
if (heightMap.Width != HeightMapMesh.NUM_SIDE_VERTICES || heightMap.Height != HeightMapMesh.NUM_SIDE_VERTICES)
{
RenderTarget2D resizedHeightMap = new RenderTarget2D(GraphicsManager.Device, HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES);
GraphicsManager.Device.SetRenderTarget(resizedHeightMap);
GraphicsManager.SpriteBatch.Begin();
GraphicsManager.SpriteBatch.Draw(heightMap, new Rectangle(0, 0, HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES), Color.White);
GraphicsManager.SpriteBatch.End();
GraphicsManager.Device.SetRenderTarget(null);
resizedHeightMap.GetData(heightColors);
}
else
{
heightMap.GetData(heightColors);
}
float[,] heights = new float[HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES];
for (int i = 0; i < heightColors.Length; i++)
{
heights[i % HeightMapMesh.NUM_SIDE_VERTICES, i / HeightMapMesh.NUM_SIDE_VERTICES] = ConvertColorToFloat(heightColors[i]);
}
if (alphaMap.Height != HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD ||
alphaMap.Width != HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD)
{
RenderTarget2D resizedAlphaMap = new RenderTarget2D(GraphicsManager.Device,
HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD,
HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD);
GraphicsManager.Device.SetRenderTarget(resizedAlphaMap);
GraphicsManager.SpriteBatch.Begin();
GraphicsManager.SpriteBatch.Draw(alphaMap,
new Rectangle(0,
0,
HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD,
HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD),
Color.White);
GraphicsManager.SpriteBatch.End();
GraphicsManager.Device.SetRenderTarget(null);
alphaMap = resizedAlphaMap;
}
HeightMapMesh heightMapMesh = new HeightMapMesh(heights, alphaMap, detailTextureNames, uvOffsets, uvScales);
AssetLibrary.AddHeightMap(level.Name + coordinate.ToString(), heightMapMesh);
level.AddNewBlock(coordinate);
return true;
}
示例2: PreMultiplyAlpha
public static void PreMultiplyAlpha(this Texture2D texture)
{
//Setup a render target to hold our final texture which will have premulitplied color values
var result = new RenderTarget2D(texture.GraphicsDevice, texture.Width, texture.Height);
texture.GraphicsDevice.SetRenderTarget(result);
texture.GraphicsDevice.Clear(Color.Black);
// Using default blending function
// (source × Blend.SourceAlpha) + (destination × Blend.InvSourceAlpha)
// Destination is zero so the reduces to
// (source × Blend.SourceAlpha)
// So this multiplies our color values by the alpha value and draws it to the RenderTarget
var blendColor = new BlendState {
ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue,
AlphaDestinationBlend = Blend.Zero,
ColorDestinationBlend = Blend.Zero,
AlphaSourceBlend = Blend.SourceAlpha,
ColorSourceBlend = Blend.SourceAlpha
};
var spriteBatch = new SpriteBatch(texture.GraphicsDevice);
spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
spriteBatch.Draw(texture, texture.Bounds, Color.White);
spriteBatch.End();
// Simply copy over the alpha channel
var blendAlpha = new BlendState {
ColorWriteChannels = ColorWriteChannels.Alpha,
AlphaDestinationBlend = Blend.Zero,
ColorDestinationBlend = Blend.Zero,
AlphaSourceBlend = Blend.One,
ColorSourceBlend = Blend.One
};
spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
spriteBatch.Draw(texture, texture.Bounds, Color.White);
spriteBatch.End();
texture.GraphicsDevice.SetRenderTarget(null);
var t = new Color[result.Width * result.Height];
result.GetData(t);
texture.SetData(t);
}
示例3: RecordFrame
// TODO: Record at certain framerate! (possibly every nth frame)
public void RecordFrame(RenderTarget2D rt, Matrix view)
{
if (!IsRecording())
return;
rt.GetData<Vector4>(tmpBufferRT);
for (int i = 0; i < 640 * 480; i++)
{
float depth = tmpBufferRT[i].X;
unsafe
{
int idepth = *((int*)&depth);
// little endian
tmpBufferDepth[4 * i + 0] = (byte)((idepth >> 0) & 255);
tmpBufferDepth[4 * i + 1] = (byte)((idepth >> 8) & 255);
tmpBufferDepth[4 * i + 2] = (byte)((idepth >> 16) & 255);
tmpBufferDepth[4 * i + 3] = (byte)((idepth >> 24) & 255);
}
}
bw.Write(view.M11);
bw.Write(view.M12);
bw.Write(view.M13);
bw.Write(view.M14);
bw.Write(view.M21);
bw.Write(view.M22);
bw.Write(view.M23);
bw.Write(view.M24);
bw.Write(view.M31);
bw.Write(view.M32);
bw.Write(view.M33);
bw.Write(view.M34);
bw.Write(view.M41);
bw.Write(view.M42);
bw.Write(view.M43);
bw.Write(view.M44);
bw.Write(tmpBufferDepth);
frameCount++;
}
示例4: TextureToJpg
public void TextureToJpg(RenderTarget2D texture, Stream stream)
{
texture.GetData<byte>(textureData);
byte blue;
for (int i = 0; i < textureData.Length; i += 4) {
blue = textureData[i];
textureData[i] = textureData[i+2];
textureData[i + 2] = blue;
}
bitmapData = bmp.LockBits(
rect,
System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
safePtr = bitmapData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
bmp.UnlockBits(bitmapData);
bmp.Save(stream, imageFormat);
}
示例5: GetParticlePositions
// We get the destinations of our particles by drawing our font to a render target and
// reading back which pixels were set.
List<Vector2> GetParticlePositions(GraphicsDevice device, SpriteFont font, string text)
{
Vector2 size = font.MeasureString(text) + new Vector2(0.5f);
int width = (int)size.X;
int height = (int)size.Y;
// Create a temporary render target and draw the font on it.
RenderTarget2D target = new RenderTarget2D(device, width, height);
device.SetRenderTarget(target);
device.Clear(Color.Black);
SpriteBatch spriteBatch = new SpriteBatch(device);
spriteBatch.Begin();
spriteBatch.DrawString(font, text, Vector2.Zero, Color.White);
spriteBatch.End();
device.SetRenderTarget(null); // unset the render target
// read back the pixels from the render target
Color[] data = new Color[width * height];
target.GetData<Color>(data);
target.Dispose();
// Return a list of points corresponding to pixels drawn by the font. The font size will affect the number of
// points and the quality of the text.
List<Vector2> points = new List<Vector2>();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// Add all points that are lighter than 50% grey. The text is white, but due to anti-aliasing pixels
// on the border may be shades of grey.
if (data[width * y + x].R > 128)
points.Add(new Vector2(x, y));
}
}
return points;
}
示例6: LightningTexture
public LightningTexture(GraphicsDevice device, int width, int height)
: base(device, width, height)
{
anim = new AnimatedLightning(Vector2.Zero, new Vector2(width, height), 45, MathHelper.ToRadians(45), 0.7f, 5, 1, 10);
var sb = new SpriteBatch(device);
var target = new RenderTarget2D(GraphicsDevice, 50, 100, false
, GraphicsDevice.PresentationParameters.BackBufferFormat
, GraphicsDevice.PresentationParameters.DepthStencilFormat);
var oldTargets = GraphicsDevice.GetRenderTargets();
GraphicsDevice.SetRenderTarget(target);
sb.Begin();
foreach (var segment in anim.Segments)
{
sb.DrawLine(segment.From, segment.To, segment.Color);
}
sb.End();
GraphicsDevice.SetRenderTargets(oldTargets);
Color[] data = new Color[target.Width * target.Height];
target.GetData<Color>(data);
this.SetData<Color>(data);
}
示例7: InvertGradient
public static void InvertGradient(RenderTarget2D textureInput, ref RenderTarget2D textureOutput, float state,
Color c1, Color c2)
{
if (textureInput.Width != textureOutput.Width || textureInput.Height != textureOutput.Height)
throw new Exception("Textures must be the same size!");
Color[] c = new Color[textureInput.Width * textureInput.Height];
textureInput.GetData<Color>(c);
float cr = (float)(c1.R + c2.R) / 2 / 255;
float cg = (float)(c1.G + c2.G) / 2 / 255;
float cb = (float)(c1.B + c2.B) / 2 / 255;
float dr = (float)(c1.R - c2.R) / 255;
float dg = (float)(c1.G - c2.G) / 255;
float db = (float)(c1.B - c2.B) / 255;
float tr, tg, tb;
for (int i = 0; i < c.Length && c[i].A != 0; i++)
{
tr = (float)c[i].R / 255;
tg = (float)c[i].G / 255;
tb = (float)c[i].B / 255;
tr = tr - (tr - cr) * 2 * state;
tg = tg - (tg - cg) * 2 * state;
tb = tb - (tb - cb) * 2 * state;
c[i].R = (byte)(tr * 255);
c[i].G = (byte)(tg * 255);
c[i].B = (byte)(tb * 255);
}
textureOutput.SetData<Color>(c);
}
示例8: LoadTexture
public static Texture2D LoadTexture(GraphicsDevice device, Stream input)
{
Texture2D file = Texture2D.FromStream(device, input);
// Setup a render target to hold our final texture which will have premulitplied alpha values
RenderTarget2D result = new RenderTarget2D(device, file.Width, file.Height);
device.SetRenderTarget(result);
device.Clear(Color.Black);
// Multiply each color by the source alpha, and write in just the color values into the final texture
BlendState blendColor = new BlendState();
blendColor.ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;
blendColor.AlphaDestinationBlend = Blend.Zero;
blendColor.ColorDestinationBlend = Blend.Zero;
blendColor.AlphaSourceBlend = Blend.SourceAlpha;
blendColor.ColorSourceBlend = Blend.SourceAlpha;
SpriteBatch spriteBatch = new SpriteBatch(device);
spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
spriteBatch.Draw(file, file.Bounds, Color.White);
spriteBatch.End();
// Now copy over the alpha values from the PNG source texture to the final one, without multiplying them
BlendState blendAlpha = new BlendState();
blendAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;
blendAlpha.AlphaDestinationBlend = Blend.Zero;
blendAlpha.ColorDestinationBlend = Blend.Zero;
blendAlpha.AlphaSourceBlend = Blend.One;
blendAlpha.ColorSourceBlend = Blend.One;
spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
spriteBatch.Draw(file, file.Bounds, Color.White);
spriteBatch.End();
// Release the GPU back to drawing to the screen
device.SetRenderTarget(null);
// RenderTarget2D are volatile and will be lost on screen resolution changes.
// So instead of using this directly, we create a non-voliate Texture2D.
// This is computationally slower, but should be safe as long as it is done
// on load.
Texture2D resultTexture = new Texture2D(device, file.Width, file.Height);
Color[] resultContent = new Color[Convert.ToInt32(file.Width * file.Height)];
result.GetData(resultContent);
resultTexture.SetData(resultContent);
// Dispose of the RenderTarget2D immediately.
result.Dispose();
return resultTexture;
}
示例9: GrabScreenshot
public static void GrabScreenshot(RenderTarget2D rendertarget)
{
Color[] data = new Color[(rendertarget.Width * rendertarget.Height) * 3];
//OpenTK.Graphics.ES11.GL.ReadPixels(0, 0, rendertarget.Width, rendertarget.Height, OpenTK.Graphics.ES11.All.Rgb, OpenTK.Graphics.ES11.All.UnsignedByte, ref data);
rendertarget.GetData<Color>(data);
}
示例10: CaptureArea
/// <summary>
/// Takes a capture and sends it to the clipboard
/// </summary>
protected Microsoft.Xna.Framework.Graphics.PackedVector.Byte4[] CaptureArea(GridRectangle Rect, float Downsample)
{
Debug.Assert((Rect.Width / Downsample) < 4096 && (Rect.Height / Downsample) < 4096);
Debug.Assert(this.PaintCallRefCount == 0);
// Vector3 OriginalCameraLookAt = this.Camera.LookAt;
//float OriginalCameraDistance = this.CameraDistance;
// Rectangle OriginalVisibleRect = this.VisibleScreenRect;
int Width = (int)Math.Round(Rect.Width / Downsample);
int Height = (int)Math.Round(Rect.Height / Downsample);
Microsoft.Xna.Framework.Graphics.PackedVector.Byte4[] data = new Microsoft.Xna.Framework.Graphics.PackedVector.Byte4[Width * Height];
try
{
// Initialize our RenderTarget
ScreenshotRenderTarget = new RenderTarget2D(Device,
Width,
Height,
false,
SurfaceFormat.Color,
DepthFormat.Depth24Stencil8);
Device.SetRenderTarget(ScreenshotRenderTarget);
bool OldAsynchTextureLoad = AsynchTextureLoad;
AsynchTextureLoad = false;
// Draw(Downsample);
AsynchTextureLoad = OldAsynchTextureLoad;
Device.SetRenderTarget(null);
data = new Microsoft.Xna.Framework.Graphics.PackedVector.Byte4[ScreenshotRenderTarget.Width * ScreenshotRenderTarget.Height];
ScreenshotRenderTarget.GetData<Microsoft.Xna.Framework.Graphics.PackedVector.Byte4>(data);
// Draw();
}
finally
{
Device.SetRenderTarget(null);
if (ScreenshotRenderTarget != null)
{
ScreenshotRenderTarget.Dispose();
ScreenshotRenderTarget = null;
}
// this.CameraLookAt = OriginalCameraLookAt;
// this.CameraDistance = OriginalCameraDistance;
}
return data;
}
示例11: FromStreamFast
/// <summary>
/// Uses GPU to do premultiply calcs. Fast, however had problems.
/// </summary>
/// <param name="stream"></param>
/// <param name="preMultiplyAlpha"></param>
/// <returns></returns>
public Texture2D FromStreamFast(Stream stream, bool preMultiplyAlpha = true)
{
Texture2D texture;
if (_needsBmp)
{
// Load image using GDI because Texture2D.FromStream doesn't support BMP
using (Image image = Image.FromStream(stream))
{
// Now create a MemoryStream which will be passed to Texture2D after converting to PNG internally
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
texture = Texture2D.FromStream(_graphicsDevice, ms);
}
}
}
else
{
texture = Texture2D.FromStream(_graphicsDevice, stream);
}
if (preMultiplyAlpha)
{
// Setup a render target to hold our final texture which will have premulitplied alpha values
using (RenderTarget2D renderTarget = new RenderTarget2D(_graphicsDevice, texture.Width, texture.Height))
{
Viewport viewportBackup = _graphicsDevice.Viewport;
_graphicsDevice.SetRenderTarget(renderTarget);
_graphicsDevice.Clear(Color.Black);
// Multiply each color by the source alpha, and write in just the color values into the final texture
_spriteBatch.Begin(SpriteSortMode.Immediate, BlendColorBlendState);
_spriteBatch.Draw(texture, texture.Bounds, Color.White);
_spriteBatch.End();
// Now copy over the alpha values from the source texture to the final one, without multiplying them
_spriteBatch.Begin(SpriteSortMode.Immediate, BlendAlphaBlendState);
_spriteBatch.Draw(texture, texture.Bounds, Color.White);
_spriteBatch.End();
// Release the GPU back to drawing to the screen
_graphicsDevice.SetRenderTarget(null);
_graphicsDevice.Viewport = viewportBackup;
// Store data from render target because the RenderTarget2D is volatile
Color[] data = new Color[texture.Width * texture.Height];
renderTarget.GetData(data);
// Unset texture from graphic device and set modified data back to it
_graphicsDevice.Textures[0] = null;
texture.SetData(data);
}
}
return texture;
}
示例12: SaveRenderTargetAsPng
/// <summary>
/// Save an RenderTarget2D as an PNG. MonoGame hasn't implemented this yet.
/// I stole the code from https://github.com/mono/MonoGame/blob/29378026d803a8bbce61abd08b8dba2ebb6b2096/MonoGame.Framework/Graphics/Texture2D.OpenGL.cs#L559
/// </summary>
/// <param name="stream">The stream to save the file to</param>
/// <param name="width">The width of the image</param>
/// <param name="height">The height of the image</param>
/// <param name="target">The RenderTarget to save</param>
public void SaveRenderTargetAsPng(Stream stream, int width, int height, RenderTarget2D target)
{
byte[] data = null;
GCHandle? handle = null;
System.Drawing.Bitmap bitmap = null;
try
{
data = new byte[width * height * 4];
handle = GCHandle.Alloc(data, GCHandleType.Pinned);
target.GetData(data);
// internal structure is BGR while bitmap expects RGB
for (int i = 0; i < data.Length; i += 4)
{
byte temp = data[i + 0];
data[i + 0] = data[i + 2];
data[i + 2] = temp;
}
bitmap = new System.Drawing.Bitmap(width, height, width * 4, System.Drawing.Imaging.PixelFormat.Format32bppArgb, handle.Value.AddrOfPinnedObject());
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
}
finally
{
if (bitmap != null)
{
bitmap.Dispose();
}
if (handle.HasValue)
{
handle.Value.Free();
}
if (data != null)
{
data = null;
}
}
}
示例13: XuatAnhKyTu
//.........这里部分代码省略.........
}
}
}
//thuc hien can le va can dong
bonenvunganh = new RenderTarget2D(thietbidohoa, rongkhung + bovien.X + bovien.Width, daikhung + bovien.Y + bovien.Height);
thietbidohoa.SetRenderTarget(bonenvunganh);
thietbidohoa.Clear(maunen);
nenve.Begin();
int dodaitoanbochu = demdong * (dodaikytu + dancachdong) - dancachdong;
int giasocandong = 0;
if (candong == 3) giasocandong = daikhung - dodaitoanbochu;
else if (candong == 2) giasocandong = (int)((daikhung - dodaitoanbochu) / 2);
vitrighepx = bovien.X; vitrighepy = giasocandong + bovien.Y;
if (canle == 3)
{
for (int i = 0; i < demdong; i++)
{
vitrighepx = rongkhung - mangdorongdong[i] + bovien.X;
vitrighepy = i * (dodaikytu + dancachdong) + giasocandong + bovien.Y;
for (int j = 0; j < demkytu[i]; j++)
{
if (matranvanban[j, i] == -1)
{
nenve.Draw(anhkytucham, new Vector2(vitrighepx, vitrighepy), Color.White);
vitrighepx += dorongkytucham + dancachchu;
}
else
{
if ((j == 0) & ((giatrikytu[matranvanban[j, i]] == " ") | (giatrikytu[matranvanban[j, i]] == "|"))) continue;
nenve.Draw(anhkytu[matranvanban[j, i]], new Vector2(vitrighepx, vitrighepy), Color.White);
vitrighepx += dorongkytu[matranvanban[j, i]] + dancachchu;
}
}
}
}
else if (canle == 2)
{
for (int i = 0; i < demdong; i++)
{
vitrighepx = (int)((rongkhung - mangdorongdong[i]) / 2) + bovien.X;
vitrighepy = i * (dodaikytu + dancachdong) + giasocandong + bovien.Y;
for (int j = 0; j < demkytu[i]; j++)
{
if (matranvanban[j, i] == -1)
{
nenve.Draw(anhkytucham, new Vector2(vitrighepx, vitrighepy), Color.White);
vitrighepx += dorongkytucham + dancachchu;
}
else
{
if ((j == 0) & ((giatrikytu[matranvanban[j, i]] == " ") | (giatrikytu[matranvanban[j, i]] == "|"))) continue;
nenve.Draw(anhkytu[matranvanban[j, i]], new Vector2(vitrighepx, vitrighepy), Color.White);
vitrighepx += dorongkytu[matranvanban[j, i]] + dancachchu;
}
}
}
}
else
{
for (int i = 0; i < demdong; i++)
{
vitrighepx = bovien.X;
vitrighepy = i * (dodaikytu + dancachdong) + giasocandong + bovien.Y;
for (int j = 0; j < demkytu[i]; j++)
{
if (matranvanban[j, i] == -1)
{
nenve.Draw(anhkytucham, new Vector2(vitrighepx, vitrighepy), Color.White);
vitrighepx += dorongkytucham + dancachchu;
}
else
{
if ((j == 0) & ((giatrikytu[matranvanban[j, i]] == " ") | (giatrikytu[matranvanban[j, i]] == "|"))) continue;
nenve.Draw(anhkytu[matranvanban[j, i]], new Vector2(vitrighepx, vitrighepy), Color.White);
vitrighepx += dorongkytu[matranvanban[j, i]] + dancachchu;
}
}
}
}
nenve.End();
thietbidohoa.SetRenderTarget(null);
if (mauchu != Color.White)
{
Color[] dulieumau = new Color[bonenvunganh.Width * bonenvunganh.Height];
bonenvunganh.GetData(dulieumau);
for (int x = 0; x < bonenvunganh.Width; x++)
{
for (int y = 0; y < bonenvunganh.Height; y++)
{
int vitri = x + y * bonenvunganh.Width;
if (dulieumau[vitri].R > 126)
{
dulieumau[vitri] = mauchu;
}
}
}
bonenvunganh.SetData(dulieumau);
}
return bonenvunganh;
}
示例14: GetPixel
public XnaColor GetPixel( int x, int y )
{
var target = new RenderTarget2D( GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.None );
GraphicsDevice.SetRenderTarget( target );
HandleDeviceReset();
Draw(_spriteBatch);
GraphicsDevice.SetRenderTarget( null );
var data = new XnaColor[ GraphicsDevice.Viewport.Width * GraphicsDevice.Viewport.Height ];
target.GetData( data );
return data[ GraphicsDevice.Viewport.Width * y + x ];
}
示例15: HandleCollision
public virtual void HandleCollision(BaseCharacter entity)
{
//if (this.Bounds.Intersects(entity.Bounds))
//{
// entity.m_tintColor = Color.Red;
//}
//else
//{
// entity.m_tintColor = Color.White;
//}
Player CroPlayer = null;
if (this.Name == "Cro")
{
CroPlayer = EntityManager.m_croPlayer;
}
if (CroPlayer != null)
{
// Check to see if the bounding rectangles of both entities are intersecting with the bottom animation.
if (this.Bounds.Intersects(entity.Bounds))
{
// Creating a Spritebatch and RenderTarget2D to facilitate perpixel collision for the animation.
SpriteBatch spriteBatch = EntityManager.m_spriteBatch;
RenderTarget2D theRenderTargetBottom = null;
RenderTarget2D theRenderTargetTop = null;
theRenderTargetBottom = new RenderTarget2D(EntityManager.m_graphics, CroPlayer.BottomAnimation.Animation.FrameWidth, CroPlayer.BottomAnimation.Animation.FrameHeight);
theRenderTargetTop = new RenderTarget2D(EntityManager.m_graphics, CroPlayer.TopAnimation.Animation.FrameWidth, CroPlayer.TopAnimation.Animation.FrameHeight);
EntityManager.m_graphics.SetRenderTarget(theRenderTargetBottom);
EntityManager.m_graphics.SetRenderTarget(theRenderTargetTop);
EntityManager.m_graphics.Clear(Color.Transparent);
spriteBatch.Begin();
Rectangle source = new Rectangle(CroPlayer.BottomAnimation.FrameIndex * CroPlayer.BottomAnimation.Animation.FrameWidth, 0, CroPlayer.BottomAnimation.Animation.FrameWidth, CroPlayer.BottomAnimation.Animation.Texture.Height);
spriteBatch.Draw(CroPlayer.BottomAnimation.Animation.Texture, Vector2.Zero, source, Color.White, 0.0f, Vector2.Zero, 1.0f, CroPlayer.Flip, 0.0f);
spriteBatch.Draw(CroPlayer.TopAnimation.Animation.Texture, Vector2.Zero, source, Color.White, 0.0f, Vector2.Zero, 1.0f, CroPlayer.Flip, 0.0f);
spriteBatch.End();
EntityManager.m_graphics.SetRenderTarget(null);
// Take the texture data for both entities and put them into colour arrays.
Color[] entityData = new Color[theRenderTargetBottom.Width * theRenderTargetBottom.Height];
theRenderTargetBottom.GetData(entityData);
Color[] entityData2 = new Color[entity.Texture.Width * entity.Texture.Height];
entity.Texture.GetData(entityData2);
// Pass in the texture data and bounds of both entites in collision.
if (IntersectPixels(this.Bounds, entityData, entity.Bounds, entityData2) == true)
{
// Change colour to red if there is a hit.
entity.m_tintColor = Color.Red;
}
else
{
// Leave it as/change it back to white if there is no collision.
entity.m_tintColor = Color.White;
}
}
}
}