XNA Creators Club Online
Page 1 of 1 (8 items)
Sort Posts: Previous Next

background png bigger than screen

Last post 11/14/2007 8:55 AM by Signot. 7 replies.
  • 11/6/2007 5:29 PM

    background png bigger than screen

    say the green represents the size of screen and the red represents the size of the background, would using a 2d camera and following the 2d sprite allow me to view the rest of the map as the sprite travels beyond the size of the screen ? or would this fall under scrolling background ?  

    http://dawgpound83.spaces.live.com/default.aspx
    RPG attempt 1
  • 11/7/2007 8:43 AM In reply to

    Re: background png bigger than screen

    Well a brute force method you could employ would be to keep track of the source rectangle used to draw the background.  Let's say for example that your screen is 800x600 and your background PNG is 1600x1200.  In your code let's keep a source rectangle called srcRect.  As you move the camera, you change the position of the source rectangle.  This would be a very simplified way of viewing the rest of the background image as your sprite moves.

    Here is some very simple code to help express my example:

    class MyGame : Game
    {
    Rectangle srcRect;
    int screenLeft, screenTop;
    const int SCREEN_WIDTH = 800;
    const int SCREEN_HEIGTH = 600;

    ...


    public void Update(GameTime gameTime)
    {
    //if player moves right
    { screenLeft += moveAmount; }
    //if player moves left
    { screenLeft -= moveAmount; }

    //This is just sample, but you get the idea

    //Make sure you do bounds checking, that is screenLeft never goes below 0
    //and also screenLeft is less than background width - SCREEN_WIDTH.
    }

    public void Draw()
    {
    srcRect.X = screenLeft;
    srcRect.Y = screenTop;

    spriteBatch.Begin();
    spriteBatch.Draw(backgroundTexture, new Rectangle(0,0,800,600), srcRect, Color.White);

    spriteBatch.End();
    }
    }

    I hope this makes sense, let me know if you want further clarification.  This is a really simple approach.  I hope it somewhat answers your question.
  • 11/7/2007 11:40 AM In reply to

    Re: background png bigger than screen

    Hi,

    I think the best way to do this is by making a Camera class:

    class Camera
    {
    Vector2 position;
    public Vector2 Position
    {
    get
    {
    return position;
    }
    set
    {
    position = value;
    }
    }
    public Camera(Vector2 position)
    {
    this.position = position;
    }

    public Matrix Transform
    {
    get
    {
    return Matrix.CreateTranslation(-position.X, -position.Y, 0);
    }
    }
    }

    Now, when you begin your sprite batch just pass it the camera transform like so:

    mySpriteBatch.Begin(A, B, C, myCamera.Transform);
    -Swordshock
  • 11/7/2007 12:09 PM In reply to

    Re: background png bigger than screen

    I figured there was a more elegant way :)  I think there is also a 2D camera class posted somwhere in these forums that might help as well.
  • 11/13/2007 3:13 AM In reply to

    Re: background png bigger than screen

    so if i have the 2d camera follow the moveable sprite, n i move towards the areas where the screen isnt showing the background, it will allow the player to view it as i move around ?
    http://dawgpound83.spaces.live.com/default.aspx
    RPG attempt 1
  • 11/13/2007 11:26 AM In reply to

    Re: background png bigger than screen

    I'm not sure I understand what you are exactly asking, but I think what you want to do is have the screen wrap around when the player moves to an area that isn't covered by the background image?  If this is the case, then you will have to do more than just use a 2D camera.  You might want to handle the screen scrolling manually instead of applying some transformation.  Basically if I understand correctly, draw as much of the background image to the current view window, and then draw the difference to the remaining empty area.  This will create an illusion of a scrolling background.

    I hope I understood you correctly.  Here is a link to the MSDN library showing how to do a scrolling background.  http://msdn2.microsoft.com/en-us/library/bb203868.aspx
  • 11/13/2007 4:38 PM In reply to

    Re: background png bigger than screen

     

    say the sprite moves down towards the end of the screen, will the 2d camera following the sprite , allow the player to see the rest of the background  that is extending beyond the screen ?  or will have to scale the image down to make it all fit the screen size ?

    http://dawgpound83.spaces.live.com/default.aspx
    RPG attempt 1
  • 11/14/2007 8:55 AM In reply to

    Re: background png bigger than screen

    Ah I see, thanks for the image.  Well the 2D camera should do just what you are looking for.  Because you are never changing the drawing position of the background image, the camera takes care of all the work for you.  The 2D camera is very similar to a 3D camera.  It only modifies the VIEW matrix which translates into what region of WORLD space you are currently looking at.  By simply changing the position of the camera's position, you are in essence changing the VIEWing space.  Thus, you will only be looking at a fixed portion of the background at any one time.

    The camera should do what you are asking.  Try it out in a small demo, and if you have any questions or concerns, just post some of your code and some of us here should be able to help you through your problems.
Page 1 of 1 (8 items) Previous Next