Ah ok thanks. Let me change the magic numbers (if I understand it correctly)
#include <SFML/Graphics.hpp>
#include <iostream>
const int xscreensize = 720;
const int yscreensize = 560;
sf::RenderWindow App(sf::VideoMode(640, 480, 32), "Fail");
sf::View View(sf::FloatRect(0,0,640,400));
void moveview(const sf::Sprite & you){
//fix this
//then remove safety checks on tiles (not needed anymore)
int setx, sety;
int x = you.GetPosition().x + 20;
int y = you.GetPosition().y + 20;
if (x < View.GetSize().x / 2)
setx = View.GetSize().x / 2;
else if (x> xscreensize - View.GetSize().x / 2)
setx = xscreensize - View.GetSize().x / 2;
else
setx = x;
if (y < View.GetSize().y / 2)
sety = View.GetSize().y / 2;
else if (y > yscreensize - View.GetSize().y / 2)
sety = yscreensize - View.GetSize().y / 2;
else
sety = y;
View.SetCenter(setx, sety);
}
int main(){
sf::Image ibackground;
ibackground.LoadFromFile("background.png");
sf::Sprite background;
background.SetImage(ibackground);
sf::Image itest;
itest.LoadFromFile("image.png");
sf::Sprite test;
test.SetImage(itest);
while (App.IsOpened()){
sf::Event Event;
while (App.GetEvent(Event)){
if (Event.Type == sf::Event::Closed)
App.Close();
}
if (App.GetInput().IsKeyDown(sf::Key::A))
test.Move(-100.f * App.GetFrameTime(), 0);
if (App.GetInput().IsKeyDown(sf::Key::D))
test.Move(100.f * App.GetFrameTime(), 0);
if (App.GetInput().IsKeyDown(sf::Key::W))
test.Move(0, -100.f * App.GetFrameTime());
if (App.GetInput().IsKeyDown(sf::Key::S))
test.Move(0, 100.f * App.GetFrameTime());
if(test.GetPosition().x < 0)
test.SetPosition(0, test.GetPosition().y);
if(test.GetPosition().x + test.GetSize().x >xscreensize)
test.SetPosition(xscreensize - test.GetSize().x, test.GetPosition().y);
if(test.GetPosition().y < 0)
test.SetPosition(test.GetPosition().x, 0);
if(test.GetPosition().y + test.GetSize().y > yscreensize)
test.SetPosition(test.GetPosition().x , yscreensize - test.GetSize().y);
moveview(test);
App.Clear();
App.SetView(View);
App.Draw(background);
App.Draw(test);
App.Display();
}
return 0;
}
Just to make sure I get views, if I draw a sprite with SetView() changed, it will get that view's position and subtract it from the sprite, and then draw it? I'm only talking about the position, not rotation and stuff. Also does a view affect a sprite's origin?