SFML community forums
Help => Graphics => Topic started by: webthomas on October 05, 2011, 09:58:36 pm
-
Hi,
I am trying to making my own game in my spare time. (It's a project I'm doing on my new website webthomas.sharkserve.com, which is actually part of the project too).
Now I've written a class called vec2D. I think it was all okay, but 1 problem kept occuring:
There's one line it doesn't want do draw.
void printvec(vector<vec2D> v, int n){
RenderWindow win(VideoMode(1000, 1000), "screen");
win.Clear(sf::Color::Black);
for(int i1=0; i1<=n;i1++){
win.Draw(Shape::Line(v[i1].getStartX(),v[i1].getStartY(),v[i1].getStartX()+v[i1].getVecX(),v[i1].getStartY()+v[i1].getVecY(),2,Color::Green));
cout <<v[i1].getStartX()<<' '<<v[i1].getStartY()<<' '<<v[i1].getStartX()+v[i1].getVecX()<<' '<<v[i1].getStartY()+v[i1].getVecY()<<endl;
}
win.Display();
while (win.IsOpened()){
Event Event;
while (win.GetEvent(Event)){
//Sluit het scherm
if (Event.Type == Event::Closed){//het scherm sluiten
win.Close();
}
}
win.Clear(sf::Color::Black);
for(int i1=0; i1<=n;i1++){
win.Draw(Shape::Line(v[i1].getStartX(),v[i1].getStartY(),v[i1].getVecX(),v[i1].getVecY(),1,Color::Green));
}
win.Display();
}
return;
}
the output of this function is:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
20 20 120 60
20 20 60 120
140 140 280 280
0 0 0 0
which is allright, except that id doesn't want to draw the line starting at (140,140) going to (280,280)
(http://dl.dropbox.com/u/11155689/Screenshot-4.png)
-
Not sure why it isn't drawing that one line, but you can clean up your code like this
void printvec(const vector<vec2D> &v){
RenderWindow win(VideoMode(1000, 1000), "screen");
win.Clear(sf::Color::Black);
for(vector<vec2D>::iterator it = v.begin() i1=0; it != v.end(); ++it){
it->Draw(win);
it->Print();
}
win.Display();
while (win.IsOpened()){
Event Event;
while (win.GetEvent(Event)){
//Sluit het scherm
if (Event.Type == Event::Closed){//het scherm sluiten
win.Close();
}
}
}
return;
}
1. Your picture isn't changing so I'm not sure why you're drawing it twice.
2. If you're using STL's vector template, you don't need to pass in the size, you can use iterators
3. Put the drawing/printing functionality into the vec2D function
4. Why do you have 5 zero vectors?
5. What is the functionality of vec2D supposed to be? In maths a 2D vector is two pieces of information, x and y. It shouldn't have a starting point. Maybe you should rename it to LineSegment or something.
6. Prefer passing in by reference to const rather than by copy. If you intend to change the vector v then take the const out on the first line.
7. Did you use "using namespace sf"? Sometimes you use sf:: and sometimes you don't. Try to be consistent
Here is how you should put the drawing functionality in your class vec2D
class vec2D {
public:
void Draw(sf::RenderWindow &win) const {
win.Draw(Shape::Line(getStartX(), getStartY(), getStartX()+getVecX(), getStartY()+ getVecY(), 2, sf::Color::Green));
return;
}
void Print() const {
cout <<getStartX()<<' '<<getStartY()<<' '<<getStartX()+getVecX()<<' '<<getStartY()+getVecY()<<endl;
}
};
-
Does it show if you increase the line thickness?
-
Yeah, I know the code is quite messy (it was written fast just to test some code, but you're wriht: this shouldn't be an excuse)
Yes vec2D is a mathematical 2dimensional vector, but I wanted it to have more functionalities(the startpoint) because for some calculations is is handy that you've got a startpoint.
The zerovectors were there to experiment with other functionalities, but this isn't that important too.
Yes I used using namespace sf, but some code I copy pasted from a former program, where I didn't use it. But that shouldn't be a problem i guess?
When I increase line thickness, nothing changes. But I just think it's weird, because it prints out the values i want it to print. And these values are being sent but nor printed in the screen.
The couts i put there were simply to test if the right values were given