Hello guys....
i need help figuring out how to screen scroll...
here is the code i have so far....
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
int main()
{
enum Direction{DOWN,LEFT,RIGHT,UP};
sf::Vector2f screenDimension(800,463),bPosition(0.0f,0.0f);
sf::Vector2i source(1,RIGHT), spriteDimension(32,32);
sf::RenderWindow window;
std::string string;
sf::Texture bTexture, pTexture;
sf::Sprite bImage, pImage;
window.create(sf::VideoMode((unsigned int)screenDimension.x,(unsigned int)screenDimension.y),"Text",sf::Style::Close | sf::Style::Titlebar);
if(!bTexture.loadFromFile("landscape.png"))
std::cout<<"Background image didnt load...\n";
bImage.setTexture(bTexture);
if(!pTexture.loadFromFile("Player.png"))
std::cout<<"Player Image didn't load...\n";
pImage.setTexture(pTexture);
pImage.setPosition(3,355.5f);
sf::View view;
view.reset(sf::FloatRect(0.0f,0.0f,screenDimension.x,screenDimension.y));
view.setViewport(sf::FloatRect(0,0,1.0f,1.0f));
sf::Clock clock;
float moveSpeed = 2500.0f;
sf::Vector2f position(0,0);
while(window.isOpen())
{
clock.restart();
sf::Event mainEvent;
while(window.pollEvent(mainEvent))
{
switch(mainEvent.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if(mainEvent.key.code==sf::Keyboard::Escape)
window.close();
break;
}
}
source.x++;
if(source.x*32 >= pTexture.getSize().x)
source.x=0;
if((unsigned int)source.x*32 >= pTexture.getSize().x)
source.x=0;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
pImage.move(moveSpeed*clock.getElapsedTime().asSeconds(),0);
source.y=RIGHT;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
pImage.move(-moveSpeed*clock.getElapsedTime().asSeconds(),0);
source.y=LEFT;
}
pImage.setTextureRect(sf::IntRect(source.x*32,source.y*32,32,32));
position.x = pImage.getPosition().x + (pImage.getPosition().x/2)-(screenDimension.x/2);
position.y = pImage.getPosition().y + (pImage.getPosition().y/2)- (screenDimension.y/2);
if(position.x < 0)
position.x=0;
if(position.y < 0)
position.y=0;
view.reset(sf::FloatRect(position.x,position.y,screenDimension.x,screenDimension.y));
window.draw(bImage);
window.setView(view);
window.draw(pImage);
window.display();
window.clear();
}
}
and a next problem is that, how can i stop my sprite animation to stop when it stop moving
Any Help is appreciated...