Skip to content

Instantly share code, notes, and snippets.

@TinkerWorX
Created May 16, 2012 13:06
Show Gist options
  • Save TinkerWorX/2710203 to your computer and use it in GitHub Desktop.
Save TinkerWorX/2710203 to your computer and use it in GitHub Desktop.
By-Movement tile drawing.
...
private void RenderTiles(GameTime gameTime)
{
// Calculate which tiles to draw
var startX = (Int32)Math.Floor((Single)this.renderArea.Left / TileWorX.TILE_PIXEL_SIZE) - 1;
var endX = (Int32)Math.Ceiling((Single)this.renderArea.Right / TileWorX.TILE_PIXEL_SIZE) + 1;
var startY = (Int32)Math.Floor((Single)this.renderArea.Top / TileWorX.TILE_PIXEL_SIZE) - 1;
var endY = (Int32)Math.Ceiling((Single)this.renderArea.Bottom / TileWorX.TILE_PIXEL_SIZE) + 1;
var changeX = startX - this.lastStartX;
var changeY = startY - this.lastStartY;
if (this.invalidateAll)
{
this.GraphicsDevice.SetRenderTarget(this.tileTarget);
this.GraphicsDevice.Clear(new Color(0x64, 0x95, 0xed, 0xff));
this.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null);
// Draw all tiles
for (var x = startX; x < endX; x += 1)
{
for (var y = startY; y < endY; y += 1)
{
this.DrawTile(this.Realm.GetTile(x, y), x, y, new Vector2((x - startX) * TileWorX.TILE_PIXEL_SIZE, (y - startY) * TileWorX.TILE_PIXEL_SIZE), Color.White);
}
}
this.spriteBatch.End();
this.GraphicsDevice.SetRenderTarget(null);
this.invalidateAll = false;
}
else if (changeX != 0 || changeY != 0)
{
this.SwapTileRenderTargets();
this.GraphicsDevice.SetRenderTarget(this.tileTarget);
this.GraphicsDevice.Clear(new Color(0x64, 0x95, 0xed, 0xff));
this.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null);
this.spriteBatch.Draw(this.oldTileTarget, new Vector2(-changeX * TileWorX.TILE_PIXEL_SIZE, -changeY * TileWorX.TILE_PIXEL_SIZE), Color.White);
foreach (var p in this.InvalidatedTiles)
{
this.DrawTile(this.Realm.GetTile(p.X, p.Y), p.X, p.Y, new Vector2((p.X - startX) * TileWorX.TILE_PIXEL_SIZE, (p.Y - startY) * TileWorX.TILE_PIXEL_SIZE), Color.White);
}
// Draw the new tiles
if (changeX != 0) // Check if we moved on the x axis
{
if (changeX > 0) // Check if we moved right
{
Debug.WriteLine("right: " + changeX);
var from = endX - changeX;
var to = endX;
Debug.WriteLine("from: " + from);
Debug.WriteLine("to: " + to);
for (var x = endX - changeX; x < endX; x += 1)
{
Debug.WriteLine("right: " + x);
for (var y = startY; y < endY; y += 1)
{
this.DrawTile(this.Realm.GetTile(x, y), x, y, new Vector2((x - startX) * TileWorX.TILE_PIXEL_SIZE, (y - startY) * TileWorX.TILE_PIXEL_SIZE), Color.White);
}
}
}
else // Else we moved left
{
Debug.WriteLine("left: " + changeX);
var from = startX;
var to = startX - changeX;
Debug.WriteLine("from: " + from);
Debug.WriteLine("to: " + to);
for (var x = startX; x < startX - changeX; x += 1)
{
Debug.WriteLine("left: " + x);
for (var y = startY; y < endY; y += 1)
{
this.DrawTile(this.Realm.GetTile(x, y), x, y, new Vector2((x - startX) * TileWorX.TILE_PIXEL_SIZE, (y - startY) * TileWorX.TILE_PIXEL_SIZE), Color.White);
}
}
}
}
if (changeY != 0) // Check if we moved on the y axis
{
if (changeY > 0) // Check if we moved down
{
for (var y = endY - changeY; y < endY; y += 1)
{
for (var x = startX; x < endX; x += 1)
{
this.DrawTile(this.Realm.GetTile(x, y), x, y, new Vector2((x - startX) * TileWorX.TILE_PIXEL_SIZE, (y - startY) * TileWorX.TILE_PIXEL_SIZE), Color.White);
}
}
}
else // Else we moved up
{
for (var y = startY; y < startY - changeY; y += 1)
{
for (var x = startX; x < endX; x += 1)
{
this.DrawTile(this.Realm.GetTile(x, y), x, y, new Vector2((x - startX) * TileWorX.TILE_PIXEL_SIZE, (y - startY) * TileWorX.TILE_PIXEL_SIZE), Color.White);
}
}
}
}
this.spriteBatch.End();
this.GraphicsDevice.SetRenderTarget(null);
}
if (this.InvalidatedTiles.Count > 0)
{
this.SwapTileRenderTargets();
this.Game.GraphicsDevice.SetRenderTarget(this.tileTarget);
this.GraphicsDevice.Clear(new Color(0x64, 0x95, 0xed, 0xff));
this.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null);
this.spriteBatch.Draw(this.oldTileTarget, Vector2.Zero, Color.White);
foreach (var p in this.InvalidatedTiles)
{
this.DrawTile(this.Realm.GetTile(p.X, p.Y), p.X, p.Y, new Vector2((p.X - startX) * TileWorX.TILE_PIXEL_SIZE, (p.Y - startY) * TileWorX.TILE_PIXEL_SIZE), Color.White);
}
this.spriteBatch.End();
this.GraphicsDevice.SetRenderTarget(null);
}
this.lastStartX = startX;
this.lastStartY = startY;
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment