SFML community forums

Help => Graphics => Topic started by: LucasShadow on February 07, 2012, 03:49:22 pm

Title: Movement and Views
Post by: LucasShadow on February 07, 2012, 03:49:22 pm
So I have been messing around with sf::View recently (SFML 1.6) and made the simple program below:

Code: [Select]
#include <SFML/Graphics.hpp>

 int main()
 {
     sf::RenderWindow App(sf::VideoMode(800, 600), "Sprite Test");
     sf::Image ForestTileImg;
     sf::Image PlayerImg;

     if(!ForestTileImg.LoadFromFile("forest_terrain_tile.png")) {
        return EXIT_FAILURE;
     }
     if(!PlayerImg.LoadFromFile("player.png")) {
        return EXIT_FAILURE;
     }

     ForestTileImg.SetSmooth(false);
     PlayerImg.SetSmooth(false);

     sf::Sprite PlayerObj(PlayerImg);
     sf::Sprite ForestTileObj(ForestTileImg);
     ForestTileObj.SetPosition(400,300);
     PlayerObj.SetPosition(400, 300);

    float NewX = 400;
    float NewY = 300;

    sf::Vector2f Center(800, 600);
    sf::Vector2f HalfSize(400, 300);
    sf::View View(Center, HalfSize);

     while (App.IsOpened())
     {
         sf::Event Event;
         while (App.GetEvent(Event))
         {
             if (Event.Type == sf::Event::Closed)
                 App.Close();
         }

         if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left))
            {
            NewX = App.GetInput().GetMouseX();
            NewY = App.GetInput().GetMouseY();
            }

         PlayerObj.SetPosition(NewX, NewY);

         View.SetCenter(NewX, NewY);

         App.SetView(View);
         App.Clear();

         App.Draw(ForestTileObj);
         App.Draw(PlayerObj);

         App.Display();
     }
     return EXIT_SUCCESS;
 }


The view stays on the play well, however, the player cannot move outside of the initial area that was first viewed.  To say that another way, the player cant move outside of the initial 800, 600 window area. Suggestions?
Title: Movement and Views
Post by: Laurent on February 07, 2012, 03:59:12 pm
Code: [Select]
           NewX = App.GetInput().GetMouseX();
            NewY = App.GetInput().GetMouseY();

This is wrong: you want world coordinates, not window coordinates. You must convert them with App.ConvertCoords.
Title: Movement and Views
Post by: LucasShadow on February 07, 2012, 04:36:39 pm
I did as you suggested, even used the example code in the documentation, but the problem still persists.  The sprite, in this case Cursor, doesnt go outside of the initial window area and the view doesnt stay centered on the sprite.

Code I am using:
Code: [Select]
int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Views");

    sf::Image BackgroundImage;
    if (!BackgroundImage.LoadFromFile("background.png"))
        return EXIT_FAILURE;
    sf::Sprite Background(BackgroundImage);

    sf::Image CursorImage;
    if (!CursorImage.LoadFromFile("player.png"))
        return EXIT_FAILURE;
    sf::Sprite Cursor(CursorImage);

    Background.Resize(2000, 2000);

    sf::Vector2f Center(1000, 1000);
    sf::Vector2f HalfSize(400, 300);
    sf::View View(Center, HalfSize);

    Cursor.SetCenter(32, 32);
    Cursor.SetPosition(Center);

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        sf::Vector2f CursorPos = App.ConvertCoords(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
        Cursor.SetPosition(CursorPos);

        View.SetCenter(Cursor.GetPosition().x, Cursor.GetPosition().y);

        App.SetView(View);

        App.Draw(Background);

        App.Draw(Cursor);

        App.SetView(App.GetDefaultView());

        App.Display();
    }

    return EXIT_SUCCESS;
}
Title: Movement and Views
Post by: Laurent on February 07, 2012, 04:46:36 pm
I think that the background shouldn't be drawn with your custom view, it should remain in the default view.
Title: Movement and Views
Post by: LucasShadow on February 07, 2012, 10:15:48 pm
Doing:
Code: [Select]
App.SetView(App.GetDefaultView());
       
        App.Draw(Background);


or:
Code: [Select]
App.Draw(Background);

        App.SetView(View);


Both result in nothing moving at all, or at least, not seeming to move at all.
Title: Movement and Views
Post by: Laurent on February 08, 2012, 08:14:21 am
But what do you want to do?
Title: Movement and Views
Post by: LucasShadow on February 08, 2012, 01:57:17 pm
I want the background to stay still while the sprite can move around, with the view staying constantly centered on the sprite.  I was able to get the player to move around just fine until I added the code for views though...

Quote
The view stays on the play(er) well, however, the player cannot move outside of the initial area that was first viewed. To say that another way, the player cant move outside of the initial 800, 600 window area.


And the view itself doesnt stay center on the sprite, even though the code says too.

Code: [Select]
View.SetCenter(Cursor.GetPosition().x, Cursor.GetPosition().y);
Title: Movement and Views
Post by: Laurent on February 08, 2012, 02:04:29 pm
I'll test your code at home and try to find out what's wrong.
Title: Movement and Views
Post by: LucasShadow on February 08, 2012, 11:18:15 pm
Alright, thanks ;)
Title: Movement and Views
Post by: neos300 on February 09, 2012, 12:49:09 am
-nevermind, wrong-
Title: Movement and Views
Post by: LucasShadow on February 10, 2012, 10:30:14 pm
I dont mean to rush you Laurent, but any progress with this?
Title: Movement and Views
Post by: Laurent on February 10, 2012, 10:38:41 pm
No, sorry. Your code uses SFML 1.6, I can only test 2.0 at home. You should upgrade anyway ;)