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

Author Topic: Views and Movement  (Read 1121 times)

0 Members and 1 Guest are viewing this topic.

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Views and Movement
« on: February 06, 2012, 03:59:04 am »
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?

  • Guest
Re: Views and Movement
« Reply #1 on: February 06, 2012, 05:23:37 am »
Use View.Move(), and then App.ConvertCoords(), becuse NewX and NewY is a position on window, but no in a gameworld.
http://www.sfml-dev.org/tutorials/1.6/graphics-views.php