1
Graphics / Re: RectangleShape vs Sprite?
« on: April 27, 2012, 05:49:24 pm »
Ah, I see. I think I'll switch them to sprites now then.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
else if (y > yscreensize - View.GetSize().y / 2 + 80)
sety = yscreensize - View.GetSize().y / 2 + 80;
but now when the sprite is going down, the view only begins going down when the y point is at 240, not 200. So instead of camera moving at 200, it moves at 240. And I don't mean the top of the sprite either, I mean the 20 pixels down point(due to the offset I set). Another thing is that the camera follows it until there is a 95 pixel gap, not an 80 pixel gap. Here is a picture to illustrate what I mean.#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;
}
#include <SFML/Graphics.hpp>
#include <iostream>
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 < 320)
setx = 320;
else if (x> 18 * 40 - 320)
setx = 18 * 40 - 320;
else
setx = x;
if (y < 200)
sety = 200;
else if (y > 14 * 40 - 200)
sety = 14 * 40 - 200;
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 >18 *40)
test.SetPosition(18 * 40 - test.GetSize().x, test.GetPosition().y);
if(test.GetPosition().y < 0)
test.SetPosition(test.GetPosition().x, 0);
if(test.GetPosition().y + test.GetSize().y >14 *40)
test.SetPosition(test.GetPosition().x , 14 * 40 - test.GetSize().y);
moveview(test);
App.Clear();
App.SetView(View);
App.Draw(background);
App.Draw(test);
App.Display();
}
return 0;
}
void cmainchar::moveview(cmap * maps){
//fix this
//then remove safety checks on tiles (not needed anymore)
if(teleport){
spawn = true;
fall = 0.f;
jump = 0.f;
maps->changemap(this, spawn);
teleport = false;
}
int setx, sety;
int x = you.GetPosition().x + 20;
int y = you.GetPosition().y + 20;
//posx and posy is the size of the map. it is multiplied by 40 due to the size of the tiles (40 by 40)
if (x < 320)
setx = 320;
else if (x> maps->posx * 40 - 320)
setx = maps->posx *40 - 320;
else
setx = x;
if (y < 200)
sety = 200;
else if (y > maps->posy * 40 - 200)
sety = maps->posy * 40 - 200;
else
sety = y;
View.SetCenter(setx, sety);
std::cout << you.GetPosition().y << " " << View.GetCenter().y - 200 << " " << View.GetSize().y << std::endl;
}