Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been trying to figure out how to do this but I have a bunch of screens, each with their own Draw method. However, I'd like to render all sprites offset by the screen position without altering their locations before I do the drawing.

So, if I render a sprite on a screen at 0,0 but the screen itself is offset to 50,50 it still renders to a window position of 0,0 instead of to the window position of 50,50.

I've been thinking about rendering each screen to their own texture and then rendering that texture to the main spriteBatch - but reading another question you could lose a lot of information such as alpha transparency

Any help will be appreciated to get me on the right path. Thanks.

Update: Also I'd prefer it to be recursive so that screens within screens are also offset appropriately.

share|improve this question

Use GraphicsDevice.Viewport to set the current viewport, which is effectively a canvas with its own coordinate space (i.e. SpriteBatch.Draw() will use the viewport's coordinates).

The viewport is defined in terms of window coordinates, so in order to have a recursive solution you'll need a class which keeps track of the translation of its own coordinate space to the window coordinates.

Something like the following:

Class CustomWindow
  CustomWindow mParent;
  int mX, mY, mWidth, mHeight; // Relative to the parent window
  Viewport mViewport; // Must be absolute coordinates, relative to the main window.
  void OnCustomWindowMoved()
    mViewport.Width = mWidth;
    mViewport.Height = mHeight;
    if (mParent == null)
      mViewport.X = mX;
      mViewport.Y = mY;
      mViewport.X = (mParent.mViewport.X + mX);
      mViewport.Y = (mParent.mViewport.Y + mY);
share|improve this answer

I think you could change the view port between calls. This would do exactly what your asking (window position would be shifted).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.