Skip to content

Instantly share code, notes, and snippets.

@kenpower
Created November 8, 2012 12:48
Show Gist options
  • Save kenpower/4038604 to your computer and use it in GitHub Desktop.
Save kenpower/4038604 to your computer and use it in GitHub Desktop.
Scrolling Background Terrain Drawing
int vpw = graphics.GraphicsDevice.Viewport.Width;
int vph = graphics.GraphicsDevice.Viewport.Height;
Rectangle rect = new Rectangle(0, 0, vpw, vph);
spriteBatch.Begin();
//construct a rectangle to select an area of background to draw
//position(upper left of) rectangle is camera position minus halh size of viewport
Rectangle terrainRect=new Rectangle(
(int)(camera.Position.X-vpw/2),
(int)(camera.Position.Y-vph/2),
graphics.GraphicsDevice.Viewport.Width,
graphics.GraphicsDevice.Viewport.Height);
spriteBatch.Draw(terrain, Vector2.Zero,
terrainRect, Color.White);
spriteBatch.End();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment