Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Movement and Views  (Read 2831 times)

0 Members and 1 Guest are viewing this topic.

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Movement and Views
« 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Movement and Views
« Reply #1 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.
Laurent Gomila - SFML developer

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Movement and Views
« Reply #2 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;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Movement and Views
« Reply #3 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.
Laurent Gomila - SFML developer

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Movement and Views
« Reply #4 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Movement and Views
« Reply #5 on: February 08, 2012, 08:14:21 am »
But what do you want to do?
Laurent Gomila - SFML developer

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Movement and Views
« Reply #6 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);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Movement and Views
« Reply #7 on: February 08, 2012, 02:04:29 pm »
I'll test your code at home and try to find out what's wrong.
Laurent Gomila - SFML developer

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Movement and Views
« Reply #8 on: February 08, 2012, 11:18:15 pm »
Alright, thanks ;)

neos300

  • Newbie
  • *
  • Posts: 6
    • MSN Messenger - omgborg@gmail.com
    • View Profile
Movement and Views
« Reply #9 on: February 09, 2012, 12:49:09 am »
-nevermind, wrong-

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Movement and Views
« Reply #10 on: February 10, 2012, 10:30:14 pm »
I dont mean to rush you Laurent, but any progress with this?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Movement and Views
« Reply #11 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 ;)
Laurent Gomila - SFML developer