public override bool AddTile(GameTexture tex, float x, float y, int srcX, int srcY, int srcW, int srcH) { var platformTex = tex?.GetTexture(); if (platformTex == null) { return(false); } if (Texture == null) { Texture = tex; } else if (Texture.GetTexture() != platformTex) { return(false); } if (vertexBuffer != null) { return(false); } var rotated = false; var pack = tex.GetTexturePackFrame(); if (pack != null) { if (pack.Rotated) { rotated = true; var z = srcX; srcX = pack.Rect.Right - srcY - srcH; srcY = pack.Rect.Top + z; z = srcW; srcW = srcH; srcH = z; } else { srcX += pack.Rect.X; srcY += pack.Rect.Y; } } var texture = (Texture2D)tex.GetTexture(); var textureSizeX = 1f / texture.Width; var textureSizeY = 1f / texture.Height; var left = srcX * textureSizeX; var right = (srcX + srcW) * textureSizeX; var bottom = (srcY + srcH) * textureSizeY; var top = srcY * textureSizeY; verticeDict.Add(new Tuple <float, float>(x, y), vertices.Count); if (rotated) { vertices.Add(new VertexPositionTexture(new Vector3(x, y + srcH, 0), new Vector2(left, top))); vertices.Add(new VertexPositionTexture(new Vector3(x, y, 0), new Vector2(right, top))); vertices.Add(new VertexPositionTexture(new Vector3(x + srcW, y + srcH, 0), new Vector2(left, bottom))); vertices.Add(new VertexPositionTexture(new Vector3(x + srcW, y, 0), new Vector2(right, bottom))); } else { vertices.Add(new VertexPositionTexture(new Vector3(x, y + srcH, 0), new Vector2(left, bottom))); vertices.Add(new VertexPositionTexture(new Vector3(x, y, 0), new Vector2(left, top))); vertices.Add(new VertexPositionTexture(new Vector3(x + srcW, y + srcH, 0), new Vector2(right, bottom))); vertices.Add(new VertexPositionTexture(new Vector3(x + srcW, y, 0), new Vector2(right, top))); } indices.Add((short)vertexCount); indices.Add((short)(vertexCount + 1)); indices.Add((short)(vertexCount + 2)); indices.Add((short)(vertexCount + 2)); indices.Add((short)(vertexCount + 1)); indices.Add((short)(vertexCount + 3)); vertexCount += 4; return(true); }
public override void DrawTexture( GameTexture tex, float sx, float sy, float sw, float sh, float tx, float ty, float tw, float th, Color renderColor, GameRenderTexture renderTarget = null, GameBlendModes blendMode = GameBlendModes.None, GameShader shader = null, float rotationDegrees = 0, bool isUi = false, bool drawImmediate = false ) { var texture = tex?.GetTexture(); if (texture == null) { return; } var packRotated = false; var pack = tex.GetTexturePackFrame(); if (pack != null) { if (pack.Rotated) { rotationDegrees -= 90; var z = tw; tw = th; th = z; z = sx; sx = pack.Rect.Right - sy - sh; sy = pack.Rect.Top + z; z = sw; sw = sh; sh = z; packRotated = true; } else { sx += pack.Rect.X; sy += pack.Rect.Y; } } var origin = Vector2.Zero; if (Math.Abs(rotationDegrees) > 0.01) { rotationDegrees = (float)(Math.PI / 180 * rotationDegrees); origin = new Vector2(sw / 2f, sh / 2f); //TODO: Optimize in terms of memory AND performance. var pnt = new Pointf(0, 0); var pnt1 = new Pointf((float)tw, 0); var pnt2 = new Pointf(0, (float)th); var cntr = new Pointf((float)tw / 2, (float)th / 2); var pntMod = Rotate(pnt, cntr, rotationDegrees); var pntMod2 = Rotate(pnt1, cntr, rotationDegrees); var pntMod3 = Rotate(pnt2, cntr, rotationDegrees); var width = (int)Math.Round(GetDistance(pntMod.X, pntMod.Y, pntMod2.X, pntMod2.Y)); var height = (int)Math.Round(GetDistance(pntMod.X, pntMod.Y, pntMod3.X, pntMod3.Y)); if (packRotated) { var z = width; width = height; height = z; } tx += width / 2f; ty += height / 2f; } if (renderTarget == null) { if (isUi) { tx += mCurrentView.X; ty += mCurrentView.Y; } StartSpritebatch(mCurrentView, blendMode, shader, null, false, null, drawImmediate); mSpriteBatch.Draw( (Texture2D)texture, new Vector2(tx, ty), new XNARectangle((int)sx, (int)sy, (int)sw, (int)sh), ConvertColor(renderColor), rotationDegrees, origin, new Vector2(tw / sw, th / sh), SpriteEffects.None, 0 ); } else { StartSpritebatch( new FloatRect(0, 0, renderTarget.GetWidth(), renderTarget.GetHeight()), blendMode, shader, renderTarget, false, null, drawImmediate ); mSpriteBatch.Draw( (Texture2D)texture, new Vector2(tx, ty), new XNARectangle((int)sx, (int)sy, (int)sw, (int)sh), ConvertColor(renderColor), rotationDegrees, origin, new Vector2(tw / sw, th / sh), SpriteEffects.None, 0 ); } }
public override bool UpdateTile(GameTexture tex, float x, float y, int srcX, int srcY, int srcW, int srcH) { var key = new Tuple <float, float>(x, y); var vertexIndex = -1; if (verticeDict.ContainsKey(key)) { vertexIndex = verticeDict[key]; } if (vertexIndex == -1) { return(false); } var platformTex = tex?.GetTexture(); if (platformTex == null) { return(false); } if (tex == null) { return(false); } if (tex.GetTexture() != platformTex) { return(false); } if (vertexBuffer == null) { return(false); } var pack = tex.GetTexturePackFrame(); var rotated = false; if (pack != null) { if (pack.Rotated) { rotated = true; var z = srcX; srcX = pack.Rect.Right - srcY - srcH; srcY = pack.Rect.Top + z; z = srcW; srcW = srcH; srcH = z; } else { srcX += pack.Rect.X; srcY += pack.Rect.Y; } } var texture = (Texture2D)tex.GetTexture(); var textureSizeX = 1f / texture.Width; var textureSizeY = 1f / texture.Height; var left = srcX * textureSizeX; var right = (srcX + srcW) * textureSizeX; var bottom = (srcY + srcH) * textureSizeY; var top = srcY * textureSizeY; if (rotated) { vertices[vertexIndex++] = new VertexPositionTexture( new Vector3(x, y + srcH, 0), new Vector2(left, top) ); vertices[vertexIndex++] = new VertexPositionTexture(new Vector3(x, y, 0), new Vector2(right, top)); vertices[vertexIndex++] = new VertexPositionTexture( new Vector3(x + srcW, y + srcH, 0), new Vector2(left, bottom) ); vertices[vertexIndex] = new VertexPositionTexture( new Vector3(x + srcW, y, 0), new Vector2(right, bottom) ); } else { vertices[vertexIndex++] = new VertexPositionTexture( new Vector3(x, y + srcH, 0), new Vector2(left, bottom) ); vertices[vertexIndex++] = new VertexPositionTexture(new Vector3(x, y, 0), new Vector2(left, top)); vertices[vertexIndex++] = new VertexPositionTexture( new Vector3(x + srcW, y + srcH, 0), new Vector2(right, bottom) ); vertices[vertexIndex] = new VertexPositionTexture(new Vector3(x + srcW, y, 0), new Vector2(right, top)); } updatesPending = true; return(true); }