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

Author Topic: SFML does not wan't to draw a line  (Read 1603 times)

0 Members and 1 Guest are viewing this topic.

webthomas

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • http://webthomas.sharkserve.com
    • Email
SFML does not wan't to draw a line
« 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.

Code: [Select]
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)
webthomas.sharkserve.com

sbroadfoot90

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
SFML does not wan't to draw a line
« Reply #1 on: October 05, 2011, 11:34:34 pm »
Not sure why it isn't drawing that one line, but you can clean up your code like this

Code: [Select]
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

Code: [Select]

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;
   }
};

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML does not wan't to draw a line
« Reply #2 on: October 06, 2011, 07:52:41 am »
Does it show if you increase the line thickness?
Laurent Gomila - SFML developer

webthomas

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • http://webthomas.sharkserve.com
    • Email
SFML does not wan't to draw a line
« Reply #3 on: October 06, 2011, 08:42:33 am »
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
webthomas.sharkserve.com

 

anything