SFML community forums
Help => Graphics => Topic started 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:
#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?
-
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.
-
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:
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;
}
-
I think that the background shouldn't be drawn with your custom view, it should remain in the default view.
-
Doing:
App.SetView(App.GetDefaultView());
App.Draw(Background);
or:
App.Draw(Background);
App.SetView(View);
Both result in nothing moving at all, or at least, not seeming to move at all.
-
But what do you want to do?
-
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...
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.
View.SetCenter(Cursor.GetPosition().x, Cursor.GetPosition().y);
-
I'll test your code at home and try to find out what's wrong.
-
Alright, thanks ;)
-
-nevermind, wrong-
-
I dont mean to rush you Laurent, but any progress with this?
-
No, sorry. Your code uses SFML 1.6, I can only test 2.0 at home. You should upgrade anyway ;)