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

Author Topic: Getting the camera to follow a sprite.  (Read 5362 times)

0 Members and 1 Guest are viewing this topic.

tiago221

  • Newbie
  • *
  • Posts: 24
    • View Profile
Getting the camera to follow a sprite.
« on: September 05, 2009, 11:18:59 pm »
This is what I have so far:

Code: [Select]

#include <SFML/Graphics.hpp>

int WINAPI WinMain(      
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow
)
{

sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Zombie Game Alpha");

sf::Image PlayerImage;
sf::Image Background;
Background.LoadFromFile("background.png");
PlayerImage.LoadFromFile("player.png");
sf::Sprite Player(PlayerImage);
sf::Sprite BackgroundS(Background);
sf::Sprite* follow = &Player;
Player.SetPosition(400,300);

sf::View Camera(sf::Vector2f(400.f, 300.f), sf::Vector2f(400.f, 300.f));  
sf::Vector2f PPos = Player.GetPosition();




while(Window.IsOpened())
{
sf::Event Event;
while (Window.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
Window.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Window.Close();
}

float ElapsedTime = Window.GetFrameTime();


if (Window.GetInput().IsKeyDown(sf::Key::W))  Player.Move(0, -425 * ElapsedTime);  
        if (Window.GetInput().IsKeyDown(sf::Key::S))  Player.Move(0,  425 * ElapsedTime);  
if (Window.GetInput().IsKeyDown(sf::Key::A))  Player.Move(-425 * ElapsedTime, 0);
if (Window.GetInput().IsKeyDown(sf::Key::D))  Player.Move(425 * ElapsedTime, 0);  
 

Player.SetCenter(Player.GetSize() / 2.f);
if (Window.GetInput().GetMouseX() <= Player.GetPosition().x) {
Player.SetRotation(((-1* 360 / 3.1415926535 * (atan2(static_cast<double>(Player.GetPosition().y - Window.GetInput().GetMouseY()), static_cast<double>(Player.GetPosition().x - Window.GetInput().GetMouseX()))))/2)+90);
} else {
Player.SetRotation(((-1* 360 / 3.1415926535 *(atan2(static_cast<double>(Player.GetPosition().y - Window.GetInput().GetMouseY()), static_cast<double>(Player.GetPosition().x - Window.GetInput().GetMouseX()))))/2)+90);
}

Camera.SetCenter(follow->GetCenter());

Window.Draw(BackgroundS);
Window.Draw(Player);
Window.SetView(Camera);
Window.Display();
}
}


And it doesn't work, the window's contents are flickering a lot.

93interactive

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • http://93-interactive.com
Getting the camera to follow a sprite.
« Reply #1 on: September 06, 2009, 06:33:40 am »
i think it is because you set the view after the drawing commands, you should set it before.

tiago221

  • Newbie
  • *
  • Posts: 24
    • View Profile
Getting the camera to follow a sprite.
« Reply #2 on: September 06, 2009, 03:25:51 pm »
The flickering stopped, but it still doesn't work.


93interactive

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • http://93-interactive.com
Getting the camera to follow a sprite.
« Reply #3 on: September 06, 2009, 04:06:02 pm »
i am not sure if i understood your problem.

imagine the view as a camera and the sprites as what the camera is pointed at - the world.

so if you move your view (your camera) but not the sprites (your objects in the world), like the background, the background image will move out of your view, as it is not moved.

if you want you background be drawn as some kind of screen background, you'll have to reposition it also on every frame, following the player or the camera.

if you want the background to stay in place (as in falling out of the view), you should call Window.Clear(); before the drawing commands, as the screen is not cleared automatically on redraw.

tiago221

  • Newbie
  • *
  • Posts: 24
    • View Profile
Getting the camera to follow a sprite.
« Reply #4 on: September 06, 2009, 04:21:20 pm »
I fixed it, I needed to have View.SetCenter(follow->GetPosition()) instead of GetCenter. And the Window.Clear() tides up everything, now I just need a bigger background and it'll be working, thanks.